期货软件TB系统源代码解读系列70-市场强弱和动量的通道系统

策略说明:

本策略是基于市场强弱指标和动量的通道突破系统 

系统要素:

1. 根据N根K线的收盘价相对前一根K线的涨跌计算出市场强弱指标

2. 最近9根K线的动量变化趋势

3. 最近N根K线的高低点形成的通道

入场条件:

1. 市场强弱指标为多头,且市场动量由空转多时,突破通道高点做多

2. 市场强弱指标为空头,且市场动量由多转空时,突破通道低点做空

出场条件: 

1. 开多以开仓BAR的最近N根BAR的低点作为止损价,反过来的开空以开仓BAR的最近N根BAR的高点作为止损价

2. 盈利超过止损额的一定倍数止盈

3. 出现反向信号止损

做多的代码及解读如下:

Params

Numeric Length(5); // 声明数值参数Length,初值5,即强弱指标和通道计算的周期值。//

Numeric Stop_Len(5); // 声明数值参数Stop_Len,初值5,即止损通道的周期值。//

Numeric ProfitFactor(3); // 声明数值参数ProfitFactor,初值3,即止盈相对止损的倍数。//

Numeric EntryStrength(95); // 声明数值参数EntryStrength,初值95,即强弱指标的进场值。//

Vars

NumericSeries CloseChange; //声明数值序列变量CloseChange,即 收盘价变动值。//

Numeric i; //声明数值变量 i ,即 循环控制变量。//

Numeric UpCloses; //声明数值变量UpCloses, 收盘价上涨累计值。//

Numeric DnCloses; // 声明数值变量DnCloses,收盘价下跌累计值。//

Numeric SumChange; // 声明数值变量SumChange, 收盘价变动累计值。//

NumericSeries MarketStrength; // 声明数值序列变量MarketStrength, 市场强弱指标。//

NumericSeries Momentum1; //  声明数值序列变量Momentum1,即当前Bar相对前4根BAR的动量。//

NumericSeries Momentum2; //  声明数值序列变量Momentum2,即前4根BAR相对前8根BAR的动量。//

NumericSeries HH; //声明数值序列变量HH, N周期高点 。//

NumericSeries LL1; // 声明数值序列变量LL1,N周期低点。//

NumericSeries LL2; // 声明数值序列变量LL2,N周期低点。//

NumericSeries StopLoss; // 声明数值序列变量StopLoss,止损位。//

NumericSeries ProfitTarget; // 声明数值序列变量ProfitTarget,止盈位。//

Begin

If(!CallAuctionFilter()) Return;// 集合竞价和小节休息过滤。//

// 计算市场强弱指标。//

CloseChange = Close - Close[1];//变量CloseChange = 当前收盘价Close - 前一收盘价Close[1]。//

UpCloses = 0; //初值赋0.//

DnCloses = 0;//初值赋0.//

For i = 0 To Length-1//循环从0到4.//

{

If(CloseChange[i] > 0) //假如变量CloseChange[i]大于0.//

UpCloses = UpCloses + CloseChange[i];// 收盘价上涨计入涨幅累计。//

Else //否则假如变量CloseChange[i]小于0 ,则计入跌幅累计。//

DnCloses = DnCloses + CloseChange[i];//计入跌幅累计。//

}

// 计算周期内涨跌。//

SumChange = Summation(CloseChange,Length);//计算5周期内变量CloseChange总值,赋值给变量SumChange值。//

// 周期内上涨,计算上涨强度,0-100之间。//

If(SumChange >= 0)//假如变量SumChange>=0。//

{

MarketStrength = SumChange/UpCloses*100;//市场强弱指标的计算,代入上面求得的数值即可。//

}

// 周期内下跌,计算下跌强度,0-100之间。//

Else//假如变量SumChange<0。//

{

MarketStrength = SumChange/Abs(DnCloses)*100;//市场强弱指标的计算.//

}

// 计算动量.//

Momentum1 = Close - Close[4];//变量Momentum1 = 当前收盘价 - 前4数位收盘价。//

Momentum2 = Close[4] - Close[8]; //变量Momentum2 = 前4数位收盘价 - 前8数位收盘价。//

// 计算周期高低点,开仓突破用。//

HH = Highest(High,Length);//计算5周期内最高价。//

LL1 = Lowest(Low,Length);//计算5周期内最低价。//

LL2 = Lowest(Low,Stop_Len); // 计算周期低点,开仓后止损用。//

// 开仓。//

