Karl,
Thanks for flattery.
For that task, I actually prefer this style:
from quantopian.algorithm import order_optimal_portfolio
import quantopian.optimize as opt
# ----------------------------------------------------------------------------------------------------------------
ASSETS, MA_F, MA_S, LEV = symbols('XLV','XLK','XLI','XLP','XLE','XLU','XLB','XLY','XLF','TLT','IEF'), 10, 100, 1.0
# ----------------------------------------------------------------------------------------------------------------
def initialize(context):
schedule_function(trade, date_rules.month_end(), time_rules.market_close(minutes = 30))
def trade(context, data):
Ratio = data.history(ASSETS,'price',MA_F,'1d').mean() / data.history(ASSETS,'price',MA_S,'1d').mean()
securities = Ratio[Ratio > 1.0].index
weights = {}
for sec in ASSETS:
weights[sec] = LEV/len(securities) if sec in securities else 0
order_optimal_portfolio(opt.TargetWeights(weights), [opt.MaxGrossExposure(LEV)])
record(leverage = context.account.leverage)
But it's not popular here lately.