Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why am I making (theoretical) money?

I used the example "multiple SID" algorithim with minor modifications. I'm a bit green when it comes to the stock market, so I'm wondering why I'm getting a large amount of profit after starting with 100k capital, despite having negative returns (and stocks?) Backtests I run show at least positive returns at points, but my recent live trading shows nothing but a steady decline, yet my portfolio's funds grow. Is it a bug in Quantopian?

    # This example runs the same momentum play as the first sample  
    # (https://www.quantopian.com/help#basic-algo), but this time it uses more  
    # securities during the backtest.  
    # Important note: All securities in an algorithm must be traded for the  
    # entire length of the backtest.  For instance, if you try to backtest both  
    # Google and Facebook against 2011 data you will get an error; Facebook  
    # wasn't traded until 2012.

    # First step is importing any needed libraries.

import datetime  
import pytz

def initialize(context):  
    # Here we initialize each stock.  Note that we're not storing integers; by  
    # calling sid(123) we're storing the Security object.  
    context.stocks = [sid(3951), sid(7027), sid(351), sid(1234), sid(24)]  
    context.vwap = {}  
    context.price = {}  
    # Setting our maximum position size, like previous example  
    context.max_notional = 1000000.1  
    context.min_notional = -1000000.0

    # initializing the time variables we use for logging  
    utc = pytz.timezone('UTC')  
    context.d=datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=utc)

def handle_data(context, data):  
    # Initializing the position as zero at the start of each frame  
    notional=0  
    # This runs through each stock.  It computes  
    # our position at the start of each frame.  
    money = []  
    for stock in context.stocks:  
        price = data[stock].price  
        money.append(price*context.portfolio.positions[stock].amount) # price*amount of shares  
        notional = notional + context.portfolio.positions[stock].amount * price  
        tradeday = data[stock].datetime  
    # This runs through each stock again.  It finds the price and calculates  
    # the volume-weighted average price.  If the price is moving quickly, and  
    # we have not exceeded our position limits, it executes the order and  
    # updates our position.  
    for stock in context.stocks:  
        vwap = data[stock].vwap(3)  
        price = data[stock].price  

        if price < vwap * 0.995 and notional > context.min_notional:  
            order(stock,+100)  
            notional = notional - price*100  
        elif price > vwap * 1.005 and notional < context.max_notional:  
            order(stock,-100)  
            notional = notional + price*100

    # If this is the first trade of the day, it logs the notional.  
    if (context.d + datetime.timedelta(days=1)) < tradeday:  
        log.debug(str(notional) + ' - notional start ' + tradeday.strftime('%m/%d/%y'))  
        context.d = tradeday  
        record(Intel_money=money[0], Sonic_money=money[1], AMD_money=money[2], Cheesecake_money=money[3], Apple_money=money[4])  
1 response

Hi Dylan,

I copied your code into a new algorithm and got the results below when I backtested it over the last year. Could you share the parameters that you were backtesting when you saw positive returns?

The easiest way to share a backtest in a new community post is in the IDE, click "Run Full Backtest" and once the backtest is complete, click "Share Results". This will show the backtest parameters and risk metrics for the algo.

One change I made to your code is I recorded your portfolio cash on line 29. In the custom data graph below, you can see that I start with $10,000 and the account becomes over-leveraged to a balance over $250,000, which I doubt a broker will allow. This leverage can easily be fixed by the code, if you can tell me what you're looking for.

Cheers,
Alisa

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.