@@ -203,8 +203,8 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
detail : " 支持按交易周或交易月调仓,例如 rebalance.weekly(weekday=5).at([ \" 10:18 \" ])、rebalance.weekly(tradingday=-1).at([ \" 10:18 \" ])、rebalance.monthly(tradingday=1).at([ \" 10:18 \" ])。`.at([...])` 的最后一个时刻会编进分钟级 schedule/time_rule;当前平台把 on_day 近似到 10:18,把 open_auction 近似到 09:31。 " . to_string ( ) ,
} ,
ManualSection {
title : " bar / tick 生命周期 " . to_string ( ) ,
detail : " 回测内核支持 平台内核 风格的 bar/tick 生命周期:日内会发布 pre_bar/bar/post_bar 过程事件;存在 tick 订阅或 tick 调度规则时,会按 execution_quotes 的时间顺序发布 pre_tick/tick/post_tick,并把 tick 阶段下单限制在当前 tick 时间窗内撮合。平台 DSL 中可通过 subscribe([...])、trading.subscription_guard(true) 和 process_event 字段配合显式订单模拟 tick 订阅策略。 " . to_string ( ) ,
title : " bar / minute execution 生命周期 " . to_string ( ) ,
detail : " 回测内核支持 平台内核 风格的 bar/分钟执行价 生命周期:日内会发布 pre_bar/bar/post_bar 过程事件;存在分钟执行价订阅或分钟 调度规则时,会按 execution_quotes 的时间顺序发布 pre_tick/tick/post_tick 兼容事件,并把日内 阶段下单限制在当前分钟执行价 时间窗内撮合。平台 DSL 中可通过 subscribe([...])、trading.subscription_guard(true) 和 process_event 字段配合显式订单模拟日内 订阅策略。 " . to_string ( ) ,
} ,
ManualSection {
title : " selection.market_cap_band / selection.limit / ordering.rank_by / ordering.rank_expr " . to_string ( ) ,
@@ -220,7 +220,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
} ,
ManualSection {
title : " execution.matching_type / execution.slippage " . to_string ( ) ,
detail : " 设置撮合模式和滑点。支持 execution.matching_type( \" next_tick_last \" | \" next_tick_best_own \" | \" next_tick_best_counterparty \" | \" counterparty_offer \" | \" vwap \" | \" current_bar_close \" | \" next_bar_open \" | \" open_auction \" )。其中 next_tick_last 使用 tick 的 last_price; next_tick _best_own / next_tick _best_counterparty 会按 L1 买一卖一近似 平台内核 的 tick 最优价语义;counterparty_offer 在存在 order_book_depth 多档盘口数据时会按真实档位逐档扫单并计算加权成交价,不存在 depth 时回退 L1 对手方报价;vwap 会在盘中执行价链路上聚合多笔成交为单条 VWAP 成交;next_bar_open 使用决策日信号并在下一可交易日开盘撮合,禁止把执行日 open/high/low/close 解释为下单前已知数据;open_auction 使用当日集合竞价开盘价 day_open 进行撮合,且不额外施加滑点,并按竞价成交量而不是盘口一档流动性限制成交;滑点支持 execution.slippage( \" none \" ) / execution.slippage( \" price_ratio \" , 0.001) / execution.slippage( \" tick_size \" , 1) / execution.slippage( \" limit_price \" ),其中 limit_price 会在限价单成交时按挂单价模拟 平台内核 的最坏成交价。 " . to_string ( ) ,
detail : " 设置撮合模式和滑点。优先使用 execution.matching_type( \" next_minute_last \" | \" next_minute_best_own \" | \" next_minute_best_counterparty \" | \" counterparty_offer \" | \" vwap \" | \" current_bar_close \" | \" next_bar_open \" | \" open_auction \" );旧的 next_tick_* 仅作为兼容别名,内部仍读取分钟执行价。next_minute_last 使用分钟执行价 last_price; next_minute _best_own / next_minute _best_counterparty 会按 L1 买一卖一近似 平台内核 的最优价语义;counterparty_offer 在存在 order_book_depth 多档盘口数据时会按真实档位逐档扫单并计算加权成交价,不存在 depth 时回退 L1 对手方报价;vwap 会在盘中执行价链路上聚合多笔成交为单条 VWAP 成交;next_bar_open 使用决策日信号并在下一可交易日开盘撮合,禁止把执行日 open/high/low/close 解释为下单前已知数据;open_auction 使用当日集合竞价开盘价 day_open 进行撮合,且不额外施加滑点,并按竞价成交量而不是盘口一档流动性限制成交;滑点支持 execution.slippage( \" none \" ) / execution.slippage( \" price_ratio \" , 0.001) / execution.slippage( \" tick_size \" , 1) / execution.slippage( \" limit_price \" ),其中 limit_price 会在限价单成交时按挂单价模拟 平台内核 的最坏成交价。 " . to_string ( ) ,
} ,
ManualSection {
title : " 期货提交校验 " . to_string ( ) ,
@@ -228,7 +228,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
} ,
ManualSection {
title : " trading.rotation / order.* / cancel.* / update_universe / subscribe " . to_string ( ) ,
detail : " 支持显式下单、撤单、AlgoOrder、动态 universe 和账户资金动作。可以用 trading.rotation(false) 关闭默认轮动链路,再用 trading.stage( \" open_auction \" | \" on_day \" ) 指定执行阶段;需要模拟 平台内核 的 tick 订阅保护时,可写 trading.subscription_guard(true),未订阅 symbol 的显式订单会被拦截,TargetPortfolioSmart + AlgoOrder 会过滤未订阅标的。用 trading.schedule.daily().at([ \" 10:18 \" ]) / trading.schedule.weekly(weekday=5).at([ \" 10:18 \" ]) / trading.schedule.weekly(tradingday=-1).at([ \" 10:18 \" ]) / trading.schedule.monthly(tradingday=1).at([ \" 10:18 \" ]) 指定触发频率和分钟级 time_rule,然后写 order.shares( \" 600000.SH \" , 1000)、order.target_shares( \" 600000.SH \" , 2000)、order.value( \" 600000.SH \" , cash * 0.25)、order.target_percent( \" 600000.SH \" , 0.05)、order.limit_value( \" 600000.SH \" , cash * 0.25, open * 0.99)、order.vwap_value( \" 600000.SH \" , cash * 0.25, \" 09:31 \" , \" 09:40 \" )、order.twap_percent( \" 600000.SH \" , 0.05, \" 10:00 \" , \" 10:30 \" )、order.target_portfolio_smart(weights={ \" 600000.SH \" : 0.3, \" 000001.SZ \" : 0.2}, order_prices=VWAPOrder(930, 940), valuation_prices={ \" 600000.SH \" : prev_close})、order.target_portfolio_smart(weights={ \" 600000.SH \" : 0.3, \" 000001.SZ \" : 0.2}, order_prices={ \" 600000.SH \" : open * 0.99}, valuation_prices={ \" 600000.SH \" : prev_close})、cancel.order(12345)、cancel.symbol( \" 600000.SH \" )、cancel.all()、update_universe([ \" 600000.SH \" , \" 000001.SZ \" ])、subscribe([ \" 000001.SZ \" ])、unsubscribe([ \" 000001.SZ \" ])、account.deposit_withdraw(100000, receiving_days=0)、account.finance_repay(50000)、account.set_management_fee_rate(0.001)。其中 order.target_shares(...) 对应 平台内核 的 order_to, order.target_portfolio_smart(...) 对应 平台内核 的 order_target_portfolio_smart 批量目标权重语义;account.deposit_withdraw(...) 和 account.finance_repay(...) 对应 平台内核 账户出入金与融资/还款语义;order_prices 既可以是逐标的限价映射,也可以是 VWAPOrder/TWAPOrder 这类全局 AlgoOrder; order.vwap_* / order.twap_* 对应 平台内核 的 AlgoOrder 时间窗订单风格,而 update_universe/subscribe/unsubscribe 对应 平台内核 的动态 universe 与订阅接口。symbol 使用标准证券代码;数量、金额、仓位、时间窗、限价、order_id 和 symbol 列表都支持表达式;这些语句也支持放进 when/unless 条件块。 " . to_string ( ) ,
detail : " 支持显式下单、撤单、AlgoOrder、动态 universe 和账户资金动作。可以用 trading.rotation(false) 关闭默认轮动链路,再用 trading.stage( \" open_auction \" | \" on_day \" ) 指定执行阶段;需要模拟 平台内核 的日内 订阅保护时,可写 trading.subscription_guard(true),未订阅 symbol 的显式订单会被拦截,TargetPortfolioSmart + AlgoOrder 会过滤未订阅标的。用 trading.schedule.daily().at([ \" 10:18 \" ]) / trading.schedule.weekly(weekday=5).at([ \" 10:18 \" ]) / trading.schedule.weekly(tradingday=-1).at([ \" 10:18 \" ]) / trading.schedule.monthly(tradingday=1).at([ \" 10:18 \" ]) 指定触发频率和分钟级 time_rule,然后写 order.shares( \" 600000.SH \" , 1000)、order.target_shares( \" 600000.SH \" , 2000)、order.value( \" 600000.SH \" , cash * 0.25)、order.target_percent( \" 600000.SH \" , 0.05)、order.limit_value( \" 600000.SH \" , cash * 0.25, open * 0.99)、order.vwap_value( \" 600000.SH \" , cash * 0.25, \" 09:31 \" , \" 09:40 \" )、order.twap_percent( \" 600000.SH \" , 0.05, \" 10:00 \" , \" 10:30 \" )、order.target_portfolio_smart(weights={ \" 600000.SH \" : 0.3, \" 000001.SZ \" : 0.2}, order_prices=VWAPOrder(930, 940), valuation_prices={ \" 600000.SH \" : prev_close})、order.target_portfolio_smart(weights={ \" 600000.SH \" : 0.3, \" 000001.SZ \" : 0.2}, order_prices={ \" 600000.SH \" : open * 0.99}, valuation_prices={ \" 600000.SH \" : prev_close})、cancel.order(12345)、cancel.symbol( \" 600000.SH \" )、cancel.all()、update_universe([ \" 600000.SH \" , \" 000001.SZ \" ])、subscribe([ \" 000001.SZ \" ])、unsubscribe([ \" 000001.SZ \" ])、account.deposit_withdraw(100000, receiving_days=0)、account.finance_repay(50000)、account.set_management_fee_rate(0.001)。其中 order.target_shares(...) 对应 平台内核 的 order_to, order.target_portfolio_smart(...) 对应 平台内核 的 order_target_portfolio_smart 批量目标权重语义;account.deposit_withdraw(...) 和 account.finance_repay(...) 对应 平台内核 账户出入金与融资/还款语义;order_prices 既可以是逐标的限价映射,也可以是 VWAPOrder/TWAPOrder 这类全局 AlgoOrder; order.vwap_* / order.twap_* 对应 平台内核 的 AlgoOrder 时间窗订单风格,而 update_universe/subscribe/unsubscribe 对应 平台内核 的动态 universe 与订阅接口。symbol 使用标准证券代码;数量、金额、仓位、时间窗、限价、order_id 和 symbol 列表都支持表达式;这些语句也支持放进 when/unless 条件块。 " . to_string ( ) ,
} ,
ManualSection {
title : " when / unless / else " . to_string ( ) ,
@@ -272,7 +272,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
ManualField { name : " upper_limit/lower_limit/price_tick/round_lot/minimum_order_quantity/order_step_size " . to_string ( ) , field_type : " float/int " . to_string ( ) , detail : " 涨跌停、最小价位、整手、最小下单量和数量步长。KSH/BJSE 等板块可与 round_lot 不同。 " . to_string ( ) } ,
ManualField { name : " paused/is_st/is_kcb/is_one_yuan/is_new_listing " . to_string ( ) , field_type : " bool " . to_string ( ) , detail : " 可交易性与板块标志。 " . to_string ( ) } ,
ManualField { name : " allow_buy/allow_sell/at_upper_limit/at_lower_limit " . to_string ( ) , field_type : " bool " . to_string ( ) , detail : " 盘中买卖与涨跌停状态。 " . to_string ( ) } ,
ManualField { name : " touched_upper_limit/touched_lower_limit/hit_upper_limit/hit_lower_limit " . to_string ( ) , field_type : " bool " . to_string ( ) , detail : " 当日 tick 曾经触达涨跌停。 " . to_string ( ) } ,
ManualField { name : " touched_upper_limit/touched_lower_limit/hit_upper_limit/hit_lower_limit " . to_string ( ) , field_type : " bool " . to_string ( ) , detail : " 当日分钟执行价 曾经触达涨跌停。 " . to_string ( ) } ,
ManualField { name : " symbol_open_order_count/symbol_open_buy_qty/symbol_open_sell_qty/latest_symbol_open_order_id " . to_string ( ) , field_type : " int " . to_string ( ) , detail : " 当前证券在挂单簿中的未成交挂单摘要和最近挂单 id。 " . to_string ( ) } ,
ManualField { name : " latest_symbol_open_order_status/latest_symbol_open_order_unfilled_qty " . to_string ( ) , field_type : " string/int " . to_string ( ) , detail : " 当前证券最近一笔挂单的状态和未成交数量。 " . to_string ( ) } ,
ManualField { name : " in_dynamic_universe/is_subscribed " . to_string ( ) , field_type : " bool " . to_string ( ) , detail : " 当前证券是否在动态 universe 内,以及是否仍在订阅集合中。 " . to_string ( ) } ,
@@ -304,7 +304,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
functions : vec ! [
ManualFunction { name : " factor " . to_string ( ) , signature : " factor( \" column_name \" ) " . to_string ( ) , detail : " 读取当前股票当日可用因子列。数值因子返回 float,字符串因子返回 string;缺失字段默认返回 0 或空字符串,建议重要条件配合 diagnostics 查看候选过滤数量。 " . to_string ( ) } ,
ManualFunction { name : " day_factor " . to_string ( ) , signature : " day_factor( \" field_name \" ) " . to_string ( ) , detail : " 读取日级/指数级字段映射。 " . to_string ( ) } ,
ManualFunction { name : " history_bars " . to_string ( ) , signature : " ctx.history_bars(symbol, count, \" 1d \" | \" 1m \" | \" tick \" , \" close \" , include_now) " . to_string ( ) , detail : " 回测内核策略上下文数据 API,返回指定证券最近 N 条数值序列。日线字段支持 open/high/low/close/last/prev_close/volume/upper_limit/lower_limit;分钟或 tick 字段支持 last/bid1/ask1/volume_delta/amount_delta。日线 include_now=false 排除当前交易日;分钟/tick 会按当前 on_bar、on_tick 或调度时刻截断,include_now=false 排除当前 bar/tick ,避免未来函数。 " . to_string ( ) } ,
ManualFunction { name : " history_bars " . to_string ( ) , signature : " ctx.history_bars(symbol, count, \" 1d \" | \" 1m \" , \" close \" , include_now) " . to_string ( ) , detail : " 回测内核策略上下文数据 API,返回指定证券最近 N 条数值序列。日线字段支持 open/high/low/close/last/prev_close/volume/upper_limit/lower_limit;分钟字段支持 last/bid1/ask1/volume_delta/amount_delta。旧 \" tick \" 频率只作为兼容别名映射到 \" 1m \" ,不再表示逐笔数据。 日线 include_now=false 排除当前交易日;分钟线 会按当前 on_bar、日内事件 或调度时刻截断,include_now=false 排除当前分钟执行价 ,避免未来函数。" . to_string ( ) } ,
ManualFunction { name : " current_snapshot " . to_string ( ) , signature : " ctx.current_snapshot(symbol) " . to_string ( ) , detail : " 读取当前交易日指定证券的日级快照,可用于获得当日 open/close/last/upper_limit/lower_limit 等字段。 " . to_string ( ) } ,
ManualFunction { name : " instrument/instruments/all_instruments " . to_string ( ) , signature : " ctx.instrument(symbol) " . to_string ( ) , detail : " 读取证券元数据,包括名称、板块、上市日期、退市日期、最小下单量、整手、最小价位等;all_instruments 按证券代码稳定排序返回全量证券。 " . to_string ( ) } ,
ManualFunction { name : " active_instruments/instruments_history " . to_string ( ) , signature : " ctx.active_instruments(&[symbol]) " . to_string ( ) , detail : " active_instruments 返回当前交易日已上市且未退市的证券;instruments_history 返回给定代码的历史证券记录,包含当前已退市标的,对齐 平台内核 的 active_instruments/instruments_history 能力。 " . to_string ( ) } ,
@@ -360,7 +360,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
} ,
ManualFactorSource {
table : " 盘口深度参数 " . to_string ( ) ,
detail : " 可选字段包括 date、symbol、timestamp、level、bid_price、bid_volume、ask_price、ask_volume。存在盘口深度时,期货 counterparty_offer / next_tick _best_counterparty 可按真实多档盘口逐档扫单;不存在时不会伪造 depth。 " . to_string ( ) ,
detail : " 可选字段包括 date、symbol、timestamp、level、bid_price、bid_volume、ask_price、ask_volume。存在盘口深度时,期货 counterparty_offer / next_minute _best_counterparty 可按真实多档盘口逐档扫单;不存在时不会伪造 depth。 " . to_string ( ) ,
fields : vec ! [ ] ,
} ,
ManualFactorSource {
@@ -383,8 +383,8 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
code : " filter.stock_expr(industry_name( \" citics \" , 1) == \" 电子 \" && factor_text( \" concept \" ) == \" ai_chip \" ) " . to_string ( ) ,
} ,
ManualExample {
title : " next tick 撮合 + tick 滑点" . to_string ( ) ,
code : " execution.matching_type( \" next_tick_last \" ) \n execution.slippage( \" tick_size \" , 1) " . to_string ( ) ,
title : " 分钟执行价撮合 + 最小价位 滑点" . to_string ( ) ,
code : " execution.matching_type( \" next_minute_last \" ) \n execution.slippage( \" tick_size \" , 1) " . to_string ( ) ,
} ,
ManualExample {
title : " 动态 universe 和订阅 " . to_string ( ) ,
@@ -518,7 +518,7 @@ pub fn build_generation_prompt(
prompt . push_str ( " - 生成的代码必须能转换为 strategy_spec 并提交 POST /v1/backtests。 \n " ) ;
prompt . push_str ( " - 不要使用手册未列出的字段、函数或外部平台 API 名称。 \n \n " ) ;
prompt . push_str ( " 只允许使用这些可编译语句:market、benchmark、signal、rebalance.every_days(...).at([...])、selection.limit、selection.market_cap_band、filter.stock_ma、filter.stock_expr、ordering.rank_by、ordering.rank_expr、allocation.buy_scale、risk.stop_loss、risk.take_profit、risk.index_exposure、execution.matching_type、execution.slippage、universe.exclude。禁止输出 filter(...)、rank(...)、select.top(...)、weight.equal()、sell_rule(...)、backtest(...)、risk.max_position(...) 这类未支持伪语法。 \n " ) ;
prompt . push_str ( " 参数形态必须严格:selection.market_cap_band 必须写 field= \" market_cap \" 或 field= \" free_float_cap \" , lower=..., upper=...;禁止使用 float_market_cap;禁止使用 ma60、stock_ma60、signal_ma60、benchmark_ma60, 60日价格均线写 rolling_mean( \" close \" , 60) 或 ma( \" close \" , 60),任意窗口均量写 rolling_mean( \" volume \" , n) 或 vma(n);不要生成 fn score() 这类零参数函数,股票字段排序直接写在 ordering.rank_expr 内或用带参数函数;布尔字段按布尔使用,写 !is_st、!paused、!at_upper_limit、!at_lower_limit,不要写 is_st == 0; risk.index_exposure 只能传一个数值表达式,不要使用 risk.exposure;完整三元表达式 cond ? a : b 可以使用,但不得输出残缺问号/冒号片段;execution.matching_type 只能取 next_tick _last、next_tick _best_own、next_tick _best_counterparty、counterparty_offer、vwap、current_bar_close、next_bar_open、open_auction; next_bar_open 只能使用决策日信号,不能把执行日价格当作下单前信息;execution.slippage 必须写 execution.slippage( \" none \" ) 或 execution.slippage( \" price_ratio \" , 0.001)。 \n " ) ;
prompt . push_str ( " 参数形态必须严格:selection.market_cap_band 必须写 field= \" market_cap \" 或 field= \" free_float_cap \" , lower=..., upper=...;禁止使用 float_market_cap;禁止使用 ma60、stock_ma60、signal_ma60、benchmark_ma60, 60日价格均线写 rolling_mean( \" close \" , 60) 或 ma( \" close \" , 60),任意窗口均量写 rolling_mean( \" volume \" , n) 或 vma(n);不要生成 fn score() 这类零参数函数,股票字段排序直接写在 ordering.rank_expr 内或用带参数函数;布尔字段按布尔使用,写 !is_st、!paused、!at_upper_limit、!at_lower_limit,不要写 is_st == 0; risk.index_exposure 只能传一个数值表达式,不要使用 risk.exposure;完整三元表达式 cond ? a : b 可以使用,但不得输出残缺问号/冒号片段;execution.matching_type 只能取 next_minute _last、next_minute _best_own、next_minute _best_counterparty、counterparty_offer、vwap、current_bar_close、next_bar_open、open_auction; 旧 next_tick_* 只是兼容别名,新生成策略不要使用; next_bar_open 只能使用决策日信号,不能把执行日价格当作下单前信息;execution.slippage 必须写 execution.slippage( \" none \" ) 或 execution.slippage( \" price_ratio \" , 0.001)。 \n " ) ;
prompt . push_str ( " 回测成功但 tradeCount=0 或 holdingCount=0 是无效策略;第一版必须保持稳定买入覆盖率,复杂因子只能在后续优化中逐步加严。 \n " ) ;
prompt . push_str ( " 可参考但不要照抄的最小模板,回复时不要包含 ``` 代码围栏: \n strategy( \" cn_a_smallcap_factor_rotation \" ) { \n market( \" CN_A \" ) \n benchmark( \" 000852.SH \" ) \n signal( \" 000001.SH \" ) \n rebalance.every_days(5).at([ \" 10:18 \" ]) \n selection.limit(40) \n selection.market_cap_band(field= \" market_cap \" , lower=0, upper=1000) \n filter.stock_expr(listed_days >= 60 && !is_st && !paused && close > 2 && !at_upper_limit && !at_lower_limit) \n ordering.rank_by( \" market_cap \" , \" asc \" ) \n allocation.buy_scale(1.0) \n risk.index_exposure(1.0) \n risk.stop_loss(holding_return < -0.08) \n execution.slippage( \" price_ratio \" , 0.001) \n } \n \n " ) ;
prompt . push_str ( " 用户目标: \n " ) ;