How can I get the pre-market high and low?

Hi,

new here and looking for the best way to get the premarket high and low values into my script. I want to defne the premarket as starting at 08.00 and ending at 9.30. Many thanks in advance

Tobbe

Best Answer

  • ptunneyptunney Posts: 246
    Answer ✓

    You can request minute bars by offset and by time

    starttime = service.time(8)
    endtime = service.time(9,29)
    prebars = minute(start=starttime, end=endtime, include_empty=False, include_extended=True, today_only=True)
    self.prehigh = max(prebars.high)
    self.prelow = min(prebars.low)
    

    As long as you start your script at 9:30 (or just use core hours) then this should work for you.

    If you are running before 9:30 then you will have to put a timecheck if statement in whereever suits :

    if event.timestamp>service.time(9,30):
    

Answers

  • Many thanks - this is exactly what I needed :)

  • Hi again,

    I am having trobles with a ValueError that interrupts the program when it comes to certain symbols that supposedly don't have any activity in the time period defined as premarket in the code. I am runing the code under the on minute bar and it will print correct resluts up until the first symboI where the "prebars" don't contain ant data where it will abort the backtest and give the eror code:

    self.prehigh = max(prebars.high)
    ValueError: max() arg is an empty sequence

    I have tried to build in an exception handling using whileTrue/try/except to give an arbitrary value to "self.prehigh" where the prebars-reference would give an error code, but this seems to make the backtest procdure very slow and the program finally aborts without giving any error codes. Any suggestions for overcoming this would be much appreciated. See my code below and the console result including the error code:

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
    handle_list = service.symbol_list.get_handle('9a802d98-a2d7-4326-af64-cea18f8b5d61')
    #this is all stocks on S&P500
    return service.symbol_list.in_list(handle_list,symbol) and md.stat.avol > 2000000 and md.stat.prev_close < 20

    def on_minute_bar(self, event, md, order, service, account, bar):

        starttime = service.time(7)
        endtime = service.time(9,29)
        prebars = md.bar.minute(start=starttime, end=endtime, include_empty=False, include_extended=True, bar_size=1, today_only=True)
        self.prehigh = max(prebars.high)
        self.prelow = min(prebars.low)
    
        print (service.time_to_string(service.system_time), self.symbol, self.prehigh, self.prelow)
    

    Console result:

    ('2019-11-07 09:30:00.015000', 'GT', 16.780000686645508, 16.5)
    ('2019-11-07 09:30:00.070000', 'F', 9.0, 8.9600000381469727)
    ('2019-11-07 09:30:00.070000', 'FLR', 18.5, 18.5)
    ('2019-11-07 09:30:00.151000', 'MYL', 17.25, 17.049999237060547)
    ('2019-11-07 09:30:00.463000', 'HBAN', 15.060000419616699, 14.989999771118164)
    ('2019-11-07 09:30:00.469000', 'UAA', 18.479999542236328, 18.25)
    ('2019-11-07 09:30:00.472000', 'UA', 16.639999389648438, 16.469999313354492)
    ('2019-11-07 09:30:00.593000', 'PBCT', 17.0, 16.989999771118164)
    ('2019-11-07 09:30:00.629000', 'RF', 17.329999923706055, 17.170000076293945)
    ('2019-11-07 09:30:00.631000', 'COTY', 13.100000381469727, 12.989999771118164)
    ('2019-11-07 09:30:00.633000', 'COG', 18.590000152587891, 18.370000839233398)
    ('2019-11-07 09:30:00.937000', 'MAT', 12.319899559020996, 12.020000457763672)
    ('2019-11-07 09:30:01.001000', 'NKTR', 19.100000381469727, 18.799999237060547)
    ('2019-11-07 09:30:01.369000', 'HBI', 16.25, 15.949999809265137)
    ('2019-11-07 09:30:01.372000', 'GPS', 18.200000762939453, 18.040000915527344)


    Simulation Exception:
    strategy=CQ2faf68ecd96e433183a2bcbee80d071f
    symbol=HST
    simulation_time=2019-11-07T09:30:01.372000

    Traceback (most recent call last):
    File "run_simulation.py", line 558, in
    main(sys.argv[1:])
    File "run_simulation.py", line 532, in main
    results = simulator()
    File "/var/jenkins_home/miniconda/conda-bld/tradesim_1564072999421/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python2.7/site-packages/tradesim/engine.py", line 597, in call
    File "/var/jenkins_home/miniconda/conda-bld/tradesim_1564072999421/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python2.7/site-packages/tradesim/engine.py", line 769, in simulate
    File "/var/jenkins_home/miniconda/conda-bld/tradesim_1564072999421/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python2.7/site-packages/tradesim/context.py", line 933, in process_event
    File "/var/jenkins_home/miniconda/conda-bld/tradesim_1564072999421/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python2.7/site-packages/tradesim/context.py", line 620, in process_event
    File "job/CQ2faf68ecd96e433183a2bcbee80d071f.py", line 83, in on_minute_bar
    self.prehigh = max(prebars.high)
    ValueError: max() arg is an empty sequence

    Probable strategy error
    * At least one expected intermediate internal (not user-managed)
    result file is missing. This usually follows an error in the (user)
    strategy, and often a potentially helpful stack trace is present
    shortly before messages like this.
    * In some cases, the first missing file might suggest the issue.
    * In this case, the missing results file(s) were: "sim_all_orders",
    "sim_executions"

  • ptunneyptunney Posts: 246

    See your more recent post for the answer, but you need to check the length of one of the lists in your prebars (it contains 16 lists). If one of those lists (ie prebars.close so len(prebars.close) ) is zero then there was no data returned and you need to take some other action.

  • Thank you - works perfectly

Sign In or Register to comment.