Back

How to get only trading time in minutes

Hi, Collegues

Could I get time beetwen two datetimes in minutes, but only minutes of trading time, this means  from 9:30 to 16:00 for all full days and plus minutes of not full days between start end end:

def HandleRenko1(self, sender, data):
'''This function is called by our renkoClose consolidator defined in Initialize()
Args:
data: The new renko bar produced by the consolidator'''
self.RenkoWindow1.Add(data)


start = self.RenkoWindow1[self.RenkoWindow1.Count - 1].Start
end = self.RenkoWindow1[0].EndTime
r_time = end - start


tradingminutes = ..... ?

 

 

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 SLs,

You can do this manually, but it is not easy. You can try using the EachTradeableDay function to get the number of trading days between two datetimes, then manually calculate the minutes. 

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.


I did not answer you right away cause I thought somebody else would come up with an elegant solution. But here's something that may help you along the way, by identifying holidays

# ------------------------------------------------------------------------------
# Business days
# ------------------------------------------------------------------------------
from datetime import timedelta #, date
from pandas.tseries.holiday import (AbstractHolidayCalendar, # inherit from this to create your calendar
Holiday, nearest_workday, # to custom some holidays
#
USMartinLutherKingJr, # already defined holidays
USPresidentsDay, # " " " " " "
GoodFriday,
USMemorialDay, # " " " " " "
USLaborDay,
USThanksgivingDay # " " " " " "
)


class USTradingCalendar(AbstractHolidayCalendar):
rules = [
Holiday('NewYearsDay', month=1, day=1, observance=nearest_workday),
USMartinLutherKingJr,
USPresidentsDay,
GoodFriday,
USMemorialDay,
Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
USLaborDay,
USThanksgivingDay,
Holiday('Christmas', month=12, day=25, observance=nearest_workday)
]

# TODO: to be tested
def last_trading_day(expiry):
# American options cease trading on the third Friday, at the close of business
# - Weekly options expire the same day as their last trading day, which will usually be a Friday (PM-settled), [or Mondays? & Wednesdays?]
#
# SPX cash index options (and other cash index options) expire on the Saturday following the third Friday of the expiration month.
# However, the last trading day is the Thursday before that third Friday. Settlement price Friday morning opening (AM-settled).
# http://www.daytradingbias.com/?p=84847

dd = expiry # option.ID.Date.date()

# if expiry on a Saturday (standard options), then last trading day is 1d earlier
if dd.weekday() == 5:
dd -= timedelta(days=1) # dd -= 1 * BDay()

# check that Friday is not an holiday (e.g. Good Friday) and loop back
while USTradingCalendar().holidays(dd, dd).tolist(): # if list empty (dd is not an holiday) -> False
dd -= timedelta(days=1)

return dd

 

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