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.