Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

# Schwager Volatility Ratio Amibroker AFL code and Analysis

1 min read

What is Volatility ratio?

The Term Volatility ratio is coined by Jack D. Schwager to identify trading range and potential trend reversal. The values of the volatility ratio range from 0.01-1.00 and typically traders predicts the reversal points if the value shows above 0.5. However marketcalls recommend to look for reversal above 0.6.

Volatility ratio formula

Volatility Ratio (VR) = Today’s True Range/True Range over N number of days

To calculate the volatility ratio, the true range is calculated using the following formula:

Today’s True Range = MAX (today’s high, yesterday’s close) – MIN (today’s low, yesterday’s close)
and

True Range over N number of days= MAX (Day1 High, Day 2 High, …Day N High, Day 0 Close) minus MIN(Day1 Low, Day 2 Low, …Day N Low, Day 0 Close)

Volatility ratio – Amibroker AFL code

```_SECTION_BEGIN("Volatility Ratio");
//{Volatility Ratio - Schwager}
//Coded By Rajandran R  - www.marketcalls.in
//Date - 18 Feb 2012

n=Param("Periods",13,1,300,1);
VRT=Param("VRthreshold",0.6,0.5,1,0.1);
Dday =Param("Decision Day Based on Prev day returns",4,3,5,1);
zero=10^-10;

r1=H-L;
r2=abs(H-Ref(C,-1));
r3=abs(Ref(C,-1)-L);

TrueRange=Max(Max(r1,r2),r3);

TrueHighPds=Max(HHV(H,n),Ref(C,-n));
TrueLowPds=Min(LLV(L,n),Ref(C,-n));
TrueRangePds=TrueHighPds-TrueLowPds;

VolatilityRatio=TrueRange/Max(TrueRangePds,zero);

SetPositionSize(1,spsShares);

decisionSell=HHV(C,5)-C;
decisionBuy=C-LLV(C,5);

Buy =decisionBuy>0 AND Cross(VolatilityRatio,VRT);
Sell =0;
Short=decisionSell>0 AND Cross(VolatilityRatio,VRT);
Cover=0;

Plot(VolatilityRatio,"Volatility Ratio - Schwager",colorRed);

_SECTION_END();```

The above code has been derived from the following metastock code as shown below

```{Volatility Ratio - Schwager}

n:=Input("Periods",1,253,14);
zero:=Power(10,-10);

r1:=H-L;
r2:=Abs(H-Ref(C,-1));
r3:=Abs(Ref(C,-1)-L);

TrueRange:=Max(Max(r1,r2),r3);

TrueHighPds:=Max(HHV(H,n),Ref(C,-n));
TrueLowPds:=Min(LLV(L,n),Ref(C,-n));
TrueRangePds:=TrueHighPds-TrueLowPds;

VolatilityRatio:=TrueRange/Max(TrueRangePds,zero);

{plot VR}
VolatilityRatio;```

Builing a Trading System using Volatility Ratio

Now to understand the predictive nature of Volaitity Ratio in idenfitying price reversal we built a simple trading system

The concept is to Buy when Volatility ratio is greater than 0.6 and the past 5 days returns are negative and on the reverse short if the Volatility ratio is greater than 0.6 and the past 5 days returns are positive. And now exit after 10 days of holding the instrument.

We tested the trading system with our benchmark index Nifty on the Daily timeframe. Here is the results about the predictive nature of the indicator followed by snapshots of the signals when volatility ratio crosses 0.6 with period =13

ParametersResults
Tested SymbolNSENIFTY
Total Number of Trades52
Holding Period10 Days
Winning Trades32
Winning Ratio 61.54%
Points Earned2000 Nifty points
Testing Period1991-2012 (23 years)

Charts below the buy and sell indications for the past 5 years when the volatility ratio shoots above 0.6

Nifty Daily Chart – 2007

Nifty Daily Chart – 2008

Nifty Daily Chart – 2009

Nifty Daily Chart – 2010

Nifty Daily Chart – 2011

Nifty Daily Chart – 2012
No signals during the year 2012 and 2013

Hope the above article gives your more understanding about the nature of volatility ratio. Test the indicator and provide your feedback here. Your feedback is much are appreciated

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

