This article enumerates how the tax lot matching process can be modelled and the run in Couchbase. While the data model itself is a subjective thing, this article is going to only list the major entities involved in model and the N1QL query that can be used to run the Tax Lot matching process.
Before we dive into the solution I am assuming that the reader is aware or is willing to invest sometime getting familiar with some of the structures in N1QL which are very SQL like but has some extra JSON specific features.
NEST clause can be used against arrays to transpose rows to columns UNNEST can be used to dis-assemble an array into rowsSince JSON documents and N1QL are so feature rich taking a quick primer on the syntax would be very useful
http://training.couchbase.com/online
http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/index.html
What is a Tax Lot? Atax lotis a record of an opening transaction (e.g. purchase or short sale) in your portfolio. If an order is filled in multiple pieces, called partial fills, the prices will be averaged and will be represented by a singletax lotnumber. You can have multipletax lotswithin a security's overall position.Each time you purchase a security, the new position is a distinct and separate tax lot ― even if you already owned shares of the same security. (A tax lot is a record of a transaction and its tax implications, including the purchase date and number of shares.)
A tax lot identification method is the way we determine which tax lots are to be sold when you have a position consisting of multiple purchases made on different dates at differing prices, and you enter a trade to sell only part of the position. We are required by law to track and maintain this information, and to report the cost basis and proceeds to you and the IRS.
Your choice of tax lot ID method can have a significant impact on the amount of taxes you may pay when you sell an asset.
Tax Lot Matching MethodologiesThe Tax Optimizer lets you select one of the following lot-matching algorithms when changing
the matching method or running what-if scenarios. The queries that are being shown in this article will showcase only the FIFO method as it seems to be the most popular methodology but I am listing the methodologies all the same
First In, First Out (FIFO) The default method for matching tax lots. Sales are paired with the earliest purchases sequentially. FIFO assumes that assets remaining in inventory are matched to the most recently purchased or produced assets. FIFO is always used for futures and options on futures. Last In, First Out (LIFO) Each sale is paired with the most recent possible purchase. LIFO assumes that an entity sells, uses or disposes of its newest inventory first.Maximize Long-Term Gain- Sales are paired based on the following set of priorities:
1. Maximize Long-Term Gain per share
2. Maximize Short-Term Gain per share
3. Minimize Short-Term Loss per share
4. Minimize Long-Term Loss per share
Maximize Long-Term Loss- Sales are paired based on the following set of priorities:
1. Maximize Long-Term Loss per share
2. Maximize Short-Term Loss per share
3. Minimize Short-Term Gain per share
4. Minimize Long-Term Gain per share
Maximize Short-Term Gain- Sales are paired based on the following set of priorities:
1. Maximize Short-Term Gain per share
2. Maximize Long-Term Gain per share
3. Minimize Long-Term Loss per share
4. Minimize Short-Term Loss per share
Maximize Short-Term Loss- Sales are paired based on the following set of priorities:
1. Maximize Short-Term Loss per share
2. Maximize Long-Term Loss per share
3. Minimize Long-Term Gain per share
4. Minimize Short-Term Gain per share
Highest Cost- Seeks to maximize losses while minimizing gains. Sales are paired based
on the following rules:
First, it looks at all possible options for matching a closing trade to an open lot. If any possible matches would result in a loss, the method chooses the match thatresults in the largest possible loss.
If no possible match would result in a loss, the method chooses the match that resultsin the smallest possible gain.
Specific Lot- Lets you see all of your tax lots and closing trades, then manually match lots
to trades. Specific Lot is available for today or a position, but not it is available as the
Account Default Match Method.
FundId TraderId BrokerId Strategy InstrumentId TradeType Quantity TradeId TradeDate100
1000
1
FIFO
1
B
100
1
2011-01-01T05:00:00.000+0000
100
1000
1
FIFO
1
S
100
2
2011-01-02T05:00:00.000+0000
100
1000
1
FIFO
1
B
150
3
2011-01-03T05:00:00.000+0000
200
1000
1
FIFO
1
B
150
4
2011-01-04T05:00:00.000+0000
200
1000
1
FIFO
1
S
100
5
2011-01-05T05:00:00.000+0000
200
1000
1
FIFO
1
B
200
6
2011-02-01T05:00:00.000+0000
300
1000
1
FIFO
1
B