For each asset in my universe, I would like to calculate the STD of returns, over the last 100days, in order to make a comparison between assets.
I see the indicator STD seems to calculate the STD of Prices.
Is there an easy way to calculate the Standard Deviation of Returns (I believe we should be using Log Returns too, not simple returns). The implementation below does not work, but I believe we should be doing something like this?
Vladimir
Mark Reeve,
You can use numpy log1p on pct_change().
sigma = np.log1p(self.History([self.spy], 100, Resolution.Daily).close.pct_change()).std()
Jared Broad
# sma = SimpleMovingAverage("SPY", 14) # rsiAverage= IndicatorExtensions.Of(rsi, sma) roc = self.ROC("SPY", 30) std = StandardDeviation("SPY", 30) self.stdROC = IndicatorExtensions.Of(roc, std)
Vladimir's method works but I'd recommend doing it with Indicator Extensions and the "online-algorithms" built into the QC toolset which should be 10x faster.
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.
Vladimir
Jared Broad,
Mark Reeve requested STD of Log Returns not simple returns.
Vladimir
There are two ways to get STD of Log Returns:
Calculate STD using numpy log1p over pct_change()
sigma1 = np.log1p(self.History([self.spy], 100, Resolution.Daily).close.pct_change()).std()
Calculate STD over difference of log.prices
sigma2 = float(np.diff(np.log(self.History([self.spy], 100, Resolution.Daily).close)).std())
Jared Broad
Yes good point it should be like this but it doesn't exist yet. We'll add it to the extensions collection.
self.stdLogROC = IndicatorExtensions.Of( IndicatorExtensions.Log(roc), std)
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.
Vladimir
There isn't much difference if you even calculate the STD for pct_change.
sigma3 = self.History([self.spy], 100, Resolution.Daily).close.pct_change().std()
Mark Reeve
Thank you Vladimir and Jared for your time and your answers!
1) Vladimir's answer does indeed achieve the desired outcome and as he correctly points out there isn't a lot of difference between using Log or Simple returns so I would be happy with either. However, taking into account the efficiency mentioned by Jared with Indicator extensions I would love to know how to create this output using them!
2) Jared: I am not sure what your method is calculating? The desired output = the STD of the last 100days of daily ROC. Changing the parameters to ROC=1, STD=100 is not yielding the desired output. What we need is 100days of daily ROC as input to the STD and I'm not sure how to create this using Indicator Extensions? Any ideas?
Many Thanks to both!
Derek Melchin
Hi Mark,
To calculate the standard deviation of the trailing 100 daily returns, we can use
# Regular rate of change self.roc = self.ROC("SPY", 1) self.std_roc = IndicatorExtensions.Of(StandardDeviation(100), self.roc) # Log returns self.log_roc = self.LOGR("SPY", 1) self.std_log_roc = IndicatorExtensions.Of(StandardDeviation(100), self.log_roc)
Note how we don't pass a Symbol to the StandardDeviation constructor. See the attached backtest for reference.
Best,
Derek Melchin
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.
Mark Reeve
Brilliant! Thanks Derek
> That is exactly what I was after.
Mark Reeve
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!