// ===== 完整策略代码 =====
// 包含:1. 原多空独立参数策略 2. 盈利保护与二次进场 3. 止损保护模块
// 注意:已移除小额保护,只有原策略盈利保护
Params
// ===== 原策略参数 =====
Numeric Lot(1); // 设置仓位
Numeric TakeProfit(3000); // 盈利保护启动阈值
Numeric TrailingPercent(30); // 跟踪回撤百分比
Numeric ReEntryPercent(50); // 二次入场回调百分比
Numeric ReEntryDelay(2); // 二次入场延迟K线数
// 多空完全独立参数(6个参数)
Numeric LongATRPeriod(23); // 多头ATR计算周期
Numeric LongHHPeriod(21); // 多头最高价(HH)计算周期(用于开多信号)
Numeric LongLLPeriod(21); // 多头最低价(LL)计算周期(用于平多止损)
Numeric ShortATRPeriod(23); // 空头ATR计算周期
Numeric ShortHHPeriod(30); // 空头最高价(HH)计算周期(用于平空止损)
Numeric ShortLLPeriod(10); // 空头最低价(LL)计算周期(用于开空信号)
// ===== 止损保护模块参数 =====
Numeric StopLossAmount(2000); // 止损触发金额(元)
Numeric StopLossBuffer(10); // 止损缓冲百分比
Numeric StopLossReEntryPercent(40); // 止损后二次入场回调百分比
Numeric StopLossReEntryDelay(2); // 止损后二次入场延迟K线数
Vars
// ===== 原策略变量 =====
Numeric Barss(5500);
Numeric deposit(10);
Numeric p(50);
Numeric radio(10);
Numeric b(1); // 做多开关
Numeric s(1); // 做空开关
Series<Numeric>h23;
Series<Numeric>l23;
// 多空独立价格序列变量
Series<Numeric>atr_long; // 多头ATR值
Series<Numeric>atr_short; // 空头ATR值
Series<Numeric>hh_long; // 多头最高价HH(开多条件)
Series<Numeric>ll_short; // 空头最低价LL(开空条件)
Series<Numeric>hh_short; // 空头最高价HH(平空条件)
Series<Numeric>ll_long; // 多头最低价LL(平多条件)
// 原有的价格序列变量
Series<Numeric>hh;
Series<Numeric>ll;
Series<Numeric>zh;
Series<Numeric>zl;
Series<Numeric>zg;
Series<Numeric>zg2;
Series<Numeric>pclow;
Series<Numeric>pchigh;
Series<Numeric>bi;
Series<Numeric>si;
Series<Numeric>fd;
Series<Numeric>atr;
Series<Numeric>filt;
Series<Numeric>filt2;
Series<Numeric>myhigh;
Series<Numeric>mylow;
Series<Numeric>myhigh2;
Series<Numeric>mylow2;
Series<Numeric>lots;
Series<Numeric> ma60;
Series<Numeric>avgzf;
Series<Numeric> barCnt;
Series<Numeric>barsum;
Series<Numeric>zf1;
Series<Numeric>zf;
Series<Numeric> pzq;
Series<Numeric>sqm;
// 盈利保护和二次入场变量
Series<Numeric> MaxProfit; // 持仓期间最大浮动盈利
Series<Numeric> TrailStopPrice; // 跟踪止损价位
Series<Bool> IsProtectionActive; // 盈利保护激活标志
// 二次入场专用变量
Series<Numeric> OriginalEntryPrice; // 原入场价格(记录)
Series<Numeric> MaxProfitPrice; // 最大盈利时的价格
Series<Numeric> ProtectionExitPrice;// 盈利保护平仓价格
Series<Integer> LastExitBar; // 上次盈利保护平仓的Bar编号
Series<String> ExitReason; // 平仓原因
Series<Bool> ReEntryEnabled; // 是否允许二次入场
Series<Numeric> ReEntryTargetPrice; // 二次入场目标价格
Series<Integer> ReEntryBarCount; // 二次入场等待计数
// 回调最低点跟踪变量
Series<Numeric> RetraceLowest; // 回调最低点
Series<Numeric> RetraceHighest; // 回调最高点(对应空单)
Series<Integer> FoundLowestBar; // 找到最低点的Bar编号
// 调试变量
Series<Numeric> DebugMaxProfit; // 调试用最大盈利
// ===== 止损保护模块变量 =====
Series<Numeric> StopLossMaxDrawdown; // 持仓期间最大亏损
Series<Numeric> StopLossTriggerPrice; // 止损触发价格
Series<Bool> StopLossActive; // 止损保护激活标志
Series<String> StopLossExitReason; // 止损平仓原因
// 止损后二次入场变量
Series<Numeric> StopLossEntryPrice; // 止损时记录的原入场价格
Series<Numeric> StopLossExitPrice; // 止损执行价格
Series<Integer> StopLossExitBar; // 止损平仓的Bar编号
Series<Bool> StopLossReEntryEnabled; // 是否允许止损后二次入场
Series<Numeric> StopLossReEntryTarget; // 止损后二次入场目标价格
Series<Integer> StopLossWaitCount; // 止损后等待计数
Events
onBar(ArrayRef<Integer> indexs)
{
// ============================================
// ===== 第一部分:原策略代码(完全不变)开始 =====
// ============================================
If(Date>20260216)
{
PlotString("通知","你的使用权限到01月16日,请提前联系作者13728003500,以免策略失效",high,Yellow);
}
If(Date>20260216)
{
PlotString("通知","你的使用权限已经过期,请联系作者",High,Yellow);
Return;
}
Numeric minpoint=PriceScale* MinMove;
Numeric hs=GetMinuteLay;
Commentary("图标数据周期="+text(hs));
Numeric bard=1440/hs;
Numeric zf2=(HighD(0)-LowD(0))/closed(1)*100;
Numeric zfzq=NthCon(TrueDate(0)<>TrueDate(1))+1;
zf1=Average(zf2,zfzq);
Commentary("当日振幅="+text(zf1));
if(CurrentBar()==0)
{
barsum=0;
}Else if(CurrentBar()>0 and CurrentBar()<Barss)
{
Barsum=barsum+1;
}
Commentary("barsum="+text(barsum));
Numeric avgzf1= round(Average(zf1,Barsum),2);
if(CurrentBar()<=bard)
avgzf=1;
Else avgzf=avgzf1;
Commentary("平均振幅="+text(avgzf));
Numeric nt=34;
Numeric a1 = exp(-1.414 * 3.14159 /nt);
Numeric b1 = 2 * a1 * Cos(1.414 * 180/nt);
Numeric c2 = b1;
Numeric c3 = -a1 * a1;
Numeric c1 = 1 - c2 - c3;
Filt2 = c1 *(close + Close[1]) / 2 + c2 * Filt2[1] + c3 * Filt2[2];
ma60=Average(c,nt)+Average((filt2-filt2[1])*minpoint,nt);
// ===== 使用独立的多空参数计算 =====
atr_long = AvgTrueRange(LongATRPeriod);
atr_short = AvgTrueRange(ShortATRPeriod);
hh_long = Highest(high,LongHHPeriod);
ll_short = Lowest(low,ShortLLPeriod);
hh_short = Highest(high,ShortHHPeriod);
ll_long = Lowest(low,LongLLPeriod);
atr= AvgTrueRange(LongATRPeriod);
hh=Highest(high,LongHHPeriod);
ll=Lowest(low,ShortLLPeriod);
Numeric i;
For i=0 to 21-1
{
if(high[i]==hh)
{
bi=i;
}
if(low[i]==ll)
{
si=i;
}
}
Commentary("bi="+text(bi));
Commentary("si="+text(si));
Numeric zfa=avgzf[1]*0.8;
Numeric zfb=avgzf[1]*0.9;
if(zg>zg[1])myhigh=zg;
if(zg<zg[1])mylow=zg;
if(zg2>zg2[1])myhigh2=zg2;
if(zg2<zg2[1])mylow2=zg2;
if(symbolName== "纯碱指数0")
lots=max(1,Lot);
else lots=Lot;
Numeric hydw=ContractUnit()*BigPointValue();
Numeric onemoney=(radio/100)*(hydw*close[1]);
Commentary("每手保证金=="+text(onemoney));
Numeric b2=NthCon(high==hh);
Numeric s2=NthCon(low==ll);
pzq=max(b2,s2);
Numeric l22=Lowest(low,pzq[1]);
Numeric h22=Highest(high,pzq[1]);
pclow= max(l22,mylow2);
pchigh=min(h22,myhigh2);
// ============================================
// ===== 盈利保护计算(原策略逻辑)开始 =====
// ============================================
Bool NeedProtectionExit = False;
Numeric TempExitPrice = 0;
If (MarketPosition != 0)
{
If (MarketPosition == 1) // 持有多单
{
If (OriginalEntryPrice == InvalidNumeric)
{
OriginalEntryPrice = AvgEntryPrice;
}
// 计算当前K线的最大浮动盈利
Numeric CurrentBarMaxProfit = (High - AvgEntryPrice) * ContractUnit() * BigPointValue() * CurrentContracts();
If (MaxProfit == InvalidNumeric)
{
MaxProfit = -999999;
}
// 更新持仓期间的最大盈利记录
If (CurrentBarMaxProfit > MaxProfit)
{
MaxProfit = CurrentBarMaxProfit;
MaxProfitPrice = High;
}
// ===== 盈利保护逻辑计算(原策略)=====
If (MaxProfit > TakeProfit And MaxProfit != -999999)
{
IsProtectionActive = True;
// 计算允许的最大回撤金额
// 公式:允许回撤金额 = 最大盈利 × 回撤百分比
Numeric AllowableLoss = MaxProfit * (TrailingPercent / 100);
// 计算回撤对应的价格距离
// 公式:价格距离 = 允许回撤金额 / (合约乘数 × 每点价值 × 当前持仓手数)
Numeric StopDistance = AllowableLoss / (ContractUnit() * BigPointValue() * CurrentContracts());
// 计算动态止损价格
// 公式:动态止损价 = 最大盈利价格 - 价格距离
Numeric DynamicStopPrice = MaxProfitPrice - StopDistance;
// 确保止损价不低于入场价(保护本金)
DynamicStopPrice = Max(DynamicStopPrice, AvgEntryPrice);
// 更新跟踪止损价格(只向上移动)
If (TrailStopPrice == InvalidNumeric Or DynamicStopPrice > TrailStopPrice)
{
TrailStopPrice = DynamicStopPrice;
}
// 检查是否触发止损
If (Low < TrailStopPrice And TrailStopPrice != InvalidNumeric)
{
NeedProtectionExit = True;
TempExitPrice = Min(Open, TrailStopPrice);
ExitReason = "盈利保护平多";
}
}
Else
{
IsProtectionActive = False;
}
DebugMaxProfit = MaxProfit;
}
Else If (MarketPosition == -1) // 持有空单
{
If (OriginalEntryPrice == InvalidNumeric)
{
OriginalEntryPrice = AvgEntryPrice;
}
// 计算当前K线的最大浮动盈利
Numeric CurrentBarMaxProfit = (AvgEntryPrice - Low) * ContractUnit() * BigPointValue() * CurrentContracts();
If (MaxProfit == InvalidNumeric)
{
MaxProfit = -999999;
}
// 更新持仓期间的最大盈利记录
If (CurrentBarMaxProfit > MaxProfit)
{
MaxProfit = CurrentBarMaxProfit;
MaxProfitPrice = Low;
}
// ===== 盈利保护逻辑计算(原策略)=====
If (MaxProfit > TakeProfit And MaxProfit != -999999)
{
IsProtectionActive = True;
// 计算允许的最大回撤金额
// 公式:允许回撤金额 = 最大盈利 × 回撤百分比
Numeric AllowableLoss = MaxProfit * (TrailingPercent / 100);
// 计算回撤对应的价格距离
// 公式:价格距离 = 允许回撤金额 / (合约乘数 × 每点价值 × 当前持仓手数)
Numeric StopDistance = AllowableLoss / (ContractUnit() * BigPointValue() * CurrentContracts());
// 计算动态止损价格
// 公式:动态止损价 = 最大盈利价格 + 价格距离
Numeric DynamicStopPrice = MaxProfitPrice + StopDistance;
// 确保止损价不高于入场价(保护本金)
DynamicStopPrice = Min(DynamicStopPrice, AvgEntryPrice);
// 更新跟踪止损价格(只向下移动)
If (TrailStopPrice == InvalidNumeric Or DynamicStopPrice < TrailStopPrice)
{
TrailStopPrice = DynamicStopPrice;
}
// 检查是否触发止损
If (High > TrailStopPrice And TrailStopPrice != InvalidNumeric)
{
NeedProtectionExit = True;
TempExitPrice = Max(Open, TrailStopPrice);
ExitReason = "盈利保护平空";
}
}
Else
{
IsProtectionActive = False;
}
DebugMaxProfit = MaxProfit;
}
}
// ============================================
// ===== 盈利保护计算(原策略逻辑)结束 =====
// ============================================
// ============================================
// ===== 原有的交易逻辑开始 =====
// ============================================
// ===== 多头交易逻辑开始 =====
if(MarketPosition<>1 and high>hh_long[1] and open<>high and (open-close[1])<2*atr_long[1] and b==1 and hh_long[1]>ma60[1] and ma60[1]>ma60[2])
{
buy(lots,max(open,hh_long[1]));
MaxProfit = -999999;
TrailStopPrice = InvalidNumeric;
IsProtectionActive = False;
OriginalEntryPrice = AvgEntryPrice;
ReEntryEnabled = False;
ExitReason = "";
// 重置回调跟踪变量
RetraceLowest = InvalidNumeric;
RetraceHighest = InvalidNumeric;
FoundLowestBar = 0;
}
if(MarketPosition==1 and BarsSinceEntry>0 and low<ll_long[1] and ll_long[1]<=mylow[1] and (close[1]-open)<2*atr_long[1] and open<>low)
{
sell(0,min(open,ll_long[1]));
MaxProfit = InvalidNumeric;
TrailStopPrice = InvalidNumeric;
IsProtectionActive = False;
OriginalEntryPrice = InvalidNumeric;
MaxProfitPrice = InvalidNumeric;
ProtectionExitPrice = InvalidNumeric;
ReEntryEnabled = False;
ReEntryTargetPrice = InvalidNumeric;
ExitReason = "";
// 重置回调跟踪变量
RetraceLowest = InvalidNumeric;
RetraceHighest = InvalidNumeric;
FoundLowestBar = 0;
}
// ===== 多头交易逻辑结束 =====
// ===== 空头交易逻辑开始 =====
if(MarketPosition<>-1 and low<ll_short[1] and open<>low and (close[1]-open)<2*atr_short[1] and s==1 and ll_short[1]<ma60[1] and ma60[1]<ma60[2])
{
SellShort(lots,min(open,ll_short[1]));
MaxProfit = -999999;
TrailStopPrice = InvalidNumeric;
IsProtectionActive = False;
OriginalEntryPrice = AvgEntryPrice;
ReEntryEnabled = False;
ExitReason = "";
// 重置回调跟踪变量
RetraceLowest = InvalidNumeric;
RetraceHighest = InvalidNumeric;
FoundLowestBar = 0;
}
if(MarketPosition==-1 and BarsSinceEntry>0 and high>hh_short[1] and (open-close[1])<2*atr_short[1] and open<>high)
{
BuyToCover(0,max(open,hh_short[1]));
MaxProfit = InvalidNumeric;
TrailStopPrice = InvalidNumeric;
IsProtectionActive = False;
OriginalEntryPrice = InvalidNumeric;
MaxProfitPrice = InvalidNumeric;
ProtectionExitPrice = InvalidNumeric;
ReEntryEnabled = False;
ReEntryTargetPrice = InvalidNumeric;
ExitReason = "";
// 重置回调跟踪变量
RetraceLowest = InvalidNumeric;
RetraceHighest = InvalidNumeric;
FoundLowestBar = 0;
}
// ===== 空头交易逻辑结束 =====
// ===== 盈利保护平仓 =====
If (NeedProtectionExit And MarketPosition != 0)
{
If (MarketPosition == 1)
{
sell(CurrentContracts(), TempExitPrice);
PlotString("盈利保护", ExitReason, High, Red);
LastExitBar = CurrentBar();
ProtectionExitPrice = TempExitPrice;
// 启动二次入场机制
ReEntryEnabled = True;
ReEntryBarCount = 0;
// 计算二次入场目标价格
// 公式:目标价 = 最大盈利价格 × (1 - 回调百分比)
ReEntryTargetPrice = MaxProfitPrice * (1 - ReEntryPercent / 100);
// 初始化回调跟踪变量
RetraceLowest = InvalidNumeric;
RetraceHighest = InvalidNumeric;
FoundLowestBar = 0;
}
Else If (MarketPosition == -1)
{
BuyToCover(CurrentContracts(), TempExitPrice);
PlotString("盈利保护", ExitReason, Low, Red);
LastExitBar = CurrentBar();
ProtectionExitPrice = TempExitPrice;
// 启动二次入场机制
ReEntryEnabled = True;
ReEntryBarCount = 0;
// 计算二次入场目标价格
// 公式:目标价 = 最大盈利价格 × (1 + 回调百分比)
ReEntryTargetPrice = MaxProfitPrice * (1 + ReEntryPercent / 100);
// 初始化回调跟踪变量
RetraceLowest = InvalidNumeric;
RetraceHighest = InvalidNumeric;
FoundLowestBar = 0;
}
MaxProfit = InvalidNumeric;
TrailStopPrice = InvalidNumeric;
IsProtectionActive = False;
}
// ============================================
// ===== 二次入场机制开始 =====
// ============================================
If (MarketPosition == 0 And ReEntryEnabled)
{
ReEntryBarCount = ReEntryBarCount + 1;
If (ReEntryBarCount >= ReEntryDelay)
{
Bool PriceRetracedDeeply = False;
Bool PriceBreakoutAgain = False;
Bool BestLocationFound = False; // 最佳位置标志
// 多单的入场模式
If (ExitReason == "盈利保护平多")
{
// === 模式A:回调低吸模式(增强版)===
// 步骤1:寻找回调最低点
If (RetraceLowest == InvalidNumeric Or Low < RetraceLowest)
{
RetraceLowest = Low;
RetraceHighest = High;
FoundLowestBar = CurrentBar();
}
// 步骤2:检查是否回调到位
If (RetraceLowest <= ReEntryTargetPrice And RetraceLowest != InvalidNumeric)
{
PriceRetracedDeeply = True;
// 步骤3:寻找最佳位置进场(回调最低点后的反弹确认)
If (PriceRetracedDeeply And CurrentBar - FoundLowestBar >= 1)
{
// 计算从最低点的反弹幅度
Numeric ReboundFromLow = Close - RetraceLowest;
Numeric RetraceRange = MaxProfitPrice - RetraceLowest;
If (RetraceRange > 0)
{
// 最佳位置条件1:反弹达到回调幅度的30%-70%(黄金区域)
// 公式:反弹百分比 = (当前收盘价 - 回调最低点) / (最大盈利价格 - 回调最低点) × 100%
Numeric ReboundPercent = ReboundFromLow / RetraceRange * 100;
Bool ReboundInGoldenZone = (ReboundPercent >= 30) And (ReboundPercent <= 70);
// 最佳位置条件2:出现企稳K线形态
Bool StabilizationSignal = False;
// 企稳信号1:长下影线
If ((Close - Low) > Abs(Close - Open) * 1.5 And (Close - Low) > minpoint * 5)
{
StabilizationSignal = True;
}
// 企稳信号2:阳线收盘
If (Close > Open And Close > (High + Low) / 2)
{
StabilizationSignal = True;
}
// 最佳位置条件3:均线支撑
Bool MASupport = False;
If (Close > ma60[0] And Low <= ma60[0] * 1.01)
{
MASupport = True;
}
// 综合判断:寻找最佳位置
If (ReboundInGoldenZone And (StabilizationSignal Or MASupport))
{
BestLocationFound = True;
Commentary("找到最佳入场位置!反弹幅度:" + Text(ReboundPercent) + "%");
}
}
}
}
// === 模式B:价格突破前高(突破追涨)===
If (high > MaxProfitPrice And MaxProfitPrice != InvalidNumeric)
{
PriceBreakoutAgain = True;
}
}
// 空单的入场模式
Else If (ExitReason == "盈利保护平空")
{
// === 模式A:回调高抛模式(增强版)===
// 步骤1:寻找反弹最高点
If (RetraceHighest == InvalidNumeric Or High > RetraceHighest)
{
RetraceHighest = High;
RetraceLowest = Low;
FoundLowestBar = CurrentBar();
}
// 步骤2:检查是否反弹到位
If (RetraceHighest >= ReEntryTargetPrice And RetraceHighest != InvalidNumeric)
{
PriceRetracedDeeply = True;
// 步骤3:寻找最佳位置进场(反弹高点后的回落确认)
If (PriceRetracedDeeply And CurrentBar - FoundLowestBar >= 1)
{
// 计算从最高点的回落幅度
Numeric DeclineFromHigh = RetraceHighest - Close;
Numeric RetraceRange = RetraceHighest - MaxProfitPrice;
If (RetraceRange > 0)
{
// 最佳位置条件1:回落达到反弹幅度的30%-70%
// 公式:回落百分比 = (反弹最高点 - 当前收盘价) / (反弹最高点 - 最大盈利价格) × 100%
Numeric DeclinePercent = DeclineFromHigh / RetraceRange * 100;
Bool DeclineInGoldenZone = (DeclinePercent >= 30) And (DeclinePercent <= 70);
// 最佳位置条件2:出现承压信号
Bool PressureSignal = False;
// 承压信号1:长上影线
If ((High - Close) > Abs(Close - Open) * 1.5 And (High - Close) > minpoint * 5)
{
PressureSignal = True;
}
// 承压信号2:阴线收盘
If (Close < Open And Close < (High + Low) / 2)
{
PressureSignal = True;
}
// 最佳位置条件3:均线压力
Bool MAPressure = False;
If (Close < ma60[0] And High >= ma60[0] * 0.99)
{
MAPressure = True;
}
// 综合判断:寻找最佳位置
If (DeclineInGoldenZone And (PressureSignal Or MAPressure))
{
BestLocationFound = True;
Commentary("找到最佳入场位置!回落幅度:" + Text(DeclinePercent) + "%");
}
}
}
}
// === 模式B:价格跌破前低(突破追空)===
If (low < MaxProfitPrice And MaxProfitPrice != InvalidNumeric)
{
PriceBreakoutAgain = True;
}
}
// ===== 入场判断逻辑 =====
// 三种入场方式:1.最佳位置 2.回调确认 3.突破
Bool CanReEnter = False;
// 优先级1:最佳位置入场(最高优先级)
If (BestLocationFound)
{
CanReEnter = True;
Commentary("入场方式:最佳位置入场");
}
// 优先级2:回调模式入场(需要K线确认)
Else If (PriceRetracedDeeply And Not CanReEnter)
{
If (ExitReason == "盈利保护平多")
{
If (Close > Open Or Close > (High + Low) / 2)
{
CanReEnter = True;
Commentary("入场方式:回调确认入场");
}
}
Else If (ExitReason == "盈利保护平空")
{
If (Close < Open Or Close < (High + Low) / 2)
{
CanReEnter = True;
Commentary("入场方式:回调确认入场");
}
}
}
// 优先级3:突破模式入场
Else If (PriceBreakoutAgain And Not CanReEnter)
{
If (ExitReason == "盈利保护平多")
{
If (open <> high and (open-close[1])<2*atr_long[1]
and b==1 and MaxProfitPrice > ma60[1] and ma60[1] > ma60[2])
{
CanReEnter = True;
Commentary("入场方式:突破追加入场");
}
}
Else If (ExitReason == "盈利保护平空")
{
If (open <> low and (close[1]-open)<2*atr_short[1]
and s==1 and MaxProfitPrice < ma60[1] and ma60[1] < ma60[2])
{
CanReEnter = True;
Commentary("入场方式:突破追加入场");
}
}
}
// ===== 执行入场 =====
If (CanReEnter)
{
If (ExitReason == "盈利保护平多")
{
Numeric ReEntryLots = lots * 0.8;
If (ReEntryLots < 1) ReEntryLots = 1;
// 根据模式选择入场价格
Numeric EntryPrice;
If (BestLocationFound)
{
// 最佳位置模式:在当前价格入场
EntryPrice = Open;
}
Else If (PriceRetracedDeeply)
{
// 回调模式:在目标价位入场
EntryPrice = max(Open, ReEntryTargetPrice);
}
Else
{
// 突破模式:在突破价位入场
EntryPrice = max(Open, MaxProfitPrice);
}
buy(ReEntryLots, EntryPrice);
PlotString("二次入场", "二次入场做多", Low, Cyan);
MaxProfit = -999999;
OriginalEntryPrice = AvgEntryPrice;
}
Else If (ExitReason == "盈利保护平空")
{
Numeric ReEntryLots = lots * 0.8;
If (ReEntryLots < 1) ReEntryLots = 1;
// 根据模式选择入场价格
Numeric EntryPrice;
If (BestLocationFound)
{
// 最佳位置模式:在当前价格入场
EntryPrice = Open;
}
Else If (PriceRetracedDeeply)
{
// 回调模式:在目标价位入场
EntryPrice = min(Open, ReEntryTargetPrice);
}
Else
{
// 突破模式:在突破价位入场
EntryPrice = min(Open, MaxProfitPrice);
}
SellShort(ReEntryLots, EntryPrice);
PlotString("二次入场", "二次入场做空", High, Cyan);
MaxProfit = -999999;
OriginalEntryPrice = AvgEntryPrice;
}
ReEntryEnabled = False;
ReEntryTargetPrice = InvalidNumeric;
// 重置回调跟踪变量
RetraceLowest = InvalidNumeric;
RetraceHighest = InvalidNumeric;
FoundLowestBar = 0;
}
// 超时重置
If (ReEntryBarCount > ReEntryDelay * 5)
{
ReEntryEnabled = False;
ReEntryTargetPrice = InvalidNumeric;
// 重置回调跟踪变量
RetraceLowest = InvalidNumeric;
RetraceHighest = InvalidNumeric;
FoundLowestBar = 0;
}
}
}
// ============================================
// ===== 二次入场机制结束 =====
// ============================================
// ============================================
// ===== 第二部分:止损保护模块(完全不变)开始 =====
// ============================================
// 初始化止损保护变量
If (CurrentBar == 0)
{
StopLossMaxDrawdown = 0;
StopLossTriggerPrice = InvalidNumeric;
StopLossActive = False;
StopLossExitReason = "";
StopLossEntryPrice = InvalidNumeric;
StopLossExitPrice = InvalidNumeric;
StopLossExitBar = 0;
StopLossReEntryEnabled = False;
StopLossReEntryTarget = InvalidNumeric;
StopLossWaitCount = 0;
}
// ===== 止损保护计算开始 =====
Bool NeedStopLossExit = False;
Numeric StopLossExitPriceTemp = 0;
If (MarketPosition != 0)
{
If (MarketPosition == 1) // 持有多单
{
// 计算当前K线最大亏损
// 公式:当前K线最大亏损 = (最低价 - 入场均价) × 合约乘数 × 每点价值 × 当前持仓手数
Numeric CurrentBarMaxLoss = (Low - AvgEntryPrice) * ContractUnit() * BigPointValue() * CurrentContracts();
// 更新最大亏损记录
If (CurrentBarMaxLoss < StopLossMaxDrawdown)
{
StopLossMaxDrawdown = CurrentBarMaxLoss;
}
// 检查是否达到止损触发条件
If (StopLossMaxDrawdown <= -StopLossAmount And StopLossMaxDrawdown != 0)
{
StopLossActive = True;
// 计算动态止损价格(类似跟踪止损)
// 公式:允许恢复金额 = 最大亏损 × 缓冲百分比
Numeric AllowableRecovery = -StopLossMaxDrawdown * (StopLossBuffer / 100);
// 公式:止损距离 = 允许恢复金额 / (合约乘数 × 每点价值 × 当前持仓手数)
Numeric StopDistance = AllowableRecovery / (ContractUnit() * BigPointValue() * CurrentContracts());
// 公式:动态止损价 = 入场均价 - 止损距离
Numeric DynamicStopPrice = AvgEntryPrice - StopDistance;
// 确保止损价格不会过于宽松
DynamicStopPrice = Min(DynamicStopPrice, AvgEntryPrice);
// 更新止损触发价格(只向不利方向移动)
If (StopLossTriggerPrice == InvalidNumeric Or DynamicStopPrice < StopLossTriggerPrice)
{
StopLossTriggerPrice = DynamicStopPrice;
}
// 检查是否需要执行止损
If (Low < StopLossTriggerPrice And StopLossTriggerPrice != InvalidNumeric)
{
NeedStopLossExit = True;
StopLossExitPriceTemp = Min(Open, StopLossTriggerPrice);
StopLossExitReason = "止损保护平多";
}
}
}
Else If (MarketPosition == -1) // 持有空单
{
// 计算当前K线最大亏损
// 公式:当前K线最大亏损 = (入场均价 - 最高价) × 合约乘数 × 每点价值 × 当前持仓手数
Numeric CurrentBarMaxLoss = (AvgEntryPrice - High) * ContractUnit() * BigPointValue() * CurrentContracts();
// 更新最大亏损记录
If (CurrentBarMaxLoss < StopLossMaxDrawdown)
{
StopLossMaxDrawdown = CurrentBarMaxLoss;
}
// 检查是否达到止损触发条件
If (StopLossMaxDrawdown <= -StopLossAmount And StopLossMaxDrawdown != 0)
{
StopLossActive = True;
// 计算动态止损价格
// 公式:允许恢复金额 = 最大亏损 × 缓冲百分比
Numeric AllowableRecovery = -StopLossMaxDrawdown * (StopLossBuffer / 100);
// 公式:止损距离 = 允许恢复金额 / (合约乘数 × 每点价值 × 当前持仓手数)
Numeric StopDistance = AllowableRecovery / (ContractUnit() * BigPointValue() * CurrentContracts());
// 公式:动态止损价 = 入场均价 + 止损距离
Numeric DynamicStopPrice = AvgEntryPrice + StopDistance;
// 确保止损价格不会过于宽松
DynamicStopPrice = Max(DynamicStopPrice, AvgEntryPrice);
// 更新止损触发价格(只向不利方向移动)
If (StopLossTriggerPrice == InvalidNumeric Or DynamicStopPrice > StopLossTriggerPrice)
{
StopLossTriggerPrice = DynamicStopPrice;
}
// 检查是否需要执行止损
If (High > StopLossTriggerPrice And StopLossTriggerPrice != InvalidNumeric)
{
NeedStopLossExit = True;
StopLossExitPriceTemp = Max(Open, StopLossTriggerPrice);
StopLossExitReason = "止损保护平空";
}
}
}
}
// ===== 止损保护计算结束 =====
// ===== 止损保护平仓执行开始 =====
If (NeedStopLossExit And MarketPosition != 0)
{
// 记录止损信息
StopLossEntryPrice = AvgEntryPrice;
StopLossExitPrice = StopLossExitPriceTemp;
StopLossExitBar = CurrentBar();
If (MarketPosition == 1)
{
sell(CurrentContracts(), StopLossExitPriceTemp);
PlotString("止损保护", StopLossExitReason, High, Magenta);
// 设置二次入场条件
StopLossReEntryEnabled = True;
StopLossWaitCount = 0;
// 计算止损后二次入场目标价格
// 公式:止损后入场目标价 = 止损执行价 × (1 + 止损后入场回调百分比)
StopLossReEntryTarget = StopLossExitPrice * (1 + StopLossReEntryPercent / 100);
}
Else If (MarketPosition == -1)
{
BuyToCover(CurrentContracts(), StopLossExitPriceTemp);
PlotString("止损保护", StopLossExitReason, Low, Magenta);
// 设置二次入场条件
StopLossReEntryEnabled = True;
StopLossWaitCount = 0;
// 计算止损后二次入场目标价格
// 公式:止损后入场目标价 = 止损执行价 × (1 - 止损后入场回调百分比)
StopLossReEntryTarget = StopLossExitPrice * (1 - StopLossReEntryPercent / 100);
}
// 重置止损保护状态
StopLossMaxDrawdown = 0;
StopLossTriggerPrice = InvalidNumeric;
StopLossActive = False;
}
// ===== 止损保护平仓执行结束 =====
// ===== 止损后二次入场机制开始 =====
If (MarketPosition == 0 And StopLossReEntryEnabled)
{
StopLossWaitCount = StopLossWaitCount + 1;
If (StopLossWaitCount >= StopLossReEntryDelay)
{
Bool PriceRecovered = False;
If (StopLossExitReason == "止损保护平多")
{
// 价格反弹到目标位以上时考虑二次入场做多
If (High >= StopLossReEntryTarget)
{
PriceRecovered = True;
}
}
Else If (StopLossExitReason == "止损保护平空")
{
// 价格回落到目标位以下时考虑二次入场做空
If (Low <= StopLossReEntryTarget)
{
PriceRecovered = True;
}
}
If (PriceRecovered)
{
// 额外的入场条件检查
Bool CanReEnter = False;
If (StopLossExitReason == "止损保护平多")
{
// 反弹强劲时入场
If (Close > Open And Close > (High + Low) / 2)
{
CanReEnter = True;
}
}
Else If (StopLossExitReason == "止损保护平空")
{
// 下跌强劲时入场
If (Close < Open And Close < (High + Low) / 2)
{
CanReEnter = True;
}
}
If (CanReEnter)
{
Numeric ReEntryLots = lots * 0.7; // 止损后仓位减小
If (ReEntryLots < 1) ReEntryLots = 1;
If (StopLossExitReason == "止损保护平多")
{
buy(ReEntryLots, max(Open, StopLossReEntryTarget));
PlotString("止损后入场", "止损后二次做多", Low, Blue);
}
Else If (StopLossExitReason == "止损保护平空")
{
SellShort(ReEntryLots, min(Open, StopLossReEntryTarget));
PlotString("止损后入场", "止损后二次做空", High, Blue);
}
// 重置止损二次入场状态
StopLossReEntryEnabled = False;
StopLossReEntryTarget = InvalidNumeric;
}
}
// 超时重置
If (StopLossWaitCount > StopLossReEntryDelay * 4)
{
StopLossReEntryEnabled = False;
StopLossReEntryTarget = InvalidNumeric;
}
}
}
// ===== 止损后二次入场机制结束 =====
// ===== 新开仓时重置止损保护 =====
If (MarketPosition != 0 And BarsSinceEntry == 0)
{
StopLossMaxDrawdown = 0;
StopLossTriggerPrice = InvalidNumeric;
StopLossActive = False;
StopLossReEntryEnabled = False;
StopLossReEntryTarget = InvalidNumeric;
StopLossWaitCount = 0;
}
// ===== 原策略正常平仓时重置 =====
If (MarketPosition == 0 And StopLossActive)
{
StopLossMaxDrawdown = 0;
StopLossTriggerPrice = InvalidNumeric;
StopLossActive = False;
StopLossReEntryEnabled = False;
StopLossReEntryTarget = InvalidNumeric;
StopLossWaitCount = 0;
}
// ============================================
// ===== 第二部分:止损保护模块(完全不变)结束 =====
// ============================================
// ===== 图表显示开始 =====
If (StopLossActive And StopLossTriggerPrice != InvalidNumeric)
{
If (MarketPosition == 1)
{
PlotNumeric("止损保护线", StopLossTriggerPrice, 0, Magenta);
}
Else If (MarketPosition == -1)
{
PlotNumeric("止损保护线", StopLossTriggerPrice, 0, Magenta);
}
}
If (StopLossReEntryEnabled And StopLossReEntryTarget != InvalidNumeric)
{
PlotNumeric("止损后入场线", StopLossReEntryTarget, 0, Blue);
}
// ===== 原策略的显示监控信息开始 =====
If (MarketPosition != 0)
{
Commentary("===== 盈利保护状态 =====");
Commentary("当前持仓:" + IIfString(MarketPosition == 1, "多单", "空单"));
Commentary("入场价格:" + Text(AvgEntryPrice));
Commentary("当前价格:" + Text(Close));
Commentary("最大盈利:" + Text(MaxProfit));
Commentary("保护激活:" + IIfString(IsProtectionActive, "是", "否"));
If (IsProtectionActive)
{
Commentary("保护级别:盈利保护(回撤" + Text(TrailingPercent) + "%)");
Commentary("跟踪止损价:" + Text(TrailStopPrice));
}
If (TrailStopPrice != InvalidNumeric)
{
PlotNumeric("跟踪止损", TrailStopPrice, 0, Red);
}
}
If (ReEntryEnabled)
{
Commentary("===== 二次入场监控 =====");
Commentary("等待计数:" + Text(ReEntryBarCount) + "/" + Text(ReEntryDelay));
Commentary("目标价位:" + Text(ReEntryTargetPrice));
Commentary("前高/前低价位:" + Text(MaxProfitPrice));
Commentary("入场模式:最佳位置/回调低吸/突破追涨");
PlotNumeric("二次入场线", ReEntryTargetPrice, 0, Green);
PlotNumeric("前高前低线", MaxProfitPrice, 0, Yellow);
// 显示回调最低点
If (RetraceLowest != InvalidNumeric)
{
Commentary("回调最低点:" + Text(RetraceLowest));
PlotNumeric("回调最低点", RetraceLowest, 0, Cyan);
}
}
// ===== 止损保护监控信息开始 =====
If (MarketPosition != 0)
{
Commentary("===== 止损保护状态 =====");
Commentary("最大亏损:" + Text(StopLossMaxDrawdown));
Commentary("止损激活:" + IIfString(StopLossActive, "是", "否"));
If (StopLossActive And StopLossTriggerPrice != InvalidNumeric)
{
Commentary("止损触发价:" + Text(StopLossTriggerPrice));
}
}
// ===== 止损保护监控信息结束 =====
// ===== 图表显示结束 =====
}
代码无法编译通过,代码2000多行,建议去找代码编写者咨询