【问题标题】:zipline backtesting using non-US (European) intraday data使用非美国(欧洲)日内数据进行 zipline 回测
【发布时间】:2014-09-29 16:34:18
【问题描述】:

我正在尝试让 zipline 处理已加载到 pandas DataFrame 中的非美国盘中数据:

                        BARC    HSBA     LLOY     STAN
Date                                                  
2014-07-01 08:30:00  321.250  894.55  112.105  1777.25
2014-07-01 08:32:00  321.150  894.70  112.095  1777.00
2014-07-01 08:34:00  321.075  894.80  112.140  1776.50
2014-07-01 08:36:00  321.725  894.80  112.255  1777.00
2014-07-01 08:38:00  321.675  894.70  112.290  1777.00

我遵循移动平均线教程here,将“AAPL”替换为我自己的符号代码,并将历史调用替换为“1m”数据而不是“1d”。

然后我使用algo_obj.run(DataFrameSource(mydf)) 进行最终调用,其中mydf 是上面的数据框。

但是,与TradingEnvironment 相关的各种问题都出现了。根据源码:

# This module maintains a global variable, environment, which is
# subsequently referenced directly by zipline financial
# components. To set the environment, you can set the property on
# the module directly:
#       from zipline.finance import trading
#       trading.environment = TradingEnvironment()
#
# or if you want to switch the environment for a limited context
# you can use a TradingEnvironment in a with clause:
#       lse = TradingEnvironment(bm_index="^FTSE", exchange_tz="Europe/London")
#       with lse:
# the code here will have lse as the global trading.environment
#           algo.run(start, end)

但是,使用上下文似乎并不完全有效。我仍然收到错误消息,例如说我的时间戳是在市场开盘之前(事实上,查看trading.environment.open_and_close 的时间是针对美国市场的。

我的问题是,有没有人设法将 zipline 用于非美国的日内数据?您能否指出一个资源以及如何执行此操作的理想示例代码?

n.b.我在 github 上看到了一些 tests 似乎与交易日历相关(tradincalendar_lse.py、tradingcalendar_tse.py 等)——但这似乎只处理日常数据。我需要修复:

  • 开/关时间
  • 基准的参考数据
  • 可能还有更多...

【问题讨论】:

  • 我有同样的经历 - zipline 对外部加载的数据仍然很不友好 =( 所以切换到 ad-hoc 解决方案。
  • @Luciano:你对这个有什么想法吗:stackoverflow.com/questions/32912392/…

标签: hft zipline


【解决方案1】:

@卢西亚诺

您可以在代码末尾添加analyze(None, perf_manual),以自动运行分析过程。

【讨论】:

  • 太棒了,我为此搜索了高低。非常感谢!
【解决方案2】:

在摆弄教程笔记本后,我已经完成了这项工作。下面的代码示例。它使用 DF mid,如原始问题中所述。有几点值得一提:

  1. 交易日历我手动创建一个并分配给trading.environment,使用tradingcalendar_lse.py中的non_working_days。或者,您可以创建一个完全适合您的数据的数据(但是对于样本外数据可能是一个问题)。您需要定义两个字段:trading_daysopen_and_closes

  2. sim_params 默认开始/结束值存在问题,因为它们不支持时区。因此,您必须创建一个 sim_params 对象并使用时区传递开始/结束参数。

  3. 此外,必须使用参数 overwrite_sim_params=False 调用 run(),因为 calculate_first_open/close 会引发时间戳错误。

我应该提一下,还可以传递 pandas 面板数据,在小轴中包含字段 open、high、low、close、price 和 volume。但在这种情况下,前一个字段是强制性的 - 否则会引发错误。

请注意,此代码仅生成每日性能摘要。我确信必须有一种方法可以在一分钟内获得结果(我认为这是由emission_rate 设置的,但显然不是)。如果有人知道请发表评论,我会更新代码。 此外,不确定调用“分析”的 api 调用是什么(即在 IPython 中使用 %%zipline 魔术时,如教程中所示,analyze() 方法会自动调用。我该如何手动执行此操作?)

import pytz
from datetime import datetime

from zipline.algorithm import TradingAlgorithm
from zipline.utils import tradingcalendar
from zipline.utils import tradingcalendar_lse
from zipline.finance.trading import TradingEnvironment
from zipline.api import order_target, record, symbol, history, add_history
from zipline.finance import trading

def initialize(context):
    # Register 2 histories that track daily prices,
    # one with a 100 window and one with a 300 day window
    add_history(10, '1m', 'price')
    add_history(30, '1m', 'price')

    context.i = 0


def handle_data(context, data):
    # Skip first 30 mins to get full windows
    context.i += 1
    if context.i < 30:
        return

    # Compute averages
    # history() has to be called with the same params
    # from above and returns a pandas dataframe.
    short_mavg = history(10, '1m', 'price').mean()
    long_mavg = history(30, '1m', 'price').mean()

    sym = symbol('BARC')

    # Trading logic
    if short_mavg[sym] > long_mavg[sym]:
        # order_target orders as many shares as needed to
        # achieve the desired number of shares.
        order_target(sym, 100)
    elif short_mavg[sym] < long_mavg[sym]:
        order_target(sym, 0)

    # Save values for later inspection
    record(BARC=data[sym].price,
           short_mavg=short_mavg[sym],
           long_mavg=long_mavg[sym])

def analyze(context,perf) : 
    perf["pnl"].plot(title="Strategy P&L")

# Create algorithm object passing in initialize and
# handle_data functions

# This is needed to handle the correct calendar. Assume that market data has the right index for tradeable days.
# Passing in env_trading_calendar=tradingcalendar_lse doesn't appear to work, as it doesn't implement open_and_closes
from zipline.utils import tradingcalendar_lse
trading.environment = TradingEnvironment(bm_symbol='^FTSE', exchange_tz='Europe/London')
#trading.environment.trading_days = mid.index.normalize().unique()
trading.environment.trading_days = pd.date_range(start=mid.index.normalize()[0],
                                                 end=mid.index.normalize()[-1],
                                                 freq=pd.tseries.offsets.CDay(holidays=tradingcalendar_lse.non_trading_days))

trading.environment.open_and_closes = pd.DataFrame(index=trading.environment.trading_days,columns=["market_open","market_close"])
trading.environment.open_and_closes.market_open = (trading.environment.open_and_closes.index + pd.to_timedelta(60*7,unit="T")).to_pydatetime()
trading.environment.open_and_closes.market_close = (trading.environment.open_and_closes.index + pd.to_timedelta(60*15+30,unit="T")).to_pydatetime()


from zipline.utils.factory import create_simulation_parameters
sim_params = create_simulation_parameters(
   start = pd.to_datetime("2014-07-01 08:30:00").tz_localize("Europe/London").tz_convert("UTC"),  #Bug in code doesn't set tz if these are not specified (finance/trading.py:SimulationParameters.calculate_first_open[close])
   end = pd.to_datetime("2014-07-24 16:30:00").tz_localize("Europe/London").tz_convert("UTC"),
   data_frequency = "minute",
   emission_rate = "minute",
   sids = ["BARC"])
algo_obj = TradingAlgorithm(initialize=initialize, 
                            handle_data=handle_data,
                            sim_params=sim_params)

# Run algorithm
perf_manual = algo_obj.run(mid,overwrite_sim_params=False) # overwrite == True calls calculate_first_open[close] (see above)

【讨论】:

    猜你喜欢
    • 2011-09-18
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多