期货软件TB系统源代码解读系列69-K线加权均值的支撑阻力线

策略说明:

本策略是基于K线加权均值的支撑阻力线突破系统 

系统要素:

1. K线的加权均值 = (最高价+最低价+2*收盘价)/4

2. 支撑线 = K线加权均值 - ( 最高价 - K线加权均值)

3. 阻力线 = K线加权均值 + ( K线加权均值 - 最低价)

入场条件:

1. 当价格向上突破阻力线做多

2. 当价格向下突破支撑线做空

出场条件: 

1. 趋势反转即反向突破时出场

2. 基于ATR的一定倍数的止盈

做多代码及解读如下:

Params

Numeric ATRs(3); // 声明数值参数ATRs,初值3,几倍ATR止盈。//

Numeric ATRLength(10); // 声明数值参数ATRLength,初值10,ATR周期。//

Vars

NumericSeries WAvgPrice; // 声明数值序列变量WAvgPrice,即K线加权均值。//

NumericSeries Resistance; //声明数值序列变量Resistance,即 阻力线。//

NumericSeries Support; //声明数值序列变量Support,即 支撑线。//

Numeric ATRVal; // 声明数值变量ATR(平均真实波幅)。//

NumericSeries myExitPrice; // 声明数值序列变量myExitPrice,即开仓BAR根据当时的ATR计算出的止盈价。//

Begin

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

// 计算当前K线的加权均值、阻力线和支撑线。//

WAvgPrice = (High + Low + (Close * 2)) / 4;  //计算当前k线的加权均值。//

Resistance = (WAvgPrice * 2) - Low; // 计算阻力线。//

Support = (WAvgPrice * 2) - High; //计算支撑线。//

PlotNumeric("Resistance",Resistance[1]);//画阻力线。//

PlotNumeric("Support",Support[1]);//画支撑线。//

  ATRVal = AvgTrueRange(ATRLength); // 计算ATR。//

If(MarketPosition == 0 And High >= Resistance[1] + MinMove * PriceScale And Vol > 0) // 假如当前没有持仓,且当前最高价大于等于前一阻力线加一跳的价格,且成交量大于0.//

{

Buy(0, Max(Open,Resistance[1] + MinMove * PriceScale)); //开仓买入。//

}

// 开仓时根据开仓BAR的ATR计算止盈价。//

If(MarketPosition == 1 And BarsSinceEntry == 0)//当前持有多单,且建仓数位等于0.//

{

myExitPrice = EntryPrice + ATRVal * ATRs;//出场价 = 进场价 + 3 * ATRVal值。//

}

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

{

// 止盈出场。//

If(High >= myExitPrice)//假如当前最高价大于等于出场价。//

{

Sell(0, Max(Open,myExitPrice));//平仓。//

Commentary("止盈出场");//在超级图表上注释“止盈出场”。//

}

// 反向突破止损出场.//

Else If(Low <= Support[1] - MinMove * PriceScale)//假如当前最低价小于等于前一支撑线减去一跳的价格。//

{

Sell(0, Min(Open,Support[1] - MinMove * PriceScale));//平仓。//

Commentary("反转出场");//在超级图表上注释“反转出场”。//

}

}

End

做空代码及结果如下:

Params

Numeric ATRs(3);

Numeric ATRLength(10);

Vars

NumericSeries WAvgPrice;

NumericSeries Resistance;

NumericSeries Support;

Numeric ATRVal;

NumericSeries myExitPrice;

Begin

If(!CallAuctionFilter()) Return;

WAvgPrice = (High + Low + (Close * 2)) / 4;

Resistance = (WAvgPrice * 2) - Low;

Support = (WAvgPrice * 2) - High;

PlotNumeric("Resistance",Resistance[1]);

PlotNumeric("Support",Support[1]);

  ATRVal = AvgTrueRange(ATRLength);

If(MarketPosition == 0 And Low <= Support[1] - MinMove * PriceScale And Vol > 0)

{

SellShort(0, Min(Open,Support[1] - MinMove * PriceScale));

}

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

{

myExitPrice = EntryPrice - ATRVal * ATRs;

}

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

{

If(Low <= myExitPrice)

{

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

Commentary("止盈出场");

}

Else If(High >= Resistance[1] + MinMove * PriceScale)

{

BuyToCover(0, Max(Open,Resistance[1] + MinMove * PriceScale));

Commentary("反转出场");

}

}

End

你可能感兴趣的:(期货软件TB系统源代码解读系列69-K线加权均值的支撑阻力线)