Volatility Index

The Volatility Index system is a trend-following system where the position is reversed at every stop. This overlay works as described in the book New Concepts in Technical Trading Systems by J Welles Wilder Jr. SAR (Stop and Reverse) points are calculated as follows: if (isLong) SAR = SIC – ATR(period)*C else SAR = SIC + ATR(period)*C. The user may change the period length and constant value. This indicator’s definition is further expressed in the raw code given in the calculation below.

Volatility Index

How To Trade Using Volatility Index

Volatility Index The Volatility Index may be used for an exit strategy. If a close is less than or equal to the SAR a sell to exit (exit long) signal will be generated. Conversely, if the close is more than or equal to the SAR a buy to cover (exit short) signal will be given.

How To Access in MotiveWave

Go to the top menu, choose Study>Welles Wilder>Volatility Index

or go to the top menu, choose Add Study, start typing in this study name until you see it appear in the list, click on the study name, click OK.

Important Disclaimer: The information provided on this page is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security. Please see our Risk Disclosure and Performance Disclaimer Statement.

Calculation

//period = user defined, default is 7
//constant = user defined, default is 3
//LT = less than, LOE = less or equal
//MT = more than, MOE = more or equal

  protected void calculate(int index, DataContext ctx)
  
    int period = getSettings().getInteger(Inputs.PERIOD, 7);
    if (index LT period) return;
    DataSeries series = ctx.getDataSeries();
    // The VX is only relevant for completed bars 
    if (!series.isBarComplete(index)) return;
    
    Double SAR = series.getDouble(index-1, Values.SAR); // Stop And Reverse 
    Double ATR = series.getDouble(index-1, Values.ATR); // Average True Range 
    double C = getSettings().getDouble(CONSTANT, 3.0);
    Double SIC = series.getDouble(index-1, Values.SIC); // Significant Close 
    Boolean isLong = series.getBoolean(index-1, Values.LONG);
    Instrument instr = ctx.getInstrument();
    boolean latest = index == series.size()-1;
    
    if (SAR == null) 
      // first entry point, calculate the first SAR 
      isLong = series.getClose(index) MT series.getClose(index-1);
      ATR = series.atr(period);
      SIC = (double)series.getClose(index-1);
      if (isLong) SAR = (double)instr.round(SIC-ATR*C);
      else SAR = (double)instr.round(SIC+ATR*C);
    end
    else 
      ATR = ((period-1)*ATR + series.getTrueRange(index))/period;
    end
    double ARC = ATR*C;
    double close = series.getClose(index);
    if (isLong) 
      if (close LOE SAR) 
        // Stop and Reverse 
        isLong = false;
        series.setBoolean(index, Signals.SAR_LONG, true);
        if (!latest) 
          ctx.signal(index, Signals.SAR_LONG, get("SIGNAL_SAR_LONG", instr.format(close), instr.format(SAR)), close);
        end
        SIC = close;
        SAR = instr.round(SIC + ARC);
      end
      else 
        SIC = Util.max(SIC, close);
        SAR = instr.round(SIC - ARC);
      end
    end
    else 
      if (close MOE SAR) 
        // Stop and Reverse 
        isLong = true;
        series.setBoolean(index, Signals.SAR_SHORT, true);
        if (!latest) {
          ctx.signal(index, Signals.SAR_SHORT, get("SIGNAL_SAR_SHORT", instr.format(close), instr.format(SAR)), close);
        end
        SIC = close;
        SAR = instr.round(SIC - ARC);
      end
      else 
        SIC = Util.min(SIC, close);
        SAR = instr.round(SIC + ARC);
      end
    end
    series.setDouble(index, Values.SAR, SAR);
    series.setDouble(index, Values.SIC, SIC);
    series.setDouble(index, Values.ATR, ATR);
    series.setBoolean(index,  Values.LONG, isLong);
    series.setComplete(index);
  end
end