Timestamp of Previous Trading Day

Dustin_LangDustin_Lang Posts: 25
edited March 2017 in FAQs

What is the best way to get the timestamp of the previous trading day close? Only solution I can think of is converting to a datetime object and maintaining a list of half days and holidays and use a conditional for the proper offset for holidays or Monday-Friday calculations.

Tagged:

Comments

  • shayneshayne Posts: 70
    edited August 2017

    try this
    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):
            @staticmethod
            def in_symbol_universe(symbol, md, service):
                return symbol == 'TQQQ'
    
    
            def on_start(self, md, order, service, account):
                print( 'current tradeing date:\t{}\n'.format( service.time_to_string(service.system_time, '%Y-%m-%d') ) )
                for i in range(0, -100, -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) )
    
                service.terminate()
    
    
    current tradeing date:  2016-02-16
    
    offset=0    2016-02-16  February    Tuesday
    offset=-1   2016-02-12  February    Friday
    offset=-2   2016-02-11  February    Thursday
    offset=-3   2016-02-10  February    Wednesday
    offset=-4   2016-02-09  February    Tuesday
    offset=-5   2016-02-08  February    Monday
    offset=-6   2016-02-05  February    Friday
    offset=-7   2016-02-04  February    Thursday
    offset=-8   2016-02-03  February    Wednesday
    offset=-9   2016-02-02  February    Tuesday
    offset=-10  2016-02-01  February    Monday
    offset=-11  2016-01-29  January Friday
    offset=-12  2016-01-28  January Thursday
    offset=-13  2016-01-27  January Wednesday
    offset=-14  2016-01-26  January Tuesday
    offset=-15  2016-01-25  January Monday
    offset=-16  2016-01-22  January Friday
    offset=-17  2016-01-21  January Thursday
    offset=-18  2016-01-20  January Wednesday
    offset=-19  2016-01-19  January Tuesday
    offset=-20  2016-01-15  January Friday
    offset=-21  2016-01-14  January Thursday
    offset=-22  2016-01-13  January Wednesday
    offset=-23  2016-01-12  January Tuesday
    offset=-24  2016-01-11  January Monday
    offset=-25  2016-01-08  January Friday
    offset=-26  2016-01-07  January Thursday
    offset=-27  2016-01-06  January Wednesday
    offset=-28  2016-01-05  January Tuesday
    offset=-29  2016-01-04  January Monday
    offset=-30  2015-12-31  December    Thursday
    offset=-31  2015-12-30  December    Wednesday
    offset=-32  2015-12-29  December    Tuesday
    offset=-33  2015-12-28  December    Monday
    offset=-34  2015-12-24  December    Thursday
    offset=-35  2015-12-23  December    Wednesday
    offset=-36  2015-12-22  December    Tuesday
    offset=-37  2015-12-21  December    Monday
    offset=-38  2015-12-18  December    Friday
    offset=-39  2015-12-17  December    Thursday
    offset=-40  2015-12-16  December    Wednesday
    offset=-41  2015-12-15  December    Tuesday
    offset=-42  2015-12-14  December    Monday
    offset=-43  2015-12-11  December    Friday
    offset=-44  2015-12-10  December    Thursday
    offset=-45  2015-12-09  December    Wednesday
    offset=-46  2015-12-08  December    Tuesday
    offset=-47  2015-12-07  December    Monday
    offset=-48  2015-12-04  December    Friday
    offset=-49  2015-12-03  December    Thursday
    offset=-50  2015-12-02  December    Wednesday
    offset=-51  2015-12-01  December    Tuesday
    offset=-52  2015-11-30  November    Monday
    offset=-53  2015-11-27  November    Friday
    offset=-54  2015-11-25  November    Wednesday
    offset=-55  2015-11-24  November    Tuesday
    offset=-56  2015-11-23  November    Monday
    offset=-57  2015-11-20  November    Friday
    offset=-58  2015-11-19  November    Thursday
    offset=-59  2015-11-18  November    Wednesday
    offset=-60  2015-11-17  November    Tuesday
    offset=-61  2015-11-16  November    Monday
    offset=-62  2015-11-13  November    Friday
    offset=-63  2015-11-12  November    Thursday
    offset=-64  2015-11-11  November    Wednesday
    offset=-65  2015-11-10  November    Tuesday
    offset=-66  2015-11-09  November    Monday
    offset=-67  2015-11-06  November    Friday
    offset=-68  2015-11-05  November    Thursday
    offset=-69  2015-11-04  November    Wednesday
    offset=-70  2015-11-03  November    Tuesday
    offset=-71  2015-11-02  November    Monday
    offset=-72  2015-10-30  October Friday
    offset=-73  2015-10-29  October Thursday
    offset=-74  2015-10-28  October Wednesday
    offset=-75  2015-10-27  October Tuesday
    offset=-76  2015-10-26  October Monday
    offset=-77  2015-10-23  October Friday
    offset=-78  2015-10-22  October Thursday
    offset=-79  2015-10-21  October Wednesday
    offset=-80  2015-10-20  October Tuesday
    offset=-81  2015-10-19  October Monday
    offset=-82  2015-10-16  October Friday
    offset=-83  2015-10-15  October Thursday
    offset=-84  2015-10-14  October Wednesday
    offset=-85  2015-10-13  October Tuesday
    offset=-86  2015-10-12  October Monday
    offset=-87  2015-10-09  October Friday
    offset=-88  2015-10-08  October Thursday
    offset=-89  2015-10-07  October Wednesday
    offset=-90  2015-10-06  October Tuesday
    offset=-91  2015-10-05  October Monday
    offset=-92  2015-10-02  October Friday
    offset=-93  2015-10-01  October Thursday
    offset=-94  2015-09-30  September   Wednesday
    offset=-95  2015-09-29  September   Tuesday
    offset=-96  2015-09-28  September   Monday
    offset=-97  2015-09-25  September   Friday
    offset=-98  2015-09-24  September   Thursday
    offset=-99  2015-09-23  September   Wednesday
    
    
Sign In or Register to comment.