Back to Community
XIV with long short+gold leverage

for use with my RH account

Clone Algorithm
74
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
##def before_trading_start(context, data):
    ##context.buycount = 0
    
    ##oo = get_open_orders()             # Dict of security objects with open orders  
    ##for sec in oo:                     # Each security object has a list  
       ## for order in oo[sec]:          # Each order in the list  
           ## cancel_order(order.id)     # The order id  

def initialize(context):
    set_benchmark(sid(40516))
    context.stock = sid(37514)
    context.spxl = sid(40516) #xiv
    context.spxs = sid(37133) #tza
    #context.spxs3 = sid(40515) #tvix
    context.spxs2 = sid(37133) #TZA
    #context.qty = 400
    #context.stddev_limit = 1.6
    #context.portfolio.cash
    #context.portfolio.portfolio_value
    #context.cashper = context.portfolio.cash/context.portfolio.portfolio_value
    #context.portfolio.positions[context.spxs].amount
    #context.portfolio.positions[context.spxl].amount
    #schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=1))
    schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=30))                  
    schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=60))                  
    #schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=90))                  
    #schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=120))                  
    #schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=150))                  
    schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=180))                  
    schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=210))                  
    #schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=240))                  
    #schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=270))                  
    #schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=300))                  
    #schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=330))                  
    schedule_function(order_handling, date_rules.every_day(), time_rules.market_open(minutes=360))
    context.timer = 20
    context.timerspxs = 0
    schedule_function(timer,date_rules.every_day(),time_rule = time_rules.market_close(minutes = 2))
    #schedule_function(timerspxs,date_rules.every_day(),time_rule = time_rules.market_open(minutes = 3))
    #schedule_function(timerspxl,date_rules.every_day(),time_rule = time_rules.market_open(minutes = 2))
    ##context.timerspxl = 1
    set_long_only()
    ##context.buycount=0
    ##context.sellcount=0
    
