Back

Rolling Window and Data Consolidator

Hello, so I am currently using the calendar.type shortcut to make consolidated weekly tradebars. In addition, I am using a basic rolling windows functions to get past data.

The probelm is when I check the logs for the rolling windows, it is logging in daily data which tells me that it is using the resolution.Daily instead of the consolidated weekly tradebar. So my question is what  input should I replace "[TradeBar]" with or what additions and/or changes do I need to make. 

Before anyone says it. Yes, I have looked through many discussions, documents, and templates. I haven't been able to find what seemingly lookings like a small fix 

You can view the code on my sample backtest attachment. Any backtest or code templates attached with your response would be much appreciated. Thank you.

 

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.


Hi,

Your data resolution determines when OnData is fired. As your data resolution is Daily, OnData will be fired daily.

Right now, you're adding a tradebar to your window in the OnData method (called daily) and the data you add is the data being passed to OnData (the daily bar). This taken together means you will get daily data added to the window at a daily basis.

If you look at my attached backtest, I'm calling Add() inside the CalendarTradeBarHandler() instead, and passing the trade bar sent in to the handler. I am thus adding weekly data to the window at a weekly basis.

1


Thank You Douglas Stridsberg  One Last Thing. 

Could you help me understand why the algorithm is not making an execution regardless of which market execution code I use, and what I can do to fix the problem.

The two market execution codes can be seen below in the sample backtest. Thank you!

 

0


You've for some reason defined your window to be 9 places long. This means if not (self.window.IsReady): return will return until you have 9 weeks in your window. Between 1 Jan and 1 Mar, you've only got 8 weeks.

You should have spotted this by noticing that your self.Log statement is never called, thus hinting that the method never reaches that point.

0

Douglas Stridsberg, that was just a change in date for the sample code. When I have longer a backtest the log still doesn't show & I get "Runtime Error: IndexError : tuple index out of range at OnData in main.py:line 72 IndexError : tuple index out of range" in the error log, but the confusing thing about that is the code doesn't have 72 lines

I'll attach a backtest so you can quickly see what I am talking about. And thank very much for taking time out your day to assist me in this.

Backtest not showing so here is the code:

def Initialize(self):

self.ticker = 'SPY'
self.AddEquity(self.ticker, Resolution.Daily)
self.SetBenchmark(self.ticker)
self.symbol_ = self.Symbol(self.ticker)
self.cash = 100000
self.SetCash(self.cash)
self.SetStartDate(2018, 10, 1)
self.SetEndDate(2019, 3, 1)

self.Consolidate(self.ticker, CalendarType.Weekly, self.CalendarTradeBarHandler)
self.window = RollingWindow[TradeBar](2)
def CalendarTradeBarHandler(self, tradeBar):
self.window.Add(tradeBar)
self.Log(f'{tradeBar.Time}: {tradeBar.Open} - {tradeBar.EndTime}: {tradeBar.Close}')

def OnData(self, data):
if not (self.window.IsReady): return

currBar = self.window[0] # Current bar had index zero.
pastBar = self.window[1]
currP = self.Securities[self.ticker].Price
prehigh = pastBar.High
self.Log("BarLogs: {0} : {1} -> {2} : {3}".format(pastBar.Time, pastBar.High, pastBar.Low))

if not self.Portfolio.Invested:
if currP>prehigh:
MarketOrder(self.ticker, 100)
'''
if not self.Portfolio.Invested:
if currBar.Price>pastBar.High:
MarketOrder(self.ticker, 100)
'''

 

0

Ignore the last comment. I quickly fixed the error with the log, but the algorithm is still not executing. Any thoughts on why?

0


You've got some weird stuff going on in your code - you're redefining two of your functions twice. It's always a good idea to sometimes take a step back and look at the overall code before sending it.

Also, you're still testing over a 1 month range which is quite likely to yield no trade signals. In fact, the window won't be ready until the second week which means you'll be acting on only 3 of the 4 weeks.

The issue preventing execution is that you forgot to call self.MarketOrder - you only called MarketOrder. Also I've replaced ticker with symbol_ but this shouldn't be necessary - it's just good practice to use the symbol rather than the string ticker as there might sometimes be conflicting tickers.

0


Thank You! Lol, that's why I shouldn't cod with a lack of sleep.

0

Douglas Stridsberg, could you give this discussion a look if you don't mind.

 

https://www.quantconnect.com/forum/discussion/5941/how-to-only-request-trades-during-market-hours/p1

 

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