Back to Community
Theta weighted exponential z scores with XIV, long only

Theta weighted exponential z scores with XIV, long only, If someone wants to add a shot with UVXY I'd be more than happy

Clone Algorithm
36
Loading...
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(37514))
    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))
    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.timer = 20
    ##context.timerspxs = 0
    ##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
    #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
    #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(dma9=dma9)
    #record(z2expbuy=z2expbuy)
    #record(std20=std20)
    #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/60)
    #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(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(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<z7: #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)
                #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):
    context.portfolio.cash
    context.portfolio.portfolio_value
    if context.portfolio.cash / context.portfolio.portfolio_value > 0.5 :
    # Add one day to timer  
        context.timer += 1
        
    if context.portfolio.cash / context.portfolio.portfolio_value < 0.5  and context.timer > 0.5 :
        context.timer -= 0.5
There was a runtime error.
13 responses

Tweaked

Clone Algorithm
36
Loading...
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(37514))
    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))
    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.timer = 20
    ##context.timerspxs = 0
    ##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
    #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
    #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(dma9=dma9)
    #record(z2expbuy=z2expbuy)
    #record(std20=std20)
    #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/60)
    #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(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(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<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)
                #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):
    context.portfolio.cash
    context.portfolio.portfolio_value
    if context.portfolio.cash / context.portfolio.portfolio_value > 0.5 :
    # Add one day to timer  
        context.timer += 1
        
    if context.portfolio.cash / context.portfolio.portfolio_value < 0.5  and context.timer > 0.5 :
        context.timer -= 0.8
There was a runtime error.

changed TZA to SH and experienced less decay, need to find a better short close signal

Clone Algorithm
75
Loading...
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(32268) #sh
    #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 = (dma9-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-0.1#
    #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)
              #  
                
    #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.

same time frame as initial with leverage

Clone Algorithm
75
Loading...
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(32268) #sh
    #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 = (dma9-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)
                
                
    #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.

Hey everyone who is listening to this thread, I have been optimizing my code and trying to set trigger records of when z6, (current price-200dma)/200std, crosses z2exp, z2exps, or z2w (derivatives of (50dma-200dma)/200std) to set shorts. Although, I have not been able to get the shorts working correctly. I'm not completely done with the trigger logic and will rework it. Long only with leverage. Also, this handles the crash in 2011 better. Any thoughts on a short signal?

Clone Algorithm
7
Loading...
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_long_only()
    set_benchmark(sid(40516))
    context.stock = sid(8554)
    context.spxl = sid(40516) #xiv
    context.spxs = sid(37083) #sh
    #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 = 5))
    #schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=1))
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=29))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=59))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=89))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=119))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=149))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=179))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=209))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=239))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=269))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=299))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=329))                  
    schedule_function(buycount, date_rules.every_day(), time_rules.market_open(minutes=359))
    #context.timerspxl = 1
    context.buycount1=0
    context.buycount2=0
    context.buycount3=0
    context.buycount4=0
    context.buycount5=0
    context.buycount6=0

    ##context.sellcount=0
    
def buycount(context, data):
    
    timer=context.timer
    timerspxs=context.timerspxs
    
    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 = (dma9-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)
    z2exph=z2*z2*z2*1.2+(std50/20)
    z2exp=z2*z2*z2*1.2-(timer/60)+(std50/20)
    z2exps=z2*z2*z2*0.9+(-timer/45)+(-std50/20)+(-timerspxs/15)
    #z2expsc= z2*z2*z2*0.9-timer/-std50/25+0.2
    #z2expbuy=-z3*z3*z3/1.6
    z2w=z2-.2
    
    

    


    
