Back to Community
Live Algorithm Life time?

Hi

I'm wondering what the lifetime of a live algorithm instance is. Is it restarted everyday, or just once when you start the algorithm? Basically wondering if I can have variables stored on "context" that can be used day to day as they can in a backtest. Tracking the max portfolio value for example.

Thanks

2 responses

Data will be retained in context but if the algo crashes, then you'll lose them. However, I don't think this is the case for recorded variables. I know that if a backtest crashes, the data can still be pulled into the research platform and analyzed. The data in context are lost, but recorded variables are retained. Presumably, for live paper/real-money trading, the behavior is the same.

Depends on how complex the algorithm is, a simple one can go for a long time, a complex one might run into an edge-case you haven't coded against, it might randomly be denied access to it's fetched CSV, or it might randomly take longer than 50 seconds to execute a piece of code and time out. I have trouble getting one of mine to run longer than 1 week consecutively. Something ALWAYS comes up... and the more edge cases I protect against the more likely it is to time out. I've seen a lot of WEIRD shit live trading, I've had them glitch out, then quantopian will restart it (don't know the criteria for this) and it doesn't run the before_trading_start() code which caused an error. I've had the pipeline fail to load after market holidays. Orders that fail to cancel, robinhood reporting stocks I don't own, etc. tons of stuff.

I learned early on to code all my algorithms so that they can be stopped and started without losing the logic behind the trades and I put lots of flags that detects when a peice of code hasn't ran yet and runs it if it hasn't. It's a pain TBH but a good learning experience in how to have a blancing act withrobust code and optimized run speed. I've had to scrap features because they caused a rare edge case that took too much computational power to check against.