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.