Python 使用zipline运行欧洲股票交易策略的问题
我在使用zipline基于欧洲股票的运营策略方面遇到了问题。我也在Github上提出了这个问题,但到目前为止我还没有得到回复 步骤:Python 使用zipline运行欧洲股票交易策略的问题,python,zipline,Python,Zipline,我在使用zipline基于欧洲股票的运营策略方面遇到了问题。我也在Github上提出了这个问题,但到目前为止我还没有得到回复 步骤: 我将数据下载到CSV文件中(两个文件名为abn.CSV和aex.CSV)。一个人的头看起来像这样: 我修改了扩展名.py文件以包含: 我接受了数据 我运行了以下策略 导致以下错误的原因: KeyError Traceback (most recent call last) pandas/_libs/
扩展名.py
文件以包含:KeyError Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1483315200000000000
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-33-9eaf08e4c73f> in <module>()
----> 1 get_ipython().run_cell_magic('zipline', '--start 2017-1-2 --end 2019-6-28 --capital-base 1050.0 --bundle eu_stocks -o out.pkl', "\ndef initialize(context):\n set_benchmark('aex')\n context.asset = symbol('abn')\n\ndef handle_data(context, data):\n order_target_percent(context.asset, 0.5)")
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2165 magic_arg_s = self.var_expand(line, stack_depth)
2166 with self.builtin_trap:
-> 2167 result = fn(magic_arg_s, cell)
2168 return result
2169
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/__main__.py in zipline_magic(line, cell)
309 '%s%%zipline' % ((cell or '') and '%'),
310 # don't use system exit and propogate errors to the caller
--> 311 standalone_mode=False,
312 )
313 except SystemExit as e:
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py in main(self, args, prog_name, complete_var, standalone_mode, **extra)
695 try:
696 with self.make_context(prog_name, args, **extra) as ctx:
--> 697 rv = self.invoke(ctx)
698 if not standalone_mode:
699 return rv
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py in invoke(self, ctx)
893 """
894 if self.callback is not None:
--> 895 return ctx.invoke(self.callback, **ctx.params)
896
897
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py in invoke(*args, **kwargs)
533 with augment_usage_errors(self):
534 with ctx:
--> 535 return callback(*args, **kwargs)
536
537 def forward(*args, **kwargs):
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/decorators.py in new_func(*args, **kwargs)
15 """
16 def new_func(*args, **kwargs):
---> 17 return f(get_current_context(), *args, **kwargs)
18 return update_wrapper(new_func, f)
19
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/__main__.py in run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, blotter)
274 local_namespace=local_namespace,
275 environ=os.environ,
--> 276 blotter=blotter,
277 )
278
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/utils/run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, data, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter)
167 equity_minute_reader=bundle_data.equity_minute_bar_reader,
168 equity_daily_reader=bundle_data.equity_daily_bar_reader,
--> 169 adjustment_reader=bundle_data.adjustment_reader,
170 )
171
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/data/data_portal.py in __init__(self, asset_finder, trading_calendar, first_trading_day, equity_daily_reader, equity_minute_reader, future_daily_reader, future_minute_reader, adjustment_reader, last_available_session, last_available_minute, minute_history_prefetch_length, daily_history_prefetch_length)
289 self._first_trading_day
290 )
--> 291 if self._first_trading_day is not None else (None, None)
292 )
293
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/trading_calendars/trading_calendar.py in open_and_close_for_session(self, session_label)
758 # http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#datetime-with-tz # noqa
759 return (
--> 760 sched.at[session_label, 'market_open'].tz_localize(UTC),
761 sched.at[session_label, 'market_close'].tz_localize(UTC),
762 )
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/pandas/core/indexing.py in __getitem__(self, key)
1867
1868 key = self._convert_key(key)
-> 1869 return self.obj._get_value(*key, takeable=self._takeable)
1870
1871 def __setitem__(self, key, value):
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/pandas/core/frame.py in _get_value(self, index, col, takeable)
1983
1984 try:
-> 1985 return engine.get_value(series._values, index)
1986 except (TypeError, ValueError):
1987
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
KeyError: Timestamp('2017-01-02 00:00:00+0000', tz='UTC')
keyrerror回溯(最近一次调用)
pandas/_libs/index.pyx在pandas中。_libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
密钥错误:148331520000000000
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在()
---->1 get_ipython().run_cell_magic('zipline')2017年1月2日开始2019年6月28日结束资本基数1050.0——捆绑欧盟股票-o out.pkl',“\ndef initialize(context):\n set_benchmark('aex')\n context.asset=symbol('abn')\n\ndef handle_数据(context,data):\n order_目标百分比(context.asset,0.5)”)
运行单元格中的~/anaconda3/envs/env\u zipline2/lib/python3.5/site-packages/IPython/core/interactiveshell.py(self,magic\u name,line,cell)
2165 magic_arg_s=self.var_expand(行、堆栈深度)
2166带自建存水弯:
->2167结果=fn(魔法参数,单元格)
2168返回结果
2169
~/anaconda3/envs/env_zipline2/lib/python3.5/site packages/zipline/_main_uuu.py在zipline\u magic中(线,单元)
309“%s%%拉链线“%”(单元格或“%”和“%”),
310#不要使用系统退出并向调用者传播错误
-->311独立_模式=错误,
312 )
313除系统退出为e外:
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py在main中(self、args、prog_name、complete_var、standalone_模式、**额外)
695尝试:
696将self.make_上下文(程序名、参数、**额外)作为ctx:
-->697 rv=自调用(ctx)
698如果不是独立_模式:
699返回rv
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py in invoke(self,ctx)
893 """
894如果self.callback不是None:
-->895返回ctx.invoke(self.callback,**ctx.params)
896
897
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py in invoke(*args,**kwargs)
533具有增强使用错误(自我):
534带ctx:
-->535返回回调(*args,**kwargs)
536
537 def forward(*args,**kwargs):
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/decorators.py在new_func(*args,**kwargs)
15 """
16定义新函数(*args,**kwargs):
--->17返回f(获取当前上下文(),*args,**kwargs)
18返回更新包装(新功能,f)
19
运行中的~/anaconda3/envs/env_zipline2/lib/python3.5/site packages/zipline/\uuuu main\uuuuuuuuuuy.py(ctx、algofile、algotext、define、数据频率、大写字母库、捆绑包、捆绑包时间戳、开始、结束、输出、交易日历、打印算法、度量集、本地命名空间、吸墨纸)
274 local_namespace=local_namespace,
275环境=操作系统环境,
-->276吸墨纸=吸墨纸,
277 )
278
~/anaconda3/envs/env\u zipline2/lib/python3.5/site-packages/zipline/utils/run\u algo.py in\u run(处理数据、初始化、交易开始前、分析、algofile、algotext、定义、数据频率、资本基数、数据、捆绑、捆绑时间戳、开始、结束、输出、交易日历、打印算法、度量集、本地名称空间、环境、吸墨纸)
167权益\分钟\读卡器=捆绑\数据。权益\分钟\条形\读卡器,
168权益日读卡器=捆绑日数据。权益日读卡器,
-->169调整_读取器=捆绑_数据。调整_读取器,
170 )
171
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/data/data_portal.py in_uuinit__(self、资产查找器、交易日历、第一个交易日、权益日读卡器、权益分钟读卡器、未来日读卡器、未来分钟读卡器、调整读卡器、最后可用会话、最后可用分钟、分钟历史预取长度、日历史预取长度)
289自我第一交易日
290 )
-->291如果self.\u第一个交易日不是别人(无,无)
292 )
293
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/trading_calendars/trading_calendar.py打开和关闭会话(self,会话标签)
758 # http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#datetime-与tz#noqa
759返回(
-->760附表[session_label,'market_open'].tz_localize(UTC),
761附表[session_label,'market_close'].tz_localize(UTC),
762 )
~/anaconda3/envs/env\u zipline2/lib/python3.5/site-packages/pandas/core/index.py in\uuuuu getitem\uuuuuuu(self,key)
1867
1868键=自转换键(键)
->1869返回self.obj.\u获取值(*key,takeable=self.\u takeable)
1870
1871定义设置项(自身、键、值):
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/pandas/core/frame.py in_get_值(self、index、col、takeable)
1983
1984尝试:
->1985返回引擎。获取_值(系列。_值,索引)
1986除外(类型错误、值错误):
1987
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_value()
熊猫/_libs/inde
import pandas as pd
from zipline.data.bundles import register
from zipline.data.bundles.csvdir import csvdir_equities
start_session = pd.Timestamp('2017-01-02', tz='utc')
end_session = pd.Timestamp('2019-06-28', tz='utc')
# register the bundle
register(
'eu_stocks',
csvdir_equities(
['daily'],
'/path/',
),
calendar_name='XAMS', # Euronext Amsterdam
start_session=start_session,
end_session=end_session
)
def initialize(context):
set_benchmark('aex')
context.asset = symbol('abn')
def handle_data(context, data):
order_target_percent(context.asset, 0.5)
KeyError Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1483315200000000000
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-33-9eaf08e4c73f> in <module>()
----> 1 get_ipython().run_cell_magic('zipline', '--start 2017-1-2 --end 2019-6-28 --capital-base 1050.0 --bundle eu_stocks -o out.pkl', "\ndef initialize(context):\n set_benchmark('aex')\n context.asset = symbol('abn')\n\ndef handle_data(context, data):\n order_target_percent(context.asset, 0.5)")
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2165 magic_arg_s = self.var_expand(line, stack_depth)
2166 with self.builtin_trap:
-> 2167 result = fn(magic_arg_s, cell)
2168 return result
2169
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/__main__.py in zipline_magic(line, cell)
309 '%s%%zipline' % ((cell or '') and '%'),
310 # don't use system exit and propogate errors to the caller
--> 311 standalone_mode=False,
312 )
313 except SystemExit as e:
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py in main(self, args, prog_name, complete_var, standalone_mode, **extra)
695 try:
696 with self.make_context(prog_name, args, **extra) as ctx:
--> 697 rv = self.invoke(ctx)
698 if not standalone_mode:
699 return rv
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py in invoke(self, ctx)
893 """
894 if self.callback is not None:
--> 895 return ctx.invoke(self.callback, **ctx.params)
896
897
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/core.py in invoke(*args, **kwargs)
533 with augment_usage_errors(self):
534 with ctx:
--> 535 return callback(*args, **kwargs)
536
537 def forward(*args, **kwargs):
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/click/decorators.py in new_func(*args, **kwargs)
15 """
16 def new_func(*args, **kwargs):
---> 17 return f(get_current_context(), *args, **kwargs)
18 return update_wrapper(new_func, f)
19
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/__main__.py in run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, blotter)
274 local_namespace=local_namespace,
275 environ=os.environ,
--> 276 blotter=blotter,
277 )
278
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/utils/run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, data, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter)
167 equity_minute_reader=bundle_data.equity_minute_bar_reader,
168 equity_daily_reader=bundle_data.equity_daily_bar_reader,
--> 169 adjustment_reader=bundle_data.adjustment_reader,
170 )
171
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/zipline/data/data_portal.py in __init__(self, asset_finder, trading_calendar, first_trading_day, equity_daily_reader, equity_minute_reader, future_daily_reader, future_minute_reader, adjustment_reader, last_available_session, last_available_minute, minute_history_prefetch_length, daily_history_prefetch_length)
289 self._first_trading_day
290 )
--> 291 if self._first_trading_day is not None else (None, None)
292 )
293
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/trading_calendars/trading_calendar.py in open_and_close_for_session(self, session_label)
758 # http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#datetime-with-tz # noqa
759 return (
--> 760 sched.at[session_label, 'market_open'].tz_localize(UTC),
761 sched.at[session_label, 'market_close'].tz_localize(UTC),
762 )
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/pandas/core/indexing.py in __getitem__(self, key)
1867
1868 key = self._convert_key(key)
-> 1869 return self.obj._get_value(*key, takeable=self._takeable)
1870
1871 def __setitem__(self, key, value):
~/anaconda3/envs/env_zipline2/lib/python3.5/site-packages/pandas/core/frame.py in _get_value(self, index, col, takeable)
1983
1984 try:
-> 1985 return engine.get_value(series._values, index)
1986 except (TypeError, ValueError):
1987
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
KeyError: Timestamp('2017-01-02 00:00:00+0000', tz='UTC')