In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the trading strategy of VNPY based on SAR and Kentner? to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
A relatively simple strategy, mainly to verify the SAR appearance indicators; and then can be combined with other single values, to do a simple combination. It's just for testing.
Entry indicator, cci, if cci is greater than 0, long, cci is less than 0 short. Under the stop order, the amount is the Kelter on and off the track. The long buying price is on track. The short buying price is the bottom of the channel.
Exit indicators, here exit and entry apply to different cycles of the k line, because this can be more flexible to run back test. SAR price as the selling price, open stop order, if it touches SAR price to sell.
First add the sar method to class ArrayManager (). Of course, it is best to inherit and add.
Click (here) to collapse or open
Def sar (self, acceleration = 0.02, maximum = 0.2, array = False):
"" sar "
Real = talib.SAR (self.high,self.low, acceleration= acceleration,maximum = maximum)
If array:
Return real
Return real [- 1]
Then create a policy.
Click (here) to collapse or open
# encoding: UTF-8
From _ _ future__ import division
From vnpy.trader.vtObject import VtBarData
From vnpy.trader.vtConstant import EMPTY_STRING
From vnpy.trader.app.ctaStrategy.ctaTemplate import (CtaTemplate
BarGenerator
ArrayManager)
#
Class SARKELStrategy (CtaTemplate):
"" based on sar and Keltner transaction strategy ""
ClassName = 'SARKELStrategy'
Author = upright BillyZhang'
# Policy parameters
SarAcceleration = 0.02 # acceleration line
SarMaximum = 0.2 #
CciWindow = 20 # CCI window
KeltnerWindow = 25 # keltner window
KeltnerlMultiplier = 6.0# multiplier
InitDays = 10 # number of days it takes to initialize the data
FixedSize = 1 # quantity per transaction
BarMins = 15
BarMinsClose = 10
# Policy variables
SarValue = 0 # sar indicator value
CciValue = 0 # CCI indicator value
Keltnerup = 0
Keltnerdown = 0
LongStop = 0 # long stop loss
ShortStop = 0 # short stop loss
# Parameter list, saving the name of the parameter
ParamList = ['name'
'className'
'author'
'vtSymbol'
'sarAcceleration'
'sarMaximum'
'cciWindow'
'keltnerWindow'
'keltnerlMultiplier'
'initDays'
'fixedSize'
'barMinsClose'
'barMins']
# list of variables, saving the name of the variable
VarList = ['inited'
'trading'
'pos'
'sarValue'
'cciValue'
'atrValue'
'intraBarHigh'
'intraBarLow'
'longStop'
'shortStop']
# synchronization list, saving the names of variables that need to be saved to the database
SyncList = ['pos'
'intraTradeHigh'
'intraTradeLow']
#-
Def _ _ init__ (self, ctaEngine, setting):
"" Constructor "
Super (SARKELStrategy, self). _ _ init__ (ctaEngine, setting)
Self.bg = BarGenerator (self.onBar, self.barMins, self.onXminBar) # create K-line synthesizer object
Self.am = ArrayManager ()
Self.bgclose = BarGenerator (self.onBar, self.barMinsClose, self.onminBarClose)
Self.amClose = ArrayManager ()
#-
Def onminBarClose (self, bar):
"" minutes as clearance cycle ""
# if you don't have a position, don't use care, just skip
# Save K-line data
AmClose = self.amClose
AmClose.updateBar (bar)
If not amClose.inited:
Return
# calculate the index value
Self.sarValue = amClose.sar (self.sarAcceleration,self.sarMaximum)
# determine whether to make a transaction or not
If self.pos = = 0:
Return
# No position currently, send opening delegation
# holding a long position
Elif self.pos > 0:
Self.cancelAll ()
Self.sell (self.sarValue, abs (self.pos), True)
# holding a short position
Elif self.pos
< 0: self.cancelAll() self.cover(self.sarValue, abs(self.pos), True) # 同步数据到数据库 self.saveSyncData() # 发出状态更新事件 self.putEvent() # ---------------------------------------------------------------------- def onInit(self): """初始化策略(必须由用户继承实现)""" self.writeCtaLog(u'%s策略初始化' % self.name) # 载入历史数据,并采用回放计算的方式初始化策略数值 initData = self.loadBar(self.initDays) for bar in initData: self.onBar(bar) self.putEvent() # ---------------------------------------------------------------------- def onStart(self): """启动策略(必须由用户继承实现)""" self.writeCtaLog(u'%s策略启动' % self.name) self.putEvent() # ---------------------------------------------------------------------- def onStop(self): """停止策略(必须由用户继承实现)""" self.writeCtaLog(u'%s策略停止' % self.name) self.putEvent() # ---------------------------------------------------------------------- def onTick(self, tick): """收到行情TICK推送(必须由用户继承实现)""" self.bg.updateTick(tick) # ---------------------------------------------------------------------- def onBar(self, bar): """收到Bar推送(必须由用户继承实现)""" self.bg.updateBar(bar) self.bgclose.updateBar(bar) # ---------------------------------------------------------------------- def onXminBar(self, bar): """收到X分钟K线""" # 全撤之前发出的委托 self.cancelAll() # 保存K线数据 am = self.am am.updateBar(bar) if not am.inited: return # 计算指标数值 self.cciValue = am.cci(self.cciWindow) self.keltnerup, self.keltnerdown = am.keltner(self.keltnerWindow,self.keltnerlMultiplier) # 判断是否要进行交易 # 当前无仓位,发送开仓委托 if self.pos == 0: if self.cciValue >0:
# ru
Self.buy (self.keltnerup,self.fixedSize, True)
Elif self.cciValue < 0:
Self.short (self.keltnerdown, self.fixedSize, True)
# synchronizing data to the database
Self.saveSyncData ()
# issue a status update event
Self.putEvent ()
#-
Def onOrder (self, order):
"" received delegated change push (must be inherited by the user) ""
Pass
#-
Def onTrade (self, trade):
# issue a status update event
Self.putEvent ()
#-
Def onStopOrder (self, so):
"" stop single push ""
Pass
This is the answer to the question about VNPY's trading strategy based on SAR and Kentner. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.