Back

Time zone aware exchange times?

I need to use the time zone of an exchange to create a tzinfo object so I can convert a time zone naive time into time zone aware. Its use is in a comparison with a supplied datetime object in an expiry time consolidator.

I can get the exchange time zone, but I don't know how to get the GMT offset for use in datetime.timezone(offset).

Any suggestions?

    def Custom(self, dt):
        '''
        Returns a CalendarInfo object for a custom daily consolidator.
        For Forex the end of day is 17:00 NY time.
        CFD end of day varies with CFD/Exchange.
        '''
        period = timedelta(days=1)
        
        if self.security.Type == SecurityType.Forex:
            start_time = dt.replace(hour=17)
        else:
            # Get the next market close time after midday today (time zone naive).
            reference_time = dt.replace(hour=12, minute=0)
            close_time = self.security.Exchange.Hours.GetNextMarketClose(reference_time, False)
            
            # Make the close time, time zone aware, so it can be compared with dt.
            exchange_tz = self.security.Exchange.Hours.TimeZone
            exchange_tx_offset = ???     # *** How to get the offset from exchange_tz?
            exchange_tzinfo = datetime.timezone(exchange_tx_offset)
            start_time = close_time.replace(tzinfo=exchange_tzinfo)
        
            self.Debug("Custom(): close_time = " + str(close_time) + ", start_time = " + str(start_time))
        
        # This comparison needs time zone aware times.
        if start_time > dt:
            start_time = start_time - period
        
        return CalendarInfo(start_time, period)

 

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.


# Make the close time, time zone aware, so it can be compared with dt.
exchange_tz = self.security.Exchange.Hours.TimeZone
exchange_tx_offset = ??? # *** How to get the offset from exchange_tz?

Is this the offset from the algorithm data to the exchange?  

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 Jared

No, the UTC offset. Some context may help.

My objective is to use the market close time to make the CalendarInfo object returned by Custom(self,dt) for QuoteBarConsolidator(self.Custom). Commodity CFDs have varied daily close times.

The market close time needs to be compared to the supplied time (dt, in this case). The time returned by GetNextMarketClose() is time zone naïve, but dt is time zone aware, and the comparison can't compare time zone aware and naïve times. Hence the need for a time zone aware market close time.

My approach in the code above is to:

  • get the market close time,

  • get the exchange time zone,

  • get the time zone offset (from UTC) (but I don't know how),

  • make a tzinfo object using the offset,

  • make a time zone aware market close time.

If there's an easier way, please advise.

Thanks.

0

Phew. Timezones. How about this to purely answer the question about getting the offset:

local = self.Securities["EURUSD"].Exchange.LocalTime.replace(tzinfo=None)
utc = self.UtcTime.replace(tzinfo=None)
self.Debug(utc - local)

66 | 15:57:16: -1 day, 19:00:00

The root question is you'd like to consolidate daily data of CFD from minute but in its local exchange timezone days? Is that right?

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.


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