MiniXC / simple-back

A simple daily python backtester that works out of the box.
Mozilla Public License 2.0
59 stars 12 forks source link

scheduling in strategies #15

Open MiniXC opened 4 years ago

MiniXC commented 4 years ago

Currently, doing specific things on certain days or events leads to messy nested ifs. I propose we add a decorators for scheduling. The big task (as always) is naming things, I was thinking of the following syntax, but there might be better options.

@run(every='monday', on='open')
@run(every='day', on='close')
@run(every='firstdayofweek', on='open') # run on tuesday if market closed on monday
@run(on='open') # sensible default for every could be 'day'
@run(every='tuesday') # while on could default to ['open', 'close'] (open & close)
...

Another option that instead of replacing ifs inside strategies would make it easier to determine if certain conditions are met would be to introduce a ScheduleConditions class, e.g. with shorthand bt.schedule

if bt.schedule.monday:
  ...
if bt.schedule.open:
  ...
if bt.schedule.first_day_of.week:
  ...
if bt.schedule.first_day_of.month:
  ...
if bt.schedule.date == '2019-1-1':
  ...
if bt.schedule.event == 'open':
  ...

We could also pass schedule to strategies directly and get rid of day and event. Decorators could still work like this:

@run(Schedule.first_day_of.week)
...

Schedule could be static (it only depends on bt, no internal state) and e.g. bt.schedule.open could just be shorthand for Schedule.open(bt). Or we could use a scheduling library but they would almost certainly have to be modified for the specific market calendars we are using.