Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Can't figure out why I am making a loss with a simple algo

I have a very simple algo:

Buy a stock with limit price.
Sell with the limit price of that cost basis.

I am only ever buying 1 unit of stock and selling that unit only when there are no open orders for that stock. I must be missing something in the way that a limit sell order executes.

Here are some logs and it clear that pretty soon I've made a loss:

pnl: 0.000000
total value: 1000.000000
buying a unit under: 732.880000
pnl: 0.000000
total value: 1000.000000
pnl :-0.000000
total value: 1000.000000
brought at: 732.760000
selling a unit at over: 732.760000
pnl: 0.748000
total value: 1000.748000
buying a unit under: 733.508000
pnl: 0.748000
total value: 1000.748000
brought at: 732.960000
selling a unit at over: 732.960000
pnl :-0.252000
total value: 999.748000

7 responses

Hi Guarav, I cloned your algo and took a look. I think what's happening is that at the end of each of the two days in this backtest, you are holding 1 share in GOOG that at the end of the day, has dropped to below the price you paid for it. So your portfolio value is down. Does this make sense?

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.

I see, that would make sense. This however raises a question about how limit orders are executed. My expectation is that a limit order will not sell under the limit price, but in this case it must be doing that somehow.

Hi Guarav,

If you take a look at the Transaction History tab in Full Backtest mode, you'll see that your understanding of the limit order is actually correct. At the end of your algorithm, you're still holding a position in GOOG; it's not sold because it doesn't satisfy the limit order condition. So at the end you're left with a GOOG share that's lower than what you bought it for, plus whatever leftover cash.

I see, thanks. I think my log errors stem from the fact that it is difficult to know the exact fill price for an order.

After you run a "Full Backtest", you can use the "Transaction Details" tab to see the price at which your orders were filled. You can find this tab on the left of the backtest view!

Thanks Jamie, that helps clear things up.

Also use track_orders() to see what's going on. (The version in this backtest is better than the one at that link, so far, I have more to add before updating it there)

The first number is minute of the day.

2014-03-28 _orders:86 INFO   1   Buy 17 GOOG now 559.712175 limit 551.31   cash 10000  
2014-04-01 _orders:86 INFO   1      Bot 17 GOOG at 548.493   cash 663  
2014-04-01 _orders:86 INFO   1   Sell -17 GOOG now 548.493 limit 560.21   cash 663  
2014-04-01 _orders:86 INFO  19      Sold -17 GOOG at 561.557106   cash 10197  
2014-04-01 _orders:86 INFO  19   Buy 18 GOOG now 561.557106 limit 553.13   cash 10197  
2014-04-04 _orders:86 INFO 110      Bot 18 GOOG at 552.0931086   cash 259  
2014-04-04 _orders:86 INFO 110   Sell -18 GOOG now 552.0931086 limit 563.14   cash 259  
2014-04-09 _orders:86 INFO 388      Sold -18 GOOG at 563.3122836   cash 10399  
2014-04-09 _orders:86 INFO 388   Buy 18 GOOG now 563.3122836 limit 554.86   cash 10399  
2014-04-10 _orders:86 INFO  23      Bot 18 GOOG at 553.8682314   cash 429