def order_handling(context, data):
    #zx best sell signal, sell>z1*z2/2+0.4
    #z1 best buy signal, buy at <-z1/5-.1 ?
    #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 = (dma9-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)
    z2exph=z2*z2*z2*1.2+(std50/20)
    z2exp=z2*z2*z2*1.2-(timer/60)+(std50/20)
    z2exps=z2*z2*z2*0.8+(-timer/45)+(-std50/20)+(-timerspxs/15)
    #z2expsc= z2*z2*z2*0.9-timer/-std50/25+0.2
    #z2expbuy=-z3*z3*z3/1.6
    z2w=z2-.2
    zdiff = (z6-z2)/std200*10
    
    
    #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(z2exph=z2exph)
    #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/60)
    #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(z2w=z2w)
    #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(zdiff=zdiff)
    #record(z2w2=z2+zdiff)
    z2w2=z2+zdiff
    #record(z9=z9)
    
    if z2w>z2exp:
        if z6>z2w:
            context.buycount1=0
            context.buycount2=0
            context.buycount4=0
            context.buycount5=0
            context.buycount6=0
        if z6>z2exp:
            context.buycount3=0
        if z6<z2w and context.buycount1==0:
            context.buycount1 += 1#buycount1=1, close short above z2w
        if z6<z2exp and context.buycount1==1 and context.buycount2==0:
            context.buycount2 += 1#buycount2=1, close short above z2exp
        if z6<z2exps and context.buycount1==1 and context.buycount2==1 and context.buycount3 == 0:
            context.buycount3 += 1  #close short above z2exps, open long above z2exp, reset to buycount 2 or 5
    #record(buycount=context.buycount1+context.buycount2+context.buycount3)
            
    if z2exp>z2w:
        if z6>z2exp:
            context.buycount1=0
            context.buycount2=0
            context.buycount3=0
            
            context.buycount4=0
            context.buycount5=0
            context.buycount6=0
        if z6<z2exp and context.buycount4==0 :
            context.buycount4 += 1#buycount5 close short above z2exp
        if z6<z2w and context.buycount4==1 and context.buycount5==0:
            context.buycount5 += 1#buycount6, close short above z2w
        if z6<z2exps and context.buycount4==1 and context.buycount5==1 and context.buycount6 == 0:
            context.buycount6 +=1
    
    

    #record(buycount=context.buycount4+context.buycount5+context.buycount6)
            
    if z2w>z2exp and context.buycount1==0:
       order_target_percent(context.spxs, 0.0)
       if cashper>0.8 :
           order_target_percent(context.spxl, 0.95)
           order(context.spxl, int(10000/spxl_price)-10)
        
    if  z2w>z2exp and context.buycount1==1 and context.buycount2==0 and context.buycount3==0:
        if z6<z2w:
            order_target_percent(context.spxl, 0.0)
           # if cashper>0.80:
             #   order_target_percent(context.spxs, 0.95)   
        if z6>z2w:
            order_target_percent(context.spxs, 0.0)
            if cashper>0.8:
                order_target_percent(context.spxl, 0.95)
                order(context.spxl, int(10000/spxl_price)-10)
                                                               
    if z2w>z2exp and context.buycount1==1 and context.buycount2==1 and context.buycount3==0:
        if z6<z2exp:
            order_target_percent(context.spxl, 0.0)
           # if cashper>0.80 and z6>z7-0.01:
              #  order_target_percent(context.spxs, 0.95)   
        if z6>z2exp:
            order_target_percent(context.spxs, 0.0)
            if cashper>0.80:
                order_target_percent(context.spxl, 0.95)
                order(context.spxl, int(10000/spxl_price)-10)
                
    if z2w>z2exp and context.buycount1==1 and context.buycount2==1 and context.buycount3== 1 or z2w<z2exp and context.buycount4==1 and context.buycount5==1 and context.buycount6==1:
        if z6<z2exps:
            order_target_percent(context.spxl, 0.0)
           # if cashper>0.80:
           #     order_target_percent(context.spxs, 0.95)   
        if z6>z2exp or z6>z2w:
            order_target_percent(context.spxs, 0.0)
            if z6>z2exps:
                if cashper>0.80:
                    order_target_percent(context.spxl, 0.95)
                    order(context.spxl, int(10000/spxl_price)-10)
                    
    if z2w<z2exp and context.buycount4==0:
         order_target_percent(context.spxs, 0.0)
      #  if cashper>0.80 and z6<z7-0.01:
       #     order_target_percent(context.spxl, 0.95) 
    
    if z2w<z2exp and context.buycount4==1 and context.buycount5==0 and context.buycount6==0:
        if z6<z2exp:
            order_target_percent(context.spxl, 0.0)
          #  if cashper>0.80:
             #   order_target_percent(context.spxs, 0.95)   
        if z6>z2exp:
            order_target_percent(context.spxs, 0.0)
            if cashper>0.80:
                order_target_percent(context.spxl, 0.95)
                order(context.spxl, int(10000/spxl_price)-10)
    
                          
    if z2w<z2exp and context.buycount4==1 and context.buycount5==1 and context.buycount6==0:
        if z6<z2exp:
            order_target_percent(context.spxl, 0.0)
           # if cashper>0.80 and z6>z7-.01:
              #  order_target_percent(context.spxs, 0.95)    
        if z6>z2exp:
            order_target_percent(context.spxs, 0.0)
            if cashper>0.80:
                order_target_percent(context.spxl, 0.95)
                order(context.spxl, int(10000/spxl_price)-10)
    
    

    
    
   # 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 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)
        
    
    #if not get_open_orders():
        #if z6 > z2exp and z6<z7-0.01: 
           #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
    timerspxs=context.timerspxs
    #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)
    z2exph=z2*z2*z2*1.2
    z2exp=z2*z2*z2*1.2-(timer/60)+(std50/20)
    z2exps=z2*z2*z2*0.8+(-timer/60)+(-std50/20)+(-timerspxs/15)
    
    context.portfolio.cash
    context.portfolio.portfolio_value
    
    if z6<z2exp :
    # Add one day to timer  
        context.timer += 1
        
    if  z6>z2exp and context.timer > 0.5 :
        context.timer -= 0.5
        
    if  z6>z2exps and context.timerspxs > 0.5 :
        context.timerspxs -= 0.5
        
    if  z6<z2exps and context.timerspxs :
        context.timerspxs +=5

            
