Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why is my algorithm so volatile and why does it have so much leverage?

My algorithm tries to mimic Paul Tudor Jones' trading strategy which is to simply sell a stock if its 200 SMA is below the stock price and buy otherwise. However, during the back-test, I see that it's really volatile and returns go into the hundred and thousand percents at times; I've also checked that at most times, my leverage elevates to above 100 which is crazy! Please help me, thank you.

7 responses

A couple of things:
1: You never actually use the max or min notional in your logic.
2: Even though you define "max_leverage", you never use it.
3: On lines 59 and 60 "leverage" and "context.account.leverage" are the same thing so it would be impossible to ever reach a "limit"-you set that in line 53 when you put "leverage = context.account.leverage"
4: If you are infact approaching the leverage limit, your algorithm does nothing but tell you about it.
5: "context.entered_short" is useless because when you open a short position you never use "context.entered_short" at all
6: You never actually liquidate the short position-to do that you need to buy back the shares not set the target position to 0.
7: You do not need the "return" statement at the end.

I'll edit the code to make it what you want one second...



def initialize(context):  
    context.stocks = symbols('AAPL', 'ABT', 'A')  
    context.order_placed = False  
    context.long_leverage = 2  
    context.short_leverage = -2  
def handle_data(context, data):  
    for stock in context.stocks:  
        record(leverage = context.account.leverage)  
        sma = data[stock].mavg(200)  
        price = data[stock].price  
        context.long_weight = context.long_leverage/len(context.stocks)  
        context.short_weight = context.short_leverage/len(context.stocks)  
        if sma > price:  
            order_target_percent(stock, context.long_weight)  
        elif sma < price:  
            order_target(stock, 0)  
        if sma < price and context.order_placed == False:  
            order_target_percent(stock, context.short_weight)  
            context.order_placed = True  
        elif sma > price and context.order_placed == True:  
            order_target_percent(stock, context.long_weight)  

Simply copy and paste this into a blank algorithm.

Thank you so much, this really helped me!

Building on Owen's suggestion, I made a couple more tweaks. I used history() to get the moving average for the last 200 days and added a check for open orders before placing subsequent orders:

def initialize(context):  
    context.stocks = symbols('AAPL', 'ABT', 'A')  
    context.order_placed = False  
    context.long_leverage = 2  
    context.short_leverage = -2  
def handle_data(context, data):  
    record(leverage = context.account.leverage)  
    prices = history(200, '1d', 'price').mean()  
    for stock in context.stocks:  
        sma = prices[stock]  #index into the prices for each stock  
        price = data[stock].price  
        context.long_weight = context.long_leverage/len(context.stocks)  
        context.short_weight = context.short_leverage/len(context.stocks)  
        if get_open_orders(stock):  
            continue  
        if sma > price:  
            order_target_percent(stock, context.long_weight)  
        elif sma < price:  
            order_target(stock, 0)  
        if sma < price and context.order_placed == False:  
            order_target_percent(stock, context.short_weight)  
            context.order_placed = True  
        elif sma > price and context.order_placed == True:  
            order_target_percent(stock, context.long_weight)  
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

len(context.stocks) what does that bit of code do? Also Alisa when I backtest your code it doesn't place any orders at all

Hello, len basically returns the length of a specific string. in this case, the string is context.stocks.

May be this will work.
I have changed the logic on opposite and did some magic touch.