In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to achieve the Ta-lib-based KDJ strategy in VNPY. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
There is no kdj strategy in VNPY's own demonstration strategy. As a common strategy in China, here is how to implement it.
First of all, the kdj strategy is not available in Ta-lib, the python library, probably mainly because it is mainly popular, but ta-lib provides a similar method, STOCH (). The KD effect can be achieved.
So first add a method kdj for class ArrayManager to output the KDJ value. I am modifying the ctaTemplate.py file directly, and it is more appropriate to inherit class ArrayManager.
Def kdj (self, fastk_period, slowk_period, slowk_matype, slowd_period, slowd_matype, array=False):
"" KDJ Metrics ""
Slowk, slowd = talib.STOCH (self.high,self.low,self.close,fastk_period, slowk_period
Slowk_matype, slowd_period, slowd_matype)
# calculate J value, J = (3 * D)-(2 * K)
Slowj = list (map (lambda XJI y: 3 * x-2 * y, slowk, slowd))
If array:
Return slowk, slowd, slowj
Return slowk [- 1], slowd [- 1], slowj [- 1]
The above is the new kdj function, first of all, the use of SHOCH to calculate kd, the use of kd to calculate j, output kdj three indicators.
SHOCH calculation requires the highest, lowest, and end value parameters in each k line, which are provided as list series; this is provided directly using ArraManger; then there are fastK_period, slowk_period and slowd_period, which are the three window parameters commonly used in kdj settings, usually set to (910).
Then there is slowk_matype, and slowd_matype is the type of averaging algorithm, where you can use SMA moving average or EMA exponential smooth moving average and so on. In order to be consistent with Wenhua, SMA is used.
# MA_Type: 0=SMA, 1=EMA, 2=WMA, 3=DEMA, 4=TEMA, 5=TRIMA, 6=KAMA, 7=MAMA, 8=T3 (Default=SMA)
It turns out later that this is not the same thing. S in SMA in Ta-lib means simple simply, and SMA means simple moving average. Unlike Mandarin SMA, Mandarin SMA is an exponentially weighted moving average, so it is more appropriate to use EWMA. However, Ta-lib itself does not provide EWMA;. According to the figure below, it is found that the default weight is 1; EMA may be more appropriate, but the weight is 2 at this time.
It is not the same thing to read this article later, and I will be free to fill this hole in the future.
Https://www.joinquant.com/post/7920
J value does not provide direct calculation, directly use KD value, use 3*k-2*d to calculate j value. By the way, these returns are a bunch of kdj group list, which can be drawn as a line. If array is false, the KDJ value of a point is returned.
All right, the above is to add a kdj method to ArrayManager, and the next is to inherit the CtaTemplate and generate the strategy, which is basically
The buying idea is that k or d is oversold when k or d is less than a certain threshold, and when k is greater than d, it describes how many orders are made when the K line runs through the d line. On the contrary, if the k or d value is more overbought than the threshold, then k is less than d, and an empty order will be issued.
If you hold a long position, then because j is more sensitive, use the j value as the closing index. If you hold multiple orders, if j is less than d, that is, multiple orders are sold when the j line crosses the d line, or the j value drops rapidly, which is greater than the fixed jlimit.
If you hold a short position, similarly, if j is greater than d, or if j increases rapidly, the position will be closed.
The code is as follows:
# encoding: UTF-8
"
The Demo here is a kdj policy implementation.
"
From _ _ future__ import division
From vnpy.trader.vtConstant import EMPTY_STRING, EMPTY_FLOAT
From vnpy.trader.app.ctaStrategy.ctaTemplate import (CtaTemplate
BarGenerator
ArrayManager)
From talib import MA_Type
#
Class KDJStrategy (CtaTemplate):
"" KDJ Policy Demo ""
ClassName = 'KDJStrategy'
Author = upright BillyZhang'
# Policy parameters
Fastk_period = 9
Slowk_period = 3
Slowk_matype = MA_Type.EMA
Slowd_period = 3
Slowd_matype = MA_Type.EMA
Kdlimit = 20
Jlimit = 10
InitDays = 10
FixedSize = 1
Barmins = 15
# Policy variables
K = 0
D = 0
J = 0
# Parameter list, saving the name of the parameter
ParamList = ['name'
'className'
'author'
'vtSymbol'
'fastk_period'
'slowk_period'
'slowk_matype'
'slowd_period'
'slowd_matype'
'fixedSize'
'barmins'
]
# list of variables, saving the name of the variable
VarList = ['inited'
'pos'
'k'
'd'
'j']
# synchronization list, saving the names of variables that need to be saved to the database
SyncList = ['pos']
#-
Def _ _ init__ (self, ctaEngine, setting):
"" Constructor "
Super (KDJStrategy, self). _ _ init__ (ctaEngine, setting)
Self.bg = BarGenerator (self.onBar, self.barmins, self.onXminBar)
Self.am = ArrayManager ()
# Note that the mutable object attributes in the policy class (usually list, dict, etc.) need to be recreated when the policy is initialized
# otherwise, there will be data sharing among multiple policy instances, which may lead to the risk of potential policy logic errors
# these mutable object attributes in the policy class can be left unwritten, all under _ _ init__, mainly for reading
# Policy is convenient (more of a choice of programming habits)
#-
Def onInit (self):
"" initialization policy (must be inherited by the user) ""
Self.writeCtaLog (u'KDJ Policy initialization')
InitData = self.loadBar (self.initDays)
For bar in initData:
Self.onBar (bar)
Self.putEvent ()
#-
Def onStart (self):
"" Startup policy (must be inherited and implemented by the user) ""
Self.writeCtaLog (u'KDJ Policy start')
Self.putEvent ()
#-
Def onStop (self):
"" stop policy (must be inherited and implemented by the user) ""
Self.writeCtaLog (u'KDJ Policy stop')
Self.putEvent ()
#-
Def onTick (self, tick):
"" receive quotation TICK push (must be inherited by the user) ""
Self.bg.updateTick (tick)
#-
Def onBar (self, bar):
"" receive Bar push (must be inherited by the user) ""
Self.bg.updateBar (bar)
#-
Def onXminBar (self, bar):
"" receive Bar push (must be inherited by the user) ""
Am = self.am
Am.updateBar (bar)
If not am.inited:
Return
# calculate kdj values
Slowk, slowd, slowj = am.kdj (self.fastk_period, self.slowk_period, self.slowk_matype
Self.slowd_period, self.slowd_matype, array=True)
Self.k = slowk [- 1]
Self.d = slowd [- 1]
Self.j = slowj [- 1]
Self.jdif = slowj [- 1]-slowj [- 2]
Tradeindictor = 0
If self.k > (100-self.kdlimit) or self.d > (100-self.kdlimit):
Tradeindictor =-1
If self.k
< self.kdlimit or self.d < self.kdlimit: tradeindictor = 1 # 当前无仓位,发送开仓委托 if self.pos == 0: self.intraTradeHigh = bar.high self.intraTradeLow = bar.low # 如果k值大于d值均线,开多单;反之,如果如果k值小于d值时候开空单 if self.k >Self.d and tradeindictor = = 1:
Self.buy (bar.close, self.fixedSize, False)
Elif self.k
< self.d and tradeindictor == -1: self.short(bar.close, self.fixedSize, False) # 持有多头仓位; 如果j小于d,或者j最近两个k线,j值下跌超过jlimi平仓, : elif self.pos >0:
If self.j
< self.d or self.jdif < -1 *self.jlimit: self.sell(bar.close * 1.03, abs(self.pos)) # 持有空头仓位;如果j大于d,或j快速上扬 平仓; elif self.pos < 0: if self.j >Self.d or self.jdif > self.jlimit:
Self.cover (bar.close * 0.97, abs (self.pos))
# 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) ""
# for policies that do not require fine-grained delegated control, onOrder can be ignored
Pass
#-
Def onTrade (self, trade):
"" receive transaction push (must be inherited by the user) ""
# for policies that do not require fine-grained delegated control, onOrder can be ignored
Pass
#-
Def onStopOrder (self, so):
"" stop single push ""
Pass
The above is how to implement the Ta-lib-based KDJ strategy in VNPY. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.