There was a runtime error.

old strategy with xiv and uvxy

Clone Algorithm
65
Loading...
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(37514))
    context.stock = sid(40516)
    context.spxl = sid(40516)
    context.spxs = sid(37133)
    #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))
    #schedule_function(timer,date_rules.every_day(),time_rule = time_rules.market_open(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.timer = 0
    ##context.timerspxs = 0
    ##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', 5, '1d')
    
    dma20 = price_history1.mean()
    dma50 = price_history2.mean()
    dma200 = price_history3.mean()
    dma5 = price_history4.mean()
    std5 = price_history4.std()
    std20 = price_history1.std()
    std50 = price_history2.std()
    std200 = price_history3.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
    #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)
    
    
    #madsqp = diff1prop/squeeze*400
    #SQINVDIFF= (-difference/100+squeeze)*(-difference/100+squeeze)
    #[0]*len(rho)
    cashper = context.portfolio.cash/context.portfolio.portfolio_value
    
    #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/20)
    #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(Zmod=zmod)
    #record(Z1=z1*0.6)
    #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(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 not get_open_orders():
        if z6 > z2*0.6+ 1.6:
            if cashper>0.80:
                order_target_percent(context.spxs,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)
    
    if not get_open_orders():
        if z6 <= z2*0.6-0.6 and z6 >lower_bb or z6 > z2*0.6+1.7:
            order_target_percent(context.spxl, 0)
            if cashper>0.80 and z6<z2*0.6-1.2 and z6 > lower_bb:
                order_target_percent(context.spxs,0.95)
                order(context.spxl, int(10000/spxs_price)-10) 
                #context.buycount += 1
                
    if not get_open_orders():
        if z6 > z2*0.6-0.6 and z6 < upper_bb and z6 < z2*0.6+1 or z6 < lower_bb : # or context.timer>20:
            order_target_percent(context.spxs,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):
    #context.portfolio.cash
    #context.portfolio.portfolio_value
    #if context.portfolio.cash / context.portfolio.portfolio_value > 0.5 :
    # Add one day to timer  
        #context.timer += 1
        
    #if context.portfolio.cash / context.portfolio.portfolio_value < 0.5 :
        #context.timer = 0 
