8

I'm looking for a Python plugin that would calculate the realized P&L for a number of stock transactions using the FIFO method.

For example, assume we have the following three MSFT trades:

+75 MSFT 25.10
+50 MSFT 25.12
-100 MSFT 25.22

The sell of 100 shares at 25.22 would fully net against the buy of 75 at 25.10 and partially net against the buy of 50 at 25.12 i.e.

Realized P&L = 75 * (25.22 - 25.10) + 25 * (25.22 - 25.12) = $ 11.50

The outstanding position would be:

+25 MSFT 25.12

martineau
  • 119,623
  • 25
  • 170
  • 301
Nikola Miles
  • 81
  • 1
  • 1
  • 2
  • not looking for you to code it for me - rather, i'm looking for a library that handles the transactional and accounting aspect of trading. – Nikola Miles Jun 24 '10 at 17:47
  • 1
    An excel version is available here https://stackoverflow.com/questions/68351891/calculate-avg-price-realized-gain-unrealized-gain-via-udf-using-fifo-method – Kawalpreet Kaur Jul 27 '21 at 17:54

3 Answers3

4

This should be easy to write yourself in Python. "FIFO" is short for "first in, first out queue". Buys are added to the back of the queue. Sells munch buys (or parts of them) off the front of the queue.

Python's collection.deque (double-ended queue) is what you need for the mechanics.

John Machin
  • 81,303
  • 11
  • 141
  • 189
3

No Python, but the R project blotter --- which is part / core of the larger TradeAnalytics project on R-Forge does just that.

I recently needed a subset of the functionality in C++ and used the blotter code to benchmark / guide my port to C++. (That was at work, so no public C++ from that, sorry.)

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • The rpy http://rpy.sourceforge.net/ and rpy2 http://rpy.sourceforge.net/rpy2.html plugins to python let you call R code from within python. They're kind of a pain to use, but you can get them working fairly easily if you know R. It's probably overkill to use them in this situation though. – Wilduck Jun 24 '10 at 21:41
  • blotter will give `Period.Realized.PL` of $11.2 and `Period.Unrealized.PL` of $1.3 for the example above. it is not the same as fifo. blotter uses `TxnQty * ConMult * (PrevPosAvgCost - TxnAvgCost)` to calculate `Period.Realized.PL` – Jim Green Mar 01 '16 at 07:07
1

There is lesser known python package accfifo which does pretty good job when comes to FIFO accounting. It is pip installable: pip install accfifo and can be used to calculate Realized P&L and outstanding position.

from accfifo import Entry

# Create FIFO queue entry for each trade
#   +75 MSFT @25.10
#   +50 MSFT @25.12
#  -100 MSFT @25.22

fifo = FIFO([Entry(+75, 25.10), Entry(+50, 25.12), Entry(-100, 25.22)])

For this queue you can calculate outstanding quantity:

>>> fifo.stock
25

or if you want information at which prices were bought remaining stocks:

>>> fifo.inventory
deque([25 @25.12])

Realized P&L can be calculated using data from fifo.trace:

>>> fifo.trace
[[75 @25.1, -75 @25.22], [25 @25.12, -25 @25.22]]

>>> sum([entry.price * entry.quantity for step in fifo.trace for entry in step])
-11.5

which corresponds to calculations of original author.

izkeros
  • 790
  • 1
  • 5
  • 23