Exception handling / Getting around ValueError

Hi,

I am trying to establish pre-market high and low values but 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"

Comments

  • ptunneyptunney Posts: 246
    edited November 2019

    Obviously, this will depend on when you ask for the data, which I why I mentioned in the previous post that you should check the time. But a lot of symbols have no pre-market activity

    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)
    if len(prebars.close)!=0: # if you do len(prebars) you will get 16 as there are 16 lists in the list (open high low close volume timestamp etc) so you ask for the length of one of the lists within, it can be any of those lists, they will all be the same length which will depend on the symbol and the data available.
        self.prehigh = max(prebars.high)
        self.prelow = min(prebars.low)
        print (service.time_to_string(service.system_time), self.symbol, self.prehigh, self.prelow)
    else:
        self.prehigh = 99999
        self.prelow = 0
        print service.time_to_string(service.system_time),self.symbol,"no pre market bar data returned at this time"
    
  • Awesome - many thanks again!

Sign In or Register to comment.