Dear community, I am new to quantconnect and to algorithm trading in general. As a first trial I would like to write trading algorithm based on daily breakout strategy. I am in stage of creating backtest. For this strategy implementation I am trying to prepare custom consolidator for forex security. My goal is to have a consolidated Quote bar starting at particular time (lets say 9:50 am) with a particular duration (lets say 65 minute). For that purpose I tried to use Calendar Consolidators. According to documentation I create an instance of QuoteBarConsolidator with custom consolidation period.
self.dataConsolidator = QuoteBarConsolidator(self.consolidation_period)
algorithm.SubscriptionManager.AddConsolidator(security.Symbol, self.dataConsolidator)
self.dataConsolidator.DataConsolidated += self.consolidatorHandler
This is a method for consolidation_period:
def consolidation_period(self, dt: datetime) -> CalendarInfo:
period = timedelta(minutes=65)
# First we create datetime in algorithm timezone
mdt = dt.replace(hour=9, minute=50)
my_datetime = mdt.replace(tzinfo = pytz.timezone(str(self.algorithm.TimeZone)))
# Then we convert it to symbol timeZine
start = my_datetime.astimezone(pytz.timezone(str(self.symbolTimeZone)))
return CalendarInfo(start.replace(tzinfo=None), period)
Important to note that algorithm time zone is Europe/London and symbol timezone is America/New_York.
Now here comes the confusion. The code is working. It delivers consolidated quote bar at 10:55 London time. However the data consolidated are not from range 9:50-10:55 (London) but 4:50-5:55 (London time). Which I checked manually by comparison with historical data obtained in research environment.
qb = QuantBook()
qb.SetTimeZone('Europe/London')
trade_symbol = qb.AddForex('EURJPY').Symbol
st = datetime(2016, 1, 7, 4, 50)
en = datetime(2016, 1, 7, 5, 55)
all_history = qb.History(trade_symbol, st, en, Resolution.Minute)
history = all_history.loc[trade_symbol]
history.set_index(history.index.tz_localize('America/New_York').tz_convert('Europe/London'), inplace=True)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', -1)
display(history)
You see there is a lot of place for timezone confusion. Does it comes from backtesting or historical data?
Jiri
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!