ptrade-strategy-writing
Skill3 starsChanged 35 days agoMITView on GitHub
Use this skill to write, review, migrate, and debug Ptrade strategies that match the Ptrade event engine, API scope, market-data formats, order behavior, and live-trading constraints documented in the bundled Ptrade references. Read references as needed: Reference loading guide: When writing or migrating a strategy:
1 other project uses a version of this, 1 of them edited.
Adopted by
Most recently taken up by
- xxbbzy/ptrade-agent-skillsedited, first seen Jun 6, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 1
- xxbbzy/ptrade-agent-skills★ 8100% same
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository33 other files it gives its agents.
- llms.txtllms.txt
- alpha-zooSkill
- breadth-chart-analystSkill
- canslim-screenerSkill
- factor-mining-pandaaiSkill
- backtesting-bias-avoidanceSkill
- backtest-reviewSkill
- data-quality-auditSkill
- paper-reproSkill
- pine-backtesterSkill
- stats-test-pickerSkill
- vectorbt-backtestSkill
- vectorbt-optimizeSkill
- akquantSkill
- ashare-pre-st-filterSkill
- cninfo-disclosureSkill
- ctp-apiSkill
- quant-buddySkill
- ssquant-futuresSkill
- tdxquantSkill
- tqsdk-tradingSkill
- wencai-screenerSkill
- defillama-dataSkill
- defillama-flowsSkill
- defillama-yieldsSkill
- perp-funding-basisSkill
- pine-debuggerSkill
- pine-developerSkill
- edgar-financialsSkill
- llmquant-equitiesSkill
- llmquant-macroSkill
- llmquant-riskSkill
- openquant-tradingSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
26 rules. About 0 are common, 8 are distinctive, and 18 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈8
Distinctive · estimate
This author's own choices. Worth a look.
18
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 16
Use this skill to write, review, migrate, and debug Ptrade strategies that match the Ptrade event engine, API scope, market-data formats, …
- Distinctive: line 18
Read references as needed:
- Very specific: line 33
When reviewing an existing strategy file, run python scripts/check_ptrade_strategy.py <strategy.py> from this skill directory when local …
- Distinctive: line 37
Treat bundled examples strictly as references.
- Distinctive: line 40
Keep comments useful and specific to the generated strategy.
- Distinctive: line 44
When writing or migrating a strategy:
- Very specific: line 48
Use handle_data(context, data) for day/minute strategies.
- Very specific: line 49
Add before_trading_start(context, data) for daily universe refresh, filters, factor data, or per-day flags.
- Very specific: line 50
Add after_trading_end(context, data) for post-close logs, reconciliation, or end-of-day persistence.
- Very specific: line 51
Use run_daily for fixed-time tasks and run_interval for live interval tasks.
- Very specific: line 52
Use tick_data and order_tick only for tick-level live trading.
- Very specific: line 53
Use on_order_response / on_trade_response only for live order/trade push handling and guard against callback order loops.
- Very specific: line 55
Put one-time setup in initialize.
- Distinctive: line 56
Use examples only to verify event/API idioms.
- Distinctive: line 57
Add Chinese comments while writing the code, especially around parameters, data retrieval, signal calculation, trade sizing, order …
- Very specific: line 58
Use g for global strategy state, but design live-trading state with Ptrade persistence and restart semantics in mind.
- Very specific: line 59
Verify every API call is legal in the target event and target module by checking references/api-map.md and, for exact details, …
- Very specific: line 143
Always define initialize(context) and handle_data(context, data).
- Very specific: line 144
Use Ptrade code suffixes: Shanghai securities can use .SS or .XSHG, Shenzhen .SZ or .XSHE, indices use .XBHS, and CFFEX futures use .CCFX.
- Very specific: line 145
Treat set_universe as mainly the default security_list for get_history.
- Very specific: line 146
Use context.blotter.current_dt and context.previous_date for engine time.
- Very specific: line 148
Use order_target and order_target_value freely in backtests, but be cautious in live trading because delayed position synchronization can …
- Very specific: line 150
Prefer explicit flags and order tracking before placing live orders.
- Very specific: line 151
Do not put live order submission in initialize or restart-sensitive before_trading_start without strong duplicate-order protection.
- Distinctive: line 157
When reviewing or debugging Ptrade code, check in this order:
- Very specific: line 168
Use rg against references/ptrade-full.md before relying on memory for exact API details.
≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
<!--
Imported from: https://github.com/xxbbzy/ptrade-agent-skills
Source Path: skills/ptrade-strategy-writing/SKILL.md
Curated for awesome-quant-skills
-->name: ptrade-strategy-writing description: Write, review, migrate, debug, and explain Ptrade quantitative trading strategies for 回测 or 交易. Use for Ptrade event functions, market-data APIs, order APIs, ETF/LOF/convertible-bond, margin, futures, persistence, live-trading restart safety, API availability, or converting other strategies to Ptrade-compatible Python.
Ptrade Strategy Writing
Overview
Use this skill to write, review, migrate, and debug Ptrade strategies that match the Ptrade event engine, API scope, market-data formats, order behavior, and live-trading constraints documented in the bundled Ptrade references.
Read references as needed:
references/runtime-and-writing-rules.md: Ptrade strategy lifecycle, event timing, live-trading persistence/restart rules, code suffixes, data objects, and safety checks.references/api-map.md: compact API navigation grouped by setup, scheduling, market data, security info, account/position/order, trading, margin, futures, indicators, and utilities.references/api-index.md: high-risk/common API index with availability, event-placement, and live-trading notes.references/example-patterns.md: patterns extracted from the bundled Ptrade example strategies.references/ptrade-full.md: full original Ptrade API document. Search this when exact signatures, return fields, restrictions, or examples matter.references/examples/*.py: original example strategy files copied from the local Ptrade examples.scripts/check_ptrade_strategy.py: static checker for event signatures, API placement, target environment mismatches, and common live-trading hazards.
Reference loading guide:
- For any live-trading, tick, restart, callback, margin, or futures task, read
runtime-and-writing-rules.mdfirst. - For exact API choice, read
api-map.md, thenapi-index.md; searchptrade-full.mdonly for exact signatures, return fields, restrictions, or examples. - For factor, rotation, intraday, pair-trading, or basket strategies, read
example-patterns.mdand then the closest file underreferences/examples/, but use examples only to understand Ptrade structure and API patterns. - When reviewing an existing strategy file, run
python scripts/check_ptrade_strategy.py <strategy.py>from this skill directory when local file access is available.
Hard Requirements
- Treat bundled examples strictly as references. Do not directly reuse example strategy logic, stock/fund pools, thresholds, timing, parameters, comments, or risk settings unless the user explicitly provides or requests those exact details.
- Generated strategies must be driven by the strategy writer's requirements and user-provided materials. If required inputs are missing, make conservative assumptions and state them clearly instead of filling gaps from an example strategy.
- Generated strategy code must include complete Chinese comments explaining strategy intent, key parameters, data windows, signal conditions, risk controls, order logic, and any live-trading safeguards.
- Keep comments useful and specific to the generated strategy. Do not paste generic comments from bundled examples.
Authoring Workflow
When writing or migrating a strategy:
- Identify the target scene: 回测, 普通股票交易, tick 交易, ETF/LOF/可转债, 融资融券, or 期货.
- Choose the event mechanism:
- Use
handle_data(context, data)for day/minute strategies. - Add
before_trading_start(context, data)for daily universe refresh, filters, factor data, or per-day flags. - Add
after_trading_end(context, data)for post-close logs, reconciliation, or end-of-day persistence. - Use
run_dailyfor fixed-time tasks andrun_intervalfor live interval tasks. - Use
tick_dataandorder_tickonly for tick-level live trading. - Use
on_order_response/on_trade_responseonly for live order/trade push handling and guard against callback order loops.
- Use
- Map the user's rules and provided materials into explicit universe selection, data windows, signal calculation, sizing, execution, and duplicate-order controls.
- Put one-time setup in
initialize; put per-day preparation inbefore_trading_start; keep trade decisions in the selected runtime event. - Use examples only to verify event/API idioms. Do not copy example strategy rules or parameters into the generated strategy unless they are also present in the user's request or materials.
- Add Chinese comments while writing the code, especially around parameters, data retrieval, signal calculation, trade sizing, order submission, and risk/restart safeguards.
- Use
gfor global strategy state, but design live-trading state with Ptrade persistence and restart semantics in mind. - Verify every API call is legal in the target event and target module by checking
references/api-map.mdand, for exact details,references/ptrade-full.md. - For generated strategy files, run
scripts/check_ptrade_strategy.pyif possible and fix any errors before finalizing. - Include a short readiness note for assumptions that cannot be proven from code: strategy frequency, benchmark, initial capital, commission/slippage settings, data permissions, broker/counter support, and live account synchronization.
Strategy Skeletons
Minimal day/minute strategy:
def initialize(context):
# 设置本策略关注的交易标的,并注册到 Ptrade 股票池。
g.security = "600570.SS"
set_universe(g.security)
def handle_data(context, data):
# 从当前 BarData 中读取最新收盘价,可在此处接入用户定义的信号逻辑。
price = data[g.security].close
log.info("price: %s" % price)Daily rebalance strategy:
def initialize(context):
# 策略参数:指数股票池、目标持仓数量、调仓周期和调仓状态。
g.index = "000300.XBHS"
g.hold_num = 10
g.rebalance_days = 20
g.day_count = 0
g.need_rebalance = False
def before_trading_start(context, data):
# 盘前判断今天是否需要调仓,并按用户规则准备候选股票池。
g.need_rebalance = (g.day_count % g.rebalance_days == 0)
if g.need_rebalance:
stocks = get_index_stocks(g.index)
# 剔除 ST、停牌和退市整理标的,避免无法交易或风险异常的股票进入候选池。
g.candidates = filter_stock_by_status(
stocks, filter_type=["ST", "HALT", "DELISTING"], query_date=None
)
g.day_count += 1
def handle_data(context, data):
# 非调仓日直接返回,避免重复计算和重复下单。
if not g.need_rebalance:
return
# 根据用户定义的排序规则计算目标持仓,先卖出不再符合条件的标的,再买入或调仓到目标权重。
g.need_rebalance = FalseLive tick strategy:
def initialize(context):
# tick 交易需要更强的重复下单保护;此处用标记位记录是否已经发过委托。
g.security = "600570.SS"
g.sent_order = False
set_universe(g.security)
# 实盘重启时避免自动重复交易,并按需关闭 L2 tick 数据。
set_parameters(tick_data_no_l2="1", not_restart_trade="1", server_restart_not_do_before="1")
def tick_data(context, data):
# 已经发出委托后直接返回,避免同一信号在 tick 回调中重复下单。
if g.sent_order:
return
tick = data[g.security]["tick"]
last_px = tick["last_px"][0]
if last_px > 0:
# 股票限价保留 2 位小数;真实策略应结合用户要求设置价格保护和委托数量。
order_tick(g.security, 100, limit_price=round(float(last_px), 2))
g.sent_order = True
def handle_data(context, data):
# tick 策略仍需保留 Ptrade 必选的 handle_data 函数。
passPtrade-Specific Rules
- Always define
initialize(context)andhandle_data(context, data). - Use Ptrade code suffixes: Shanghai securities can use
.SSor.XSHG, Shenzhen.SZor.XSHE, indices use.XBHS, and CFFEX futures use.CCFX. - Treat
set_universeas mainly the defaultsecurity_listforget_history;data[security]only contains subscribed universe data. - Use
context.blotter.current_dtandcontext.previous_datefor engine time. In examples, daily date guards often usecontext.blotter.current_dt.strftime("%Y%m%d"). - In live trading,
context.portfolioandPositiondata have synchronization latency, commonly around 6 seconds depending on broker setup. Do not rely on immediate position updates after an order. - Use
order_targetandorder_target_valuefreely in backtests, but be cautious in live trading because delayed position synchronization can cause repeated orders. - Round limit prices by product precision: stocks 2 decimals, convertible bonds/ETF/LOF 3 decimals, stock index futures 1 decimal.
- Prefer explicit flags and order tracking before placing live orders. Store order ids and check
get_open_orders,get_orders, callbacks, or broker state before sending another order. - Do not put live order submission in
initializeor restart-sensitivebefore_trading_startwithout strong duplicate-order protection. - For live restart safety, use
set_parameters(not_restart_trade="1", server_restart_not_do_before="1")when appropriate and design persistedgvalues carefully. - Handle missing data, suspended stocks, ST/delisting status, limit-up/limit-down, and empty API returns explicitly.
Review Workflow
When reviewing or debugging Ptrade code, check in this order:
- Static checks: run
scripts/check_ptrade_strategy.py <strategy.py>when possible; address errors before relying on manual review. - Event structure: required functions, selected frequency, legal API calls in each event, no accidental tick-only APIs in
handle_data. - Data correctness: no lookahead, correct
includebehavior, correctcount/date windows, suspension handling,is_dict/DataFrame return shape, and code suffix compatibility. - Trading correctness: order size units, lot rounding, limit price precision, live duplicate-order risk, unavailable
enable_amount, and limit-up/limit-down handling. - Live resilience: persistence, restart behavior, callback loop prevention, broker/counter API support, and account/position synchronization.
- Backtest realism: benchmark, commission, slippage, volume limits,
set_limit_mode, initial positions, and whether target APIs are backtest-only or trading-only.
Reference Search
Use rg against references/ptrade-full.md before relying on memory for exact API details. Helpful searches:
rg -n "#### order_target_value|order_target_value\\(" references/ptrade-full.md
rg -n "#### get_history|include|fill|is_dict" references/ptrade-full.md
rg -n "initialize\\(必选\\)|before_trading_start|handle_data\\(必选\\)|tick_data" references/ptrade-full.md
rg -n "set_parameters|not_restart_trade|server_restart_not_do_before|持久化" references/ptrade-full.md
rg -n "交易模块可用|回测模块可用|仅在交易模块" references/ptrade-full.mdDoes it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