There was a runtime error.

promising algo that needs some work. If anyone wants to tweak, please share the results, I have to run. currently only trading off of 9dma zscore. Ordering the resistances and placing trigger logic on where where the current price zscore is coming from still needs to be done and there are. The beginning workings of the code in there.

Clone Algorithm
17
Loading...
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 initialize(context):
    set_benchmark(sid(40516))
    context.stock = sid(40516)
    context.spxl = sid(40516) #xiv
    context.spxs = sid(41969) #tza
    #context.spxs3 = sid(40515) #tvix
    context.spxs2 = sid(37133) #TZA
    
    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.buycount1=0
    context.buycount2=0
    context.buycount3=0
    context.buycount4=0
    context.buycount5=0
def order_handling(context, data):
    current_price = data.current(context.stock, 'price')
    spxs_price = data.current(context.spxs, 'price')
    spxl_price = data.current(context.spxl, 'price')
  
    price_history6 = data.history(context.stock, 'price', 20, '1d')
    dma = price_history6.mean()
    
    price_history7 = data.history(context.stock, 'price', 50, '1d')
    dma200 = price_history7.mean()
    std200=price_history7.std()
    
    z1 = (current_price-dma200)/std200
    z2 = (dma-dma200)/std200
    record(z1=z1)
    record(buycount4=z2+.5)
    record(buycount2=-z2-1.5)
    record(buycount5=z2+1)
    record(buycount3=-z2-2)
    
    buycount4=z2+.5
    buycount2=z2-.5
    buycount3=z2 
    buycount5=z2+1
    buycount1=z2-1
    
    
    
    
   #resistances = {'z2w': 0,'z2exp': z2exp,'z2exps': z2exps }#need to add lower bb
   
    #ordered = sorted(resistances.items(), key = lambda t: t[1])
    #print ordered
    #print ordered[0][1]
    
    
    #buy xiv
    if z1<z2-1:#bc1 bottom
        context.buycount1=0
        context.buycount2=0
    if z1<z2-.5:# bc2 2nd bottom
        context.buycount3=0
    if z1<z2:#bc3 middle
        context.buycount4=0
    if z1<z2+.5:#bc4 2nd top
        context.buycount5=0
    
    #sell xiv
    if z1>z2-1:
        context.buycount1=1
    if z1>z2-.5:
        context.buycount2=1
    if z1>z2:
        context.buycount3=1
    if z1>z2+.5:
        context.buycount4=1
    if z1>z2+1:
        context.buycount5=1
        
        
 
    if z1 > z2+.5:
        order_target_percent(context.spxl, 0)
        #order_target_percent(context.spxs, 0.95)
    if z1 < z2+.5:
        order_target_percent(context.spxl, 0.95)
        #order_target_percent(context.spxs, 0.0)

     
    
    
There was a runtime error.

Note: might have better luck with a smoother zscore*0.8-0.5 cutoffs

Went in a different direction with this after talking to a programmer friend of mine and then trying to implement my idea. The ordering logic hasn't been put together yet, but the basis of this is it record the min max minutely z-scores for the last 20 days and trades based off of when it goes above/below 20% and switches positions(long<>short) along ((dma20-dma200)/std200)+-.5 based off trigger logic that rests when the upper/lower line is breached . It looks VERY promising.

Clone Algorithm
17
Loading...
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 initialize(context):
    set_benchmark(sid(40516))
    context.stock = sid(40516)
    context.spxl = sid(40516) #xiv
    context.spxs = sid(41969) #tza
    #context.spxs3 = sid(40515) #tvix
    context.spxs2 = sid(37133) #TZA
    
    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.buycount1=0
    context.buycount2=0
    context.buycount3=0
    context.buycount4=0
    context.buycount5=0
