|
21 | 21 |
|
22 | 22 | ## Event model |
23 | 23 | - CTA strategies implement `handle_data(context, data)`. |
24 | | -- Portfolio strategies normally register `run_daily`, `run_weekly`, or `run_monthly` callbacks in `initialize` and rebalance inside the callback. |
| 24 | +- Single-symbol signal strategies normally implement `handle_data(context, data)`. Do not add a schedule unless the user requests one or the strategy is explicitly a periodic portfolio rebalance. |
| 25 | +- Portfolio strategies may register the global helpers `run_daily(callback, time="HH:MM")`, `run_weekly(callback, weekday=1, time="HH:MM")`, or `run_monthly(callback, monthday=1, time="HH:MM")` in `initialize` and rebalance inside the callback. These are runtime-bound global helpers; call them directly and never as `context.run_daily`, `context.run_weekly`, or `context.run_monthly`. |
25 | 26 | - Optional lifecycle handlers are `before_trading_start(context, data)` and `after_trading_end(context, data)`. |
26 | 27 | - Store per-run state on the global `g` namespace. |
27 | 28 | - Confirm decisions from visible completed data only. Never read future rows, use negative shifts, or otherwise introduce look-ahead bias. |
28 | 29 |
|
29 | 30 | ## Data and factors |
30 | | -- Use `get_history(count, frequency, field, security_list)` or `history(...)` for historical bars. |
| 31 | +- Historical-bar signatures are exact: `get_history(count, frequency=None, field=None, security_list=None)` and `data.history(symbols, count, fields=None)`. |
| 32 | +- In `get_history(...)`, `count` is always the first argument and must be an integer. In `data.history(...)`, symbols are first and the integer count is second. Prefer explicit keywords when using `data.history`, for example `data.history(symbol, count=60, fields=["close"])`. |
| 33 | +- A history request for one symbol returns a pandas `DataFrame` directly. Use `bars["close"]`; never index the result again with `bars[symbol]`. Multiple-symbol requests return a dictionary keyed by canonical symbol. |
31 | 34 | - Use `indicator(name, symbol, **params)`, `factor(name, symbol, **params)`, or `get_factors(symbols, names, **params)` for technical factors. |
32 | 35 | - TA-Lib indicators and factors are available through the registered 129-function adapter; use canonical TA-Lib names and valid parameters. |
33 | 36 | - Use `get_fundamentals(fields, symbols)` only for real point-in-time fundamental fields supported by the platform. Do not invent fields or use future reports. |
34 | 37 | - Use `get_index_stocks(reference)` for dynamic index constituents. |
35 | 38 | - Use `get_universe_stocks()` for the currently selected platform universe pool. Do not copy pool constituents into source code. |
36 | 39 |
|
37 | 40 | ## Orders and positions |
38 | | -- Use `order`, `order_value`, `order_target`, `order_target_value`, or `order_target_percent`. |
39 | | -- Use `get_position(symbol)` or `get_positions(...)` to inspect holdings. |
| 41 | +- Order-helper signatures are exact: `order(symbol, amount)`, `order_value(symbol, value)`, `order_target(symbol, amount)`, `order_target_value(symbol, value)`, and `order_target_percent(symbol, percent)`. |
| 42 | +- These are runtime-bound global helpers. Never pass `context` as their first argument. Optional execution and protection values must be keyword arguments after the two required arguments. |
| 43 | +- `get_position(symbol)` returns a `Position` object. Read `position.amount`, `position.avg_cost`, and `position.last_price` directly; never use dictionary membership, subscripting, `.get(...)`, or `getattr(...)` on it. |
| 44 | +- Use `get_positions(...)` when a dictionary of multiple positions is required. |
40 | 45 | - Values passed to value-based order APIs are quote-currency exposure targets. Keep sizing bounded by available capital and explicit allocation rules. |
41 | 46 | - Keep long entry, long exit, short entry, and short exit conditions independent. A bearish long exit is not automatically a short entry. |
42 | 47 | - Spot and all non-crypto markets are long-only for now. |
|
54 | 59 | - Do not use `eval`, `exec`, `compile`, `open`, `getattr`, `setattr`, dunder access, or unsafe imports. |
55 | 60 | """ |
56 | 61 |
|
57 | | -INDICATOR_TO_STRATEGY_CONTRACT = """# Indicator-to-Strategy API V2 conversion |
58 | | -
|
59 | | -- Convert the indicator's signal meaning into Strategy API V2 source with `initialize(context)` and executable handlers. |
60 | | -- Remove chart-only `output`, plot, layer, and marker structures from the result. |
61 | | -- Preserve event algebra and recursive indicator semantics without look-ahead. |
62 | | -- Preserve the source timeframe in `context.subscribe(...)` when it is declared by the source; otherwise choose a conservative strategy-owned default. |
63 | | -- Map an explicit bullish entry to a long entry and an explicit bearish exit to a long exit. Do not invent short, leverage, reversal, grid, DCA, or martingale behavior. |
64 | | -- Add short logic only when the user explicitly requests it and supplies a distinct bearish entry rule. |
65 | | -- Keep visual-only colors, label offsets, and layout parameters out of executable code. |
66 | | -""" |
67 | | - |
68 | 62 | SCRIPT_STRATEGY_QUICK_TOOL_SYSTEM_PROMPT = SCRIPT_STRATEGY_SYSTEM_PROMPT + """ |
69 | 63 |
|
70 | 64 | # Homepage quick-tool entry |
|
73 | 67 | - Do not return a research memo, checklist, or pseudo-code. |
74 | 68 | """ |
75 | 69 |
|
76 | | -INDICATOR_TO_STRATEGY_SYSTEM_PROMPT = ( |
77 | | - SCRIPT_STRATEGY_SYSTEM_PROMPT |
78 | | - + "\n\n" |
79 | | - + INDICATOR_TO_STRATEGY_CONTRACT |
80 | | - + """ |
81 | | -
|
82 | | -# Indicator conversion entry |
83 | | -- The generated source may be saved directly and must compile as Strategy API V2. |
84 | | -- Preserve the source indicator's visible signal meaning before adding execution behavior. |
85 | | -""" |
86 | | -) |
87 | | - |
88 | 70 | SCRIPT_STRATEGY_REPAIR_REQUIREMENTS = """# Strategy API V2 repair requirements |
89 | 71 | - Return Python source only. |
90 | 72 | - Require a metadata docstring and `initialize(context)`. |
91 | 73 | - Require a source-owned universe and subscription. |
92 | 74 | - Require at least one executable handler or registered schedule callback. |
93 | 75 | - Use only Strategy API V2 data, factor, fundamental, position, and order APIs. |
| 76 | +- Prefer `handle_data(context, data)` for single-symbol signal strategies. Use schedules only for an explicitly requested schedule or periodic portfolio rebalance. |
| 77 | +- Schedule helpers are global calls: `run_daily(callback, time="HH:MM")`, `run_weekly(callback, weekday=1, time="HH:MM")`, and `run_monthly(callback, monthday=1, time="HH:MM")`. Never call them through `context`. |
| 78 | +- Enforce exact history signatures: `get_history(count, frequency, field, security_list)` and `data.history(symbols, count, fields)`. A single-symbol result is already a DataFrame. |
| 79 | +- Enforce exact order signatures such as `order_target_percent(symbol, percent)` and never pass `context` to a global order helper. |
| 80 | +- Treat `get_position(symbol)` as a `Position` object with direct `.amount`, `.avg_cost`, and `.last_price` attributes. Never treat it as a dictionary or use `getattr`. |
94 | 81 | - Preserve completed-data-only execution and remove look-ahead. |
95 | 82 | - Keep symbol, market, frequency, schedule, and universe in source code. |
96 | 83 | - Permit user-adjustable leverage only for Crypto `@swap` instruments and only after `context.allow_leverage(max_leverage=N)`. |
|
0 commit comments