Back to Community
G

G

1 response

Leverage is a hell of a drug.

Pretty much always make sure you're tracking it, with

record('Leverage', context.account.leverage)

As for the problem, looks like you're doing a ton of shorting. -shares means either you sell what you own, or you are willing to short. If you're not meaning to short, then maybe order_target instead. It just looks like you keep compounding that short over and over. You should have some sort of limit on that. Your buys would likely be the same story, you just are selling at the moment only.

Clone Algorithm
0
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):
    context.security = symbol('SPY')

def handle_data(context, data):
    cash = context.portfolio.cash
    SPY = data[context.security].price
    H = history(900,'1m','price')
    MA1 = H.tail(10).mean()[context.security]
    MA2 = H.tail(100).mean()[context.security]
    MA3 = H.tail(200).mean()[context.security]
    MA4 = H.tail(300).mean()[context.security]    
    MA5 = H.tail(400).mean()[context.security]    
    MA6 = H.tail(500).mean()[context.security]    
    MA7 = H.tail(600).mean()[context.security]    
    MA8 = H.tail(700).mean()[context.security]
    MA9 = H.tail(800).mean()[context.security]    
    MA10 = H.tail(900).mean()[context.security]        
    A2 = (MA1 + MA2)/2
    A3 = (MA1 + MA2 + MA3)/3
    A4 = (MA1 + MA2 + MA3 + MA4)/4
    A5 = (MA1 + MA2 + MA3 + MA4 + MA5)/5
    A6 = (MA1 + MA2 + MA3 + MA4 + MA5 + MA6)/6
    A7 = (MA1 + MA2 + MA3 + MA4 + MA5 + MA6 + MA7)/7
    A8 = (MA1 + MA2 + MA3 + MA4 + MA5 + MA6 + MA7 + MA8)/8
    A9 = (MA1 + MA2 + MA3 + MA4 + MA5 + MA6 + MA7 + MA8 + MA9)/9
    A10 = (MA1 + MA2 + MA3 + MA4 + MA5 + MA6 + MA7 + MA8 + MA10)/10
    
    if A2<A3 and A3<A4 and A4<A5 and A5<A6 and A6<A7 and A7<A8 and A8<A9 and A9<A10 and cash>SPY:
        Shares = int(cash/SPY)
        order(context.security, + Shares)
        log.info("Buying %s" % (context.security.symbol))
    elif A2>A3 or A3>A4 or A4>A5 or A5>A6 or A6>A7 or A7>A8 or A8>A9 or A9>A10:
        order_target(context.security, 0)
        log.info("Selling %s" % (context.security.symbol))
    
    if A2>A3 and A3>A4 and A4>A5 and A5>A6 and A6>A7 and A7>A8 and A8>A9 and A9>A10 and cash>SPY:
        Shares = int(cash/SPY)
        order(context.security, - Shares)
        log.info("Selling %s" % (context.security.symbol))
    elif A2<A3 or A3<A4 or A4<A5 or A5<A6 or A6<A7 or A7<A8 or A8<A9 or A9<A10:
        order_target(context.security, 0)
        log.info("Buying %s" % (context.security.symbol))
        
  
        
    record('Leverage', context.account.leverage)
    
    
    
    
    
    
    
There was a runtime error.