日期时间管理 – backtrader中文教程
日期时间Management
直到释放1.5.0, backtrader使用直接的办法,以时间管理,无论日期时间是由data sources是只需在脸上value.
与同为任何用户输入等中的情况下,parameter
fromdate
(或sessionstart
),其可以提供给任何data source
该方法被罚款给出了冰冻数据源的直接控制回溯测试。这是很容易假设输入日期时间里已经拍摄他们进入system.
但随着1.5.0,live data sources的支持和这个部队采取考虑日期时间management。这样的管理就没有必要了,如果下面总是true:
从上面直接输入 – 输出日期时间的方法会工作,becase的了贸易商,在柏林例如,总是可以做这样的事情:
class Strategy(bt.Strategy): def next(self): # The DAX future opens at 08:00 CET if self.data.datetime.time() < datetime.time(8, 30): # don"t operate until the market has been running 30 minutes return #
用直接的方法表面的问题时,在柏林这名交易员决定交易的ES-Mini
。因为改变从DST发生在不同的时间点在今年,这将导致时间差是同步年内几个星期的。该总会以下不工作:
class Strategy(bt.Strategy): def next(self): # The SPX opens at 09:30 US/Eastern all year long # This is most of the year 15:30 CET # But it is sometimes 16:30 CET or 14:30 CET if a DST switch on-off # has happened in the USA and not in Europe # That"s why the code below is unreliable if self.data.datetime.time() < datetime.time(16, 0): # don"t operate until the market has been running 30 minutes return #
用timezones
To操作解决上述情况,仍保持与兼容直接输入输出时间的方法,backtrader
为最终用户提供的following
Datetime Input
- As一个默认的平台不会接触datetime由数据source
- The终端用户提供可以覆盖该输入:
- 提供一个
tzinput
参数到数据源。这必须是一个对象与datetime.tzinfo
接口兼容。最有可能的用户将提供一个pytz.timezone
instance
有了这个决定采用国内的时候
backtrader
是考虑是在UTC-like
的格式,即:- 如果数据源已经通过
UTC
已经存储在它 - format
tzinput
- After转换’这不是真的
UTC
但它是用户的参考,因此,UTC-like
日期时间output
- If数据馈送可以自动确定时区的输出,这将是default
This有意义的活的饲料的情况下,特别是在使用情况下,像一个在其中贸易商柏林(
CET
时区),换产品与US/Eastern
timezone.Because交易者总是得到正确的时间在上面的例子中opening时间
09:30 US/Eastern
,而不是保持不变15:30 CET
最多的一年,但有时16:30 CET
有时14:30 CET
. - 如果不能确定,则输出将是什么确定输入时(在
UTC-like
)time - The终端用户可以覆盖和确定用于output
- Providing实际的时区一个
tz
参数到数据源。这必须是一个对象与datetime.tzinfo
接口兼容。最有可能的用户将提供一个pytz.timezone
instance
- Providing实际的时区一个
Note
Input fromt如例如参数`用户fromdate
或sessionstart
预计将同步与实际tz
,无论是由data source自动计算,由用户提供或左为默认(None
,这意味着直接输入 – 输出datetime)
有了这些想法让我们回忆一下柏林商人,买卖US/Eastern
:
import pytz import bt data = bt.feeds.MyFeed("ES-Mini", tz=pytz.timezone("US/Eastern")) class Strategy(bt.Strategy): def next(self): # This will work all year round. # The data source will return in the frame of the "US/Eastern" time # zone and the user is quoting "10:00" as reference time # Because in the "US/Eastern" timezone the SPX index always starts # trading at 09:30, this will always work if self.data.datetime.time() < datetime.time(10, 0): # don"t operate until the market has been running 30 minutes return #
在的数据source其可以自动地确定输出的情况下时区:
import bt data = bt.feeds.MyFeedAutoTZ("ES-Mini") class Strategy(bt.Strategy): def next(self): # This will work all year round. # The data source will return in the frame of the "US/Eastern" time # zone and the user is quoting "10:00" as reference time # Because in the "US/Eastern" timezone the SPX index always starts # trading at 09:30, this will always work if self.data.datetime.time() < datetime.time(10, 0): # don"t operate until the market has been running 30 minutes return #
比above.
Obviously工作甚至更少MyFeed
和MyFeedAuto
在上述的例子只是虚设names.
!注意:
At the time of writing the only data source included in the distribution which can automatically determine the timezone is the one connecting to *Interactive Brokers*
评论被关闭。