Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
sid calling dataframe index

I am trying to build a strategy which looks at last 2 day's return and trade the most winning/losing stocks.

I have trouble calling sid() with index of a dataframe

for example, if i want to trade the first stock in the dataframe below

p = history(bar_count=2, frequency='1d', field='price')  
order(sid(p.columns[0]), 1000)  

The system gives me the error:
Error The sid(id) method takes one parameter.

but p.columns[0] is an int, how come it dosn't work with sid?

Thanks!

9 responses

Hello Lei,

You could try this:

set_slippage(slippage.FixedSlippage(spread=0.00))

def initialize(context):  
    context.stocks = [sid(33652),sid(8554)]  
    context.printed = False  
def handle_data(context, data):  
    prices = history(5,'1d','price')  
    prices_reindexed = prices.reindex(columns=context.stocks)  
    if not context.printed:  
        print prices  
        print '---------------------------'  
        print prices_reindexed  
        order(context.stocks[0],100)  
        context.printed = True  

The history method returns a dataframe w/ columns sorted in ascending order, by sid id, but you can reindex as illustrated (clone the algo, run it, and have a look at the log output and transaction details).

Grant

Thanks, Grant.
I think my question is
why this is ok:

sid(33652)  

but this is not:

s = 33652  
sid(s)  

Hi Lei,

We do static analysis on the code to find securities and to add them to the data stream sent to your algo. Our static analyzer doesn't handle the second case, so we raise an exception to prompt you to revert to the former.

thanks,
fawce

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.

Thanks John.
I see how it works now.

What would you suggest to do if I want to sort a list of stock according to their historic returns and be able to pick the one with the greatest return?

Thanks

Hi Lei,

I would get a window of data using the history function. That will return a dataframe with sid columns and a datetimeindex for the rows. You can then calculate returns for all sids and sort. You could hand code a specific set of sids, all of which will be in the dataframe from history, or you could use set_universe to choose a segment of the market based on dollar volume traded.

thanks,
fawce

Hi John,

I see.

Suppose I get the historic price and get the one stock with the greatest return. I can only access it with

stock_max_return = sorted_history_returns.columns[0]  

I cannot trade this because this won't work:
order(sid(stock_max_return),100)

I can think of some ways to find the stock corresponds to stock_max_return, just want to know if there is any efficient way to do so?

Thank you
LY

Hello LY,

I don't know if this works. Probably not!

P.

Hello Lei,

You could use something like this:

def initialize(context):  
    context.stocks = [sid(8554),sid(33652)]  
    context.stocks_dict = {}  
    for stock in context.stocks:  
        context.stocks_dict[stock.sid] = stock

def handle_data(context, data):  
    s = 8554

    order(context.stocks_dict[s],100)  

Make sense?

Grant

Thanks Peter and Grant. I got it!