Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Simple ETF strategy - rebalance issue

Hi Quantopians,

I would like to optimize this simple strategy. Would also love to hear your opinions.

Current strategy is 2 steps:

Condition 1 (High exposure to market).
If (TQQQ) Fast DMA (1D) > Slow DMA (20D):
Buy 3x Leveraged ETF (TQQQ) - 60%
Buy Daily 20+ Year Treasury Bull 3X Shares - 20%
Buy Daily MSCI Emerging Markets Bear 3X Shares - 20%

Condition 2 (Low exposure to market).
If Fast DMA (1D) < Slow DMA (20D):
Buy 3x Leveraged ETF (TQQQ or SPXL) - 30%
Buy Daily 20+ Year Treasury Bull 3X Shares - 40%
Buy Daily MSCI Emerging Markets Bear 3X Shares - 30%

I would like to add some improvements, but not sure how to structure it.

If Condition 1 is met (DMA(1D) > DMA(20D)) and initial positions are opened [60%/20%/20%]:
- Check every day if DMA(1D) > DMA(20D) and DO NOT rebalance as long as true.
- If DMA(1D) < DMA(20D) rebalance to the rules of Condition 2 and do not rebalance again as long as true.
- Enter into Condition 1 if (DMA(1D) > DMA(20D) once again and keep original positions as long as true.

The basic idea of the improvement is to keep the exposure weight tilted to the beta [initial weights of 60%/20%/20%] when daily close crosses above 20 DMA and not rebalance/trade as long as true. This will increase exposure to trend and costs low as it will not rebalance every day.
If daily close crosses below 20 DMA, switch to low market(beta) exposure portfolio [initial weights of 30%/40%/30%] and not rebalance as long as price is below 20 DMA.

Sorry for the poor explanation. Hope you get the idea.
Please suggest other improvements so we can lower Drawdowns.

Regards,
Tim

2 responses

Hi Tsvetimir,

You may try something like this:

import quantopian.optimize as opt  
# ----------------------------------------------------------------------------------------------------  
BULL, BEAR, BOND, MA_F, MA_S, LEV, wt = symbol('TQQQ'), symbol('EDZ'), symbol('TMF'), 10, 100, 1.0, {}  
# ----------------------------------------------------------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 35))  
    set_commission(commission.PerShare(cost = 0.005, min_trade_cost = 1.0))  

def trade(context,data):  
    mavg_f = data.history(BULL,'price', MA_F + 1,'1d').rolling(MA_F).mean()  
    mavg_s = data.history(BULL,'price', MA_S + 1,'1d').rolling(MA_S).mean()  

    condition_1 = (mavg_f[-1] > mavg_s[-1] and mavg_f[-2] < mavg_s[-2])  
    condition_2 = (mavg_f[-1] < mavg_s[-1] and mavg_f[-2] > mavg_s[-2])  

    if   condition_1: wt[BULL], wt[BEAR], wt[BOND] = 0.6, 0.2, 0.2  
    elif condition_2: wt[BULL], wt[BEAR], wt[BOND] = 0.3, 0.2, 0.5  

    if condition_1 or condition_2:  
        order_optimal_portfolio(opt.TargetWeights(wt), [opt.MaxGrossExposure(LEV)])  

    record(leverage = context.account.leverage)  

Cheers, Vladimir. Looks good. Will try to further lower DDs