Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Statistical Arbitrage Part II - Steady Gains

Hey guys, I updated my algorithm and it is making a pretty steady amount of money. I am also seeing that my returns are suffering a bit but this is in contrast to the money I am making. I want to know if this is an 'illegal' algorithm in the sense that it is using money I don't have or it is having false success. Thanks.

def initialize(context):

    set_slippage(slippage.FixedSlippage(spread=.05))  
    set_commission(commission.PerTrade(cost=7))

    context.jblu = sid(23599)  
    context.dal = sid(33729)  
    context.luv = sid(4589)  
    context.ual = sid(28051)

    context.max_notional = 100000.1  
    context.min_notional = -100000.0

def handle_data(context, data):

    record(Cash=context.portfolio.cash)

    pavg = ((((data[context.jblu].close_price)-(data[context.jblu].open_price))/(data[context.jblu].open_price))*100)  
    pavg2 = ((((data[context.dal].close_price)-(data[context.dal].open_price))/(data[context.dal].open_price))*100)  
    pavg3 = ((((data[context.luv].close_price)-(data[context.luv].open_price))/(data[context.luv].open_price))*100)  
    pavg4 = ((((data[context.ual].close_price)-(data[context.ual].open_price))/(data[context.ual].open_price))*100)

    price = ((((data[context.jblu].close_price)-(data[context.jblu].open_price))/data[context.jblu].open_price)*100)  
    average = ((pavg + pavg2 + pavg3 + pavg4)/4)  
    notional = context.portfolio.positions[context.jblu].amount * price

    if price < (average) and notional > context.min_notional:  
        message = context.portfolio.positions[context.jblu].amount  
        log.info(message)  
        message = context.portfolio.cash  
        log.info(message)  
        message = 'Buy'  
        log.info(message)  
        order(context.jblu, -10)  
    elif price >= (average) and context.portfolio.positions[context.jblu].amount > 1 and notional < context.max_notional:  
        message = context.portfolio.positions[context.jblu].amount  
        log.info(message)  
        message = context.portfolio.cash  
        log.info(message)  
        message = 'Sell'  
        log.info(message)  
        order(context.jblu, +10)  
2 responses

Here is the code with a backtest so you can see the cash graph which I told it to record.

Hi Jeremy,

Yes, unfortunately, this is a false success. You're generating cash, but that's only because you keep selling more and more and more stock short. That gives you cash - but you owe so much stock, you're actually losing money at a terrible rate. You should believe that "returns" number - it's telling you that you lost a third of your money, and it's true, you did!

I cloned your algo and added two more lines to the graph. One is your total portfolio value - it's going down even though your cash is going up. The other is the number of shares you own of jblu. You go very short.

Hope this helps!

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.