Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Is it possible to track separate lots for the same security?

Lets say I buy 10 shares of AAPL on the 1st of the month and then buy another 10 shares on the 15th. In my portfolio I'll see portfolio.ammount == 20 and portfolio.cost_basis == the cost basis of the two purchases

At the end of the month I want to sell the 10 shares I bought on the 1st and keep the 10 shares I bought on the 15th. Is this possible?

The example above is a really simple example. I'm asking this question because I'd like to write a script that can split my portfolio's cash into several buckets and then test multiple trading strategies side-by-side. Right now I need to write and run one script per strategy and it's a time consuming process.

Thanks

7 responses

It is possible, but you have to write (or commission somebody else to write) all the bookkeeping code. You could create a class named eg. MyPortfolio, with functionality similar to that of the context.portfolio object, and instantiate as many MyPortfolio objects as you need, each with its own cash budget, securities list, and possibly initialize and handle_data functions.

Meanwhile, nothing currently stops you from selling at the end of the month only the number of shares you bought on the 1st. You can buy, sell, or short any number of shares at any time.

Well, portfolio tracking isn't the only thing that will get in your way. I was trying to see if I can better differentiate between long term and short term securities in my tax simulation, but decided doing so well would require keeping track of lots, and doing so well is difficult. There are many things that would be a headache, fortunately most of them don't seem to be tracked in the backtest. To name a few:

  1. Stock awards, splits, reverse-splits, etc.
  2. Related to stock awards, stock symbol change.
  3. M&A

If you use functions such as order_target_percent where you don't give a precise amount you're purchasing, that can also be challenging. Note that Position type in Quantopian doesn't have an open date.

In my case, since I already had a custom commission model, I would have extended it to record every transaction I made. At that point I would have the precise date, amount, price, and would be able to record the transaction. You would need some special handling for stocks that became unlisted, but that's relatively easy to handle: re-align the portfolio positions in the context object with the transactions you have recorded.

I decided this level of fidelity wasn't important to me, and did something a lot simpler.

Hope that helps.

Sunil

Thanks @Sunil, you seem to have a pretty good take on what I'm trying to do and I haven't even thought through more advanced scenarios like awards, splits and reverse-splits.

Last night I wrote a new script that lets me split my portfolio's cash into different buckets and allows each bucket to buy and sell independently. All of the script's shares are still dumped into a common portfolio but each bucket keeps track of the number of shares it purchases, the entry price for each lot and how much cash it is spending/earning based on it's share of the portfolio's total cash amount. The problem with this approach is that since buy/sell orders don't get executed immediately the buy and sell prices that the buckets track are always slightly off from the actual buy/sell price. When you look at each bucket's tabulated performance the end result is always a few percentage points off from the portfolio's actual performance. The margin of error increases pretty reliably based on the number of trades the bucket makes.

To make it work I need to know exactly when the buy/sell orders are executed and at what price. Not sure how this is handled by Quantopian's backtesting logic...

Hi Jay,

Not with the intent of promoting my implementation... But here's some code I'd written for tracking precise transaction details for tax modeling via the commission model:

https://www.quantopian.com/posts/implementation-of-tax-simulation

You should be able to incorporate this sort of approach into your code to get more precise transaction information.

Sunil

Hey Jay, thanks for the great question, I keep coming back to this thread.

How did you setup the portfolio buckets you mentioned? That would be massively helpful in the trading strategy I'm trying to implement. Going off of André's reply I have been exploring the zipline code hunting for the portfolio object in order to create these buckets and have them interact correctly, but it's been tedious. Did you use multiple initialize and handle_data functions as he mentioned?

I would also appreciate anyone else's input on this as well

Hi Derek,

I actually found this to be too complex to execute within Quantopian's suite. The way Quantopian manages your portfolio makes bookkeeping overly difficult for the types of testing I want to run, plus I started running into Quantopian's memory limit when I tried simulating too many strategies at the same time.

Instead, I written my own Python script that mimics Quantopian's behavior. I use this Yahoo Finance library to pull stock quotes (https://pypi.python.org/pypi/yahoo-finance) and I have a generic "Trader" class that can buy, sell and keep track of profits. This allows me to write custom traders and then test & compare all sorts of trading strategies side by side.

This worked very well before Quantopian launched their 2.0 platform but since the switchover I'm seeing a lot of discrepancies between my apps behavior and Quantopian's trading behavior. I haven't had time to debug this yet but if I get it sorted out I might drop the generic framework on github so other people can use and extend it.

My process is pretty simple.
1. I use my script to simulate and test a large number of solutions simultaneously.
2. After I see the results I can cherry pick the best options and drop them into Quantopian for individual testing.
3. If the Quantopian simulation matches my test script then I know I have a good strategy based on Quantopian's trading logic.