LEAN is the open source
algorithmic trading engine powering QuantConnect. Founded in 2013 LEAN has been built by a
global community of 80+ engineers and powers more than a dozen hedge funds today.
Alpha League Competition: $1,000 Weekly Prize Pool
Qualifying Alpha Streams Reentered Weekly Learn
more
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?
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.
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'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.
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.
Vladimir
94.7k Pro
,
Jared Broad,
Mark Reeve requested STD of Log Returns not simple returns.
0
Vladimir
94.7k Pro
,
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())
1
Jared Broad
Pro
,
Yes good point it should be like this but it doesn't exist yet. We'll add it to the extensions collection.
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
94.7k Pro
,
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()
3
Mark Reeve
5.8k Pro
,
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!
2
Derek Melchin
STAFF Pro
,
Hi Mark,
To calculate the standard deviation of the trailing 100 daily returns, we can use
Note how we don't pass a Symbol to the StandardDeviation constructor. See the attached backtest for reference.
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.
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...
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!