3.0平台的data模块无法查询历史记录
由csowen创建,最终由csowen 被浏览 4 用户
from bigmodule import M
# <aistudiograph>
# @param(id="m7", name="initialize")
# 交易引擎:初始化函数,只执行一次
def m7_initialize_bigquant_run(context):
# 加载预测数据
pass
# @param(id="m7", name="before_trading_start")
# 交易引擎:每个单位时间开盘前调用一次。
def m7_before_trading_start_bigquant_run(context, data):
# 盘前处理,订阅行情等
pass
# @param(id="m7", name="handle_tick")
# 交易引擎:tick数据处理函数,每个tick执行一次
def m7_handle_tick_bigquant_run(context, tick):
pass
# @param(id="m7", name="handle_data")
def m7_handle_data_bigquant_run(context, data):
dt = data.current_dt.strftime('%Y-%m-%d')
df = context.data[context.data['date']==dt].sort_values('pre_label', ascending=False)
instruments = list(df[df['pre_label']>0].instrument)[:10]
holding = context.get_account_positions()
holding = {k: v for k, v in holding.items() if v.current_qty > 0}
holding_list = list(holding.keys())
# 卖出不在买入池中的股票
for ins in holding_list:
vol_h = data.history(ins, 'volume', bar_count=11, frequency='1d')
print (len(vol_h),'debug',type(vol_h))
print (vol_h[-11])
if ins not in instruments and data.can_trade(ins):
context.order_target(ins, 0)
holding_list.remove(ins)
# 买入持仓中没有的票
for ins in instruments:
if ins not in holding_list and data.can_trade(ins) and len(holding_list)<10:
context.order_target_percent(ins, 1/10)
holding_list.append(ins)
# @param(id="m7", name="handle_trade")
# 交易引擎:成交回报处理函数,每个成交发生时执行一次
def m7_handle_trade_bigquant_run(context, trade):
pass
# @param(id="m7", name="handle_order")
# 交易引擎:委托回报处理函数,每个委托变化时执行一次
def m7_handle_order_bigquant_run(context, order):
pass
# @param(id="m7", name="after_trading")
# 交易引擎:盘后处理函数,每日盘后执行一次
def m7_after_trading_bigquant_run(context, data):
pass
# @module(position="-114,-188", comment="""训练集因子和标签""", comment_collapsed=False)
m2 = M.input_features_dai.v30(
mode="""表达式""",
expr="""pe_ttm
m_lag(close, 90) / close AS return_90
m_lag(close, 30) / close AS return_30
-- 标签
m_lead(close, 5) / m_lead(open, 1) AS _future_return
c_quantile_cont(_future_return, 0.01) AS _future_return_1pct
c_quantile_cont(_future_return, 0.99) AS _future_return_99pct
clip(_future_return, _future_return_1pct, _future_return_99pct) AS label
""",
expr_filters="""-- DAI SQL 算子/函数: https://bigquant.com/wiki/doc/dai-PLSbc1SbZX#h-%E5%87%BD%E6%95%B0
-- 数据&字段: 数据文档 https://bigquant.com/data/home
-- 表达式模式的过滤都是放在 QUALIFY 里, 即数据查询、计算, 最后才到过滤条件
-- c_pct_rank(-return_90) <= 0.3
-- c_pct_rank(return_30) <= 0.3
-- cn_stock_bar1d.turn > 0.02
""",
expr_tables="""cn_stock_prefactors""",
extra_fields="""date, instrument""",
order_by="""date, instrument""",
expr_drop_na=True,
sql="""-- 使用DAI SQL获取数据, 构建因子等, 如下是一个例子作为参考
-- DAI SQL 语法: https://bigquant.com/wiki/doc/dai-PLSbc1SbZX#h-sql%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B
-- 使用数据输入1/2/3里的字段: e.g. input_1.close, input_1.* EXCLUDE(date, instrument)
SELECT
-- 在这里输入因子表达式
-- DAI SQL 算子/函数: https://bigquant.com/wiki/doc/dai-PLSbc1SbZX#h-%E5%87%BD%E6%95%B0
-- 数据&字段: 数据文档 https://bigquant.com/data/home
m_lag(close, 90) / close AS return_90,
m_lag(close, 30) / close AS return_30,
-- 下划线开始命名的列是中间变量, 不会在最终结果输出 (e.g. _rank_return_90)
c_pct_rank(-return_90) AS _rank_return_90,
c_pct_rank(return_30) AS _rank_return_30,
c_rank(volume) AS rank_volume,
close / m_lag(close, 1) as return_0,
-- 日期和股票代码
date, instrument
FROM
-- 预计算因子 cn_stock_bar1d https://bigquant.com/data/datasources/cn_stock_bar1d
cn_stock_prefactors
-- SQL 模式不会自动join输入数据源, 可以根据需要自由灵活的使用
-- JOIN input_1 USING(date, instrument)
WHERE
-- WHERE 过滤, 在窗口等计算算子之前执行
-- 剔除ST股票
st_status = 0
QUALIFY
-- QUALIFY 过滤, 在窗口等计算算子之后执行, 比如 m_lag(close, 3) AS close_3, 对于 close_3 的过滤需要放到这里
-- 去掉有空值的行
COLUMNS(*) IS NOT NULL
-- _rank_return_90 是窗口函数结果,需要放在 QUALIFY 里
AND _rank_return_90 > 0.1
AND _rank_return_30 < 0.1
-- 按日期和股票代码排序, 从小到大
ORDER BY date, instrument
""",
extract_data=False,
m_name="""m2"""
)
# @module(position="-123,-9", comment="""""", comment_collapsed=True)
m3 = M.extract_data_dai.v17(
sql=m2.data,
start_date="""2020-01-01""",
start_date_bound_to_trading_date=False,
end_date="""2020-12-31""",
end_date_bound_to_trading_date=False,
before_start_days=90,
debug=False,
m_name="""m3"""
)
# @module(position="230,-183", comment="""""", comment_collapsed=True)
m6 = M.input_features_dai.v30(
mode="""表达式""",
expr="""pe_ttm
m_lag(close, 90) / close AS return_90
m_lag(close, 30) / close AS return_30""",
expr_filters="""-- DAI SQL 算子/函数: https://bigquant.com/wiki/doc/dai-PLSbc1SbZX#h-%E5%87%BD%E6%95%B0
-- 数据&字段: 数据文档 https://bigquant.com/data/home
-- 表达式模式的过滤都是放在 QUALIFY 里, 即数据查询、计算, 最后才到过滤条件
-- c_pct_rank(-return_90) <= 0.3
-- c_pct_rank(return_30) <= 0.3
-- cn_stock_bar1d.turn > 0.02
""",
expr_tables="""cn_stock_prefactors""",
extra_fields="""date, instrument""",
order_by="""date, instrument""",
expr_drop_na=True,
sql="""-- 使用DAI SQL获取数据, 构建因子等, 如下是一个例子作为参考
-- DAI SQL 语法: https://bigquant.com/wiki/doc/dai-PLSbc1SbZX#h-sql%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B
-- 使用数据输入1/2/3里的字段: e.g. input_1.close, input_1.* EXCLUDE(date, instrument)
SELECT
-- 在这里输入因子表达式
-- DAI SQL 算子/函数: https://bigquant.com/wiki/doc/dai-PLSbc1SbZX#h-%E5%87%BD%E6%95%B0
-- 数据&字段: 数据文档 https://bigquant.com/data/home
m_lag(close, 90) / close AS return_90,
m_lag(close, 30) / close AS return_30,
-- 下划线开始命名的列是中间变量, 不会在最终结果输出 (e.g. _rank_return_90)
c_pct_rank(-return_90) AS _rank_return_90,
c_pct_rank(return_30) AS _rank_return_30,
c_rank(volume) AS rank_volume,
close / m_lag(close, 1) as return_0,
-- 日期和股票代码
date, instrument
FROM
-- 预计算因子 cn_stock_bar1d https://bigquant.com/data/datasources/cn_stock_bar1d
cn_stock_prefactors
-- SQL 模式不会自动join输入数据源, 可以根据需要自由灵活的使用
-- JOIN input_1 USING(date, instrument)
WHERE
-- WHERE 过滤, 在窗口等计算算子之前执行
-- 剔除ST股票
st_status = 0
QUALIFY
-- QUALIFY 过滤, 在窗口等计算算子之后执行, 比如 m_lag(close, 3) AS close_3, 对于 close_3 的过滤需要放到这里
-- 去掉有空值的行
COLUMNS(*) IS NOT NULL
-- _rank_return_90 是窗口函数结果,需要放在 QUALIFY 里
AND _rank_return_90 > 0.1
AND _rank_return_30 < 0.1
-- 按日期和股票代码排序, 从小到大
ORDER BY date, instrument
""",
extract_data=False,
m_name="""m6"""
)
# @module(position="265,-36", comment="""""", comment_collapsed=True)
m5 = M.extract_data_dai.v17(
sql=m6.data,
start_date="""2021-01-01""",
start_date_bound_to_trading_date=False,
end_date="""2021-12-31""",
end_date_bound_to_trading_date=False,
before_start_days=90,
debug=False,
m_name="""m5"""
)
# @module(position="66,119", comment="""""", comment_collapsed=True)
m1 = M.randomforest.v2(
input_1=m3.data,
input_2=m5.data,
n_estimators=10,
criterion="""squared_error(回归)""",
max_depth=20,
min_samples_split=2,
min_samples_leaf=2,
max_features=0.9,
random=0,
render_chart=True,
m_name="""m1"""
)
# @module(position="105,266", comment="""""", comment_collapsed=True)
m7 = M.bigtrader.v25(
data=m1.prediction,
start_date="""""",
end_date="""""",
initialize=m7_initialize_bigquant_run,
before_trading_start=m7_before_trading_start_bigquant_run,
handle_tick=m7_handle_tick_bigquant_run,
handle_data=m7_handle_data_bigquant_run,
handle_trade=m7_handle_trade_bigquant_run,
handle_order=m7_handle_order_bigquant_run,
after_trading=m7_after_trading_bigquant_run,
capital_base=1000000,
frequency="""daily""",
product_type="""股票""",
rebalance_period_type="""交易日""",
rebalance_period_days="""1""",
rebalance_period_roll_forward=True,
backtest_engine_mode="""标准模式""",
before_start_days=0,
volume_limit=1,
order_price_field_buy="""open""",
order_price_field_sell="""close""",
benchmark="""沪深300指数""",
plot_charts=True,
debug=False,
backtest_only=False,
m_name="""m7"""
)
完整的代码,运行出错,
[2025-01-27 10:55:23] INFO: input_features_dai.v30 开始运行 ..[2025-01-27 10:55:23] INFO: input_features_dai.v30 命中缓存[2025-01-27 10:55:23] INFO: input_features_dai.v30 运行完成 [0.221s].[2025-01-27 10:55:23] INFO: extract_data_dai.v17 开始运行 ..[2025-01-27 10:55:23] INFO: extract_data_dai.v17 命中缓存[2025-01-27 10:55:23] INFO: extract_data_dai.v17 运行完成 [0.180s].[2025-01-27 10:55:23] INFO: input_features_dai.v30 开始运行 ..[2025-01-27 10:55:23] INFO: input_features_dai.v30 命中缓存[2025-01-27 10:55:23] INFO: input_features_dai.v30 运行完成 [0.169s].[2025-01-27 10:55:23] INFO: extract_data_dai.v17 开始运行 ..[2025-01-27 10:55:23] INFO: extract_data_dai.v17 命中缓存[2025-01-27 10:55:23] INFO: extract_data_dai.v17 运行完成 [0.189s].[2025-01-27 10:55:23] INFO: randomforest.v2 开始运行 ..[2025-01-27 10:55:24] INFO: randomforest.v2 命中缓存[2025-01-27 10:55:24] INFO: randomforest.v2 运行完成 [0.269s].[2025-01-27 10:55:24] INFO: bigtrader.v25 开始运行 ..[2025-01-27 10:55:24] INFO: read input 'data' ..[2025-01-27 10:55:24] INFO: 2021-02-22, 2021-12-31, instruments=4444[2025-01-27 10:55:24] INFO: bigtrader module V2.1.0[2025-01-27 10:55:24] INFO: bigtrader engine v1.10.10 2024-12-253 debug <class 'pandas.core.series.Series'>
您可以去社区论坛问答交流板块反馈咨询 去发帖>>
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[4], line 231
216 m1 = M.randomforest.v2(
217 input_1=m3.data,
218 input_2=m5.data,
(...)
227 m_name="""m1"""
228 )
230 # @module(position="105,266", comment="""""", comment_collapsed=True)
--> 231 m7 = M.bigtrader.v25(
232 data=m1.prediction,
233 start_date="""""",
234 end_date="""""",
235 initialize=m7_initialize_bigquant_run,
236 before_trading_start=m7_before_trading_start_bigquant_run,
237 handle_tick=m7_handle_tick_bigquant_run,
238 handle_data=m7_handle_data_bigquant_run,
239 handle_trade=m7_handle_trade_bigquant_run,
240 handle_order=m7_handle_order_bigquant_run,
241 after_trading=m7_after_trading_bigquant_run,
242 capital_base=1000000,
243 frequency="""daily""",
244 product_type="""股票""",
245 rebalance_period_type="""交易日""",
246 rebalance_period_days="""1""",
247 rebalance_period_roll_forward=True,
248 backtest_engine_mode="""标准模式""",
249 before_start_days=0,
250 volume_limit=1,
251 order_price_field_buy="""open""",
252 order_price_field_sell="""close""",
253 benchmark="""沪深300指数""",
254 plot_charts=True,
255 debug=False,
256 backtest_only=False,
257 m_name="""m7"""
258 )
259 # </aistudiograph>
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/modules.py:28, in __call__(self, **kwargs)
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:203, in module_invoke(name, version, kwargs)
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:169, in _module_invoke(name, version, kwargs)
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:41, in _module_run(module, kwargs)
File dist/build/bigtrader/v25/__init__.py:373, in v25.run()
File dist/build/bigtrader/v25/__init__.py:180, in v25._run()
File dist/build/bigtrader/v25/__init__.py:168, in v25._run_backtest()
File dist/build/bigtrader/v25/core/pybacktest/__init__.py:485, in v25.core.pybacktest.BigQuantModule.run()
File dist/build/bigtrader/v25/core/pybacktest/__init__.py:432, in v25.core.pybacktest.BigQuantModule.run_algo()
File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:649, in run_backtest(start_date, end_date, strategy, strategy_setting, capital_base, frequency, market, product_type, instruments, options_data, **kwargs)
635 capital_base = kwargs.pop("capital")
637 rt = RunTrading(RunMode.BACKTEST,
638 strategy=strategy,
639 strategy_setting=strategy_setting,
(...)
647 options_data=options_data,
648 **kwargs)
--> 649 return rt.run(**kwargs)
File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:409, in RunTrading.run(self, **kwargs)
406 return
408 if self.trading_env.run_mode == RunMode.BACKTEST:
--> 409 return self._run_backtest(**kwargs)
410 else:
411 if not self.trading_env.account_id:
File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:436, in RunTrading._run_backtest(self, **kwargs)
434 debug_print("run_backtest: running...")
435 t0 = time.time()
--> 436 bkt_engine.run()
437 cost_time = round(time.time() - t0, 3)
438 if show_debug_info:
File bigtrader/strategy/backtest_engine.py:259, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.run()
File bigtrader/strategy/backtest_engine.py:512, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.transform()
File bigtrader/strategy/backtest_engine.py:474, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.transform.replay_bars_dt()
File bigtrader/event/event_engine.py:88, in bigtrader2.bigtrader.event.event_engine.EventEngine.process_events()
...
1101 # GH#50617
1102 "Series.__getitem__ treating keys as positions is deprecated. "
(...)
1107 stacklevel=find_stack_level(),
1108 )
-> 1109 return self._values[key]
1111 elif key_is_scalar:
1112 return self._get_value(key)
IndexError: index -11 is out of bounds for axis 0 with size 3
Output is truncated. View as a open in a text editor. Adjust cell output settings...
\