Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Erratic results

Alright, so I just know I'm doing something wrong here. Can anyone tell me what it is? I'm just trying to implement logic to not buy more stocks than it should to cause my portfolio cash to go in the negatives. I'm pretty sure turning $1000 into $59,344,350,791 is just outrageous.

3 responses

Hi Timothy,

You are running into a common initial pitfall with our backtester which is that we do not enforce margin limits on your backtest by default - we do plan to implement a margin model in the future.

But as of now, your algo is buying or selling more shares of AAPL regardless of how much cash is in your portfolio. If you check out your log statemtent that prints how much cash you have you'll see it quickly goes negative which then makes your 'buy amount' a negative number of shares which our order method interprets as an order to sell the stock short (see: https://www.quantopian.com/help#api-order)

In the code snippet below I added an additional log statement to show you how many shares you're ordering. And I also added an additional IF statement that will only order place orders for a number of shares > 0 (that cuts your feedback loop off).

Attaching the backtest as well that includes this fix.
Best,
Jess

  # This is the meat of the algorithm, placed in this if statement.  If the  
  # price of the security is .5% less than the 3-day volume weighted average  
  # price AND we haven't reached our maximum short, then we call the order  
  # command and sell 100 shares.  Similarly, if the stock is .5% higher than  
  # the 3-day average AND we haven't reached our maximum long, then we call  
  # the order command and buy 100 shares.  
  log.info("Cash:" + str(portfolio_amount))  
  buy_amount = min(100, math.ceil(portfolio_amount / price))  
  log.info(buy_amount)  
  if buy_amount > 0:  
      if price  context.min_notional:  
        if buy_amount > 0 and portfolio_amount > 0:  
            log.info(context.portfolio.positions[context.stock].amount)  
            order(context.stock, buy_amount)  
      elif price > vwap * 1.005 and notional  
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.

Thanks for the reply Jess. This clears things up a bit. Is there any possible way to stop the backtest programmatically?

You can't currently insert a break statement in the IDE, though we have lots of requests to make the IDE interactive and we are definitely interested in meeting that need in the future. But I know we won't get to it this year.

The best options currently are using and abusing the logging capabilities (though we also do place limits on log lines so you have to be strategic with that) and running short sample backtests when you're in a debug mode and saving longer history tests for once you know you have your code working correctly.

Also if you get stuck please feel free to contact us via the [email protected] email address (clicking Feedback in the upper right on any page will open a window to this list as well) and the team here can give you a hand.

Best wishes,
Jess