Hi,
I am trying to write an algorithm that does the following:
Calculate the Stochastic Ks of some (29 in this case) symbols based on 5 minute bars.
Sort the symbols by this Stochastic K.
If a symbol was in the top-5 3 calculations ago (based on Stochastic K), but during the last 2 calculations it is out of the top 5, then emit a Down insight.
When I didn't include the "minutes%5==0" part, my Rolling Windows got updated every minute, despite me only wanting them to update every 5 minutes.
Is my code doing what I want and is there a way to do this more elegantly inside an Alpha Stream? (I found some Consolidation methods but they were all inside an Algorithm, not an AlphaStream and I couldn't make them work in my AlphaStream.)
Derek Melchin
Hi Wilford Birmingham,
To ensure the RollingWindows are updated everytime the indicator is updated instead of every minute, we can place the logic inside a consolidation handler.
class SymbolData: def __init__(self, algorithm, security, StochasticPeriod, KPeriod, DPeriod, resolution): # ... self.consolidator = TradeBarConsolidator(timedelta(minutes=5)) self.consolidator.DataConsolidated += self.consolidation_handler algorithm.SubscriptionManager.AddConsolidator(self.Symbol, self.consolidator) def consolidation_handler(self, sender, consolidated): if self.Stochastic.Update(consolidated): self.window.Add(self.Stochastic.StochK.Current.Value)
To simplify the alpha model's Update method, we can place all of the timestamp conditions at the top of the method. In addition, we can use pandas to easily rank all of the symbols by their historical stochastic values.
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.
Wilford Birmingham
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!