Mariner Backtesting - service.get_market_hours()

service.get_market_hours()

Returns a tuple of date-time integers of the market hours on the requested day.

ie It returns two timestamps, [0] is the opening time of the date requested [1] is the closing time

Example

Run on Friday 10/4/2019

 opentime,closetime = service.get_market_hours[-10] # get market hours for 2 weeks ago
print opentime,closetime
print service.time_to_string(opentime),service.time_to_string(closetime)
Returns
 [1568986200000][1569009600000]
2019/09/20 09:30:00.000 2019/09/20 16:00:00.000
Note

This command only works into the past.

If you run a single multiday job (where each day must finish before starting the backtest of the next day - used for multi day holds where backtest cannot be in parallel) then this command will return the offset from the final day of the backtest. In this case you should request the daily bars and use the timestamp from the daily bars for your offet. ie

 yesterday = md['SPY'].bar.daily(-1).timestamp[0]
Interface:
 get_market_hours(date_offset=0)

Sample - Calling Method

 service.get_market_hours( -1 )
Parameters:
Name Type Default Information
date_offset integer 0 Trading days offset of the current trading day
Returns:
Type Notes
tuple (market_open_date_time, market_close_date_time)
Remarks:
  • Excludes holiday and weekends (non-trading days)
Working Example:

use previous_trading_date = service.get_market_hours( date_offset=i )[0] to get the day desired via the offset.

use service.time_to_string(previous_trading_date, '%Y-%m-%d') to desired format

formatting options strftime.org/

from cloudquant.interfaces import Strategy


class GetTradingHours(Strategy):
    @classmethod
    def on_strategy_start(cls, md, service, account):
        print service.time_to_string(service.system_time, '%Y-%m-%d')
        for i in range(0, -50, -1):
            #  service.get_market_hours( date_offset=i ) returns a tuple like (1455633000000000, 1455656400000000)
            previous_trading_date = service.get_market_hours(date_offset=i)[0]

            # string formatting http://strftime.org/
            date = service.time_to_string(previous_trading_date, '%Y-%m-%d')
            month = service.time_to_string(previous_trading_date, '%B')
            day_of_week = service.time_to_string(previous_trading_date, '%A')
            print('offset={}\t{}\t{}\t{}'.format(i, date, month, day_of_week))

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return False

Console

2017-02-09
offset=0    2017-02-09  February    Thursday
offset=-1   2017-02-08  February    Wednesday
offset=-2   2017-02-07  February    Tuesday
offset=-3   2017-02-06  February    Monday
offset=-4   2017-02-03  February    Friday
offset=-5   2017-02-02  February    Thursday
offset=-6   2017-02-01  February    Wednesday
offset=-7   2017-01-31  January Tuesday
offset=-8   2017-01-30  January Monday
offset=-9   2017-01-27  January Friday
offset=-10  2017-01-26  January Thursday
offset=-11  2017-01-25  January Wednesday
offset=-12  2017-01-24  January Tuesday
offset=-13  2017-01-23  January Monday
offset=-14  2017-01-20  January Friday
offset=-15  2017-01-19  January Thursday
offset=-16  2017-01-18  January Wednesday
offset=-17  2017-01-17  January Tuesday
offset=-18  2017-01-13  January Friday
offset=-19  2017-01-12  January Thursday
offset=-20  2017-01-11  January Wednesday
offset=-21  2017-01-10  January Tuesday
offset=-22  2017-01-09  January Monday
offset=-23  2017-01-06  January Friday
offset=-24  2017-01-05  January Thursday
offset=-25  2017-01-04  January Wednesday
offset=-26  2017-01-03  January Tuesday
offset=-27  2016-12-30  December    Friday
offset=-28  2016-12-29  December    Thursday
offset=-29  2016-12-28  December    Wednesday
offset=-30  2016-12-27  December    Tuesday
offset=-31  2016-12-23  December    Friday
offset=-32  2016-12-22  December    Thursday
offset=-33  2016-12-21  December    Wednesday
offset=-34  2016-12-20  December    Tuesday
offset=-35  2016-12-19  December    Monday
offset=-36  2016-12-16  December    Friday
offset=-37  2016-12-15  December    Thursday
offset=-38  2016-12-14  December    Wednesday
offset=-39  2016-12-13  December    Tuesday
offset=-40  2016-12-12  December    Monday
offset=-41  2016-12-09  December    Friday
offset=-42  2016-12-08  December    Thursday
offset=-43  2016-12-07  December    Wednesday
offset=-44  2016-12-06  December    Tuesday
offset=-45  2016-12-05  December    Monday
offset=-46  2016-12-02  December    Friday
offset=-47  2016-12-01  December    Thursday
offset=-48  2016-11-30  November    Wednesday
offset=-49  2016-11-29  November    Tuesday