
等级: 新手上路
- 注册:
- 2025-4-20
- 曾用名:
|
以下是适配日K线周期、基于实时价格突破(非收盘价确认)的金字塔交易策略代码,重点捕捉价格突破瞬间开平仓:
vb
Vars
Decimal EntryPriceBuy = 0; // 多单开仓价
Decimal EntryPriceSell = 0; // 空单开仓价
Integer BuyPos = 0; // 多单持仓量
Integer SellPos = 0; // 空单持仓量
Boolean bBreakLong = False; // 多单突破标记
Boolean bBreakShort = False; // 空单突破标记
// 定义前一日阴阳线(日K线专用)
PrevIsUp = Ref(C, 1) > Ref(O, 1) // 前一日阳线
PrevIsDown = Ref(C, 1) < Ref(O, 1) // 前一日阴线
// 实时价格突破判断(当前tick价格触发)
If BarStatus = 2 Then // 仅在非历史回测阶段(实盘/模拟)生效
// 实时突破前一日阴线高点(开多)
If PrevIsDown And H > Ref(H, 1) Then
bBreakLong = True
End If
// 实时突破前一日阳线低点(开空)
If PrevIsUp And L < Ref(L, 1) Then
bBreakShort = True
End If
End If
// 持仓状态更新
BuyPos = GetPosition("Buy")
SellPos = GetPosition("SellShort")
// 开多单逻辑(实时突破触发,以市价开仓)
If bBreakLong Then
// 平空单 + 开多单
If SellPos > 0 Then
Sell(SellPos, MKT) // 市价平空单(空单止盈)
End If
If BuyPos = 0 Then
Buy(1, MKT) // 市价开1手多单
EntryPriceBuy = C // 记录开仓价(触发时最新价)
End If
bBreakLong = False // 重置标记
End If
// 开空单逻辑(实时突破触发,以市价开仓)
If bBreakShort Then
// 平多单 + 开空单
If BuyPos > 0 Then
SellShort(BuyPos, MKT) // 市价平多单(多单止盈)
End If
If SellPos = 0 Then
SellShort(1, MKT) // 市价开1手空单
EntryPriceSell = C // 记录开仓价(触发时最新价)
End If
bBreakShort = False // 重置标记
End If
// 日K线专用止损逻辑(收盘前1分钟检查,可自定义时间)
If Time > 1450 And Time < 1500 Then // 尾盘10分钟触发止损(上海期货交易所时间)
If BuyPos > 0 And C < EntryPriceBuy - 20 Then
Sell(BuyPos, MKT)
EntryPriceBuy = 0
End If
If SellPos > 0 And C > EntryPriceSell + 20 Then
Cover(SellPos, MKT)
EntryPriceSell = 0
End If
End If
|
|