Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Indexing and selecting stock data

I just started with quantopian and I can wrap my head around indexing stock prices. I know how indexing works, [0] = beginning/start while [-1] would reference the last piece of data in the list/df or what ever

please someone shed some light on this, my brain if fried

In this block of code the current stock price is referenced as [-1] and the previous day is as [-2]

def initialize(context):  
    # AAPL, MSFT, and SPY  
    context.securities = [sid(24), sid(5061), sid(8554)]

def handle_data(context, data):  
    price_history = data.history(context.securities, "price", bar_count=2, frequency="1d")  
    for s in context.securities:  
        prev_bar = price_history[s][-2]  
        curr_bar = price_history[s][-1]  
        if curr_bar > prev_bar:  
            order(s, 20)  

In this block of code todays New Price is referenced as [-1] while the Old price is referenced as [0]

def initialize(context):  
    # AAPL, MSFT, and SPY  
    context.securities = [sid(24), sid(5061), sid(8554)]

def handle_data(context, data):  
    prices = data.history(context.securities, "price", bar_count=10, frequency="1d")  
    pct_change = (prices.ix[-1] - prices.ix[0]) / prices.ix[0]  
    log.info(pct_change)

then again using the iloc, where I would guess the New Price is [0] and the Old Price is [-1]

def initialize(context):  
    # AAPL, MSFT, and SPY  
    context.securities = [sid(24), sid(5061), sid(8554)]

def handle_data(context, data):  
    price_history = data.history(context.securities, "price", bar_count=10, frequency="1d")  
    pct_change = price_history.iloc[[0, -1]].pct_change()  
    log.info(pct_change)  
2 responses

Hi Luka,

The last example you pasted is calculating the percent_change between the most recent price [-1] and the oldest price [0]. More specifically, iloc[[0,-1]] is getting you the [0] and [-1] rows from the price_history dataframe.

Does this make sense?

Jamie

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.

Thank you for your reply Jamie,

It was very helpful even though I got stuck on a simple detail :)

It was a bit confusing to understand because of the bar_count Im calling

the bar_count = 10 would correspond to this dataframe and its indexes

                          Open        High         Low       Close      Volume  
Date  
[0] or [-10] 2016-11-08  110.309998  111.720001  109.699997  111.059998  24054500.0   
[1] or [-9]  2016-11-09  109.879997  111.320000  108.050003  110.879997  58430900.0   
[2] or [-8]  2016-11-10  111.089996  111.089996  105.830002  107.790001  57123700.0   
[3] or [-7]  2016-11-11  107.120003  108.870003  106.550003  108.430000  34094100.0   
[4] or [-6]  2016-11-14  107.709999  107.809998  104.080002  105.709999  50872500.0   
[5] or [-5]  2016-11-15  106.570000  107.680000  106.160004  107.110001  32216900.0   
[6] or [-4]  2016-11-16  106.699997  110.230003  106.599998  109.989998  58702500.0   
[7] or [-3]  2016-11-17  109.809998  110.349998  108.830002  109.949997  26964600.0   
[8] or [-2]  2016-11-18  109.720001  110.540001  109.660004  110.059998  27404300.0   
[9] or [-1]  2016-11-21  110.120003  111.989998  110.010002  111.730003  29119100.0   

Thank you again