Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Backtesting Problem(Positions vs Transactions)

I'm very new to the site so forgive me in advance if this is an already known problem:

On a full Backtest, the Positions seem to be totally disconnected from the Transactions. More specifically, I'm running a simple momentum strategy that is limited to owning around 100 stocks at any one time. When I begin the simulation say at 1/03/17, the code makes all the initial buys and the "Positions" in the Backtest results show the 100 specific stocks that I would expect given the logic of my code. However, when I look at the "Transactions" in the Backtest results it shows 460 transactions on 1/03/17. I can't make any sense out of this. In addition, I specify the initial starting capital to be $500,000, however the 460 transactions total some $1.3million worth of buys.

This certainly doesn't give one any faith in ANY of the the other results. Can anyone shed some light on this?

Thanks,
Ron

6 responses

First off, welcome!

The backtest 'positions' tab is an end of day snapshot of securities held. It shows the quantity of shares held (a negative value indicates a short position), the closing price (labeled 'unit price'), and the net unrealized gains.

The 'transactions' tab is a minute by minute list of what the algorithm bought and sold. It shows the quantity of shares traded (positive is bought negative is sold or shorted) and the price at which it was traded. This would be similar to the transaction statement one gets from your broker.

The number of transactions could very well be more than the final positions at the end of the day. If one trades a large number of shares the transaction may be spread across several minutes. This is controlled by the slippage model used (see the docs https://www.quantopian.com/help#ide-slippage ). Additionally, one could open and close a position during the day. This would create multiple transactions but net zero positions at the end of the day.

As far as generating $1.3M in buys but only starting with $500k capital. That is quite possible though may be unintended. The Quantopian backtester doesn't place any restrictions on leverage. One can buy more than one has cash. That's buying on margin and similar to how it would work with your broker. One needs to check portfolio value and cash before placing orders to ensure leverage doesn't get too high (1.0 for the contest).

You can attach a backtest to a post here in the forums using the dropdown in the upper right corner of the input text box. Doing that helps to better show/explain issues. If you wish, do that and I could perhaps walk through how specific transactions relate to the final positions.

Dan....Thank you for your detailed post. I totally understand how you can have more transactions than positions if you are buying AND selling on the same day. However, in my specific backtest, on the very FIRST day of the backtest there were 100 positions and 460 transactions and ALL of the 460 transactions were BUYS. There were NO Sells. This scenario is impossible. 460 buys on the first day of the backtest should produce 460 positions.

He's talking about partial fills, they happen as part of the default slippage model.

As an experiment, compare versus this, it will force complete fills instead of any partial.

def initialize(context):  
    set_slippage(slippage.FixedSlippage(spread=0))  

Also you can watch end of day values like this. record() only registers the last value each day so this is fine.
With that set_slippage() line active vs not, you'll see the differences.

def before_trading_start(context, data):  
    record(cash = context.porfolio.cash)  
    record(lvrg = context.account.leverage)  

Maybe append /old to the URL line in a full backtest to more easily view the custom chart.
By the way, two or more of the same backtest but with small changes can be run in different tabs or windows, I find that useful.

Blue Seahawk.....you are awesome. You are completely correct. Being new to the site, it never occurred to me that a backtest would default to partial fills for stocks in which your buying ~600 shares of a $36 stock trading 500,000 shares a day, but after I read your post I went back and looked at the transaction list and sure enough there were multiple transactions for a given asset. I will add your code to force complete fills. I really appreciate your help.

Take care,
Ron

Yeah that line setting slippage to 0 can be useful temporarily in gathering information. For approximating real-world trading, there are various preferences out there on the best slippage line to use. https://www.google.com/search?q=slippage+site:quantopian.com That sort of search is always handy. Thanks for the kind words Ron.

Other ways of contending with partial fills:

  • Reducing the default $10M initial capital. (as a test at least, often useful)
  • Canceling all orders some number of minutes after the ordering.
  • Filtering for a daily volume threshold in pipeline
  • Rebalancing some minutes before market close (at which point incomplete orders are automatically canceled).
  • And the margin Dan referred to is often from market conditions on a given day happening to be more partial fills in selling vs those that are buys, a difficult problem. So another route is scheduling sells first, followed by buys some time later. When also doing shorting, that introduces another level for coping with leverage needed, not of the margin type of leverage but the sort that results from short vs long & cash.

You're already examining transactions and another way to view ordering is track_orders() which will quickly overwhelm the logging window with 100 stocks except you can limit it to a list of 'symbols'. Looks intimidating but once used to it is a snap. One could keep a count of the number of partial fills with some code added to that and cancel any that reach a maximum allowed, for example.

Blue Seahawk....Thanks again. Your advice has been very helpful. I looked at the google link you suggested and I noticed that you and Dan have provided help to newbies like me in the past. Keep up the good work!!

Ron