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

Hi Guys

Me again

I'm trying my hand at this coding but like I say I'm quite new to this. I have a lot of background knowledge of trading, but new to coding.

I've been having error problems with some items saying they're not defined but as far as I can see my working out through the batch transforms has been able to define the items. I also get an error on line 22 for 'wk2=week2(data,context.stock)'. I don't know if I'm being a bit stupid here with the coding but I'd really appreciate it if anyone could take a look at this and see if I'm going wrong here.

I also have about 21 batch transforms because my algorithm requires different data samples for different times to develop the MACD Histogram and also the Elder-Ray. Is it okay to have this many batch transforms?

I would also like this to work across a wide range of instruments (sids) within the algorithm because the chances of this event coming up on one is few and far between. How many instruments can we place into this algorithm?

I would really appreciate it if someone could look through this and check out anything I've done wrong.

I sorry if I seem a bit wet behind the ears with this stuff.

Many Thanks

Steven

import numpy as np  
# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    context.stock=[sid(24), sid(36080), sid(36241)]  


# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that security.  
    # data.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.  
     ft = 2/(60+1)  
     sw = 2/(130+1)  
    wk2=week2(data,context.stock)  
    week1_fast = week1_fast(data,sid)  
    week1_slow = week1_slow(data,sid)

    wk2fema = week2*ft+week1_fast*(1-ft)  
    wk2sema = week2*sw+week1_slow*(1-sw)  
    wk2macd = wk2fema - wk2sema  
    wk3fema = week3(data,sid)*ft+wk2fema*(1-ft)  
    wk3sema = week3(data,sid)*sw+wk2sema*(1-sw)  
    wk3macd = wk3fema - wk3sema  
    wk4fema = week4(data,sid)*ft+wk3fema*(1-ft)  
    wk4sema = week4(data,sid)*sw+wk3sema*(1-sw)  
    wk4macd = wk4fema - wk4sema  
    wk5fema = week5(data,sid)*ft+wk4fema*(1-ft)  
    wk5sema = week5(data,sid)*sw+wk4sema*(1-sw)  
    wk5macd = wk5fema - wk5sema  
    wk6fema = week6(data,sid)*ft+wk5fema*(1-ft)  
    wk6sema = week6(data,sid)*sw+wk5sema*(1-sw)  
    wk6macd = wk6fema - wk6sema  
    wk7fema = week7(data,sid)*ft+wk6fema*(1-ft)  
    wk7fema = week7(data,sid)*sw+wk6sema*(1-sw)  
    wk7macd = wk7fema - wk7sema  
    wk8fema = week8(data,sid)*ft+wk7fema*(1-ft)  
    wk8sema = week8(data,sid)*sw+wk7sema*(1-sw)  
    wk8macd = wk8fema - wk8sema  
    wk9fema = week9(data,sid)*ft+wk8fema*(1-ft)  
    wk9sema = week9(data,sid)*sw+wk8sema*(1-sw)  
    wk9macd = wk9fema - wk9sema  
    wk10fema = week10(data,sid)*ft+wk9fema*(1-ft)  
    wk10sema = week10(data,sid)*sw+wk9sema*(1-sw)  
    wk10macd = wk10fema - wk10sema  
    wk11fema = week11(data,sid)*ft+wk10fema*(1-ft)  
    wk11sema = week11(data,sid)*sw+wk10sema*(1-sw)  
    wk11macd = wk11fema - wk11sema

    wk12fema = week12(data,sid)*ft+wk11fema*(1-ft)  
    wk12sema = week12(data,sid)*sw+wk11sema*(1-sw)  
    wk12macd = wk12fema - wk12sema  
    wk13fema = week13(data,sid)*ft+wk12fema*(1-ft)  
    wk13sema = week13(data,sid)*sw+wk12sema*(1-sw)  
    wk13macd = wk13fema - wk13sema  
     sma1sig=wk2macd+wk3macd+wk4macd+wk5macd+wk6macd+wk7macd+wk8macd+wk9macd+wk10macd/9  
    x = 2/(9-1)  
    sig1 = wk11macd*x+sma1sig*(1-x)  
    sig2 = wk12macd*x+sig1*(1-x)  
    sig3 = wk13macd*x+sig2*(1-x)  

    histwk1 = wk11macd - sig1  
    histwk2 = wk12macd - sig2  
    histwk3 = wk13macd - sig3  
     tt = 2/(13-1)  
    day2ema = dayb(data,sid)*tt+daya(data,sid)*(1-tt)  
    day3ema = dayc(data,sid)*tt+day2ema*(1-tt)  

    forceindex=get_force_index(data,context.sid)  
    bull1=day1max(data,sid) - day2ema  
    bull2=day2max(data,sid) - day3ema  
    bear1=day1min(data,sid) - day2ema  
    bear2=day2min(data,sid) - day3ema  
    for stock in context.sid:  
        price = data[sids].price  
        high = data[sids].high  
        low = data[sids].low  

    if forceindex<0 and histwk3>histwk2>histwk1 and bear1<bear2<0 and price>high:  
        order(context.sids,100)  
    if forceindex>0 and histwk3<histwk2<histwk3 and bear1>bear2>0 and price<low:  
        order(context.sids,-100)  
