Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to save data permanently

Hi

I'm trying to keep a record of the positions opened in my portfolio.

The portfolio variable only gives you the total amount of shares you have currently with no dates. I'd like to keep track of every position i've opened, date, number of shares etc.

Is there any way to save all that info permanently in a way that is not lost in case of the algorithm being restarted?

Thanks!

9 responses

Hi Jorge,

Thanks for posting!

Although there is currently no way to permanently save your data (for security reasons), we are working on ways to provide flexibility for our users in terms of how to use your backtest/algorithm's results. There are currently ways to share your backtests on the community page or invite others to collaborate on your algorithms as well!

We'll clean up the other posts, but thank you for your interest and let me know if you have any other questions!

Seong

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.

Hi Seong

Sorry, I think I wasn't very clear in my first post. I don't want to share any algorithm results. What I want to know is how to save some variables that I need for my algorithm and be sure that those variables don't lose their values after an algorithm restart (I'm thinking about live trading). I need to keep some state values that are vital for my strategy and I don't know if that's posible in quantopian.

For instance I buy 100 APPLE shares today, 50 tomorrow and 80 next day. I need to keep in someway the dates and number of shares of those positions because if I check the portfolio variable I only get that my portfolio has 230 APPLE shares but not when those positions where opened and other information. Is it posible to save that information and guarantee that it keeps those values even after a restart of the algorithm?

Jorge.

Seong & Jorge,

I'm wondering if the data could be manually downloaded from the broker, and then read into the algo upon re-start using fetcher?

Grant

Hi Grant

That could be an option but the point of automatic algorithmic trading is not to have to do anything manually...

Jorge

Hi Jorge,

What I want to know is how to save some variables that I need for my algorithm and be sure that those variables don't lose their values after an algorithm restart (I'm thinking about live trading)

For this, you should use "context" which saves and passes variable state between functions and algorithm restarts. Context is a Python dictionary used for holding state between methods and its properties can be accessed using dot notation. Any variable you have, you should create it in the initialize() function and then can manipulate in the handle_data or custom functions. In the very simple contrived example below, it will order 1 share of each stock that is in your universe. The universe contains Apple, Facebook, and Google.

def intialize(context):  
   context.my_stocks = symbols('aapl', 'fb', goog')

def handle_data(context,data):  
   for stock in data:  
       order(stock, 1)  

For instance I buy 100 APPLE shares today, 50 tomorrow and 80 next day. I need to keep in someway the dates and number of shares of those positions because if I check the portfolio variable I only get that my portfolio has 230 APPLE shares but not when those positions where opened and other information

You can use the get_order method to track the order ID for each position and its values. Here is example code that Grant wrote to track order fills: https://www.quantopian.com/posts/how-to-find-purchase-time-of-positions

If a live algorithm has a restart, it will return to the same state provided that "context" was used to save state. If you have an algorithm you'd like me to spot check, I can take a look at your code. You can post it here or if you want it to remain private, email us at [email protected]

Cheers,
Alisa

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.

Hi Alisa

Thank you very much for your answer. I thought that the context values were erased after the algorithm restarts. In that case in the initialize function i should check if the context values are already initialized and in that case not reinitialize them. Can you please confirm this is correct?

Thank you!

Jorge

Quite the opposite! :)

Context is used to save state, and if an algorithm restarts, the variables will return to the same position they were at when the algorithm stopped. To get a better idea of how to use context, take a look at this algorithm we wrote for live trading: https://www.quantopian.com/posts/rebalance-algo-9-sector-etfs

Ok!! Thank you very much!!

Jorge

One more point I thought I'd clarify - context is used to restore your algo if your algorithm gets disconnected from the broker, if we have an outage, or for any other technical reason.

If you manually stop a live trading algorithm by pressing the "Stop Algorithm" button, then that algorithm has completely stopped. If you restart it, we will not resume from the stopping point. It will be considered a new algorithm with a new track record, logs, metrics, and history.