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








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.


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