Back

Get the Current Market Open/Close Times

Hi,

In the Algorithm Framework, I usually get the Market Open and Close Times by doing this right at the market open:

self.todayOpen = {} # store open times
self.todayClose = {} # store close times

for security in algorithm.ActiveSecurities.Values:
# get Open and Close Hours for today
hours = algorithm.ActiveSecurities[security.Symbol].Exchange.Hours

if hours.IsDateOpen(algorithm.Time):
self.todayOpen[security.Symbol] = hours.GetNextMarketOpen(algorithm.Time - timedelta(hours=2), False)
self.todayClose[security.Symbol] = hours.GetNextMarketClose(algorithm.Time - timedelta(hours=2), False)

Basically, every trading day, I use the function GetNextMarketOpen and I pass the current time minus 2 hours to get the current day market open....it works but it's ugly! Is there any elegant way of getting the current trading day market open and close time?

I need this time for the duration of my insights as I like to add/deduct time from the open/close for the duration of the signals.

Thanks!

Emilio

 

 

 

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 Emilio Freire 

I would suggest creating a class to hold the relevant open/close hours:

class MarketHours:
def __init__(self, algorithm, symbol):
self.hours = algorithm.Securities[symbol].Exchange.Hours
self.CurrentOpen = self.hours.GetNextMarketOpen(algorithm.Time, False)
self.CurrentClose = self.hours.GetNextMarketClose(self.CurrentOpen, False)
self.NextOpen = self.hours.GetNextMarketOpen(self.CurrentClose, False)

def Update(self):
self.CurrentOpen = self.NextOpen
self.CurrentClose = self.hours.GetNextMarketClose(self.CurrentOpen, False)
self.NextOpen = self.hours.GetNextMarketOpen(self.CurrentClose, False)

Create the objects in OnSecuritiesChanged and save them in dictionaries:

def OnSecuritiesChanged(self, algorithm, changes):
for security in changes.AddedSecurities:
symbol = security.Symbol
if not symbol in self.marketHours:
self.marketHours[symbol] = MarketHours(algorithm, symbol)

In Update method, we will update those objects:

if self.day != algorithm.Time.day:
for symbol, markethours in self.markethours.items():
markethours.Update()

self.day = algorithm.Time.day

 

1

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