Data[0]图层为原油加权30分钟
Data[1]图层为原油加权1分钟
平空单条件为Data[1]. H>= Data[0]. ShortExitPrice
条件在图中满足,为什么没有平仓呢?
空单平仓代码如下:
// 平空仓:从开仓后最低点往上涨1.6%时平仓
If (MarketPosition == -1)
{
//
If (BarsSinceEntry==0)
{
LowestSinceEntry = LOW;
Commentary("LowestSinceEntry= "+Text(LowestSinceEntry));
}
If (BarsSinceEntry>0)
{
LowestSinceEntry =Min(LowestSinceEntry[1],Low); //随后的bar更新最高价
Commentary("LowestSinceEntry= "+Text(LowestSinceEntry));
}
// 计算回撤平仓价格
ShortExitPrice = LowestSinceEntry * (1 + DrawdownPercent / 100);
Commentary("ShortExitPrice= "+Text(ShortExitPrice));
Data[1]. PlotNumeric("ShortExitPrice", Data[0]. ShortExitPrice);
// 检查是否需要平仓
If (Data[1]. H>= Data[0]. ShortExitPrice )
{
BuyToCover(TradeLots, ShortExitPrice);
}
} If (Data[1]. H>= Data[0]. ShortExitPrice )
表面data.1最高价大于data.0出场价格shortexitprice,
在data.0图层已经输出data.1的最高价(说明判断条件是没有问题的)
为什么平仓信号却没有出来,而是往后延迟呢?
平仓代码如下
// 检查是否需要平仓
If (MarketPosition==-1 And Data[1]. H>= Data[0]. ShortExitPrice )
{
Data[1].Commentary("Data[1]. H= "+Text(Data[1]. H));
BuyToCover(TradeLots, ShortExitPrice);
} 
在data.1图层已经输出data.1的最高价(说明判断条件是没有问题的)
我觉得如果你是求助,那就把完整能复现问题的代码发出来,只发一小段是没用的。
如果不想发代码,不想泄露策略,也可以精简代码,把不影响的地方去掉,留一个最简单的能复现问题的代码demo也行。
如果这都不行,那就只能自己调试了
好的,老师,策略代码如下
Params
Numeric FastLength(12);
Numeric SlowLength(26);
Numeric MACDLength(9);
Numeric LongMAPeriod(101);
Numeric TradeLots(1);
Numeric PriceBandPercent(1.9);
Numeric DrawdownPercent(1.6);
Numeric N(2);
Vars
Series<Numeric> MACDDiff;
Series<Numeric> MACDDEA;
Series<Numeric> MACDValue;
Bool MACDDeathCross;
Series<Numeric> LongMA;
Series<Numeric> LowerPriceLimit;
Bool BelowLongMA;
Bool InPriceBandShort;
Numeric RecentLow;
Bool BreakRecentLow;
Bool EntryShortSignal;
Bool ExitShortSignal;
Numeric EntryPrice;
Series<Numeric> LowestSinceEntry;
Series<Numeric> ShortExitPrice;
Events
OnBar(ArrayRef<Integer> indexs)
{
MACDDiff = XAverage(Close, FastLength) - XAverage(Close, SlowLength);
MACDDEA = XAverage(MACDDiff, MACDLength);
MACDValue = 2 * (MACDDiff - MACDDEA);
MACDDeathCross = MACDDiff[1]<MACDDEA[1];
LongMA = AverageFC(Close, LongMAPeriod);
PlotNumeric("LongMA", LongMA,LongMA, RGB(255, 0, 0));
LowerPriceLimit = LongMA * (1 - PriceBandPercent / 100);
PlotNumeric("LowerPriceLimit", LowerPriceLimit,LowerPriceLimit, Yellow);
BelowLongMA = Close[1] < LongMA[1];
InPriceBandShort = BelowLongMA && Close[1] >= LowerPriceLimit[1];
RecentLow = Lowest(Low[2],N);
BreakRecentLow = Close[1] < RecentLow;
EntryShortSignal = MACDDeathCross && InPriceBandShort && BreakRecentLow;
If (EntryShortSignal && MarketPosition != -1)
{
EntryPrice = OPEN;
SellShort(TradeLots, Open);
}
If (MarketPosition == -1)
{
If (BarsSinceEntry==0)
{
LowestSinceEntry = LOW;
Commentary("LowestSinceEntry= "+Text(LowestSinceEntry));
}
If (BarsSinceEntry>0)
{
LowestSinceEntry =Min(LowestSinceEntry[1],Low);
Commentary("LowestSinceEntry= "+Text(LowestSinceEntry));
}
ShortExitPrice = LowestSinceEntry * (1 + DrawdownPercent / 100);
Commentary("ShortExitPrice= "+Text(ShortExitPrice));
Data[1]. PlotNumeric("ShortExitPrice", Data[0]. ShortExitPrice);
If (Data[1]. H>= Data[0]. ShortExitPrice )
{
Data[1].Commentary("Data[1]. H= "+Text(Data[1]. H));
BuyToCover(TradeLots, ShortExitPrice);
}
}
}以SC000合约为例,Data[0]是30分钟,Data[1]是1分钟
Data[1]图层2026年4月17日21:48已满足空单出场条件
为什么平仓信号却没有出来,而是往后延迟呢?

平什么仓?这前面不是已经平掉了吗
老师,数据图层你弄反了
Data[0]是30分钟,Data[1]是1分钟
SC000合约Data[1]图层2026年4月17日21:48已满足空单出场条件
但是没有出场信号
data0是30分钟,data1是1分钟?
你的信号是出在大周期上的?
我们的教程和直播答疑里反复说了很多遍,跨周期,信号一定是出在小周期上才好处理。
你如果一定要出在大周期上那我们也没有办法处理了。
我觉得这个问题好像也不难诊断吧,在平仓的业务逻辑代码前输出一下marketposition,还有你的平仓条件的变量数据,总是能发现哪里不对的
data0这个图层之前开过仓的,如图二所示

data0这个图层之前开过仓么?