时间:2020-04-14 | 栏目:TB交易开拓者策略 | 点击:次
我看了很多TB里写的程序化系统,很多止损止盈都用ATR指标来写,这个效果也许很好,可我总用不惯,所以还是建议各位用自己的习惯来止盈止损好点。先来看策略吧,如下:
策略说明:
用特定时间周期内的最高价位和最低价位计算交易范围,然后计算真实的波动范围和周期内的ATR对比,如果
当前k线的波动范围比n*交易范围的值大,并且真实波动范围比ATR大,这就满足了前两个系统挂单的条件
入场条件:
1. 7周期区间“空隙”之和 >7周期区间高度的2倍
当前的k线比交易范围的最高值大, 而且如果当前k线的中间价格高于之前一根k线的最高值
做多
2. 7周期区间“空隙”之和 >7周期区间高度的2倍
当前的k线比交易范围的最低值小, 而且如果当前k线的中间价格低于之前一根k线的最低值
做空
出场条件:
1.初始止损
2.跟踪止损(盈利峰值价回落ATR的一定倍数)
3.收盘价创7周期低点,且K线中点低于前K线最低价多头出场
做多程序化系统代码解读如下:
Params
Numeric RangeLen(7);//声明数值参数RangeLen,初值7.//
Numeric RngPcnt(200);//声明数值参数RngPcnt,初值200.//
Numeric ATRs(8);//声明数值参数ATRs,初值8.//
Numeric ATRLen(2);//声明数值参数ATRLen,初值2.//
Vars
NumericSeries RangeH(0); //声明数值序列变量RangeH,初值0,即7周期高点。//
NumericSeries RangeL(0); //声明数值序列变量RangeL,初值0,即7周期低点。//
NumericSeries TRange(0); //声明数值序列变量TRange,初值0,即7周期区间。//
NumericSeries NoTrades(0); //声明数值序列变量NoTrades,初值0,即记录7周期高低点分别与7周期内各K线最高最低值的距离之和。//
NumericSeries LongRisk(0); //声明数值序列变量LongRisk,初值0,即初始止损价。//
NumericSeries LongHigh(0); //声明数值序列变量LongHigh,初值0,即跟踪止盈价。//
NumericSeries ATR; //声明数值序列变量ATR,即2周期ATR均值。//
NumericSeries ATRMA; //声明数值序列变量ATRMA,即7周期ATR均值。//
Numeric value1;//声明数值变量value1.//
BoolSeries Condition1;//声明布尔型序列变量Condition1.//
BoolSeries Condition2;//声明布尔型序列变量Condition2.//
BoolSeries Condition3;//声明布尔型序列变量Condition3.//
BoolSeries Condition4;//声明布尔型序列变量Condition4.//
Begin
If(!CallAuctionFilter()) Return;// 集合竞价和小节休息过滤。//
//初始设置。//
RangeH = Highest(High[1], RangeLen); //把前一最高价与参数7代入函数Highest求值,即可求得变量RangeH值。//
RangeL = Lowest(Low[1], RangeLen); //把前一最低价与参数7代入函数Lowest求值,即可求得变量RangeL值。//
TRange = RangeH - RangeL; //把求得的值代入公式,即可求得变量TRange值了。//
ATR = AvgTrueRange(ATRLen); //指标ATR求值公式了,具体可参考之前的抛物线解读。//
NoTrades = 0;// 变量NoTrades初值0了。//
ATRMA=AvgTrueRange(RangeLen); //同上的,可参考之前的抛物线解读。//
For value1 = 1 To RangeLen //循环语句了,即1-7循环。//
{
If (High[value1] <= RangeH ) //先看第一个值,假如前高价High[1]<=RangeH了。//
NoTrades = NoTrades + (RangeH - High[value1]); //代入第一个值NoTrades = 0 + (RangeH - High[1]),循环连续算7周期高点与7周期内各K线最高值的距离之和了。//
If (Low[value1] >= RangeL )//还是第一个值,假如前低价Low[1] >=RangeL。//
NoTrades = NoTrades + (Low[value1] - RangeL); //代入第一个值NoTrades = 0 + (Low[1] - RangeL),同样循环连续算7周期低点与7周期内各K线最低值的距离之和。//
}
Condition1 = NoTrades>= TRange*(RngPcnt*0.01); //7周期区间“空隙”之和 >7周期区间高度的2倍。//
Condition2 = TrueRange >ATRMA[1] ; //函数TrueRange即当根K线ATR>前根7周期均值。//
Condition3 = Close > RangeH And (High+Low)*0.5 >High[1]; //收盘价创7周期高点,且K线中点高于前K线最高价。//
Condition4 = Close < RangeL And (High+Low)*0.5 < Low[1]; //收盘价创7周期低点,且K线中点低于前K线最低价。//
//多头入场。//
If (Condition1[1] And Condition2[1]) //假如前一变量Condition1[1]和前一变量Condition2[1]条件成立的。//
{
If (Condition3[1] And MarketPosition==0 And Vol > 0)//假如Condition3[1]条件成立,且当前没有持仓,且成交量大于0.//
{
Buy(0,Open);//以开盘价开仓买入。//
LongRisk = RangeL; //记录初始止损价。//
LongHigh = High; //记录跟踪止盈价。//
}
}
//更新盈利峰值价。//
If(MarketPosition == 1 And BarsSinceEntry > 0) //假如当前持有多单,且建仓数位大于0.//
LongHigh = Max(LongHigh,High);//对比取大值了。//
//多头出场。//
If(MarketPosition == 1 And BarsSinceEntry > 0 And Vol > 0)//假如持有多单,且建仓数位大于0,且成交量大于0.//
{
If(Condition4[1])//前一变量Condition4[1]条件成立,即收盘价创7周期低点,且K线中点低于前K线最低价多头出场。//
{
Sell(0,Open);//以开盘价平仓。//
}
If(Low<=LongRisk) //跌破初始止损价多头出场了。//
{
Sell(0,Min(Open,LongRisk));//平仓。//
}
If(Low<=LongHigh[1]-(ATRs * ATR[1])) //盈利峰值价回落ATR一定倍数多头出场。//
{
Sell(0,Min(Open,LongHigh[1]-(ATRs*ATR[1])));//平仓。//
}
}
End
做空程序化系统代码及结果如下:
Params
Numeric RangeLen(7);
Numeric RngPcnt(200);
Numeric ATRs(8);
Numeric ATRLen(2);
Vars
NumericSeries RangeH(0);
NumericSeries RangeL(0);
NumericSeries TRange(0);
NumericSeries NoTrades(0);
NumericSeries ShortRisk(0);
NumericSeries ShortLow(0);
NumericSeries ATR;
NumericSeries ATRMA;
Numeric value1;
BoolSeries Condition1;
BoolSeries Condition2;
BoolSeries Condition3;
BoolSeries Condition4;
Begin
If(!CallAuctionFilter()) Return;
RangeH = Highest(High[1], RangeLen);
RangeL = Lowest(Low[1], RangeLen);
TRange = RangeH - RangeL;
ATR = AvgTrueRange(ATRLen);
NoTrades = 0;
ATRMA=AvgTrueRange(RangeLen);
For value1 = 1 To RangeLen
{
If (High[value1] <= RangeH )
NoTrades = NoTrades + (RangeH - High[value1]);
If (Low[value1] >= RangeL )
NoTrades = NoTrades + (Low[value1] - RangeL);
}
Condition1 = NoTrades>= TRange*(RngPcnt*0.01);
Condition2 = TrueRange >ATRMA[1] ;
Condition3 = Close > RangeH And (High+Low)*0.5 >High[1];
Condition4 = Close < RangeL And (High+Low)*0.5 < Low[1];
If (Condition1[1] And Condition2[1] )
{
If (Condition4[1] And MarketPosition==0 And vol > 0)
{
SellShort(0,Open);
ShortRisk = RangeH;
ShortLow = Low;
}
}
If(MarketPosition == -1 And BarsSinceEntry > 0) ShortLow = Min(ShortLow,Low);
If(MarketPosition == -1 And BarsSinceEntry > 0 And Vol > 0)
{
If(Condition3[1])
{
BuyToCover(0,Open);
}
If(High>=ShortRisk)
{
BuyToCover(0,Max(Open,ShortRisk));
}
If(High>=ShortLow[1]+(ATRs*ATR[1]))
{
BuyToCover(0,Max(Open,ShortLow[1]+(ATRs*ATR[1])));
}
}
End
十年专业期货服务平台,全国最低手续费保证金开户,咨询微信:cclk88