Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Array of 11k Stock Picks with symbol, time and idea

I read a report on the alpha generated by users of SumZero (http://blogs.cfainstitute.org/investor/2012/06/25/do-good-investment-managers-give-away-great-ideas-its-entirely-rational-according-to-new-study/) and it got me thinking about another community. I scraped the 11,000 stock picks, with time data, symbol and idea raw text.

I have an array of time data and an array of symbols, how can I have historical trades made based on time data alon and can I see all the backtested info with this?

3 responses

Hi Simon,

That's an awesome idea. There are a few ways to go about doing this, the way that I would normally do is to load in my data through fetch_csv, define in the pre_func parameter a method that simply copies the date column as another name like "date_copy", set my universe of stocks to be the stocks found in the CSV (using fetcher's set_universe) function, and finally in handle_data(context, data) check whether or not the current date (get_datetime()) matches the date_column of the CSV I pulled in and trade if it does match.

Here are a few code snippets that I think will help you get in the right direction.

def pre_func(df):  
    #: We're going to make a copy of the date column to make sure we know when to trade at the appropriate date  
    #: This is where we make a copy of the date column  
    df['date_copy'] = df['date']  
    return df

def initialize(context):  
    #: This preserves the extra date_column that we'll need for each ticker  
    fetch_csv(url,  
              pre_func=pre_func,  
              symbol_column='ticker')  
    #: This simply loads our stocks into our universe  
    fetch_csv(  
              url,  
              pre_func=pre_func,  
              symbol_column='ticker',  
              universe_func=my_universe)  

and later in handle_data I use this check before trading a stock:

if  'date_column' in data[stock]:  
    date = data[stock]['date_copy']  
    #: Convert date to datetime object using something like  
    date = datetime.strptime(date, date_format)  
    if date.month == get_datetime().month and date.day == get_datetime().day and date.year = get_datetime().year:  
          #: Order here  
          order_me

If this is working properly, you should be able to see the trade data for each of those securities in the backtesting results window (https://www.quantopian.com/help#backtests-results)

I realize this is a little vague, but try starting out with these bits of code and let's keep this thread going with more 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.

Hey Seong,

This is awesome, thanks so much!

Just reading the docs about setting get_datetime() to the right timezone. Do I need to do this?

SB

By default, get_datetime() will return UTC time. You can pass an optional timezone parameter to change this. For example, if you do get_datetime('US/Eastern') this will give you all the timestamps in US eastern time.

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.