@batch_transform(window_length=2, refresh_period=1)  
def get_force_index(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    v = datapanel['volume'].as_matrix(sid)  
    delta_p = p[1,:]-p[0,:]  
    force_index = np.multiply(delta_p,v[1,:])  
    return force_index

@batch_transform(window_length=190, refresh_period=1)  
def week1_slow(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    slow = p[0,:]+p[1,:]+p[2,:]+p[3,:]+p[4,:]+p[5,:]+p[6,:]+p[7,:]+p[8,:]+p[9,:]+p[10,:]+p[11,:]+p[12,:]+p[13,:]+p[14,:]+p[15,:]+p[16,:]+p[17,:]+p[18,:]+p[19,:]+p[20,:]+p[21,:]+p[22,:]+p[23,:]+p[24,:]+p[25,:]+p[26,:]+p[27,:]+p[28,:]+p[29,:]+p[30,:]+p[31,:]+p[32,:]+p[33,:]+p[34,:]+p[35,:]+p[36,:]+p[37,:]+p[38,:]+p[39,:]+p[40,:]+p[41,:]+p[42,:]+p[43,:]+p[44,:]+p[45,:]+p[46,:]+p[46,:]+p[47,:]+p[48,:]+p[49,:]+p[50,:]+p[51,:]+p[52,:]+p[53,:]+p[54,:]+p[55,:]+p[56,:]+p[57,:]+p[58,:]+p[59,:]+p[60,:]+p[61,:]+p[62,:]+p[63,:]+p[64,:]+p[65,:]+p[66,:]+p[67,:]+p[68,:]+p[69,:]+p[70,:]+p[71,:]+p[72,:]+p[73,:]+p[74,:]+p[75,:]+p[76,:]+p[77,:]+p[78,:]+p[79,:]+p[80,:]+p[81,:]+p[82,:]+p[83,:]+p[84,:]+p[85,:]+p[86,:]+p[87,:]+p[88,:]+p[89,:]+p[90,:]+p[91,:]+p[92,:]+p[93,:]+p[94,:]+p[95,:]+p[96,:]+p[97,:]+p[98,:]+p[99,:]+p[100,:]+p[101,:]+p[102,:]+p[103,:]+p[104,:]+p[105,:]+p[106,:]+p[107,:]+p[108,:]+p[109,:]+p[110,:]+p[111,:]+p[112,:]+p[113,:]+p[114,:]+p[115,:]+p[116,:]+p[117,:]+p[118,:]+p[119,:]+p[120,:]+p[121,:]+p[122,:]+p[123,:]+p[124,:]+p[125,:]+p[126,:]+p[127,:]+p[128,:]+p[129,:]/130  
    return slow

@batch_transform(window_length=190, refresh_period=1)  
def week1_fast(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    fast = p[70,:]+p[71,:]+p[72,:]+p[73,:]+p[74,:]+p[75,:]+p[76,:]+p[77,:]+p[78,:]+p[79,:]+p[80,:]+p[81,:]+p[82,:]+p[83,:]+p[84,:]+p[85,:]+p[86,:]+p[87,:]+p[88,:]+p[89,:]+p[90,:]+p[91,:]+p[92,:]+p[93,:]+p[94,:]+p[95,:]+p[96,:]+p[97,:]+p[98,:]+p[99,:]+p[100,:]+p[101,:]+p[102,:]+p[103,:]+p[104,:]+p[105,:]+p[106,:]+p[107,:]+p[108,:]+p[109,:]+p[110,:]+p[111,:]+p[112,:]+p[113,:]+p[114,:]+p[115,:]+p[116,:]+p[117,:]+p[118,:]+p[119,:]+p[120,:]+p[121,:]+p[122,:]+p[123,:]+p[124,:]+p[125,:]+p[126,:]+p[127,:]+p[128,:]+p[129,:]/60  
    return fast

@batch_transform(window_length=190, refresh_period=1)  
def week2(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    a = p[130,:]+p[131,:]+p[132,:]+p[133,:]+p[134,:]/5  
    return a

@batch_transform(window_length=190, refresh_period=1)  
def week3(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    b = p[135,:]+p[136,:]+p[137,:]+p[138,:]+p[139,:]/5  
    return b  

@batch_transform(window_length=190, refresh_period=1)  
def week4(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    c = p[140,:]+p[141,:]+p[142,:]+p[143]+p[144,:]/5  
    return c  

@batch_transform(window_length=190, refresh_period=1)  
def week5(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    d = p[145,:]+p[146,:]+p[147,:]+p[148,:]+p[149,:]/5  
    return d


@batch_transform(window_length=190, refresh_period=1)  
def week6(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    e = p[150,:]+p[151,:]+p[152,:]+p[153,:]+p[154,:]/5  
    return e


@batch_transform(window_length=190, refresh_period=1)  
def week7(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    f = p[155,:]+p[156,:]+p[157,:]+p[158,:]+p[159]/5  
    return f

@batch_transform(window_length=190, refresh_period=1)  
def week8(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    g = p[160,:]+p[161,:]+p[162,:]+p[163,:]+p[164,:]/5  
    return g


@batch_transform(window_length=190, refresh_period=1)  
def week9(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    h = p[165,:]+p[166,:]+p[167,:]+p[168,:]+p[169,:]/5  
    return h


@batch_transform(window_length=190, refresh_period=1)  
def week10(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    i = p[170,:]+p[171,:]+p[172,:]+p[173,:]+p[174,:]/5  
    return i


@batch_transform(window_length=190, refresh_period=1)  
def week11(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    j = p[175,:]+p[176,:]+p[177,:]+p[178,:]+p[179,:]/5  
    return j


@batch_transform(window_length=190, refresh_period=1)  
def week12(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    k = p[180,:]+p[181,:]+p[182,:]+p[183,:]+p[184,:]/5  
    return k


@batch_transform(window_length=190, refresh_period=1)  
def week13(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    l = p[185,:]+p[186,:]+p[187,:]+p[188,:]+p[189,:]/5  
    return l


@batch_transform(window_length=15, refresh_period=1)  
def daya(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    bbsma = p[0,:]+p[1,:]+p[2,:]+p[3,:]+p[4,:]+p[5,:]+p[6,:]+p[7,:]+p[8,:]+p[9,:]+p[10,:]+p[11,:]+p[12,:]/13  
    return bbsma

@batch_transform(window_length=15, refresh_period=1)  
def dayb(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    bbpday2 = p[13,:]  
    return bbpday2

@batch_transform(window_length=15, refresh_period=1)  
def dayc(datapanel,sid):  
    p = datapanel['price'].as_matrix(sid)  
    bbpday3 = p[14,:]  
    return bbpday3


@batch_transform(window_length=2, refresh_period=1)  
def day1min(datapanel,sid):  
    l = datapanel['low'].as_matrix(sid)  
    mn1 = l[0,:]  
    return mn1

@batch_transform(window_length=2, refresh_period=1)  
def day2min(datapanel,sid):  
    l = datapanel['low'].as_matrix(sid)  
    mn2 = l[1,:]  
    return mn2

@batch_transform(window_length=2, refresh_period=1)  
def day1max(datapanel,sid):  
    h = datapanel['high'].as_matrix(sid)  
    mx1 = h[0,:]  
    return mx1

@batch_transform(window_length=2, refresh_period=1)  
def day2max(datapanel,sid):  
    h = datapanel['high'].as_matrix(sid)  
    mx2 = h[1,:]  
    return mx2  


5 responses

Hello Steven,

I can't offer much help but the line 22 error is due to an addditional space that is indenting the code too much. This occurs in a few places.

P.

Hi Steven,

I did a quick look at your code last night in the Quantopian editor. I agree with Peter that you need to look at the indents, since they matter in Python.

Also, I gathered that you may need to chose a different variable name from the function being called when making assignments such as:

week1_fast = week1_fast(data,sid)  

Regarding the number of sids, I believe it is 100 max. You can access more with the get_universe functionality, but you won't be able to specify them (see the help page for details).

Another thing to keep in mind is that when you call a batch transform, it will return None until the window is full (the data have accumulated). So, you need to check for None before attempting to do calculations.

It looks like you might be doing division with integers here:

ft = 2/(60+1)  
sw = 2/(130+1)  

I suggest checking that ft and sw are in fact type float and have the expected values. If not, you can be explicit:

ft = 2.0/float((60+1))  
sw = 2.0/float((130+1))  

Generally, there should be some way to re-factor your code so that you don't have so many functions (but perhaps this can wait until you get it running without errors).

Grant

Thanks a lot guys, I really appreciate this and will look into the this.

Thanks

Steven

Hi guys

I'm trying to see how to check for none with all the batch transforms and having trouble.

Is anyone able to explain to me how to do this with my code?

Thanks

Steven

Steven,

Here's one way:

    result = my_batch_transform(data, context.stocks)  
    if result is None:  
        return  

Grant