搭好 Python 量化基础环境,装好必要的库。
# 核心依赖
pip install pandas numpy backtrader
pip install requests python-dotenv
pip install ta # 技术指标
pip install vnpy # 可选,对接 CTP
创建项目目录结构:data/ strategy/ backtest/ executor/
配置 .env 文件存 API Key(不上传到 GitHub)
写好行情数据下载脚本(1 天搞定)
Python 3.11+venv 隔离
用 Backtrader 实现突破回撤策略,核心是找出红蓝线交叉买点。
# 伪代码:红蓝线逻辑
fast_ma = SMA(close, 5) # 蓝线
slow_ma = SMA(close, 20) # 红线
if crossover(fast_ma, slow_ma):
# 蓝线上穿红线 → 交叉成立
entry_price = close
stop_loss = slow_ma # 回踩红线
⚠️ 红蓝线参数文档中未公开,先用 5/20 作为占位,后续调参优化
集成 TA-Lib 或 pandas_ta 计算指标
输出回测报告:胜率、最大回撤、夏普比率
Backtrader回测报告
每天定时扫描 85+ 品种,按规则过滤出候选合约。
# 筛选条件
contracts = all_contracts()
def filter(ct):
return (
10 <= ct.days_to_expiry <= 40
and ct.open_interest >= 1000
and ct.moneyness == 'OTM'
)
candidates = [c for c in contracts if filter(c)]
对接行情源获取合约列表(天勤/掘金/米筐)
自动计算到期时间、持仓量、虚实值
输出 Excel / CSV 候选表,每日盘前跑一次
10-40天持仓 ≥1000虚值
对筛选出的品种,调 DeepSeek V4 Flash API 自动分析新闻情绪。
注意:你当前正在用的就是同一个模型——Claude Code CLI 背后就是 DeepSeek V4。
# 运行时调 DeepSeek API(和 CLI 是同一个模型)
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DEEPSEEK_API_KEY"),
base_url="https://api.deepseek.com"
)
prompt = f"""近一周 {品种} 的新闻:
{news_text}
请判断:利好 ✅ / 利空 ❌ / 中性 ⏸
一句话说明理由。"""
resp = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role":"user", "content": prompt}]
)
DeepSeek V4 定价极低:每月 1 万次分析成本不到 ¥50
开发时:终端里跟 Claude Code CLI 对话,它帮你写量化代码
运行时:Python 代码直接调 DeepSeek API 做新闻分析
DeepSeek V4利好/利空¥20-80/月
把廖老师的全套买卖规则写成可执行的代码。
# 卖出规则
def exit_rule(position, current_price):
cost = position.avg_price
multiple = current_price / cost
if multiple >= 2.0:
sell_half(position) # 赚一倍 → 卖一半保本
if multiple >= 5.0:
sell_all(position) # 赚 5-10 倍 → 清仓
if holding_days(position) >= 30:
sell_all(position) # 满 30 天 → 强制清仓
仓位管理:单品种 ≤ 总资金 5%,同板块只选 1 个
买入后 30 天内不管涨跌不准动(窖藏酒理论)
日志系统:记录每笔开仓/平仓的时间、价格、盈亏
止损-不动止盈-分批30天戒律
对接券商 CTP 接口,让代码自动下单,彻底摆脱手动交易的情绪干扰。
# 伪代码:自动执行流程
while market_is_open():
signal = strategy.check() # 红蓝线交叉?
if signal == "BUY":
order = broker.buy(
contract=contract,
qty=calc_qty(capital * 0.05),
order_type="LIMIT",
price=red_line
)
log_order(order)
if signal == "SELL":
broker.sell(position)
先用模拟盘对接(SimNow 仿真环境)
实盘用 CTP(上期技术)或掘金量化 API
邮件/微信告警:成交通知 + 异常提醒
SimNowCTP自动下单