Back

RollingWindow example strategy in Python

Hey guys. I'm trying to use RollingWindow in Python. Keeps crashing .I'm not being lazy just I can't find any references to Python and Rolling Window. Neither documentation, or 30 examples in Python have and as mentioned above, access to the Python classes/functions not working. I searched the forum, only C# references. Please could anyone give a barebones example of using a RollingWindow in Python. Perhaps output to log, today's close and yesterday's close. Would be fantastic. Thanks for reading. Nice one

Update Backtest








Here is the simplest example: keeping a RollingWindow with 2 TradeBar:

# In Initialize
# Creates a Rolling Window indicator to keep the 2 TradeBar
self.window = RollingWindow[TradeBar](2)

# In OnData
# Add SPY TradeBar in rollling window
self.window.Add(data["SPY"])

# Wait for window to be ready: needs two additions
if not self.window.IsReady: return

currBar = self.window[0] # Current bar has index zero.
pastBar = self.window[1] # Past bar has index one.

Please checkout the working example below:

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.


Now an example with an indicator:

# In Initialize
# Creates an indicator and adds to a rolling window when it is updated
self.SMA("SPY", 5).Updated += self.SmaUpdated
self.smaWin = RollingWindow[IndicatorDataPoint](5)

# Define the event handler that will receive the indicator updates
def SmaUpdated(self, sender, updated):
'''Adds updated values to rolling window'''
self.smaWin.Add(updated)

# In OnData
currSma = self.smaWin[0] # Current SMA had index zero.
pastSma = self.smaWin[self.smaWin.Count-1] # Oldest SMA has index of window count minus 1.

Please checkout the working example below:

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 Alex, thank-you for both examples - I thanked you on chat but forgot to reply here. First example helped a lot and I can now see past history in my code. I didn't totally understand all mechanism of 2nd example.. but have a rough idea how mechanism works, eg add the method to get called by event handler when initilisting object etc.. looking very much forward to release your more extensive Juypiter notebooks your are working on. Thank-you

 

0

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