Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
TAA implementation with optimization api

I am trying to implement TAA strategy , with optimization api, I am not sure the weight calculation in the rebalance method is doing the correct calculation job.

Questions:

1.How do I sell a equity if that equity doesn't show up in the pipeline output for that month, does the optimize api automatically take cares of it or I have explicitly give a negative weight for absent equity ?
2. Does the MaxGrossExposure call even out the equity weights for rest of the equities if some of the equities are absent.

3 responses

Boney,

I hope this very similar algo helps you.

from quantopian.algorithm import (attach_pipeline, pipeline_output, order_optimal_portfolio,)  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
import quantopian.pipeline.filters as Filters  
import quantopian.pipeline.factors as Factors  
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_start(),time_rules.market_open(minutes = 65))  
    attach_pipeline(make_pipeline(), 'pipeline')

def make_pipeline():  
    sec_set = Filters.StaticAssets(ASSETS)  
    sma_f   = Factors.SimpleMovingAverage(inputs = [USEquityPricing.close], window_length = MA_F, mask = sec_set)  
    sma_s   = Factors.SimpleMovingAverage(inputs = [USEquityPricing.close], window_length = MA_S, mask = sec_set)  
    pipe    = Pipeline(screen = sec_set & (sma_f > sma_s))  
    return pipe

def trade(context, data):  
    securities = pipeline_output('pipeline').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)  

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.

Vladimir, it is elegent piece of code, pythonic!. I didn't even know there is a trade method in Q. Would you please elaborate how ratio comes into picture here?