时间:2020-04-14 | 栏目:TB交易开拓者策略 | 点击:次
这个跟踪止盈,我在之前就写出来过,但没具体解读它,学过编程的朋友,可能看一眼就明白什么意思的。这次写呢,就是想告诉各位,这个模板可以随意复制调用的,只要把参数改成个人操作习惯就可以。
还是老样子,直接附上代码,解读一遍,再随便找个指标写个交易程序出来,后期要用的朋友,可依据自己的交易理念,改一下相应条件就行,很方便的。其实下边止盈的声明变量,可改成声明参数的,这样写,后期要做测试优化的时候,很不方便,所以把跟踪止盈改成声明参数更好点。
模板的规则如下:当盈利达到50跳之后启动第一级跟踪止盈,止盈的回撤值为30跳,当盈利达到80跳之后启动第二级的跟踪止盈,止盈的回撤值为20跳。当然你也可以将这些固定的设置修改为盈利百分比,或者是某个价格的百分比。
好了,了解了规则,来看真正的代码了,如下:
Vars
Numeric MinPoint; // 声明变量MinPoint,一个最小变动单位,也就是一跳。//
Numeric MyEntryPrice; // 声明变量MyEntryPrice,英文好的,看英文意思都知道的,开仓价格,本例是开仓均价,也可根据需要设置为某次入场的价格。//
Numeric TrailingStart1(50); // 声明变量TrailingStart1,初始值为50,这是止盈启动设置1。//
Numeric TrailingStart2(80); // 声明变量TrailingStart2,初始值为80,这是止盈启动设置2。//
Numeric TrailingStop1(30); // 声明变量TrailingStop1,初始值为30,这是真正跟踪止盈设置1。//
Numeric TrailingStop2(20); // 声明变量TrailingStop2,初始值为20,这是真正跟踪止盈设置2。//
Numeric StopLossSet(50); //声明变量StopLossSet,初始值为50,这是止损设置。//
Numeric MyExitPrice; // 声明变量MyExitPrice,平仓价格。//
NumericSeries HighestAfterEntry; //声明序列变量HighestAfterEntry,开仓后出现的最高价。//
NumericSeries LowestAfterEntry; //声明序列变量LowestAfterEntry,开仓后出现的最低价。//
Begin
...
If(BarsSinceentry == 0)//获得当前持仓的第一个建仓位置到当前位置的Bar计数,这里就是开仓的那根k线了。//
{
HighestAfterEntry = Close;//开仓后的最高价为收盘价。//
LowestAfterEntry = Close;//开仓后的最低价为收盘价。//
If(MarketPosition <> 0)//假如有持仓的情况下。//
{
HighestAfterEntry = Max(HighestAfterEntry,AvgEntryPrice); // 开仓的Bar(通常说的k线了),将开仓价和当时的收盘价的较大值保留到HighestAfterEntry。//
LowestAfterEntry = Min(LowestAfterEntry,AvgEntryPrice); // 开仓的Bar,将开仓价和当时的收盘价的较小值保留到LowestAfterEntry。//
}
}
else//这个对应着不是开仓k线之后的情况。//
{
HighestAfterEntry = Max(HighestAfterEntry,High); // 这个是逐个判断的,即开仓那条K线,跟下一根k线最高价对比,记录当前的最高点;再接着继续对比,直到符合止盈启动设置条件,这样好用于下一个Bar的跟踪止盈判断。//
LowestAfterEntry = Min(LowestAfterEntry,Low); // 简略说就是,记录下当前k线的最低点,用于下一个K线的跟踪止盈判断。//
}
Commentary("HighestAfterEntry="+Text(HighestAfterEntry));//在超级图表当前Bar添加一行注释信息,解读意思就是在K线图上显示最高价HighestAfterEntry等于多少的。//
Commentary("LowestAfterEntry="+Text(LowestAfterEntry));//解读同上,显示最低价。//
MinPoint = MinMove*PriceScale;//固定的最小跳动价公式。//
MyEntryPrice = AvgEntryPrice;//把开仓均价赋值给MyEntryPrice。//
If(MarketPosition==1) // 有多仓的情况下。//
{
If(HighestAfterEntry[1] >= MyEntryPrice + TrailingStart2*MinPoint) // 假如最高价大于等于开仓均价加上固定的止盈启动设置2乘以最小跳动价,这里先写了第二级跟踪止盈的条件表达式。//
{
If(Low <= HighestAfterEntry[1] - TrailingStop2*MinPoint)//再接着假如低价小于等于最高价减去真正止盈设置2系数乘以最小跳动价。//
{
MyExitPrice = HighestAfterEntry[1] - TrailingStop2*MinPoint;//平仓价呢等于最高价减去真正止盈设置2系数乘以最小跳动价。//
If(Open < MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替,这句我习惯性的经常去掉,很不严谨的。//
Sell(0,MyExitPrice);//以上面计算得的平仓价,平仓了。//
}
}
else if(HighestAfterEntry[1] >= MyEntryPrice + TrailingStart1*MinPoint)// 第一级跟踪止盈的条件表达式。解读跟上面没有什么区别,这里就是把设置2变成设置1而已。//
{
If(Low <= HighestAfterEntry[1] - TrailingStop1*MinPoint)//解读同上。//
{
MyExitPrice = HighestAfterEntry[1] - TrailingStop1*MinPoint;//平仓价,主要计算就是那个止盈系数设置1了,解读同上。//
If(Open < MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替。//
Sell(0,MyExitPrice);//依据算得的平仓价,平掉了。//
}
}
else if(Low <= MyEntryPrice - StopLossSet*MinPoint) //这里写上初始的固定止损处理,即为低价小于等于开仓均价减去固定止损系数乘以最小跳动价。//
{
MyExitPrice = MyEntryPrice - StopLossSet*MinPoint;//平仓价就等于开仓价减去止损系数乘以一跳。//
If(Open < MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替。//
Sell(0,MyExitPrice);//依据算得的平仓价,全平了。//
}
}else if(MarketPosition==-1) // 有空仓的情况。代码都差不多,但就是意思反过来了。//
{
If(LowestAfterEntry[1] <= MyEntryPrice - TrailingStart2*MinPoint) // 第二级跟踪止盈的条件表达式,即最低价小于等于开仓价减去止盈启动设置2乘以最小跳动价。//
{
If(High >= LowestAfterEntry[1] + TrailingStop2*MinPoint)//假如高价大于等于最低价加上真正止盈设置2乘以最小跳动价。//
{
MyExitPrice = LowestAfterEntry[1] + TrailingStop2*MinPoint;//平仓价等于最低价加上真正止盈设置2乘以最小跳动价了。//
If(Open > MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替。//
BuyToCover(0,MyExitPrice);//平仓。//
}
}
else if(LowestAfterEntry[1] <= MyEntryPrice - TrailingStart1*MinPoint)// 第一级跟踪止盈的条件表达式,基本就是把止盈设置2都改成设置了,解读同上了。//
{
If(High >= LowestAfterEntry[1] + TrailingStop1*MinPoint)//解读同上。//
{
MyExitPrice = LowestAfterEntry[1] + TrailingStop1*MinPoint;//计算平仓价了,直白解读。//
If(Open > MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替。//
BuyToCover(0,MyExitPrice);//平仓。//
}
}
else If(High >= MyEntryPrice + StopLossSet*MinPoint)//这里写上初始的止损处理,高价大于开仓均价加上固定止损系数乘以最小跳动价。//
{
MyExitPrice = MyEntryPrice + StopLossSet*MinPoint;//平仓价等于开仓均价加上止损系数乘以一跳。//
If(Open > MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替。//
BuyToCover(0,MyExitPrice);//平仓。//
}
}
...
End
以上就是模板代码的具体解读,其实还可以写第三级或第四级的跟踪止盈,就是把代码复制一下,对应改成设置3或4就可以。下面就是看一个实例了,还是简单的吧,直接用KD指标加一条200移动均线了。代码及结果如下:
Params
Numeric Length(14);
Numeric SlowLength(3);
Numeric SmoothLength(3);
Numeric DslowLength(200);
Vars
NumericSeries HighestValue;
NumericSeries LowestValue;
NumericSeries KValue;
Numeric SumHLValue;
Numeric SumCLValue;
NumericSeries DValue;
NumericSeries AvgValue3;
Numeric MinPoint;
Numeric MyEntryPrice;
Numeric TrailingStart1(50);
Numeric TrailingStart2(100);
Numeric TrailingStop1(30);
Numeric TrailingStop2(20);
Numeric StopLossSet(50);
Numeric MyExitPrice;
NumericSeries HighestAfterEntry;
NumericSeries LowestAfterEntry;
Begin
AvgValue3 = AverageFC(Close,DslowLength);
PlotNumeric("MA3",AvgValue3);
HighestValue = HighestFC(High, Length);
LowestValue = LowestFC(Low, Length);
SumHLValue = SummationFC(HighestValue-LowestValue,SlowLength);
SumCLValue = SummationFC(Close - LowestValue,SlowLength);
If(SumHLValue <> 0)
{
KValue = SumCLValue/SumHLValue*100;
}Else
{
KValue = 0;
}
DValue = AverageFC(KValue,SmoothLength);
If(!CallAuctionFilter()) Return;
If (MarketPosition <> 1 And KValue[1] > DValue[1] And Close[1] > AvgValue3)
{
Buy(1,Open);
}
If(MarketPosition == 1 And Close[1] < AvgValue3)
{
Sell(1,Open);
}
If(MarketPosition <> -1 And KValue[1] < DValue[1] And Close[1] < AvgValue3)
{
SellShort(1,Open);
}
If(MarketPosition == -1 And Close[1] > AvgValue3)
{
BuyToCover(1,Open);
}
If(BarsSinceentry == 0)
{
HighestAfterEntry = Close;
LowestAfterEntry = Close;
If(MarketPosition <> 0)
{
HighestAfterEntry = Max(HighestAfterEntry,AvgEntryPrice);
LowestAfterEntry = Min(LowestAfterEntry,AvgEntryPrice);
}
}else
{
HighestAfterEntry = Max(HighestAfterEntry,High);
LowestAfterEntry = Min(LowestAfterEntry,Low);
}
Commentary("HighestAfterEntry="+Text(HighestAfterEntry));
Commentary("LowestAfterEntry="+Text(LowestAfterEntry));
MinPoint = MinMove*PriceScale;
MyEntryPrice = AvgEntryPrice;
If(MarketPosition==1)
{
If(HighestAfterEntry[1] >= MyEntryPrice + TrailingStart2*MinPoint)
{
If(Low <= HighestAfterEntry[1] - TrailingStop2*MinPoint)
{
MyExitPrice = HighestAfterEntry[1] - TrailingStop2*MinPoint;
If(Open < MyExitPrice) MyExitPrice = Open;
Sell(0,MyExitPrice);
}
}else if(HighestAfterEntry[1] >= MyEntryPrice + TrailingStart1*MinPoint)
{
If(Low <= HighestAfterEntry[1] - TrailingStop1*MinPoint)
{
MyExitPrice = HighestAfterEntry[1] - TrailingStop1*MinPoint;
If(Open < MyExitPrice) MyExitPrice = Open;
Sell(0,MyExitPrice);
}
}else if(Low <= MyEntryPrice - StopLossSet*MinPoint)
{
MyExitPrice = MyEntryPrice - StopLossSet*MinPoint;
If(Open < MyExitPrice) MyExitPrice = Open;
Sell(0,MyExitPrice);
}
}else if(MarketPosition==-1)
{
If(LowestAfterEntry[1] <= MyEntryPrice - TrailingStart2*MinPoint)
{
If(High >= LowestAfterEntry[1] + TrailingStop2*MinPoint)
{
MyExitPrice = LowestAfterEntry[1] + TrailingStop2*MinPoint;
If(Open > MyExitPrice) MyExitPrice = Open;
BuyToCover(0,MyExitPrice);
}
}else if(LowestAfterEntry[1] <= MyEntryPrice - TrailingStart1*MinPoint)
{
If(High >= LowestAfterEntry[1] + TrailingStop1*MinPoint)
{
MyExitPrice = LowestAfterEntry[1] + TrailingStop1*MinPoint;
If(Open > MyExitPrice) MyExitPrice = Open;
BuyToCover(0,MyExitPrice);
}
}else If(High >= MyEntryPrice + StopLossSet*MinPoint)
{
MyExitPrice = MyEntryPrice + StopLossSet*MinPoint;
If(Open > MyExitPrice) MyExitPrice = Open;
BuyToCover(0,MyExitPrice);
}
}
End
十年专业期货服务平台,全国最低手续费保证金开户,咨询微信:cclk88