Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get biggest movers from N days ago?

Hi All,

I'd like to get the top 25 % gainers from N days ago.
How could i go about doing this in quantopian or zipline?

Thank you!

6 responses

Hello AimFor,

Here's an example using Pandas. Since there are only 4 securities, I end up selecting the top 50%. There's more coding to be done, but this will depend on what you need to do. Hopefully, this will get you started.

Grant


def initialize(context):  
    context.stocks = [symbol('SPY'),symbol('QQQ'),symbol('DIA'),symbol('IWM')]  
    schedule_function(pct_change,date_rules.every_day(),time_rules.market_open())  
def handle_data(context, data):  
    pass

def pct_change(context,data):  
    prices = history(31,'1d','price')[0:-1] # Pandas DataFrame of prior daily closing prices  
    prices_pct_change = prices.pct_change(periods=5).tail(1) # current changes  
    print prices_pct_change  
    rank = prices_pct_change.rank(axis=1) # rank changes  
    print rank  
    print rank[rank>2].dropna(axis=1) # select top 2 changes  

this is awesome! thank you!! i'm trying it on zipline, but there doesnt seem to exist a "schedule_function"?

any thought / workarounds?

Not sure about the schedule_function on zipline. It's actually in there (see https://github.com/quantopian/zipline/blob/master/zipline/algorithm.py), but perhaps usage differs from the online backtester. There's actually a separate discussion group for zipline-specific questions:

[email protected]
https://groups.google.com/forum/#!forum/zipline

You might post there.

Regarding a workaround, you can write your own code (e.g. if (a certain time) then (do something)), which was what was done before schedule_function was released.

Grant

yes strange, i installed it 2 days ago with "conda install -c Quantopian zipline"
but in the local copy of the file i have that function is missing. must be some version change.
I might just use github and pull a local copy and try to use that instead.

Hi Grant,

Thanks for posting this.I'm new to Quantopian. This is interesting. I want to implement a simple rebalancing strategy based on this. Suppose I have a list of stocks and I want to find the top Nth based on returns percentage calculated on weekly/biweekly basis. Then buy a basket of top Nth. Then rebalance biweekly. If you can help me get started on that then much appreciated. I will add more parameters as I see fit.

:)

Hello Peter,

Sorry, I don't have time to write a full example algorithm. And in the long run, it'll pay off if you dig in and try to sort things out for yourself. That said, once you get an algo up an running, just post it for assistance, if you get stuck. You might start with simply re-allocating weekly/biweekly according to a fixed percentage for each security. Then you can build it up from there.

Also, if you have the time, I'd recommend reading through the help/API/FAQ docs in their entirety (I did this awhile back and need to do it again, since a lot has been added).

Grant