def order_handling(context, data):
    #zx best sell signal, sell>z1*z2/2+0.4
    #z1 best buy signal, buy at <-z1/5-.1 ?
    #buycount=context.buycount
    #sellcount=context.sellcount
    timer = context.timer
    timerspxs = context.timerspxs
    #timerspxl = context.timerspxl
    #context.numexp = pow(timerspxl,  2)
    #context.denexp = pow(timerspxl, 2)
    
    
    current_price = data.current(context.stock, 'price')
    spxs_price = data.current(context.spxs, 'price')
    spxl_price = data.current(context.spxl, 'price')
    price_history1 = data.history(context.stock, 'price', 20, '1d')
    price_history2 = data.history(context.stock, 'price', 50, '1d')
    price_history3 = data.history(context.stock, 'price', 200, '1d')
    price_history4 = data.history(context.stock, 'price', 3, '1d')
    #price_history5 = data.history(context.stock, 'price', 400, '1d')
    price_history6 = data.history(context.stock, 'price', 9, '1d')
    
    dma20 = price_history1.mean()
    dma50 = price_history2.mean()
    dma200 = price_history3.mean()
    #dma400 = price_history5.mean()
    dma5 = price_history4.mean()
    dma9 = price_history6.mean()
    #std5 = price_history4.std()
    #std20 = price_history1.std()
    std50 = price_history2.std()
    std200 = price_history3.std()
   #std400 = price_history5.std()
    cash = context.portfolio.cash
    #upper_bb = 3*std5/std50
    #lower_bb = -2.5*std20/std50
    ##moving_buy = (std20/std50)-3
    ##moving_sell = (-std20/std50)+3
    #upper_bb2 = 20dma + 1*20std
    #lower_bb2 = 20dma - 1*20std
    #moving_sell2 = 0.3*stddev2/2
    #moving_buy2 = -0.4*stddev2/
    #upper_bb2 = mean2 + 3*stddev
    difference1 = (dma20 - dma50)
    difference2 = (dma50 - dma200)
    #difference3 = (dma5- dma20)
    #squeeze = upper_bb - lower_bb
    z1 = difference1/(std50)
    z2 = difference2/(std200)
    #z3 = difference3/(std20)
    #z4 = (current_price-dma20)/std20
    #z5 = (current_price-dma50)/std50
    z6 = (current_price-dma200)/std200
    z7 = (dma5-dma200)/std200
    z9 = (dma20-dma200)/std200
    #z8 = (dma50-dma400)/std400
    #zexp = 0.5
    #zw = (z1+2*z3+0.5*z2)/3.5
    #zw2 = (z4+z5+z6)/3
    #zwsell = (zw+1)*context.numexp/context.denexp
    #zw = (zw*context.numexp/context.denexp)
    #zw= z5
    #z1sell=3.8
    #z1buy= z3
    #zx = z1*z2
    #zwsell= 4.8
    #zwbuy= std50
    #zmod=z1/(std20/std50)
    z2exp=z2*z2*z2*1.2-(timer/60)+std50/20
    z2exps=z2*z2*z2*0.9-timer/45-std50/20
    #z2expsc= z2*z2*z2*0.9-timer/-std50/25+0.2
    #z2expbuy=-z3*z3*z3/1.6
    
    
    #madsqp = diff1prop/squeeze*400
    #SQINVDIFF= (-difference/100+squeeze)*(-difference/100+squeeze)
    #[0]*len(rho)
    cashper = context.portfolio.cash/context.portfolio.portfolio_value
    
    record(z2exp=z2exp)
    record(z2exps=z2exps)
    #record(std200=std200)
    #record(dma9=dma9)
    #record(z2expbuy=z2expbuy)
    #record(std50=std50/5)
    #record(buycount=context.buycount)
    #record(dmadiff=dma20-dma50)
    #record(dma20=dma20)
    #record(dma50=dma50)
    #record(dma200=dma200)
    #record(priceprop=priceprop)
    #record(spxs=context.portfolio.positions[context.spxs].amount)
    #record(spx1=context.portfolio.positions[context.spxl].amount)
    #record(value=context.portfolio.portfolio_value)
    #record(timer=timer)
    #record(timerspxs=timerspxs)
    #record(timerspxsl=timerspxl)
    #record(CMG=current_price)
    #record(Upper=upper_bb)
    #record(uppermod=10/std20-z1)
    #record(uppermod= (z1/3)+4)
    #record(lowermod= (z1+z2+z3)/6-0.2*std20)
    #record(MA1=mean1)
    #record(Lower=lower_bb)
    #record(cashper=cashper)
    #record(cash=cash)
    #record(MA2=mean2)
    #record(MA3=mean3)
    #record(z7=z7)
    #record(z8=z8)
    #record(Z1=z1*0.6-0.65)
    #record(z2=z2)
    #record(z2up=z2/upper_bb)
    #record(z1low=1)
    #record(z1low=z2*.6+1.4)
   # record(z2sell=z2*.6-.6)
    #record(spxsu=2.4)
   # record(shrtclsu=z2*0.6+1)
   # record(shrtopu=z2*0.6+1.7)
    #record(shrtopl= z2*0.6-1.1)
    #record(shrtclsl= z2*0.6-0.1)
    #record(zavg=(z1+z2)/2)
    #record(Z4=z4)
    #record(Z5=z5)
    record(z6=z6)
    record(z9=z9)
    #record(zavg= (z6))
    #record(z1buy=z1buy)
    #record(z1sell=z1sell)
    #record(zw=zw)
    #record(zwbuy=zwbuy)
    #record(zwsell=zwsell)
    #record(zw2=zw2)
    #record(Z3=z3)
    #record(zx=z1*z2)
    #record(zd2=
    #record(zdiv= z1/z2)
    #record(Zdiff=zdiff)
    #record(madpq=madpq)
    #record(timesell=0.5)
    #record(movbuy=moving_buy)
    #record(movsell=moving_sell)
    #record(movbuy2=moving_buy2)
    #record(movsell2=moving_sell2)
    #record(sell=z1*z2*.8)
    #record(buy=-z1/5-.1)
    #record(diff1prop=diff1prop*400)
    #record(short=40)
    #record(close=30)
    #record(squeeze=10/squeeze)
    #record(diff1sq=difference1*difference1)
    #record(difference1=difference1)
    #record(squeeze=((((squeeze)-0.2)/4)*(((squeeze)-0.2)/4)))
    #record(SQINVDIFF= (difference+squeeze)/100)
    
    #if context.timerspxs > 10:
            #order_target(context.spxs, 0)
            
    #if madsq >= moving_buy:
            #order_target(context.spxs, 0)
            #context.timerspxs=0
                
    #if madsq >= moving_sell and context.timerspxs < 1 :
            #if .9 < cashper > 0 :
                #if cash > (int(cash/spxs_price)*spxl_price-10)>0:
                        #order(context.spxs, (int(cash/spxs_price)-10))

    #zx best sell signal, sell>z1*z2/2+0.4
    #z1 best buy signal, buy at <-z1/5-.1 ?
    
    #if greater than shrtclsu and holding sxps do not buy anything esle
    #if lower than z2low and greater than and greater than shrtclsu, sell all+buy sp6
    
    #if cash is greater than $100k, go 75/20 spxs/spxs2
    
    #if not get_open_orders():
       # if z6 > z2*0.6+ 1.6:
          #  if cashper>0.80:
                #order_target_percent(context.spxs,0.0)
               # order_target_percent(context.spxs2,0.95)
                #context.buycount += 1
                    
    #if not get_open_orders():
      #  if z6 < z2*0.6+1 and z6> z2*0.6-1.2:
            #order_target_percent(context.spxs,0)
            #order_target_percent(context.spxs2,0)
    
    if not get_open_orders():
        if z6 <= z2exp: #and z6 >lower_bb or z6 > z2*0.6+1.7:
            order_target_percent(context.spxl, 0)
            #if cashper>0.80:
             #order_target_percent(context.spxs,0.0)
                #order_target_percent(context.spxs2,0.95)
                #context.buycount += 1
   # if not get_open_orders():
        #if z6 < z2exp: #and z6 >lower_bb or z6 > z2*0.6+1.7:
            #order_target_percent(context.spxs, 0.9)
    if not get_open_orders():
        if z6 > z2exps-0.01: #and z6 >lower_bb or z6 > z2*0.6+1.7:
            order_target_percent(context.spxs, 0.0)
            
    if not get_open_orders():
        if z6 < z2exps and z6< z2exp:
            if cashper>0.80:
                order_target_percent(context.spxs, 0.95)
                order(context.spxl, int(10000/spxs_price)-10)
                
    #if not get_open_orders():
       # if z6 >= z2exps: #and z6 >lower_bb or z6 > z2*0.6+1.7:
            #order_target_percent(context.spxs, 0)
        
    
    if not get_open_orders():
        if z6 > z2exp and z6<z7-0.01: #and z6 < upper_bb and z6 < z2*0.6+1 or z6 < lower_bb : # or context.timer>20:
            #order_target_percent(context.spxs,0)
            #order_target_percent(context.spxs2,0)
            if cashper>0.80:
                order_target_percent(context.spxl, 0.95)
                order(context.spxl, int(10000/spxl_price)-10) 
                #context.buycount += 1
                         
    #if z1 >= 0.9 and cashper>1:
    
    #if z1 >= 1 or zx >.8  or z2> 1.45 and .95 < cashper:
        #order_target_percent(context.spxs, 0.95)
        
    #if z1 <= 1 or zx <.8  or z2< 1.45:
        #order_target(context.spxs, 0)
    
    #if not get_open_orders():
        #if z6 <= z2*0.6 and z6 > lower_bb and cashper>0.8 : # or context.timer>20:
           # order_target_percent(context.spxs, 0.95)
                    
   # elif madsq <= context.timer/60: 
       # if madsq <= 0.5 :
           # if .95 <= cashper: 
                #order_target_percent(context.spxl, 0.95)
            
    #BUG FIXING
     
    #if -.15 > cash/context.portfolio.portfolio_value > 1 :
        #order_target(context.spxs, 0)
        #order_target(context.spxl, 0)
        
    #if context.portfolio.positions[context.spxs].amount < -5 :
        #order_target(context.spxs, 0)
    #if context.portfolio.positions[context.spxl].amount < -5 :
        #order_target(context.spxl, 0)
        


            
