rentBar > 1 And isProcessing == False)
{
isProcessing = True; // 设置处理状态
tradingSymbol = Data(0).Symbol;
// 获取前一根K线数据
prevClose = Close[1];
prevOpen = Open[1];
prevIsYin = (prevClose < prevOpen);
prevIsYang = (prevClose > prevOpen);
// 使用最新价作为基准
currentPrice = Close;
// 重置执行标志
orderExecuted = False;
needReverse = False;
needStopLoss = False;
// --- 止损检查 ---
If(MarketPosition != 0 And BarsSinceEntry >= 0)
{
If(MarketPosition == 1) // 多仓止损
{
stopLossPrice = AvgEntryPrice - StopLossPoints * MinMove * PriceScale;
If(Low <= stopLossPrice)
{
Commentary("→ 触发多仓止损, 止损价=" + Text(stopLossPrice));
}
}
Else If(MarketPosition == -1) // 空仓止损
{
stopLossPrice = AvgEntryPrice + StopLossPoints * MinMove * PriceScale;
If(High >= stopLossPrice)
{
Commentary("→ 触发空仓止损, 止损价=" + Text(stopLossPrice));
}
}
// 持仓时间过长平仓
If(BarsSinceEntry >= MaxBarsSinceEntry)
{
needReverse = True;
Commentary("→ 持仓超时平仓, 持仓周期=" + Text(BarsSinceEntry));
}
}
// ---1:止损操作 ---
If(needStopLoss And orderExecuted == False)
{
Commentary("→ 执行止损操作");
If(MarketPosition == 1) // 平多仓止损
{
A_SendOrder(Enum_Sell, Enum_Exit, Abs(MarketPosition), 0);
Commentary("→ 市价止损平多仓");
}
Else If(MarketPosition == -1) // 平空仓止损
{
A_SendOrder(Enum_Buy, Enum_Exit, Abs(MarketPosition), 0);
Commentary("→ 市价止损平空仓");
}
}
// ---2:反向操作(先平后开)---
Else If(needReverse And orderExecuted == False)
{
Commentary("→ 执行反向操作");
// 平仓操作
If(MarketPosition == -1) // 平空仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice - PriceOffset * MinMove * PriceScale;
If(targetPrice >= Low)
{
A_SendOrder(Enum_Buy, Enum_Exit, Abs(MarketPosition), targetPrice);
Commentary("→ 限价平空仓:" + Text(targetPrice));
}
}
If(orderExecuted == False)
{
A_SendOrder(Enum_Buy, Enum_Exit, Abs(MarketPosition), 0);
Commentary("→ 市价平空仓");
}
}
Else If(MarketPosition == 1) // 平多仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice + PriceOffset * MinMove * PriceScale;
If(targetPrice <= High)
{
A_SendOrder(Enum_Sell, Enum_Exit, Abs(MarketPosition), targetPrice);
Commentary("→ 限价平多仓:" + Text(targetPrice));
}
}
If(orderExecuted == False)
{
A_SendOrder(Enum_Sell, Enum_Exit, Abs(MarketPosition), 0);
Commentary("→ 市价平多仓");
}
}
// 平仓后立即开反向仓
If(orderExecuted)
{
If(prevIsYin) // 开多仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice - PriceOffset * MinMove * PriceScale;
If(targetPrice >= Low)
{
A_SendOrder(Enum_Buy, Enum_Entry, Lots, targetPrice);
Commentary("→ 限价开多仓:" + Text(targetPrice));
}
Else
{
A_SendOrder(Enum_Buy, Enum_Entry, Lots, 0);
Commentary("→ 市价开多仓");
}
}
Else
{
A_SendOrder(Enum_Buy, Enum_Entry, Lots, 0);
Commentary("→ 市价开多仓");
}
}
Else If(prevIsYang) // 开空仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice + PriceOffset * MinMove * PriceScale;
If(targetPrice <= High)
{
A_SendOrder(Enum_Sell, Enum_Entry, Lots, targetPrice);
Commentary("→ 限价开空仓:" + Text(targetPrice));
}
Else
{
A_SendOrder(Enum_Sell, Enum_Entry, Lots, 0);
Commentary("→ 市价开空仓");
}
}
Else
{
A_SendOrder(Enum_Sell, Enum_Entry, Lots, 0);
Commentary("→ 市价开空仓");
}
}
}
}
// --- 3:正常反向信号 ---
Else If(orderExecuted == False)
{
// 检查是否需要反向(基于阴阳线信号)
If(MarketPosition == -1 And prevIsYin)
needReverse = True;
If(MarketPosition == 1 And prevIsYang)
needReverse = True;
If(needReverse And orderExecuted == False)
{
Commentary("→ 执行信号反向操作");
// 平仓操作
If(MarketPosition == -1) // 平空仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice - PriceOffset * MinMove * PriceScale;
If(targetPrice >= Low)
{
A_SendOrder(Enum_Buy, Enum_Exit, Abs(MarketPosition), targetPrice);
orderExecuted = True;
Commentary("→ 限价平空仓:" + Text(targetPrice));
}
}
If(orderExecuted == False)
{
A_SendOrder(Enum_Buy, Enum_Exit, Abs(MarketPosition), 0);
orderExecuted = True;
Commentary("→ 市价平空仓");
}
}
Else If(MarketPosition == 1) // 平多仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice + PriceOffset * MinMove * PriceScale;
If(targetPrice <= High)
{
A_SendOrder(Enum_Sell, Enum_Exit, Abs(MarketPosition), targetPrice);
orderExecuted = True;
Commentary("→ 限价平多仓:" + Text(targetPrice));
}
}
If(orderExecuted == False)
{
A_SendOrder(Enum_Sell, Enum_Exit, Abs(MarketPosition), 0);
Commentary("→ 市价平多仓");
}
}
// 平仓后立即开反向仓
If(orderExecuted)
{
If(prevIsYin) // 开多仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice - PriceOffset * MinMove * PriceScale;
If(targetPrice >= Low)
{
A_SendOrder(Enum_Buy, Enum_Entry, Lots, targetPrice);
Commentary("→ 限价开多仓:" + Text(targetPrice));
}
Else
{
A_SendOrder(Enum_Buy, Enum_Entry, Lots, 0);
Commentary("→ 市价开多仓");
}
}
Else
{
A_SendOrder(Enum_Buy, Enum_Entry, Lots, 0);
Commentary("→ 市价开多仓");
}
}
Else If(prevIsYang) // 开空仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice + PriceOffset * MinMove * PriceScale;
If(targetPrice <= High)
{
A_SendOrder(Enum_Sell, Enum_Entry, Lots, targetPrice);
Commentary("→ 限价开空仓:" + Text(targetPrice));
}
Else
{
A_SendOrder(Enum_Sell, Enum_Entry, Lots, 0);
Commentary("→ 市价开空仓");
}
}
Else
{
A_SendOrder(Enum_Sell, Enum_Entry, Lots, 0);
Commentary("→ 市价开空仓");
}
}
}
}
}
// --- 4:开新仓(无持仓时需要开仓)---
If(MarketPosition == 0 And orderExecuted == False)
{
If(prevIsYin) // 开多仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice - PriceOffset * MinMove * PriceScale;
If(targetPrice >= Low)
{
A_SendOrder(Enum_Buy, Enum_Entry, Lots, targetPrice);
Commentary("→ 限价开多仓:" + Text(targetPrice));
}
}
If(orderExecuted == False)
{
A_SendOrder(Enum_Buy, Enum_Entry, Lots, 0);
Commentary("→ 市价开多仓");
}
}
Else If(prevIsYang) // 开空仓
{
If(EnableLimitOrder)
{
targetPrice = currentPrice + PriceOffset * MinMove * PriceScale;
If(targetPrice <= High)
{
A_SendOrder(Enum_Sell, Enum_Entry, Lots, targetPrice);
Commentary("→ 限价开空仓:" + Text(targetPrice));
}
}
If(orderExecuted == False)
{
A_SendOrder(Enum_Sell, Enum_Entry, Lots, 0);
Commentary("→ 市价开空仓");
}
}
}
// --- 5:继续持仓 ---
If(orderExecuted == False And MarketPosition != 0)
{
Commentary("→ 继续持仓, 持仓周期=" + Text(BarsSinceEntry));
}
isProcessing = False; // 重置处理状态
}
代码不全请勿直接使用