Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
High Gains - Low Returns. Is this right?

I have made a recent algorithm that is generating negative returns but my overall cash gain is high. Can anyone explain this?

def initialize(context):  
      
    #set_slippage()  
        #Come back to this later  
    #set_slippage(MyCustomSlippage(slippage-param))  
      
    #window = 255  
    #refresh_rate = 10  
      
    context.jblu = sid(23599)  
    context.dal = sid(33729)  
    #context.aal = sid(45971)  
    context.luv = sid(4589)  
    context.ual = sid(28051)  
    context.current_stock = sid(23599)  
    #context.current_stock = sid(33729)  
    #context.current_stock = sid(45971)  
    #context.current_stock = sid(4589)  
    #context.current_stock = sid(28051)  
    #context.orders_placed = 0  
      
    context.max_notional = 100000.1  
    context.min_notional = -100000.0

def handle_data(context, data):  
      
    record(Cash=context.portfolio.cash)  
    #record(amount=context.portfolio.amount)  
        #^This is incorrect in its current state - try to illustrate number of shares held for current sid  
      
    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)  
    #price2 = data[context.dal].price  
    #price3 = data[context.aal].price  
    #price3 = data[context.luv].price  
    #price4 = data[context.ual].price  
      
    average = ((pavg * pavg2 * pavg3 * pavg4)/4)  
    #This is the average of all four stocks whcih JetBlue will be bought in comparison to  
      
    #returns = data[context.jblu].returns  
      
    notional = context.portfolio.positions[context.jblu].amount * price

    message = average  
    log.info(message)  
    message = price  
    log.info(message)  
      
    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,+1)  
    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_target_percent(context.jblu,-1)  
        return()  
          
    #if context.orders_placed < 1:  
     #   order(context.current_stock, -1000)  
      #  context_orders_placed=1  
3 responses

You are shorting the stock. Instead of -1 use 0.

order_target_percent(context.jblu,-1)  

If I make it:

order_target_percent(context.jblu,0)  

instead, then it doesn't give me any returns although the cash return still fluctuates. Is there something wrong with the code or is it unrealistic if I am shorting with:

order_target_percent(context.jblu,-1)  

?

Here are some ways to record variables to understand what your algo is doing.

To record the number of shares you have per stock (in this case JetBlue) you can use:

record(amount=context.portfolio.positions[context.jblu].amount  

and you can track your returns by saying:
record(port_returns = context.portfolio.returns)

For the slippage model, here is a good overview article . Granted it's from a Futures mag, but the idea is the same :)

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.