#def timerspxs(context, data):
    #order_target(context.spxs, 0)
    #context.portfolio.positions[context.spxs].amount
    #if context.portfolio.positions[context.spxs].amount >= 4 :
        # Add one day to timer  
       # context.timerspxs += 1

   #if context.portfolio.positions[context.spxs].amount <4 :
                        # Reset timer
        #context.timerspxs = 0 
        
#def timerspxl(context, data):


    
   # if zwbuy>z1<zwsell:
        # Add one day to timer  
       # context.timerspxl += 1
   # elif zwbuy<z1>zwsell:
        # Reset timer
       # context.timerspxl = 1 
        

def timer(context, data):
    current_price = data.current(context.stock, 'price')
    spxs_price = data.current(context.spxs, 'price')
    spxl_price = data.current(context.spxl, 'price')
    #price_history1 = data.history(context.stock, 'price', 20, '1d')
    price_history2 = data.history(context.stock, 'price', 50, '1d')
    price_history3 = data.history(context.stock, 'price', 200, '1d')
    price_history4 = data.history(context.stock, 'price', 3, '1d')
    #price_history5 = data.history(context.stock, 'price', 400, '1d')
    price_history6 = data.history(context.stock, 'price', 9, '1d')
    timer=context.timer
    #dma20 = price_history1.mean()
    dma50 = price_history2.mean()
    dma200 = price_history3.mean()
    #dma400 = price_history5.mean()
    dma5 = price_history4.mean()
    dma9 = price_history6.mean()
    #std5 = price_history4.std()
    #std20 = price_history1.std()
    std50 = price_history2.std()
    std200 = price_history3.std()
   #std400 = price_history5.std()
    cash = context.portfolio.cash
    #upper_bb = 3*std5/std50
    #lower_bb = -2.5*std20/std50
    ##moving_buy = (std20/std50)-3
    ##moving_sell = (-std20/std50)+3
    #upper_bb2 = 20dma + 1*20std
    #lower_bb2 = 20dma - 1*20std
    #moving_sell2 = 0.3*stddev2/2
    #moving_buy2 = -0.4*stddev2/
    #upper_bb2 = mean2 + 3*stddev
    #difference1 = (dma20 - dma50)
    difference2 = (dma50 - dma200)
    #difference3 = (dma5- dma20)
    #squeeze = upper_bb - lower_bb
    #z1 = difference1/(std50)
    z2 = difference2/(std200)
    #z3 = difference3/(std20)
    #z4 = (current_price-dma20)/std20
    #z5 = (current_price-dma50)/std50
    z6 = (current_price-dma200)/std200
    z7 = (dma5-dma200)/std200
    #z8 = (dma50-dma400)/std400
    #zexp = 0.5
    #zw = (z1+2*z3+0.5*z2)/3.5
    #zw2 = (z4+z5+z6)/3
    #zwsell = (zw+1)*context.numexp/context.denexp
    #zw = (zw*context.numexp/context.denexp)
    #zw= z5
    #z1sell=3.8
    #z1buy= z3
    #zx = z1*z2
    #zwsell= 4.8
    #zwbuy= std50
    #zmod=z1/(std20/std50)
    z2exp=z2*z2*z2*1.2-timer/60
    
    
    context.portfolio.cash
    context.portfolio.portfolio_value
    if context.portfolio.cash / context.portfolio.portfolio_value > 0.5 or context.portfolio.positions[context.spxs].amount > 5 and context.timer<40 :
    # Add one day to timer  
        context.timer += 1
        
    if context.portfolio.cash / context.portfolio.portfolio_value < 0.1  and context.timer > 0.5 :
        context.timer -= 0.5
    if  context.portfolio.positions[context.spxs].amount > 5 :
        context.timerspxs = 1
        
    if  z6 > z2exp :
        context.timerspxs = 0
There was a runtime error.
2 responses

Hi Ryan,

Thanks for sharing this with the community. Can you please explain your logic behind this algo?

I'm also curious what your logic is behind this algorithm.

Thanks!