策略说明:
本策略基于当前收盘价与之前k线的高低进行打分, 并通过打分的均值与对应的收盘价均值进行交易
系统要素:
1. 当当前收盘价格大于之前LookBack根K线内某一根k线的收盘价时记+1分, 否则记-1分, 加总这些分数以获得当前K线的得分
2. 对k线的打分计算一条均线
3. 对k线的收盘计算一条均线
入场条件:
1. 当价格高于收盘价均线, 且打分也高于打分均线时的入场做多
2. 当价格低于收盘价均线, 且打分也低于打分均线时的入场做空
出场条件:
1. 基于ATR的保护性止损
2. 基于ATR的跟踪止损
3. 基于ATR的盈亏平衡止损
做多的源代码及解读如下:
Params
Numeric LookBack(10); //声明数值参数LookBack,初值10,用于给当前K线打分的回溯根数。//
Numeric MALength(18); //声明数值参数MALength,初值18,均线值。//
Numeric ATRLength(10); //声明数值参数ATRLength,初值10,ATR的值。//
Numeric ProtectStopATRMulti(0.5); //声明数值参数ProtectStopATRMulti,初值0.5,保护性止损的ATR乘数。//
Numeric TrailStopATRMulti(3); //声明数值参数TrailStopATRMulti,初值3,跟踪止损的ATR乘数。//
Numeric BreakEvenStopATRMulti(5); //声明数值参数BreakEvenStopATRMulti,初值5,盈亏平衡止损的ATR乘数。//
Vars
NumericSeries MA(0); //声明数值序列变量MA,初值0,收盘价均线。//
NumericSeries TrendScore(0); //声明数值序列变量TrendScore,初值0,当前K线的打分。//
NumericSeries TrendScoreMA(0); //声明数值序列变量TrendScoreMA,初值0,k线打分的均线。//
NumericSeries ATR(0); //声明数值序列变量ATR,初值0.//
Numeric i; //声明数值变量i。//
Numeric Temp;//声明数值变量Temp。//
NumericSeries HighAfterEntry; //声明数值序列变量HighAfterEntry,持仓后的高点记录。//
NumericSeries ProtectStopL; //声明数值序列变量ProtectStopL,基于ATR的保护性止损。//
Numeric TrailStopL; //声明数值变量TrailStopL,基于ATR的跟踪止损。//
Numeric BreakEvenStopL; //声明数值变量BreakEvenStopL,基于ATR的盈亏平衡止损。//
Numeric ExitLineL; //声明数值变量ExitLineL,平仓线。//
NumericSeries MP; //声明数值序列变量MP,即MarketPosition状态记录。//
Begin
If(!CallAuctionFilter()) Return;// 集合竞价和小节休息过滤。//
//系统设置,K线打分 - 当当前收盘价格大于之前LookBack根K线内某一根k线的收盘价时记+1分, 否则记-1分, 加总这些分数以获得当前K线的得分。//
TrendScore = 0;//初始赋值0.//
SetGlobalVar(1,0);//设置第一个位置全局变量,初值0.//
for i = LookBack DownTo 1 //从10循环到1//
{
If(i == LookBack)//假如变量 i 等于参数LookBack值。//
{
Temp = 0;//变量Temp赋值0.//
}
Else//变量i不等于参数LookBack值的。//
{
Temp = GetGlobalVar(1); //变量Temp赋值为位置1的全局变量值0。//
}
If(C>=C[i]) Temp = Temp +1;//假如当前收盘价c >=c[i],则变量Temp = Temp(初值0) + 1.//
Else Temp = Temp - 1;//假如当前收盘价C <C[i],则变量Temp = Temp(初值0)-1.//
SetGlobalVar(1,Temp); //设置位置为1的全局变量,值为Temp值。//
}
TrendScore = GetGlobalVar(1);//获取位置1的全局变量值Temp,赋值给变量TrendScore。//
//均线和ATR计算。//
MA = AverageFC(C,MALength); //收盘价均线。//
TrendScoreMA = AverageFC(TrendScore,MALength);//代入相应数值,求得变量TrendScoreMA均线。//
ATR = AvgTrueRange(ATRLength);//计算ATR公式了,前面解读过了。//
//系统入场,当价格高于收盘价均线, 且打分也高于打分均线时的入场做多。//
If(MarketPosition <> 1 and MA[1] <> 0) //假如当前没有持多单,且前一均线值不等于0.//
{
If(Close[1] >= MA[1] and TrendScore[1] >= TrendScoreMA[1] And Vol > 0)//假如前一收盘价大于等于前一均线值,且前一变量TrendScore[1]大于等于前一变量TrendScoreMA[1],且成交量大于0的。//
{
Buy(0,Open); //以开盘价开仓买入。//
ProtectStopL = Low[1] - ProtectStopATRMulti * ATR[1];//基于ATR的保护性止损计算公式,代入相应数值就行了。//
}
}
//系统出场。//
If(BarsSinceEntry == 0)//假如建仓数位等于0.//
HighAfterEntry = High;//记录持仓高价为当前最高价。//
Else //建仓数位不等于0的。//
HighAfterEntry = Max(HighAfterEntry[1],High);//把前一个持仓高价与当前最高价对比,取大值,再赋值给变量HighAfterEntry值。//
TrailStopL = HighAfterEntry[1] - TrailStopATRMulti * ATR[1];//基于ATR的跟踪止损计算公式,也是代入相应数值即可。//
BreakEvenStopL = LastEntryPrice;//基于ATR的盈亏平衡止损。//
If(MarketPosition == 1 and mp[1] == 1)//假如当前持有多单,且前一个mp[1]等于1.//
{
If(HighAfterEntry[1] >= BreakEvenStopL + BreakEvenStopATRMulti * ATR[1])//代入相应数值计算,与前一个持仓记录高价对比。//
{
if(TrailStopL >= BreakEvenStopL) ExitLineL = TrailStopL;//假如变量TrailStopL 大于等于变量BreakEvenStopL值。则出场价ExitLineL = 变量TrailStopL值。//
Else ExitLineL = BreakEvenStopL;//假如变量TrailStopL值小于变量BreakEvenStopL值的,则出场价ExitLineL = 变量BreakEvenStopL值。//
}
Else//持仓记录高价小的情况。//
{
if(TrailStopL >= ProtectStopL[1]) ExitLineL = TrailStopL;//假如变量TrailStopL大于等于前一变量ProtectStopL[1]值,则出场价ExitLineL = 变量TrailStopL值。//
Else ExitLineL = ProtectStopL[1];//假如变量TrailStopL小于前一变量ProtectStopL[1]值,则出场价ExitLineL = ProtectStopL[1]。//
}
//出场。//
if(L <= ExitLineL And Vol > 0)//假如当前低价小于等于ExitLineL值,且成交量大于0.//
{
Sell(0,Min(Open, ExitLineL));//平仓。//
}
}
MP = MarketPosition; //记录持仓状态。//
End
做空代码及结果如下:
Params
Numeric LookBack(10);
Numeric MALength(18);
Numeric ATRLength(10);
Numeric ProtectStopATRMulti(0.5);
Numeric TrailStopATRMulti(3);
Numeric BreakEvenStopATRMulti(5);
Vars
NumericSeries MA(0);
NumericSeries TrendScore(0);
NumericSeries TrendScoreMA(0);
NumericSeries ATR(0);
Numeric i;
Numeric Temp;
NumericSeries LowAfterEntry;
NumericSeries ProtectStopS;
Numeric TrailStopS;
Numeric BreakEvenStopS;
Numeric ExitLineS;
NumericSeries MP;
Begin
If(!CallAuctionFilter()) Return;
TrendScore = 0;
SetGlobalVar(1,0);
for i = LookBack DownTo 1
{
If(i == LookBack)
{
Temp = 0;
}
Else
{
Temp = GetGlobalVar(1);
}
If(C>=C[i]) Temp = Temp +1;
Else Temp = Temp - 1;
SetGlobalVar(1,Temp);
}
TrendScore = GetGlobalVar(1);
MA = AverageFC(C,MALength);
TrendScoreMA = AverageFC(TrendScore,MALength);
ATR = AvgTrueRange(ATRLength);
If(MarketPosition <> -1 and MA[1] <> 0)
{
If(Close[1] <= MA[1] and TrendScore[1] <= TrendScoreMA[1] And Vol > 0)
{
SellShort(0,Open);
ProtectStopS = High[1] + ProtectStopATRMulti * ATR[1];
}
}
If(BarsSinceEntry == 0)
LowAfterEntry = Low;
Else
LowAfterEntry = Min(LowAfterEntry[1],Low);
TrailStopS = LowAfterEntry[1] + TrailStopATRMulti * ATR[1];
BreakEvenStopS = LastEntryPrice;
If(MarketPosition == -1 and mp[1] == -1)
{
If(LowAfterEntry[1] <= BreakEvenStopS - BreakEvenStopATRMulti * ATR[1])
{
if(TrailStopS <= BreakEvenStopS) ExitLineS = TrailStopS;
Else ExitLineS = BreakEvenStopS;
}
Else
{
if(TrailStopS <= ProtectStopS[1]) ExitLineS = TrailStopS;
Else ExitLineS = ProtectStopS[1];
}
if(H >= ExitLineS And Vol > 0)
{
BuyToCover(0,Max(Open, ExitLineS));
}
}
MP = MarketPosition;
End
整体看着也很一般,但能从中学到的就是全局变量可以灵活使用,当然,这只是个人观点。