Back to Community
Is there a way to persist the context, so that it could be re-loaded if a live algorithm is stopped and restarted?

Can some of the elements in context be written to file or saved in some way so as to be restored back to original state when a algorithm is stopped mid-way and subsequently re-started?

8 responses

I don't believe there is - likely due to the restrictions to avoid blatantly dumping out market data for your own use. But, as you may have already thought of, you could print out context information in the log and then copy/paste out the last known (printed) context back into the code (during initialize perhaps) so that it is available when restarted.

Out of curiosity, why are you trying to persist the context variables? Are you training a model?

But to answer your question, no, variables in "context' cannot be shared between algorithms. Once an algorithm is stopped, that session is finished and data can't be written to an external file.

That being said, algorithms "warm up" before they are launched. All of the variables are readied so they have the correct values from the first bar. For example, if you have a 50 day moving average, you don't need to wait 50 days to accumulate the data. We automatically pull historical data, compute the 50 day moving average, and have this value available to your algo in the first bar of live trading (FWIW the same is true in backtesting).

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Also, any time you start an algorithm that is connected to IB, the first thing your algorithm does is download all cash balances and positions. In that sense, the algorithm state is always stored.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks Dan and Alisa. I think I can re-instate my state variables during initialize based on the balances and positions, and the history. Just to be sure, the original trade date should be available for all open positions right?

Thanks Rob I suppose I have to find other ways to restore the state.

By "original trade date for open positions" do you mean the time the order was submitted? If so, you can access this information from the order object (see the created field): https://www.quantopian.com/help#api-orderobj

you can also do something like this:

# save your order information  
context.my_order = order_target(stock, 100)

# query for information about the order  
context.order_info = get_order(context.my_order)

#see when the order was created  
if context.order_info.created > X:  
   # more logic here  

@Alisa. I have a question. I understand that the algorithms "warm up" before they are launched. However, it will only warm up using minutely data for the contest. Am correct in assuming that?

That's right. All algorithms in live trading (paper trading on Quantopian or trading with the broker) warm-up and run on minutely data.

If you're developing a strategy in the IDE, I'd recommend to backtest in minute mode to closely simulate live trading. This way your algorithm gets more accurate frequency data.