策略说明:
模拟交易产生一次亏损后才启动真实下单交易。
系统要素:
1、两条指数平均线
2、RSI指标
3、唐奇安通道
入场条件:
1、模拟交易产生一次亏损、短期均线在长期均线之上、RSI低于超买值、创新高,则开多单
2、模拟交易产生一次亏损、短期均线在长期均线之下、RSI高于超卖值、创新低,则开空单
出场条件:
1、持有多单时小于唐奇安通道下轨,平多单
2、持有空单时大于唐奇安通道上轨,平空单
做多代码及解读如下:
Params
Numeric FastLength(9); // 声明数值参数FastLength,初值9,即短期指数平均线参数。//
Numeric SlowLength(19); // 声明数值参数SlowLength,初值19,即长期指数平均线参数。//
Numeric Length(9); // 声明数值参数Length,初值9,即RSI参数。//
Numeric OverSold(30); // 声明数值参数OverSold,初值30,即超卖。//
Numeric OverBought(70); // 声明数值参数OverBought,初值70,即超买。//
Numeric Lots(0); // 声明数值参数Lots,初值0,即交易手数设置。//
Vars
NumericSeries AvgValue1; // 声明数值序列变量AvgValue1,即短期指数平均线。//
NumericSeries AvgValue2; // 声明数值序列变量AvgValue2,即长期指数平均线。//
NumericSeries NetChgAvg(0);//声明数值序列变量NetChgAvg,初值0。//
NumericSeries TotChgAvg(0);//声明数值序列变量TotChgAvg,初值0.//
Numeric SF(0);//声明数值变量SF,初值0.//
Numeric Change(0); //声明数值变量Change,初值0.//
Numeric ChgRatio(0);//声明数值变量ChgRatio,初值0.//
NumericSeries RSIValue; // 声明数值序列变量RSIValue,即RSI指标。//
NumericSeries ExitHiBand(0); // 声明数值序列变量ExitHiBand,初值0,唐奇安通道上轨。//
NumericSeries ExitLoBand(0); //声明数值序列变量ExitLoBand,初值0, 唐奇安通道下轨。//
NumericSeries myEntryPrice(0); //声明数值序列变量myEntryPrice,初值0, 进场价格。//
NumericSeries myExitPrice(0); // 声明数值序列变量myExitPrice,初值0,出场价格。//
NumericSeries myProfit(0); // 声明数值序列变量myProfit,初值0,即利润。//
NumericSeries myPosition(0); // 声明数值序列变量myPosition,初值0,即多空标志。//
Begin
If(!CallAuctionFilter()) Return;// 集合竞价和小节休息过滤。//
AvgValue1 = Xaverage(Close,FastLength);// 计算短期指数平均线,即把收盘价与周期9返回函数Xaverage求值。//
AvgValue2 = Xaverage(Close,SlowLength);//同理,计算长期指数平均线参数。//
// 计算RSI。//
If(CurrentBar <= Length - 1)//假如当前索引k线数位值小于等于周期8(9-1)的。//
{
NetChgAvg = (Close - Close[Length])/Length;//代入相应数值计算,即得NetChgAvg = (close - close[9])/ 9的。//
TotChgAvg = Average(Abs(Close - Close[1]),Length);//先算绝对值函数Abs里的,再把绝对值与周期9返回均值函数Average求均值,最后赋值给变量TotChgAvg。//
}Else//就是k线数位值大于周期8的。//
{
SF = 1/Length;//代入相应数值,即SF= 1/9 了.//
Change = Close - Close[1];//同理,代入当期k线收盘价与前一k线收盘价即可。//
NetChgAvg = NetChgAvg[1] + SF*(Change - NetChgAvg[1]);//这里也是代入上边求得的相应数值即可。//
TotChgAvg = TotChgAvg[1] + SF*(Abs(Change) - TotChgAvg[1]); //同上解读。//
}
If(TotChgAvg <> 0)//假如变量TotChgAvg不等于0.//
{
ChgRatio = NetChgAvg/TotChgAvg;//则两变量相除了。//
}Else//等于0的。//
{
ChgRatio = 0;//变量ChgRatio=0了。//
}
RSIValue = 50*(ChgRatio + 1);//指标RSI的计算结果。//
ExitHiBand = Highest(High,20); // 唐奇安通道上轨。//
ExitLoBand = Lowest(Low,20);// 唐奇安通道下轨。//
If(myPosition == 1 And myPosition[1] == 1 and Low <= ExitLoBand[1])// 持有多单时,下破唐奇安通道下轨,平多单。//
{
myExitPrice = Min(Open,ExitLoBand[1]);//出场价的计算,开盘价与前一个唐奇安通道下轨的比较,取较小值。//
Sell(0,myExitPrice);//平仓。//
myProfit = myExitPrice - MyEntryPrice;//利润算法。//
myPosition = 0;//持仓多空标志myPosition = 0.//
}
If(myPosition == 0 And myPosition[1] == 0 And AvgValue1[1] > AvgValue2[1] And RSIValue[1] < OverBought and High >= High[1])// 模拟交易产生一次亏损、短期均线在长期均线之上、RSI低于超买值、创新高,则开多单。//
{
myEntryPrice = Max(Open,High[1]);//进场价计算,即开盘价与前一个最高价的比较,取较大值。//
myPosition = 1;//持仓多空标志myPosition = 1.//
If(myProfit < 0) Buy(Lots,myEntryPrice);//假如利润myProfit <0的,以进场价开仓。//
}
End
做空代码及结果如下:
Params
Numeric FastLength(9);
Numeric SlowLength(19);
Numeric Length(9);
Numeric OverSold(30);
Numeric OverBought(70);
Numeric Lots(0);
Vars
NumericSeries AvgValue1;
NumericSeries AvgValue2;
NumericSeries NetChgAvg(0);
NumericSeries TotChgAvg(0);
Numeric SF(0);
Numeric Change(0);
Numeric ChgRatio(0);
NumericSeries RSIValue;
NumericSeries ExitHiBand(0);
NumericSeries ExitLoBand(0);
NumericSeries myEntryPrice(0);
NumericSeries myExitPrice(0);
NumericSeries myProfit(0);
NumericSeries myPosition(0);
Begin
If(!CallAuctionFilter()) Return;
AvgValue1 = Xaverage(Close,FastLength);
AvgValue2 = Xaverage(Close,SlowLength);
If(CurrentBar <= Length - 1)
{
NetChgAvg = (Close - Close[Length])/Length;
TotChgAvg = Average(Abs(Close - Close[1]),Length);
}Else
{
SF = 1/Length;
Change = Close - Close[1];
NetChgAvg = NetChgAvg[1] + SF*(Change - NetChgAvg[1]);
TotChgAvg = TotChgAvg[1] + SF*(Abs(Change) - TotChgAvg[1]);
}
If(TotChgAvg <> 0)
{
ChgRatio = NetChgAvg/TotChgAvg;
}Else
{
ChgRatio = 0;
}
RSIValue = 50*(ChgRatio + 1);
ExitHiBand = Highest(High,20);
ExitLoBand = Lowest(Low,20);
If(myPosition == -1 And myPosition[1] == -1 and High >= ExitHiBand[1])
{
myExitPrice = Max(Open,ExitHiBand[1]);
BuyToCover(0,myExitPrice);
myProfit = myEntryPrice - MyExitPrice;
myPosition = 0;
}
If(myPosition == 0 And myPosition[1] == 0 And AvgValue1[1] < AvgValue2[1] And RSIValue[1] > OverSold and Low <= Low[1])
{
myEntryPrice = Min(Open,Low[1]);
myPosition = -1;
If(myProfit < 0) SellShort(Lots,myEntryPrice);
}
End