Back

The right way to calculate volitality

I know this is sort of basic and should be well addressed, but is there a well defined method to calculate volatility?
This is how I currently do it:

var dailyReturn = ROCP(symbol, 1, Resolution.Daily);
var volitality = STD(symbol, 84).Of(dailyReturn);

I first define a variable called daily return, and calculated 84 days (trading days in 4 months) of standard deviation of it. However it is far from what I manually calculated from the prices. I know the numbers may be not exactly the name (Annualized, not annualized etc.) but the shape of the data curve is totally different. Can anyone confirm if this is the right way to calculate vol in QC? or if there is a better way to do that?

Thank you!
Update Backtest








0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


There is no one way to calculate volatility from what I've seen. We used the deviation in daily performance over 252 days to calculate the vol for sharpe ratio formulas. If you have a specific case you'd like help with please share an algorithm or refer us to a project-id and we can help.
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hey @Travis,

You'll need to give us some more details as to what method you're using to compute it manually. The code as you described will compute the standard deviation of the previous 84 days percentage change. Maybe you should be using log returns instead of ROCP?

public class LogReturn : WindowIndicator
{
public LogReturn(string name, int period)
: base(name, period)
{
}

public LogReturn(int period)
: base("LOGR" + period, period)
{
}

protected override decimal ComputeNextValue(IReadOnlyWindow window, IndicatorDataPoint input)
{
decimal valuef = input;

decimal value0 = !window.IsReady
? window[window.Count - 1]
: window.MostRecentlyRemoved;

return (decimal) Math.Log((double) (valuef/value0));
}
}
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi @MichaelH,

Thank you very much for providing this snippet, would you pointing out a way if I want to apply this in the online version of QC? I am sure it is easy to just add this in the indicator class in Lean project on my local machine, but it would be great to know how to extend the capability online.

Thank you
Travis
0

Hey @Travis,

I've attached an example of creating your own indicator (LogReturn) and registering it for automatic updates. I've also included some code to plot the value. The curve here seems more typical of a volatility curve.

I hope this helps!
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Here's a comparison of the two techniques. They seem to have similar shape in my tests.

Let me know what you guys think.
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hey @Travis, one more thing...

It looks like you've named your indicators all the same thing! You pass in 'symbol' as the name of the indicator. I'm not sure how you're generating the plots, but some of the plot helper functions use the name of the indicator for the plot, so if there are multiple indicators with the same name you may end up with very unexpected results. Maybe this is why my recent share above doesn't suffer from the jitter you described.

In the future it's easiest for us if you share an algorithm which demonstrates the problem, it will greatly improve our ability to help you. Thanks!
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi @MichaelH , Thank you for your replies and suggestions!

However, I still have questions about your code, I see your code provided above generate nothing. I am a little confused, clearly you can see the shapes on your machine, but I couldn't see any plot, I just need to compare the plot you generated and compare with the manually calculated vol shape in my excel to confirm

Thanks for pointing out the same "symbol" name, actually I have a ticker list, and I just have a loop go through them and variables generated by "symbol" will be passed to the tickers in the list individually.

Greatly appreciate your help! Sorry didn't reply promptly, was busy at work, but I will work on this this weekend.

Travis
0

@Travis, did you clone and run the backtest to look at the plots? The chart shown in this thread is the equity chart, which is flat because I didn't place any trades.

Clone the algorithm and look at the plots. There's a volatility plot and a returns plot which computes the vol using ROCP and LogReturns indicator I've previously provided.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


There are various ways to measure volatility, it just matters if you're trying to estimate current volatility or future volatiltiy, as well as realized or implied volatility. Let's look at the indicator route first. 

For current realized volatility, you could certainly use STD (which is essentially also what Bollinger Bands uses), Keltner channels which provide volatilty in absolute pricing with a moving average, or you can even go the statistical arbitrage route if you have asset cointegration, measured by the divergeance between one or more other assets (this is how Renaissance fund is rumored to do it). ATR is also another one if you want just the absolute pricing.

For predicting future realized volatility, you can do that surprisingly accurately using the GARCH method (link to paper is below), but as mentioned in many talks on YouTube, realized volatility is not what ultimately determines asset prices, implied volatility does, and there is no known accurate way to predict future implied volatility. If you find it though, well, you'd be rich :-) Sidenote: this is why options traders are always talking about implied volatility, because implied volatility is a derivative (calculus) of asset price: the underlying asset price is baked into it.

The good news is that the GARCH method has been shown to be a better predictor of future realized volatility than even the VIX, so I'd go that route if you really want to get your hands dirty.

hec.unil.ch/agoyal/docs/Garch.pdf

 

0

Hi John,

Thank you for providing a research paper for users to learn more about GARCH models. I noticed the link to the paper returns a 404 error though. Here's a working link for those interested in learning more: www.hec.unil.ch/agoyal/docs/Garch.pdf

Best,
Derek Melchin

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed