之前写过双均线与四均线系统,这个也是tb自带的指数移动平均线系统,看了代码,其实就是三均线系统罢了,顺便也解读一下了。
策略说明:
1.计算三条指数移动平均线(Avg1, Avg2 , Avg3);
2.通过指数移动平均线的组合来判断趋势
入场条件:
1.当Avg1向上穿过Avg2并且Avg2大于Avg3时,在下一根k线开盘处买入
2.当Avg1向下穿过Avg2并且Avg2小于Avg3时,在下一根k线开盘处卖出
出场条件:
1.Avg1下穿Avg2多头出场
2.跟踪止损
做多的代码及解读如下:
Params
Numeric AvgLen1(6);//声明数值参数AvgLen1,初值6.//
Numeric AvgLen2(12);//声明数值参数AvgLen2,初值12.//
Numeric AvgLen3(28);//声明数值参数AvgLen3,初值28.//
Numeric RLength(4);//声明数值参数RLength,初值4.//
Vars
NumericSeries Avg1; //声明数值序列变量Avg1,即指数移动平均1。//
NumericSeries Avg2; //声明数值序列变量Avg2,即指数移动平均2.//
NumericSeries Avg3; //声明数值序列变量Avg3,即指数移动平均3.//
BoolSeries BuyCon1(False); //声明布尔型序列变量BuyCon1,初值为假,即做多条件之一。//
NumericSeries LongStopPrice; //声明数值序列变量LongStopPrice,即跟踪止损价。//
NumericSeries Range; //声明数值序列变量Range,即K线幅度。//
NumericSeries RangeL; //声明数值序列变量RangeL。//
Begin
If(!CallAuctionFilter()) Return;// 集合竞价和小节休息过滤。//
//初始设置。//
Avg1=XAverage(Close,AvgLen1);//指数移动平均1。//
Avg2=XAverage(Close,AvgLen2);//指数移动平均2。//
Avg3=XAverage(Close,AvgLen3);//指数移动平均3。//
Range=High-Low; //定义K线幅度.//
PlotNumeric("Avg1",Avg1);//画均线1.//
PlotNumeric("Avg2",Avg2);//画均线2.//
PlotNumeric("Avg3",Avg3);//画均线3.//
BuyCon1=CrossOver(Avg1,Avg2); //Avg1向上穿过Avg2为买入条件之一。//
If(BuyCon1[1] And Avg2[1]>Avg3[1] And Vol > 0) //假如前一个BuyCon1满足,且前一个Avg2大于前一个Avg3时,做多。//
Buy(0,Open);//开盘价买入。//
If(MarketPosition==1 And BarsSinceEntry>0 And Avg1[1]<Avg2[1] And Vol > 0) //假如当前持有多单,且建仓数位大于0,且前一个Avg1小于前一个Avg2,则多头出场。//
Sell(0,Open);//开盘价平仓。//
//设置跟踪止损价。//
RangeL= Average(Range,RLength);//计算4周期振幅的均线。//
If(MarketPosition==1 And BarsSinceEntry==0)//假如当前持有多单,且建仓数位等于0的。//
{
LongStopPrice=Low-RangeL;//止损价 = 最低价Low - 振幅均线RangeL。//
}Else If(MarketPosition==1 And BarsSinceEntry>0)//假如持有多单,且建仓数位大于0 的。//
{
LongStopPrice=LongStopPrice+(Low-LongStopPrice)*0.25;//跟踪止损的计算,代入数值,等号右边的两个LongStopPrice值为上一步计算所得的止损价,先算完右边的了,最后把所得值,再赋值给左边的LongStopPrice即可得新的跟踪止损价。//
}
If(MarketPosition==1 And BarsSinceEntry>0 And Low<=LongStopPrice[1] And Vol > 0)//当前持有多单,且建仓数位大于0,且向下跌破跟踪止损价,则多头出场。//
{
Sell(0,Min(Open,LongStopPrice[1]));//平仓。//
}
End
做空代码及结果如下:
Params
Numeric AvgLen1(6);
Numeric AvgLen2(12);
Numeric AvgLen3(28);
Numeric RLength(4);
Vars
NumericSeries Avg1;
NumericSeries Avg2;
NumericSeries Avg3;
BoolSeries SellCon1(False);
NumericSeries ShortStopPrice;
NumericSeries Range;
NumericSeries RangeS;
Begin
If(!CallAuctionFilter()) Return;
Avg1=XAverage(Close,AvgLen1);
Avg2=XAverage(Close,AvgLen2);
Avg3=XAverage(Close,AvgLen3);
Range=High-Low;
PlotNumeric("Avg1",Avg1);
PlotNumeric("Avg2",Avg2);
PlotNumeric("Avg3",Avg3);
SellCon1=CrossUnder(Avg1,Avg2);
If(SellCon1[1] And Avg2[1]<Avg3[1] And Vol > 0)
SellShort(0,Open);
If(MarketPosition==-1 And BarsSinceEntry>0 And Avg1[1]>Avg2[1] And Vol > 0)
BuyToCover(0,Open);
RangeS=Average(Range,RLength);
If(MarketPosition==-1 And BarsSinceEntry==0)
{
ShortStopPrice=High+RangeS;
}Else If(MarketPosition==-1 And BarsSinceEntry>0)
{
ShortStopPrice=ShortStopPrice-(ShortStopPrice-High)/3;
}
If(MarketPosition==-1 And BarsSinceEntry>0 And High>=ShortStopPrice[1] And Vol > 0)
{
BuyToCover(0,Max(Open,ShortStopPrice[1]));
}
End
结果一般,我只说这些参数是没有经过什么优化的,而且选择的周期都是很短的。要想用这个系统,还得自己统计相应商品的数据,修改一个合适的参数才行。