请大家帮忙看看这段代码的状态变量设置的位置、重置的位置对吗,为什么只开了一次仓之后,既无法执行再入场,也无法执行首开仓。
另外,con5 的在不符合条件的位置也标记了,请问是什么原因呢?
Params
//----------均线参数----------
Numeric Length1(5);
Numeric Length2(10);
Numeric Length3(20);
Numeric Length4(40);
Numeric Length5(60);
Numeric lots(1); //交易手数
//----------止损参数----------
Numeric StopLossPct(0.02); //2%止损比例
Vars
//----------均线指标变量----------
Numeric ma1;
Numeric ma2;
Numeric ma3;
Numeric ma4;
Numeric ma5;
//----------布林带指标变量----------
Series<Numeric> UpLine;
Series<Numeric> DownLine;
Series<Numeric> MidLine;
Numeric Band;
//----------开平空仓变量----------
series<bool> con1;
series<bool> con2;
series<bool> con3;
series<bool> con4;
Series<Bool> con5;
series<bool> kk1;//首空仓条件
Series<Numeric> LowestShortPositionPrice; //空头持仓期间最低价
Series<Numeric> ShortEntryPrice_1; //空头首开仓价格
Series<Numeric> ShortReEntryPrice; //空头再空仓价格
Series<Numeric> Stop_price; //止损价
Series<Numeric> Stop_price_1; //止损价
Series<Numeric> ShortStopPrice; //止损平空仓价格
Series<Numeric> ShortStopProfiPrice; //止盈平空仓价格
//----------开平空仓控制----------
series<bool> ShortEntry; //首开空标记
series<bool> ShortReEntry; //再空入标记
series<bool> ShortPositionClosed; //止盈平空标记
Events
OnBar(ArrayRef<Integer> indexs)
{
//>>>>>>>>>>指标计算<<<<<<<<<<
If(CurrentBar==0)
{
//——————————————均线——————————————
ma1 = Close;
ma2 = Close;
ma3 = Close;
ma4 = Close;
ma5 = Close;
//——————————————布林带—————————————
MidLine = Close;
UpLine = Close;
DownLine = Close;
//——————————————状态初始化——————————————
ShortEntry = False;
ShortReEntry = False;
ShortPositionClosed = False;
}
//——————————————均线——————————————
ma1 = Average(Close,Length1);
ma2 = Average(Close,Length2);
ma3 = Average(Close,Length3);
ma4 = Average(Close,Length4);
//ma5 = Average(Close,Length5);
PlotNumeric("ma1",ma1);
PlotNumeric("ma2",ma2);
PlotNumeric("ma3",ma3);
PlotNumeric("ma4",ma4);
//PlotNumeric("ma5",ma5,0,White);
//——————————————布林带——————————————
MidLine = AverageFC(Close,20);
Band = StandardDev(Close,20,2);
UpLine = MidLine + 2 * Band;
DownLine = MidLine - 2 * Band;
//PlotNumeric("UpLine",UpLine);
PlotNumeric("DownLine",DownLine);
//PlotNumeric("MidLine",MidLine);
//>>>>>首空仓<<<<<
con1 = High < ma4;
con2 = ma1 < ma2 and ma2 < ma3;
kk1 = con1[1] and con2[1];//
If(kk1) ShortEntry = True;
//if(kk1) Commentary("满足开1");
If(High < DownLine) con3 = True;
If(con3 And Close > DownLine) con4 = True;
//>>>>>>>>>>平仓<<<<<<<<<<
If(MarketPosition == -1)
{
Commentary("<<<<<持仓<<<<<");
Commentary("止损价:"+Text(Stop_price));
If(High[1]>Stop_price and BarsSinceEntry>=1)
{
ShortStopPrice = Min(Open,High[1]);
BuyToCover(lots,ShortStopPrice);
Commentary("止损");
ShortEntry = True;
}
if(con4[1] and BarsSinceEntry>=1)
{
ShortStopProfiPrice = Min(Open,Low[1]);
BuyToCover(lots,ShortStopProfiPrice);
Commentary("止盈");
ShortEntry = False;
//ShortReEntry = True;
ShortPositionClosed = True;
}
Commentary("<<<<<平仓<<<<<");
LowestShortPositionPrice = Min(low,LowestShortPositionPrice[1]);
Commentary("最低价"+Text(LowestShortPositionPrice));
}
//>>>>>>>>>>开仓<<<<<<<<<<
If(MarketPosition <>-1)
{
Commentary(">>>>>上轮持仓>>>>>");
LowestShortPositionPrice = LowestShortPositionPrice[1];
Commentary("最低价"+Text(LowestShortPositionPrice));
If(High[1] < LowestShortPositionPrice) con5 = True;
PlotBool("con5",True,low+minmove);
//>>>>>>>>>>再入场<<<<<<<<<<
If(con5 And ShortPositionClosed And ShortReEntry)//再空入
{
ShortReEntryPrice = Min(Open,Low[1]);
SellShort(lots,ShortReEntryPrice);
Commentary("再空入");
Stop_price = Floor(ShortReEntryPrice*(1+StopLossPct),MinMove*pricescale);
ShortReEntry = False;
ShortEntry = True;
ShortPositionClosed = False;
LowestShortPositionPrice = low;
}
//>>>>>>>>>>首开空仓<<<<<<<<<<
If(kk1 And ShortEntry And !ShortPositionClosed)
{
ShortEntryPrice_1 = Min(Open,Low[1]);
SellShort(lots,ShortEntryPrice_1);
Commentary(">>>>>开仓>>>>>");
Commentary("首开空");
ShortEntry = False;
Stop_price = Floor(ShortEntryPrice_1*(1+StopLossPct),MinMove*pricescale);
LowestShortPositionPrice = low;
}
}
}
把影响再入场的所有条件子集,挨个用plotbool画在图上看看,肯定有某个条件是一直false。分析这个false子集就行了。
当然有可能不是单个元素条件导致的,有可能是a条件有true有false,b条件也有true有false,但是a and b放一起,那就有true有false了
谢谢刘老师,找出问题了
👍