Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to analyze historical prices?

Hello,

I just have joined Quantopian, greetings to all :)

I need to create a strategy but I also need to study historical prices of many US stocks to understand which stocks to use in backtests.
This is what i need to do:

  1. I need to retrive stocks with a specific market_cap (around 80 stocks)
  2. Get historical prices of those (three years)
  3. Filter them with my strategy rules

Then I will have the stocks that i can use for backtesting.

So, the first step is to find good stocks that fit my needs, and the second create a strategy to trade those stocks.

Can I do it with Quantopian? or do i have to filter them with another tool (or locally) and then create the strategy?

Thank you!

7 responses

Welcome Damiano,

You can retrieve stocks within a dollar-volume range (see https://www.quantopian.com/help#ide-universe and https://www.quantopian.com/help#sample-setuniverse), but you cannot filter on market cap. So, you could create the list manually by typing it into the code editor.

Once you define the list of stocks, you will have access to daily or minutely OHLCV data.

Regarding filtering, the Quantopian backtester allows access to lots of Python modules (see https://www.quantopian.com/help#ide-module-import and https://www.quantopian.com/faq), so you should be all set.

So, yes, I expect you can do it with Quantopian.

Grant

Hello Grant!
Thank you so much!

As i wrote i need to create two algorithms, first to search good stocks, second to trade them. Reading the documentation i notice that quantopian's system will call few functions automatically (like initialize() and handle_data() so now the question is. I do not need to test the strategy, i only need to retrieve historical data and do some tests. Can i write this script without using handle_data() function? I do not need it i think..no?

You'll get the error "Algorithm must implement handle_data(context, data) method." For example, try the code below. However, you should still be able to write your analysis script with handle_data() in your code. I suggest developing a trivial stand-in for your analysis code so you can post it publicly (assuming you don't want to share your actual analysis).

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    context.stock = sid(24)

# # Will be called on every trade event for the securities you specify.  
# def handle_data(context, data):  
    # # Implement your algorithm logic here.  
#  
    # # data[sid(X)] holds the trade event data for that security.  
    # # context.portfolio holds the current portfolio state.  
#  
    # # Place orders with the order(SID, amount) method.  
#  
    # # TODO: implement your own logic here.  
    # order(sid(24), 50)  

Grant, ok... I am doing some tests.

I also thought that I can "build algorithm" and set the date interval of backtest to one day, so my handle_data() will be called one time. What do you think?

Then... in the handle_data() I will retrieve the history of all the stocks of universe I set in initialize() and history() will give me OHLCV data, right?

Thank you!

Yes, you might be able to just run the algo over one day. Sounds correct. --Grant

Grant, thank you so much for your fast replies!
I will study the first algo. I hope is there no timeout error for long running algo?

Damiano,

You might run into a timeout or memory limitations if you are thinking of grabbing years of data for a large number of securities and analyzing it all at once. Give it a try, but you might need to sort out how to do your analysis on a rolling basis, which is the natural mode of operation for the Quantopian backtester.

Grant