Back to Community
Help! I'm trying to adjust my leverage by using SMA

Hello...I am very new to Python and Quantopian and I was wondering if you could let me know where I am going wrong here.

Basically I want to use the crossover SMA of SPY to define what leverage I should be using...now I'm not great at coding so Im not sure where I have gone wrong here?!? Please see my attached code.

Any help would be greatly appreciated. Thanks!

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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
 
    
def initialize(context):

    context.spy = [sid(8554)]

def iLong(context, data):
    
    if data.history(context.spy, 'price', 31, '1d')[:-1].mean() > data.history(context.spy, 'price', 71, '1d')[:-1].mean():
        iLong = 1.5
    if data.history(context.spy, 'price', 31, '1d')[:-1].mean() < data.history(context.spy, 'price', 71, '1d')[:-1].mean():
        iLong = 0.5        

def iShort(context, data):
    
    if data.history(context.spy, 'price', 31, '1d')[:-1].mean() > data.history(context.spy, 'price', 71, '1d')[:-1].mean():
        iShort = -1.0
    if data.history(context.spy, 'price', 31, '1d')[:-1].mean() < data.history(context.spy, 'price', 71, '1d')[:-1].mean():
        iShort = -0.5   

        
There was a runtime error.