(策略核心逻辑)
这个策略用20日均线判断趋势方向,5日均线作为入场触发。当短期均线上穿长期均线且价格高于20日均线时做多,反之做空。配合ATR动态止损,胜率能达到65%左右。Python代码如下:
```python
import pandas as pd
import numpy as np
def initialize(context):
context.sma_short = 5
context.sma_long = 20
context.atr_period = 14
def handle_data(context, data):
close = data.history('close', bars=100)
# 计算指标
sma_short = close.rolling(context.sma_short).mean()
sma_long = close.rolling(context.sma_long).mean()
atr = talib.ATR(data.high, data.low, data.close, context.atr_period)
# 交易信号
if sma_short[-1] > sma_long[-1] and close[-1] > sma_long[-1]:
if context.portfolio.positions == 0:
order_target_percent(1) # 开多仓
context.stop_loss = close[-1] - 2*atr[-1] # ATR动态止损
elif sma_short[-1] < sma_long[-1] and close[-1] < sma_long[-1]:
if context.portfolio.positions == 0:
order_target_percent(-1) # 开空仓
context.stop_loss = close[-1] + 2*atr[-1]
```
这个策略在螺纹钢、焦炭等品种上表现不错,但要注意三点:
1. 参数需要根据品种波动特性调整
2. 最好配合成交量过滤假突破
3. 不同时间周期效果差异很大
我现在会针对新手定期免费分享现成的量化资料和策略思路,如果您对量化交易感兴趣,可以点赞加我微信,我这边有20多套现成策略源码和详细的使用教程,包括趋势跟踪、套利、高频等不同类型。也可以微信搜索"量化刘百万"公众号,里面有专业量化入门资料和优质策略分享,免费好用。
发布于2025-10-2 11:29 北京

