Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to make static daily data

Hi,

I trade on multiple periods of time and want to set up various daily static calculations during the initialisation function of Quantopian.

As I'm quite a novice python programmer, I'm having a little difficulty and would like your assistance.

Here's my code:

import math  
import talib as ta  
import pandas as pd  
import numpy as np

def initialize(context):  
    context.aapl = sid(24)  
    context.max_notional = 100000.1  
    context.min_notional = 0

    historical_closes = history(bar_count=365, frequency='1d', field='close_price')  
    context.daily = historical_closes[context.aapl]    ######   line 14  
    context.sma50_daily = ta.SMA(context.daily, timeperiod=50)  
    context.sma200_daily = ta.SMA(context.daily, timeperiod=200)  
    macd_daily = ta.MACD(context.daily, fastperiod=12, slowperiod=26, signalperiod=9)  
    context.macd_daily = macd_daily  
    context.macd_ema30_daily = ta.EMA(macd_daily, timeperiod=30)  
    weekly = historical_closes  
    context.weekly = weekly.resample('1W')  
    context.sma50_weekly = ta.SMA(np.array(context.weekly), timeperiod=50)  
    context.sma200_weekly = ta.SMA(np.array(context.weekly), timeperiod=200)  
    macd_weekly = ta.MACD(np.array(context.weekly), fastperiod=12, slowperiod=26, signalperiod=9)  
    context.macd_weekly = macd_weekly  
    context.macd_ema30_weekly = ta.EMA(np.array(macd_weekly), timeperiod=30)  

But I'm getting this error in the IDE:
14 Error Runtime exception: KeyError: u'no item named Security(24 [AAPL])'

What am I doing wrong and how should it be done please?

Thanks in advance for your help.
Andrew

5 responses

Hi, I think i got the daily plots working, but weekly is not correct. I am not an expert so elders please weigh in since id also like to know how to work with weekly and daily data

Thanks for this Darell.

Doesn't the [-1] at the end of the statement mean it takes the last item of an array? (Sorry I've limited python but know other languaes). This isn't quite what I wanted as I need to reference the whole array in the handle_data function. Any ideas anyone?

You've put your code in the handles_data function which executes every minute. I only need this code to execute once per day hence the reason I put it in the initialise section. Will this work or do I need to do excessive history calls every minute which doesn't feel right at all!

Thanks again,
Andrew

I got this running, but I am not sure what your goal was. You are taking the sma and ema of the macd from the previous calculations. talib.MACD returns 3 arrays in a tuple, so that was giving you problems. You need to use the history function in handle_data, not initialize as well, that is why you were getting the key error. This should give you a jump off point to get going again, share the algo again if you have any issues. Remember that initialize is only called once at the beginning of the simulation, and all the trading logic takes place in handle_data.

David

Thanks for your reply David, I'll have a look at it when I get back from holiday in a few weeks.

Thanks again.
Andrew

Thanks David