So, Nathan, why won't a dynamic property attach to the Expando Object below?
Mark, see code below, this appears to work in QII.
Well, aside from all the Q-broke-the-Web messages.
Criminy Q, give me a "QVersion = 1.0" option in my algos so I don't have to experience your adolescent growing pains.
I don't have time to let this trickle the excruciatingly slow backtest, so I post the code here.
import talib
import math
HMAPeriods = 11
HMAPeriodsb = 21
PricePeriods = 5
def initialize(context):
symbols('SPY', 'DIA', 'QQQ')
schedule_function(func=InitFramework, date_rule=date_rules.every_day())
schedule_function(func=CalculateHMA, date_rule=date_rules.every_day())
schedule_function(func=CalculateHMAb, date_rule=date_rules.every_day())
schedule_function(func=HandleEntry, date_rule=date_rules.every_day())
context.Indicators = {}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def handle_data(context, data):
plotStock = data.keys()[0]
record(Leverage = context.account.leverage)
record(Close = data[plotStock].close_price)
if (not plotStock in context.Indicators):
return
indicators = context.Indicators[plotStock]
if (not indicators is None):
record(HMA = indicators.HMA)
record(HMAbUp = indicators.HMAb if indicators.HMAbIsUp else None)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def InitFramework(context, data):
for stock in data:
if (stock not in context.Indicators):
context.Indicators[stock] = Expando()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def HandleEntry(context, data):
eligible = []
ineligible = []
for stock in data:
indicators = context.Indicators[stock]
if (indicators is None):
continue
if (indicators.HMAb > indicators.HMA):
eligible.append(stock)
else:
ineligible.append(stock)
eligibleCount = float(len(eligible))
for stock in eligible:
order_target_percent(stock, 1.0 / eligibleCount)
if (context.portfolio.positions[stock].amount == 0.0):
print(" Entry Long {0:<5} @ {1:>6.2f}".format(
stock.symbol, data[stock].close_price))
for stock in ineligible:
order_target_percent(stock, 0.0)
if (context.portfolio.positions[stock].amount != 0.0):
print(" Exit Long {0:<5} @ {1:>6.2f}".format(
stock.symbol, data[stock].close_price))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def CalculateHMA(context, data):
closes = history(HMAPeriods * 2, "1d", "close_price")
closes = closes.dropna(axis=1)
valid = [sid for sid in closes if sid in data]
closes = closes[valid]
wmaA = closes.apply(talib.MA, timeperiod = HMAPeriods / 2, matype = MAType.WMA).dropna() * 2.0
wmaB = closes.apply(talib.MA, timeperiod = HMAPeriods, matype = MAType.WMA).dropna()
wmaDiffs = wmaA - wmaB
hma = wmaDiffs.apply(talib.MA, timeperiod = math.sqrt(HMAPeriods), matype = MAType.WMA)
try:
for stock in data:
context.Indicators[stock].HMA = hma[stock][-1]
context.Indicators[stock].HMAIsUp = hma[stock][-1] > hma[stock][-2]
except:
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def CalculateHMAb(context, data):
closes = history(HMAPeriodsb * 2, "1d", "close_price")
closes = closes.dropna(axis=1)
valid = [sid for sid in closes if sid in data]
closes = closes[valid]
wmaA = closes.apply(talib.MA, timeperiod = HMAPeriodsb / 2, matype = MAType.WMA).dropna() * 2.0
wmaB = closes.apply(talib.MA, timeperiod = HMAPeriodsb, matype = MAType.WMA).dropna()
wmaDiffs = wmaA - wmaB
hma = wmaDiffs.apply(talib.MA, timeperiod = math.sqrt(HMAPeriodsb), matype = MAType.WMA)
try:
for stock in data:
context.Indicators[stock].HMAb = hma[stock][-1]
context.Indicators[stock].HMAbIsUp = hma[stock][-1] > hma[stock][-2]
except:
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class MAType():
SMA = 0; EMA = 1; WMA = 2; DEMA = 3; TEMA = 4;
TRIMA = 5; KAMA = 6; MAMA = 7; T3 = 8
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Expando(object):
def __init__(self):
self.HMA = None
self.HMAIsUp = False
self.HMAb = None
self.HMAbIsUp = False