If(MarketPosition == 0 And MarketStrength[1] >= EntryStrength And Momentum1[1] >= 0 And Momentum2[1] < 0 And High >= HH[1] And Vol > 0)//假如当前没有持仓,且前一强弱指标大于等于95,且前一变量Momentum1大于等于0,前一变量Momentum2小于0,且当前最高价大于前一最高价,成交量大于0.//

{

Buy(0, Max(Open,HH[1]));//开多单,值为开盘价与前一最高价的比较,取较大值。//

StopLoss = LL2;// 记录开仓BAR的周期低点作为止损位。//

// 根据止损位计算止盈位。//

ProfitTarget = EntryPrice + (EntryPrice - StopLoss) * ProfitFactor;//代入相应数值了,函数EntryPrice即开仓价意思。//

Commentary("ProfitTarget="+text(ProfitTarget));//在超级图表上注释 止盈数值。//

}

// 平仓。//

If(MarketPosition == 1 And BarsSinceEntry > 0 And Vol > 0)//假如当前持有多单,且建仓数位大于0,成交量大于0。//

{

If(High >= ProfitTarget)//假如当前最高价大于等于止盈位。//

{

Sell(0,Max(Open,ProfitTarget));//平仓,价格为开盘价与止盈位的比较,取较大值。//

Commentary("止盈");//注释止盈。//

}

Else If(Low <= StopLoss)//假如当前最低价Low 小于等于止损位。//

{

Sell(0,Min(Open,StopLoss));//平仓,价格为开盘价与止损价的比较,取较小值。//

Commentary("止损");//注释止损。//

}

Else If(MarketStrength[1] <= -1*EntryStrength And Momentum1[1] < 0 And Momentum2[1] >= 0 And Low <= LL1[1])//假如前一强弱指标小于等于-95,且前一变量Momentum1小于0,且前一变量Momentum2大于等于0,且当前最低价小于等于前一最低价。//

{

Sell(0,Min(Open,LL1[1]));//平仓,价格为开盘价与前一最低价的比较,取较小值。//

Commentary("反向出场");//注释为反向出场。//

}

}

End

做空代码及结果如下:

Params

Numeric Length(5);

Numeric Stop_Len(5);

Numeric ProfitFactor(3);

Numeric EntryStrength(95);

Vars

NumericSeries CloseChange;

Numeric i;

Numeric UpCloses;

Numeric DnCloses;

Numeric SumChange;

NumericSeries MarketStrength;

NumericSeries Momentum1;

NumericSeries Momentum2;

NumericSeries HH1;  

NumericSeries HH2;  

NumericSeries LL;

NumericSeries StopLoss;

NumericSeries ProfitTarget;

Begin

If(!CallAuctionFilter()) Return;

CloseChange = Close - Close[1];

UpCloses = 0;

DnCloses = 0;

For i = 0 To Length-1

{

If(CloseChange[i] > 0) 

UpCloses = UpCloses + CloseChange[i];

Else

DnCloses = DnCloses + CloseChange[i];

}

SumChange = Summation(CloseChange,Length);

If(SumChange >= 0)

{

MarketStrength = SumChange/UpCloses*100;

}

Else

{

MarketStrength = SumChange/Abs(DnCloses)*100;

}

Momentum1 = Close - Close[4];

Momentum2 = Close[4] - Close[8];

HH1 = Highest(High,Length);

LL = Lowest(Low,Length);

HH2 = Highest(High,Stop_Len);

If(MarketPosition == 0 And MarketStrength[1] <= -1*EntryStrength And Momentum1[1] <= 0 And Momentum2[1] > 0 And Low <= LL[1] And Vol > 0)

{

SellShort(0, Min(Open,LL[1]));

StopLoss = HH2;

ProfitTarget = EntryPrice - (StopLoss - EntryPrice) * ProfitFactor;

Commentary("ProfitTarget="+text(ProfitTarget));

}

If(MarketPosition == -1 And BarsSinceEntry > 0 And Vol > 0)

{

If(Low <= ProfitTarget)

{

BuyToCover(0,Min(Open,ProfitTarget));

Commentary("止盈");

}

Else If(High >= StopLoss)

{

BuyToCover(0,Max(Open,StopLoss));

Commentary("止损");

}

Else If(MarketStrength[1] >= EntryStrength And Momentum1[1] > 0 And Momentum2[1] <= 0 And High >= HH1[1])

{

BuyToCover(0,Max(Open,HH1[1]));

Commentary("反向出场");

}

}

End

你可能感兴趣的:(期货软件TB系统源代码解读系列70-市场强弱和动量的通道系统)