Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Add a value to a list everyday

Hi everyone,

From Price_low = history(100, '1d', 'low'),

I grab the lowest low price with lowest = prices_low.idxmin()

Now, I would like to use this lowest data for tomorrow. And tomorrow my code will be calculating as well the new ''lowest'' data. And so on.

How to create the list in handle_data and put a data in the list without seeing this list go empty at every beginning of the day? I f I create my empty list outside of handle_data, it tells me it doesn't exist.

def initialize(context):
#set_universe(universe.DollarVolumeUniverse(94.0, 99.0))
context.security = symbol('AAPL')

def handle_data(context, data):
low_list=[]
Price_low = history(100, '1d', 'low')
lowest = prices_low.idxmin()
low_list.append(lowest)

I know the way I try to put my lowest data in the list is not right and, tomorrow, when the handle data will run again, the low_list will go blank to add only one value that will be deleted the next day....

Help please !

3 responses

Any help ? I can't figure out how to build a list overtime by putting calculated value in it everyday if at the beginning, I have to create the empty list used for the first day low_list=[]. Every future day, the handle date will start by resetting to NIL my list..... I know it has a simple twist to overcome this problem...

Your help is appreciated

Hi Pierre,
You want to create your low_list in initialize, so that it's only created once. You also want to create it as a context variable, so that you can use it from day to day and it will retain it's state.

You code becomes,

def initialize(context):  
#set_universe(universe.DollarVolumeUniverse(94.0, 99.0))  
context.security = symbol('AAPL')  
context.low_list=[]


def handle_data(context, data):  
Price_low = history(100, '1d', 'low')  
lowest = prices_low.idxmin()  
context.low_list.append(lowest)  

Depending on what you want to do with this, you might also consider using the Pipeline API. With Pipeline, you can rank and sort stocks to define your own custom tradable universe of securities. In Pipeline, you have access to historical pricing (including yesterday's low) and fundamental data. There are also some sample algos here.

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.

Great !! I tried to put the list in the initialize but didn't work as I did not put the ''context.'' before.

Thanks for the tips and your time. Much appreciated. !!!