On Line 52 I'm getting " Runtime exception: TypeError: 'type' object has no attribute '__getitem__' "
and I have no Idea why.
import talib
def initialize(context):
set_symbol_lookup_date('2013-01-14')
context.stocks = symbols('aapl', 'amzn', 'armh', 'atvi', 'csco', 'crm', 'ebay', 'ea', 'fb', 'hpq', 'ibm', 'intc', 'lnkd', 'msft', 'nflx', 'nvda', 'orcl', 'pcln', 'qcom', 'bbry', 'sap', 't', 'symc', 'vz', 'yhoo')
set_benchmark(symbol('qqq'))
set_commission(commission.PerTrade(cost=4.75))
context.date = None
context.freeCash = 100000
context.startCount = 25
context.stockCount = 0
context.bottomed = None
context.rising = None
context.peaked = None
context.falling = None
dict = {'AAPL': 'falling', 'AMZN': 'falling', 'ARMH': 'falling', 'ATVI': 'falling', 'BBRY': 'falling', 'CSCO': 'falling', 'CRM': 'falling', 'EBAY': 'falling', 'EA': 'falling', 'FB': 'falling', 'HPQ': 'falling', 'IBM': 'falling', 'INTC': 'falling', 'LNKD': 'falling', 'MSFT': 'falling', 'NFLX': 'falling', 'NVDA': 'falling', 'ORCL': 'falling', 'PCLN': 'falling', 'QCOM': 'falling', 'SAP': 'falling', 'T': 'falling', 'SYMC': 'falling', 'VZ': 'falling', 'YHOO': 'falling'}
def handle_data(context, data): #Line 20
todays_date = get_datetime().date()
if todays_date==context.date:
return
for stock in context.stocks:
hist100 = history(bar_count=100, frequency='1d', field='price')
series100 = hist100[stock]
ema_result100 = talib.EMA(series100, timeperiod=100)
ema_result20 = talib.EMA(series100, timeperiod=20)
ema_result5 = talib.EMA(series100, timeperiod=5)
ema100 = ema_result100[-1]
ema20 = ema_result20[-1]
ema5 = ema_result5[-1]
lowerLimit = 0
upperLimit = 0
getCash(context,data) #Line40
if ema20 > ema100:
upperLimit = ema20
lowerLimit = ema100
else:
upperLimit = ema100
lowerLimit = ema20
if ema5 < lowerLimit and dict[str(stock.symbol)] == 'falling':
dict[str(stock.symbol)] = 'bottomed'
if ema5 > lowerLimit and ema5 < upperLimit and dict[str(stock.symbol)] == 'bottomed': #Line 52
dict[str(stock.symbol)] = 'rising'
order_value(stock, (context.freeCash/(context.stockCount-context.startCount)), style=LimitOrder(ema5*1.03))
context.stockCount += 1
if ema5 > upperLimit and dict[str(stock.symbol)] == 'rising':
dict[str(stock.symbol)] = 'peaked'
if ema5 > lowerLimit and ema5 < upperLimit and dict[str(stock.symbol)] == 'peaked': #Line 52
dict[str(stock.symbol)] = 'falling'
order_target(stock, 0, style=LimitOrder(ema5*(-1.03)))
context.stockCount += (-1)
getCash(context,data)
if context.portfolio.positions[stock].amount < 0:
context.shorts+=1
getCash(context,data)
record(Cash=context.freeCash)
record(Shorts=context.shorts)
context.shorts=0
def getCash(context,data):
if context.portfolio.cash < (context.portfolio.portfolio_value-context.portfolio.positions_value):
context.freeCash = context.portfolio.cash-5
else:
context.freeCash = (context.portfolio.portfolio_value-context.portfolio.positions_value)-5