Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Sample algo for weekly picks of 3-4 symbols based on ranking/sort of 15 symbols

I have a simple strategy that picks 3-4 symbols every week based on a sorted ranking of around 12-15 symbols, that works well, but I'm a newbie to Python. Is there a sample algo i can use and tweak? I struggled with the ranking/sorting function.

Urgent, as i'm trying to enter the contest before March 2nd (so no time to learn Python and write the algo from scratch).

thanks
Kiran

7 responses

Here's a tip that might help.
For quickly running through many Quantopian threads, try a search like this
and drag links from one browser window to another,
especially great if you happen to have two or more monitors:

This search limits all of the results to quantopian.com, it is:
ranking site:quantopian.com

https://www.google.com/search?q=ranking+site%3Aquantopian.com

Kiran,

Here's a sorting example, using Pandas. The log output is:

2015-02-26PRINT                       2015-02-26 14:31:00+00:00  
Security(19654 [XLB])                     51.930  
Security(19655 [XLE])                     80.230  
Security(19656 [XLF])                     24.469  
Security(19657 [XLI])                     57.960  
Security(19658 [XLK])                     43.070  
Security(19659 [XLP])                     49.610  
Security(19660 [XLU])                     45.690  
Security(19661 [XLV])                     72.390  
Security(19662 [XLY])                     76.070  
2015-02-26PRINT                       2015-02-26 14:31:00+00:00  
Security(19656 [XLF])                     24.469  
Security(19658 [XLK])                     43.070  
Security(19660 [XLU])                     45.690  
Security(19659 [XLP])                     49.610  
Security(19654 [XLB])                     51.930  
Security(19657 [XLI])                     57.960  
Security(19661 [XLV])                     72.390  
Security(19662 [XLY])                     76.070  
Security(19655 [XLE])                     80.230  

So it is working. Maybe you can adapt it to your needs. Good luck in the contest!

Grant

import pandas as pd

def initialize(context):  
    context.stocks =  [sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660)]  # XLU Utilities SPRD Fund

def handle_data(context, data):  
    prices = history(5,'1m','low')  
    prices_last = prices.tail(1).transpose()  
    print prices_last  
    print prices_last.sort(columns=prices_last.columns[0])  

So Grant and everyone else, I think there's probably an opportunity here to supplement the Q's inadequate forum/help mechanism. Let me explain...

prices.tail(1).transpose()  

"What does that do and why did he use it? Not to mention that the sorting mechanism at the last line..."

prices_last.sort(columns=prices_last.columns[0])  

Ya schoore, ve cann search de vast and poorly constructed forum archives using goooogle, Gary's right there. But what a pain.

Why couldn't there a wiki of handy algos, code blocks, snippets, api ref examples -- all quant curated and maintained? Or some better repository for all the bits and pieces any (and every) quant would need to both get started and to advance their cause? I've ended up keeping this ugly unmanageable stack of private algos with names like "LANGUAGE REF: ..." or "API REF: ..." and when I need a sorting algo or an RSI algo, or a fundamental data retrieval block I hunt through my expanding and nearly useless stack of algos for the tool "I know I wrote some time in the past..." This is nonsense!

Maybe building one's own repo locally, some massive python project or perhaps a github repo might work a bit better. But still, why isn't there a better way to store and find useful code blocks in some library of some sort? How many new quants will come in ask the same redundant question, over and over, before some better system is developed?

So that's the opportunity here. The Q's code maintenance system is sorely inadequate. At this time it's an open question but a question that the Q is not going to solve.

Thoughts anyone?

[Update: list of possible solutions (will build as I find them)]

https://gist.github.com/search?q=Quantopian

Hi Market Tech,

I agree that the forum could use better organization, and some sort of code archive (both shared and private) would be nice. Regarding the code I posted above, if you or others are stuck after doing some googling, just let me know. There's no custom code, so I figure Pandas docs and examples should cover it, but if not, I'm game to add comments.

Grant

Grant, I'm sure there must be some solution out there that comes pre-built and can just be used. Gist.github.com is also inadequate. One would have to dedicate specific words as search keys -- not gonna happen.

Per your code, yeah, I had to go dig up what transpose did to understand what the output looked like with/without. But it's snippets like this that would be really useful if we all knew where to look to find them (and no, searching site:quantopian.com "transpose" does not cut it in my book. Yeah it kinda works but there's not metadata around it).

Hi Grant,
After you sorted the list, how would you place an order in the top (or bottom) four stocks? Thanks a ton,

Calvin

Calvin,

It's kinda quick and dirty and nonsensical, but hopefully it helps. Basically, you just need to get the list of indices, and then use it to place orders.

Note that you don't need the 'import pandas as pd' as it was a carry-over from some earlier tinkering.

Also note that Scott Sanderson did a tutorial on Pandas: https://www.quantopian.com/posts/working-with-history-dataframes.

Grant

import pandas as pd

def initialize(context):  
    context.stocks =  [sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660)]  # XLU Utilities SPRD Fund

def handle_data(context, data):  
    prices = history(5,'1m','low')  
    prices_last = prices.tail(1).transpose()  
    print prices_last  
    prices_last_sorted = prices_last.sort(columns=prices_last.columns[0])  
    print prices_last_sorted  
    sids_sorted = prices_last_sorted.index  
    print sids_sorted  
    for stock in sids_sorted[0:3]:  
        order_target_percent(stock,1.0/3)