Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to reference calculation from "n" days ago
def order_logic(context, data):  
    high = history(60, "1d", "high")  
    low = history(60, "1d", "low")

    rolling52_max = pandas.stats.moments.rolling_max(high,52)  
    rolling52_min = pandas.stats.moments.rolling_min(low,52  
    period52_high = rolling52_max[context.security][-2]  
    period52_low = rolling52_min[context.security][-2]

    kumo_b = ((period52_high + period52_low) / 2)  

Let's say I need kumo_b from 26 days ago, how do I reference it. It will be very helpful

6 responses

Please, I need an answer, my question is not that difficult for expert programmer like your guys, come on!!

That's a deprecated history() function. It's now data.history

So it depends on what's there. Some possibilities:
kumo_b.loc[-26]
kumo_b.iloc[-26]
kumo_b.T[-26] (that's transpose)
kumo_b.T.iloc[-26]

Click the line number next for that kumo_b = line.
Run it.
When the debugger pauses on that line, type kumo_b and hit enter to see what's there first and go from there trying some things.

When you paste code in a post, highlight it and use the code icon, that'll wrap it with three backticks and a blank line above and below preserving indentations for clarity. You can edit the message now and do that.

Thank you for your willingness to help, Blue Seahawk.

Although It still doesn't work, "attributeError" is different for each try. Here is a summary of the error:

.loc : numpy.float64' object has no attribute 'loc
.iloc : numpy.float64' object has no attribute 'iloc

.T : invalid index to scalar variable

.T.loc : numpy.float64' object has no attribute 'loc

.T.iloc : numpy.float64' object has no attribute 'iloc

If you can post a backtest without giving away anything precious, someone will be able to help. Run a full backtest and then it will show up under Attach.

@Christian,

Hope this will help:

# -------------------------------------------  
stock, period, offset = symbol('SPY'), 52, 26  
# -------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65))

def trade(context, data):  
    bars = period + offset 

    H = data.history(stock, 'high', bars, '1d')  
    L = data.history(stock, 'low', bars, '1d')

    kumo_b = (H.rolling(period, center = False).max() + L.rolling(period, center = False).min())/2  
    kumo_bo = kumo_b.shift(offset)

    record(kumo_b = kumo_b[-1], kumo_bo = kumo_bo[-1])  

Hi Vlad,

First thank you very much, you're a genius !