Trying to create monthly bars

Hello everyone, im trying to use consolidators and rolling windows in an attempt to obtain OHLC of a previous monthly bar. Im not sure if this is even possible or if there is a better way to do this, so any info is appreciated.

Below is the code i have so far. I try to create monthly data with a consolidator and add it to a rolling window. It throws the error: 

Runtime Error: Python.Runtime.PythonException: ArgumentOutOfRangeException : Must be between 0 and 0

I then change my arguement like so...... monthlyhigh = self.monthly[0].High
it gives me a bunch of daily highs. i cant figure out what im doing wrong.

from QuantConnect.Data.Market import TradeBar
from datetime import timedelta
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d

class MyAlgorithm(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

self.SetStartDate(2017,1,1) #Set Start Date
self.SetCash(100000) #Set Strategy Cash
self.AddEquity("SPY", Resolution.Second)

consolidator = TradeBarConsolidator(timedelta(1))
consolidator.DataConsolidated += self.OnMonthlyData
self.SubscriptionManager.AddConsolidator("SPY", consolidator)

self.monthly = RollingWindow[TradeBar](2)

def OnData(self, data):


# Add monthly bar to monthly rolling window
def OnMonthlyData(self, sender, bar):

monthlyhigh = self.monthly[1].High

Update Backtest

still cant figure it out. ayone have some advice?


Hi Edvinas Jablonskis,

Here you use the second resolution and set the timedelta to be one day, that's why you get a bunch of daily highs.

you need to specify how many bars you want to consolidate when using consolidators. As the trading days in each month is different, it's not accurate to get the monthly OHLC with data consolidators.  

I would suggest you add the daily equity data and try the monthly schedule event. In schedule event function, you could make a history request to get the history daily OHLC price in the last month. Then you could calculate the monthly OHLC manually.


Thank you Jing! that sounds like it would work. Going to try it


Update Backtest


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.


This discussion is closed