^^^would be really nice to have in the metadata to detect if the script is currently trading live or just a backtest.
Currently I use:
storage.live = storage.get('live', False)
if info.tick == 0:
storage.live = False
end = info.end # info.end only exists in backtest mode
storage.live = True
This looks like an easy fix but would require restarting of your bots.
I like to do stuff like that
"next time things are unexpectedly down"
This is better:
is_backtest = hasattr(info, "end")
I had been doing this at the global level, and I never really liked it:
LIVE = False
try: end = info.end
except: LIVE = True
BACKTEST = not LIVE
Your solution is classy and proper. Very nice.
BACKTEST = hasattr(info, "end")
LIVE = not BACKTEST
log('This is a live session')
log('This is a backtest')
@sai if its not already in the next platform, consider this ticket closed... this solution alleviates concerns with the "crash and catch" method.
well... wait a minute... how is this really any different? It is less lines of code, but its still a "crash and catch".
hasattr(object, name)The arguments are an object and a string.
The result is True if the string is the name of one of the object’s attributes, False if not.
(This iimplemented by calling getattr(object, name) and seeing whether it raises an exception or not.)
it seems like this is really no better or worse than the try/except method.
Except for the fact that
hasattr is implemented in pure C, so it's almost twice as fast
foo = None
bar = foo.bar
bar = None
foo = None
bar = hasattr(foo, 'bar')
Thankfully, speed doesn't really matter in this scenario, so it's not a big deal.
I just like clean syntax (always) even if I have to pay CPU cycles for it (which is the opposite of the case here though as lewk pointed out).