首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的zipline schedule_function不能用于每月计划?backtest总是每天运行

为什么我的zipline schedule_function不能用于每月计划?backtest总是每天运行
EN

Stack Overflow用户
提问于 2021-03-10 12:11:09
回答 1查看 100关注 0票数 0

我正在尝试在zipline 1.4.1中安排我的重新平衡功能,然而,我的回测是每天运行,而不是按月运行。

实际代码:

代码语言:javascript
复制
#%%
import zipline.api as zapi
from zipline import run_algorithm
import pandas as pd

#%%
def initialize(context):
    zapi.schedule_function(
        rebalance,
        algo.date_rules.month_start(),
        algo.time_rules.market_open(hours=2)
    )

def rebalance(context, data):
    print(f"rebalancing at {zapi.get_datetime()}")

def analyze(context, perf):
    print('We are now analyzing')

#%%
start_date = pd.Timestamp('2010-1-3', tz='UTC')
end_date = pd.Timestamp('2010-4-3', tz='UTC')

result = run_algorithm(
        start = start_date,
        end = end_date,
        initialize=initialize,
        handle_data=rebalance,
        # benchmark_returns=benchmarkReturns,
        capital_base= 10000,
        analyze=analyze,
        data_frequency='daily',
        bundle='csv-bundle')

回测结果:

代码语言:javascript
复制
rebalancing at 2010-01-04 21:00:00+00:00
rebalancing at 2010-01-04 21:00:00+00:00
rebalancing at 2010-01-05 21:00:00+00:00
rebalancing at 2010-01-06 21:00:00+00:00
rebalancing at 2010-01-07 21:00:00+00:00
rebalancing at 2010-01-08 21:00:00+00:00
rebalancing at 2010-01-11 21:00:00+00:00
rebalancing at 2010-01-12 21:00:00+00:00
rebalancing at 2010-01-13 21:00:00+00:00
rebalancing at 2010-01-14 21:00:00+00:00
rebalancing at 2010-01-15 21:00:00+00:00
rebalancing at 2010-01-19 21:00:00+00:00
rebalancing at 2010-01-20 21:00:00+00:00
rebalancing at 2010-01-21 21:00:00+00:00
rebalancing at 2010-01-22 21:00:00+00:00
rebalancing at 2010-01-25 21:00:00+00:00
rebalancing at 2010-01-26 21:00:00+00:00
rebalancing at 2010-01-27 21:00:00+00:00
rebalancing at 2010-01-28 21:00:00+00:00
rebalancing at 2010-01-29 21:00:00+00:00
rebalancing at 2010-02-01 21:00:00+00:00
rebalancing at 2010-02-01 21:00:00+00:00
rebalancing at 2010-02-02 21:00:00+00:00
rebalancing at 2010-02-03 21:00:00+00:00
rebalancing at 2010-02-04 21:00:00+00:00
rebalancing at 2010-02-05 21:00:00+00:00
rebalancing at 2010-02-08 21:00:00+00:00

我尝试将zipline降级到1.3.0版本,得到了同样的结果。有没有人有同样的问题?有什么办法解决这个问题吗?感谢您的好意回复。

EN

回答 1

Stack Overflow用户

发布于 2021-04-01 03:36:34

我也有同样的问题。我不认为schedule_function和zipline一起工作。run_algorithm

我通过创建一个函数来解决这个问题:

代码语言:javascript
复制
def monthly_cal(cal_name,start,end):
xnys=get_calendar(cal_name)
xnys_cal= xnys.sessions_in_range(start,end)
df=pd.DataFrame(index=xnys_cal)
df['month']=pd.DatetimeIndex(df.index).month
trading_days=df['month'][df['month'] != df['month'].shift(-1)].index
return trading_days

并将其加载到初始化中

代码语言:javascript
复制
context.available_dates=monthly_cal('XNYS',start,end)

如果handle_data中的context.get_datetime().date()存在以下情况,则在context.available_dates中检查此条件:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66558257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档