def order_handling(context, data):
    current_price = data.current(context.stock, 'price')
    spxs_price = data.current(context.spxs, 'price')
    spxl_price = data.current(context.spxl, 'price')
  
    price_history6 = data.history(context.stock, 'price', 7800, '1m')
    dma = price_history6.mean()
    dmalow = price_history6.min()  
    dmahigh = price_history6.max()
    
    price_history7 = data.history(context.stock, 'price', 50, '1d')
    dma200 = price_history7.mean()
    std200=price_history7.std()
    
    z1 = (current_price-dma200)/std200
    z2 = (dma-dma200)/std200
    z2low= .80*(dmalow-dma200)/std200
    z2high= .8*(dmahigh-dma200)/std200
    record(z1=z1)
    record(z2low=z2low)
    record(z2high=z2high)
    record(z2=z2)
    #record(high_trigger_reset=z2+.5)
    record(low_trigger_reset=z2-.5)
    #record(buycount5=z2+1)
    #record(buycount3=-z2-2)
    
    buycount4=z2+.5
    buycount2=z2-.5
    buycount3=z2 
    buycount5=z2+1
    buycount1=z2-1
    
    
    
    
   #resistances = {'z2w': 0,'z2exp': z2exp,'z2exps': z2exps }#need to add lower bb
   
    #ordered = sorted(resistances.items(), key = lambda t: t[1])
    #print ordered
    #print ordered[0][1]
    
    
    #buy xiv
    if z1<z2-1:#bc1 bottom
        context.buycount1=0
        context.buycount2=0
    if z1<z2-.5:# bc2 2nd bottom
        context.buycount3=0
    if z1<z2:#bc3 middle
        context.buycount4=0
    if z1<z2+.5:#bc4 2nd top
        context.buycount5=0
    
    #sell xiv
    if z1>z2-1:
        context.buycount1=1
    if z1>z2-.5:
        context.buycount2=1
    if z1>z2:
        context.buycount3=1
    if z1>z2+.5:
        context.buycount4=1
    if z1>z2+1:
        context.buycount5=1
        
        
 
    if z1 > z2+.5:
        order_target_percent(context.spxl, 0)
        #order_target_percent(context.spxs, 0.95)
    if z1 < z2+.5:
        order_target_percent(context.spxl, 0.95)
        #order_target_percent(context.spxs, 0.0)

     
    
    
There was a runtime error.

So after I wrote some trading logic for that last post, this is the comprehensive analysis. I am holding on to my code as of now.

Loading notebook preview...

Get the margin-leverage under control with minimal Max Intraday Leverage code or PvR.

2017-01-12 06:31 pvr:333 INFO PvRp 0.5066 %/day 2011-12-09 to 2017-01-12 $1500 2017-01-27 22:10 US/Eastern
2017-01-12 06:31 pvr:334 INFO Profited 73788 on 11371 activated/transacted for PvR of 648.9%
2017-01-12 06:31 pvr:335 INFO QRet 4919.22 PvR 648.92 CshLw -22334 MxLv 7.58 RskHi 11371 Shrts 0
2017-01-12 06:31 pvr:430 INFO
Runtime 0 hr 3.3 min End: 2017-01-27 22:13 US/Eastern

Thanks Blue

Hi ryan,

Could you please describe a little bit about your algo's stratergy? Or just simply the "Theta weighted exponential z scores"?

Cheers

Hey Thomas There are actually multiple strategies here, but the theta wieghted one I stated out with was an exponential z score (z2exp=z2*z2*z2*1.2-timer/60) where z2 = 50 day zscore against the 200dma. The issue I had with trading the dma's was that they were slow, hence the exponential form to try increase the cutoff when it is >1. Also when the price was below the cutoff I had to find a way to bring the moving average down because money has a time value, so the timer would increase the longer the account was holding cash. It worked to some extent, but I don't trust it with real money. I have it running on q paper trade to see how it will preform over the next year or so. Also please forgive any of my algorithms' messiness as I only started coding in December.