## [Free Course] Mini Certification on Amibroker AFL Programming

Interested in getting to know the basics of Amibroker AFL Programming then here is a free live training course to access hands-on training on...

53 sec read

## Smart Button Trading Module – Trade Automation – Amibroker…

This Algomojo Button trading module provides 4 modes of the button to place orders with a single click directly from Amibroker using simple smart...

4 min read

## AlphaTrend – Amibroker AFL Code to Manage Sideways Markets

Very simple and interesting trend following strategy which could manage sideways markets to a greater extent while following the trend. Recently I got inspired...

3 min read

## 8 Replies to “Schwager Volatility Ratio Amibroker AFL code and Analysis”

1. alrby3ee says:

Amazing ..thanks Rajandran R

2. kumar says:

nice

3. Dick says:

I found this RSI most interesting but noted no Buy/Sell arrows so added (see below). They should work but don’t so any help will be appreciated.
Dick
Note: send correction to me @ areehoi@yahoo.com
Thanks

SECTION_BEGIN(“Volatility”);
//{Volatility Ratio – Schwager}
//Coded By Rajandran R – http://www.marketcalls.in
//Date – 18 Feb 2012
/*The concept is to Buy when Volatility ratio is greater than 0.6 and the past 5 days returns are negative and on the reverse short if the Volatility
ratio is greater than 0.6 and the past 5 days returns are positive. And now exit after 10 days of holding the instrument.*/

n=Param(“Periods”,13,1,300,1);
VRT=Param(“VRthreshold”,0.6,0.5,1,0.1);
Dday =Param(“Decision Day Based on Prev day returns”,4,3,5,1);
zero=10^-10;

r1=H-L;
r2=abs(H-Ref(C,-1));
r3=abs(Ref(C,-1)-L);

TrueRange=Max(Max(r1,r2),r3);

TrueHighPds=Max(HHV(H,n),Ref(C,-n));
TrueLowPds=Min(LLV(L,n),Ref(C,-n));
TrueRangePds=TrueHighPds-TrueLowPds;

VolatilityRatio=TrueRange/Max(TrueRangePds,zero);

SetPositionSize(1,spsShares);

decisionSell=HHV(C,5)-C;
decisionBuy=C-LLV(C,5);

Buy =decisionBuy>0 AND Cross(VolatilityRatio,VRT);
//Sell =0;
Sell =decisionSell>0 AND Cross(VolatilityRatio,VRT);
Cover=0;
//Plot( Buy, “Buy”, colorBlack, styleLine );

HLine = Param( “Upper Line” ,0.6, 60,95, 5);
LLine = Param( “Lower Line”,0.2, 5, 40, 5);
Plot( Lline, “0.6”, colorBrown,styleLine|styleDashed );
Plot( Hline, “0.2”, colorBrown,styleLine|styleDashed );

Plot(VolatilityRatio,”Volatility Ratio – Schwager”,colorRed,styleThick);
shape = Buy * shapeHollowUpArrow + Sell * shapeHollowDownArrow;
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorBrightGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeHollowUpArrow,shapeNone),colorBrightGreen,0,L,-15);

PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Sell,shapeHollowDownArrow,shapeNone),colorRed,0,H,-15);

_SECTION_END ();

4. srinivas murthy says:

thanks for ur valuable effort ,plse guide me how to copy into ami charts the above afl code

1. Dick Hoiermn says:

Raj,
I left response (above) but never received a reply.

Dick

5. keshav says:

its definitely going to generate a lot of short signals than the long ones.

1. Kannan says:

I tested this strategy using MetaStock. While this strategy performance is good post 2007, the period between 2000 to 2007 it actually lost money (almost all of it). How can the draw down could be reduced. Also wondering how Nifty data in Amibroker is available from 1991 when NSE started it from 1994.

6. Karuna says:

Hello Sir, I want to know if you have a candlestick AFL. I want to scan for candlestick patterns. Is there a possibility to get realtime alerts? For example if a morning star pattern has occurred in SBIN, then it must alert me that SBIN has a morning star pattern.

I want to congratulate you on all the new things that you bring on your website. Thanks for everything.

Karuna

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More