In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the minute line to synthesize the day K line in the strategy of VNPY. The article is very detailed and has a certain reference value. Interested friends must read it!
In the forum to see a lot of discussion about the minute synthesis daily line, but also try to achieve. This is for vnpy2.0, but 1.92 is pretty much the same.
Here, the synthetic daily line HLOC information is put in the pandas.DataFrame, because if the daily line analysis is not very high, DataFrame is enough.
The synthesis process is put in the on_bar method, and daily lines are merged for each incoming minute.
Here, trading = = False is used to judge, that is, daily merging of historical data is performed only during the policy initialization process. In the course of the transaction, it is not necessary to pass in the minute data on the same day for processing. Because the daily line is long-period data, it can be called and used when the program starts. There is no need for intraday analysis. If you really want to, you can comment out this.
Here only provides the DataFrame daily line data, how to analyze it depends on the user.
Scheduled for dayFrame to store daily data, insert global variables in the policy definition
Class * Strategy (CtaTemplate): author = "traders with Python" import pandas as pd dayFrame = pd.DataFrame (columns= ['datetime',' high', 'low',' open', 'close'])
During the merge process, insert the following code into the on_bar method
Def on_bar (self, bar: BarData): "if self.trading = = False: # adjustedBarTime = bar.datetime + timedelta (hours = 5) if self.dayFrame.empty: # if dayFrame is empty First add a self.dayFrame = self.dayFrame.append ({'datetime': bar.datetime.date (),' high':bar.high_price, 'low': bar.low_price,' open': bar.open_price, 'close': bar.close_price}) Ignore_index=True) else: self.dayFrame = self.dayFrame.sort_values (['datetime']) .reset_index (drop=True) # if dayFrame is not empty Sort by date first, if bar.datetime.date () in self.dayFrame ['datetime'] .values: # if it is an existing date, compare with high,low update And use the new close self.dayFrame.loc [self.dayFrame ['datetime'] = = bar.datetime.date (),' high'] =\ max (max (self.dayFrame.loc [self.dayFrame ['datetime'] = = bar.datetime.date (),' high'] .values), bar.high_price) self.dayFrame.loc [self.dayFrame ['datetime'] = = bar.datetime.date () 'low'] =\ min (min (self.dayFrame.loc [self.dayFrame [' datetime'] = = bar.datetime.date (), 'low'] .values), bar.low_price) self.dayFrame.loc [self.dayFrame [' datetime'] = = bar.datetime.date (), 'close'] = bar.close_price else: # if it is a new date Create a new self.dayFrame = self.dayFrame.append ({'datetime': bar.datetime.date (),' high': bar.high_price, 'low': bar.low_price,' open': bar.open_price, 'close': bar.close_price}, ignore_index=True)
In addition, the default here is Natural Day. If you want to follow the domestic futures time, that is, the opening time at 9: 00 p. M. is the next day. One trick is to add 5 hours of bar time, and 9: 00 p. M. becomes 1: 00 tomorrow, so that dataframe will be stored as data for the next day. And 15:00 plus 20:00 for the day is still the same day's data.
However, this change is very rough, can only support domestic time and domestic futures, if the server is in other time zones, or other products will be analyzed separately.
Modify the method to define the local variable adjustedBarTime, which is passed in bar.datetime time plus 5; instead of all the bar.datetime places used in the new code.
I think of a problem on Friday. For domestic futures, the data on Friday night belongs to Monday. It is really big to think about it. Here, we have to add one day to judge whether the next day is Saturday, and if so, it will be changed to two days. Fortunately, there is no night trading on the trading day before holidays, otherwise it will be more troublesome.
From datetime import datetime, timedeltaif self.trading = = False: adjustedBarTime = bar.datetime + timedelta (hours = 5) if adjustedBarTime.weekday () is 5: adjustedBarTime = adjustedBarTime + timedelta (days= 2)
The following data will be inserted, and according to the number of days of initialization, there will be as many as possible.
The above is all the content of the article "how to use minute Line to synthesize Daily K Line in VNPY Strategy". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to 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.