Hi guys,
I am trying to apply the Bollinger Bands feature of the TA Lib for each security within a set universe, but am not sure how to extract the upper band, lower band, or moving average. I read the API documentation but am getting runtime errors when I try to extract the 0th, 1st, or 2nd element from the tuple. Can someone please help me with my syntax?
I tried to mimic the RSI example on the forums using TA Lib but I had no luck.
Best,
Joe
import pandas
import numpy as np
import math
bband = ta.BBANDS(timeperiod=10,nbdevup=2,nbdevdn=2,matype=0)
def initialize(context):
set_universe(universe.DollarVolumeUniverse(99.5, 100.0))
context.SPY=sid(8554)
#Sets notional post trade limits
context.max_notional = 1000000.0
context.min_notional = -1000000.0
def handle_data(context, data):
for sid in data:
bband_data=bband(data)
price = data[sid].price
if not np.isnan(bband[data[sid]][0]):
if (price <= bband_data[data[sid]][2]):
order(data[sid], 100)
log.info("bought 100 shares of " + str(data[sid]) + " at " + str(price) + " and mavg was " + str(bband_data[data[sid]][1]) + ".")
#If currently long then we sell
#This avoids shorting
elif (price >= bband_data[data[sid]][0]):
order(data[sid], -100)
log.info("sold 100 shares of " + str(context.sid_names[sid]) + " at " + str(price) + " and mavg was " + str(ta.BBANDS(10,2,2))[1]))
# record(upperbband=bband_data[data[sid]][0],lowerbband=bband_data[data[sid]][2])