In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the example analysis of the simple implementation of VNPY based on statistics-based trading strategy. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Trading thinking is based on historical data, such as a set of data, such as the difference between the highest or lowest point in the K line and the starting price in 100 days, and then use numpy's function numpy.percentile () to calculate the spread at, for example, 95% chance, the highest or lowest point. If the spread is 5 points, you can assume that there is a 95% chance that the next K line will benefit from 5 points.
Try to implement it in VNPY.
Train of thought:
1. Admission: if the recent N (30) D-minute k line is calculated by the following code, for example, 90% of the probability prb, if there is a point difference greater than TickValueLimit and a value of TickValue, it means that in the past N minutes, there is a probability of P, bar begins to place an order, and there is the highest or lowest point in bar to get TickValue. So at the beginning of the next bar, buy.
two。 Exit, if you reach the hold price POSprice + /-TickValue, then sell; re-enter the analysis. If the bar does not reach the target price, analyze whether the entry conditions are met at the end of the bar. If it continues to meet, hold the position, otherwise close the position. If it is in the opposite direction, the bill will be issued in reverse.
3. Stop loss, if held sometimes, falls to the reverse POSPrice + /-Multiple * TickValue price, close the position. Multiple decreased gradually with the increase of time.
The sample code is as follows, but unfortunately, the result of the back test is not very good.
From _ _ future__ import divisionfrom vnpy.trader.vtConstant import EMPTY_STRING, EMPTY_FLOAT, OFFSET_OPEN,OFFSET_CLOSEfrom vnpy.trader.app.ctaStrategy.ctaTemplate import (CtaTemplate, BarGenerator, ArrayManager) import numpy as npfrom datetime import datetime Time####class PercentileStrategy (CtaTemplate): "MACD policy Demo" className = 'PercentileStrategy' author = upright BillyZhang' fixedSize = 1 # policy parameter CalWindow = 15 percentile = 95 tickValueLimit = 5 Multiple = 0.8 # Policy variable p = 0 tickValue = 0 tradeSign = 0 tickValueHigh = 0 tickValueLow = 0 longStop = 0 # long stop shortStop = 0 # short stop margin = 0 lowerLimit = 0 upperLimit = 50000 # time initDays = 0 DAY_START = time (9 10) # Day disk start and stop time DAY_END = time (14,55) NIGHT_START = time (21,10) # Night disk start and stop time NIGHT_END = time (10,55) # Parameter list Saved parameter name paramList = ['name',' className', 'author',' vtSymbol', 'initDays',' fixedSize', 'calWindow',' percentile', 'tickValueLimit' 'Multiple'] # list of variables Save the name of the variable varList = ['inited',' trading', 'pos',' longStop', 'shortStop',' posPrice', 'lowerLimit',' packs, 'tickValue',' tradeSign' 'tickValueHigh',' tickValueLow'] # synchronization list Save the variable name syncList = ['pos',' posPrice', 'longStop'' to save to the database 'shortStop'] #-def _ init__ (self, ctaEngine, setting): "Constructor"super (PercentileStrategy) Self). _ init__ (ctaEngine, setting) self.am = ArrayManager (size = self.calWindow) # Note the mutable object attributes in the policy class (usually list, dict, etc.) It needs to be recreated when the policy is initialized. # otherwise, data sharing among multiple policy instances will occur, which may lead to the potential risk of policy logic errors. # these variable object attributes in the policy class can choose not to be written, all under _ _ init__. The main purpose of writing is to read # policies (more of a programming habit) #-def onInit (self): "" "initialization policy (which must be inherited by the user)"self.writeCtaLog (policy initialization of%% s) initData = self.loadBar (self.initDays) for bar in initData: self.onBar (bar) self.putEvent () #--" -- def onStart (self): "Startup policy (must be inherited by the user)" if self.pos = = 0: self.writeCtaLog (uplink% s policy startup'% self.name) # currently has no position " Send open order # hold long position self.putEvent () #-def onStop (self): "" stop policy (must be inherited by the user) "" self.writeCtaLog (Utility% s policy stops'% self.name) self.putEvent () #-def onTick (self Tick): "" receive the TICK push of the market (must be inherited by the user) "if self.lowerLimit = = 0 or self.upperLimit = = 0: self.lowerLimit = tick.lowerLimit self.upperLimit = tick.upperLimit self.bg.updateTick (tick) #--" -def onBar (self Bar): "received Bar push (must be inherited by the user)"# if it is, of course, the last 5 minutes Skip am = self.am am.updateBar (bar) if not am.inited: return # currentTime = datetime.now () .time () currentTime = time (9meme 20) # Compute p And tickValue MaxHigh = am.high / am.open MaxLow = am.low / am.open MaxClose = am.close / am.open lpHigh = np.percentile (MaxHigh, 100-self.percentile) lpLow = np.percentile (MaxLow Self.percentile) self.tickValueHigh = abs (bar.open-bar.open*lpHigh) self.tickValueLow = abs (bar.open-bar.open* lpLow) if self.tickValueHigh > self.tickValueLow and self.tickValueHigh > self.tickValueLimit: self.tradeSign = 1 elif self.tickValueHigh
< self.tickValueLow and self.tickValueLow >Self.tickValueLimit: self.tradeSign =-1 else: self.tradeSign = 0 # level the current position, if the current time is 15: 28 minutes before the end of the day, or 10: 58 minutes in the night, if there is a position, close the position. If ((currentTime > = self.DAY_START and currentTime = self.NIGHT_START and currentTime self.lowerLimit: self.buy (bar.close + 5 Self.fixedSizePowerFalse) elif self.tradeSign =-1 and bar.close
< self.upperLimit: self.short(bar.close - 5,self.fixedSize,False) elif self.pos >0: if self.tradeSign = = 1 or self.tradeSign = 0: pass elif self.tradeSign =-1: self.sell (bar.close-5, abs (self.pos), False) elif self.pos
< 0: if self.tradeSign == -1 or self.tradeSign == 0: pass elif self.tradeSign ==1: self.cover(bar.close+5, abs(self.pos), False) else: if self.pos >0: self.sell (bar.close-5, abs (self.pos), False) elif self.pos < 0: self.cover (bar.close+5, abs (self.pos)) False) elif self.pos = 0: return #-def onOrder (self Order): "received delegated change push (must be inherited by the user)"# for policies that do not require fine-grained delegation control You can ignore onOrder pass #-def onTrade (self Trade): # send out status update event "receive transaction push (must be inherited by user)"# for policies that do not require fine-grained delegation control You can ignore onOrder if trade.offset = = OFFSET_OPEN: self.posPrice = trade.price if self.tradeSign = = 1: self.sell (self.posPrice + self.tickValueHigh,abs (self.pos), False) self.sell (self.posPrice-self.Multiple*self.tickValueHigh, abs (self.pos) True) elif self.tradeSign =-1: self.cover (self.posPrice-self.tickValueLow, abs (self.pos), False) self.cover (self.posPrice + self.Multiple*self.tickValueLow, abs (self.pos)) True) elif trade.offset = = OFFSET_CLOSE: self.cancelAll () self.tradeSign = 0 # synchronize data to the database self.saveSyncData () #-- -def onStopOrder (self So): "stop single push" pass example analysis of the simple implementation of statistics-based trading strategy VNPY is shared here I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.