
等级: 专业版
- 注册:
- 2024-11-13
- 曾用名:
|
// 策略名称:螺纹钢网格交易策略
// 策略类型:期货、网格交易、趋势自适应
// 适用品种:螺纹钢主力合约(按需修改参数)
// 平台说明:适用于金字塔量化交易平台
Params
Numeric GridSize(30); // 网格间距(元)
Numeric MaxLayers(5); // 最大持仓层数
Numeric Lots(1); // 每层交易手数
Numeric StopLoss(200); // 止损点数
End
Vars
Numeric BasePrice; // 网格基准价(动态调整)
Numeric CurrentLayer(0); // 当前持仓层数
Numeric TotalLots(0); // 总持仓手数
Numeric AvgPrice; // 平均开仓价格
Numeric TotalCost(0); // 总开仓成本
Bool TradeEnabled(True); // 交易开关
End
If BarStatus == 2 Then Begin
//=== 初始化 ============================================
If CurrentBar == 1 Then Begin
BasePrice = Close; // 首根K线收盘价作为初始基准
TradeEnabled = True; // 启用交易
End
//=== 风控检查 ==========================================
// 示例:当日持仓亏损超过止损点时强制平仓
If TotalLots > 0 And Close < (AvgPrice - StopLoss) Then Begin
Sell(TotalLots, Close);
TradeEnabled = False; // 触发止损后暂停交易
BasePrice = Close;
CurrentLayer = 0;
TotalLots = 0;
TotalCost = 0;
AvgPrice = 0;
Commentary("触发止损平仓!");
End
//=== 网格交易逻辑 ========================================
If TradeEnabled Then Begin
// 买入逻辑:价格下跌触发网格
If CurrentLayer < MaxLayers Then Begin
Numeric NextLayer = CurrentLayer + 1;
Numeric TriggerPrice = BasePrice - NextLayer * GridSize;
// 价格触发且未达到最大层数
If Close <= TriggerPrice Then Begin
Buy(Lots, Close);
// 更新持仓数据
TotalLots = TotalLots + Lots;
TotalCost = TotalCost + Close * Lots;
AvgPrice = TotalCost / TotalLots;
CurrentLayer = CurrentLayer + 1;
Commentary("第",CurrentLayer,"层买入@",Close);
End
End
// 卖出逻辑:价格上涨统一止盈
If TotalLots > 0 And Close >= (AvgPrice + GridSize) Then Begin
Sell(TotalLots, Close);
// 重置参数并更新基准价
BasePrice = Close; // 动态调整基准价
CurrentLayer = 0;
TotalLots = 0;
TotalCost = 0;
AvgPrice = 0;
Commentary("全部止盈@",Close,"新基准价:",BasePrice);
End
End
End
//=== 图表标注 ================================================
If TotalLots > 0 Then Begin
PlotNumeric("AvgPrice",AvgPrice);
PlotNumeric("TP",AvgPrice + GridSize);
End
For i = 1 To MaxLayers Begin
PlotNumeric("BuyLine"+Text(i),BasePrice - i*GridSize);
End
你好老师,在网上找了一个pel代码的网格策略,好多地方提示错误。能帮我验证修改一下吗? |
|