Params
Numeric SLength(200);
Numeric FastLength(12);
Numeric SlowLength(26);
Numeric MACDLength(9);
Numeric NearMAPer(2);
Numeric Lots(1);
Numeric StartPer3(10); //3级跟踪止盈,盈利6%启动
Numeric StopPer3(10); //3级跟踪止盈,盈利回撤10%触发
Numeric StartPer2(2); //3级跟踪止盈,盈利2%启动
Numeric StopPer2(2); //3级跟踪止盈,盈利回撤2%触发
Numeric a;
Vars
Bool BuyEntry1(False);
Bool SellEntry1(False);
NumericSeries MA1;
NumericSeries MACDValue;
NumericSeries AvgMACD;
NumericSeries MACDDiff;
NumericSeries HighestAfterEntry; // 开仓后出现的最高价
NumericSeries LowestAfterEntry; // 开仓后出现的最低价
NumericSeries BarsEntry;
Begin
MA1 = AverageFC(Close,SLength);
MACDValue = XAverage( Close, FastLength ) - XAverage( Close, SlowLength ) ;
AvgMACD = XAverage(MACDValue,MACDLength);
MACDDiff = MACDValue - AvgMACD;
BarsEntry = BarsSinceEntry;
HighestAfterEntry = Highest(High,BarsSinceEntry);
LowestAfterEntry = Lowest(low,BarsSinceEntry);
If(MarketPosition==1 && BarsSinceLastEntry>0) Commentary("开仓后最大盈利比率:"+Text((HighestAfterEntry[1]-LastEntryPrice)/LastEntryPrice*100)+"%");
If(MarketPosition==-1 && BarsSinceLastEntry>0) Commentary("开仓后最大盈利比率:"+Text((LastEntryPrice-LowestAfterEntry[1])/LastEntryPrice*100)+"%");
//记录开仓后高低点
If(BarsSinceentry == 0)
{
HighestAfterEntry = High;
LowestAfterEntry = Low;
}
else
{
HighestAfterEntry = Max(HighestAfterEntry,High); // 记录下当前Bar的最高点,用于下一个Bar的跟踪止损判断
LowestAfterEntry = Min(LowestAfterEntry,Low); // 记录下当前Bar的最低点,用于下一个Bar的跟踪止损判断
Commentary("持仓k线数量:"+Text(BarsEntry[1]));
}
//主体程序
//开仓方式1:
BuyEntry1 = MACDValue[1] > AvgMACD[1] && MACDValue[1]> 0 && c[1] > MA1[1] ;
SellEntry1 = MACDValue[1] < AvgMACD[1] && MACDValue[1]< 0 && c[1] < MA1[1] ;
If((MarketPosition == -1&& BuyEntry1 && BarsSinceEntry > a) || (MarketPosition == 0 && BuyEntry1))
{
Buy(lots,Open);
}
If((MarketPosition == 1 && SellEntry1 && BarsSinceEntry > a) || (MarketPosition == 0 && SellEntry1 ))
{
SellShort(lots,Open);
}
//止盈
If(MarketPosition==-1 && BarsSinceLastEntry>0 && LowestAfterEntry[1]<=LastEntryPrice*(1-0.01*StartPer3) && High>=LowestAfterEntry[1]+(LastEntryPrice-LowestAfterEntry[1])*0.01*StopPer3)
{
BuyToCover(0,Max(Open,LowestAfterEntry[1]+(LastEntryPrice-LowestAfterEntry[1])*0.01*StopPer3));
Commentary("最大盈利达到"+Text(StartPer3)+"%之后盈利回撤"+Text(StopPer3)+"%平空");
}
If(MarketPosition==1 && BarsSinceLastEntry>0 && HighestAfterEntry[1]>=LastEntryPrice*(1+0.01*StartPer3) && Low<=HighestAfterEntry[1]-(HighestAfterEntry[1]-LastEntryPrice)*0.01*StopPer3)
{
Sell(0,Min(Open,HighestAfterEntry[1]-(HighestAfterEntry[1]-LastEntryPrice)*0.01*StopPer3));
Commentary("最大盈利达到"+Text(StartPer3)+"%之后盈利回撤"+Text(StopPer3)+"%平多");
}
//止损
If(MarketPosition==-1 && BarsSinceLastEntry>0 && LowestAfterEntry[1]>=LastEntryPrice*(1-0.01*StartPer2) && High>=LowestAfterEntry[1]*(1+StopPer2*0.01))
{
BuyToCover(0,Max(LowestAfterEntry[1]*(1+StopPer2*0.01),Open));
Commentary("最大盈利不超过"+Text(StartPer2)+"%之后回撤"+Text(StopPer2)+"%平空");
}
If(MarketPosition==1 && BarsSinceLastEntry>0 && HighestAfterEntry[1]<=LastEntryPrice*(1+0.01*StartPer2) && Low<=HighestAfterEntry[1]*(1-StopPer2*0.01))
{
Sell(0,Min(HighestAfterEntry[1]*(1-StopPer2*0.01),Open));
Commentary("最大盈利不超过"+Text(StartPer2)+"%之后回撤"+Text(StopPer2)+"%平多");
}
//画线
PlotNumeric("MA1",MA1);
End
哪部分的数据读有问题?
新平开仓后,第二根K线显示的还是之前持仓bar的数量,而不是最新的,导致回撤止损出现计算错误。
定义bar的表述是不是有问题?