title: KDB: pnl in FIFO manner
body: <p>Consider the following table:</p>
  
  <pre><code>Id Verb Qty Price
  `1 Buy  6 10.0
  `2 Sell 5 11.0
  `3 Buy  4 10.0
  `4 Sell 3 11.0
  `5 Sell 8 9.0
  `6 Buy  1 8.0
  etc...
  </code></pre>
  
  <p>What I would like is to associate a PNL with each transaction, computed on a FIFO (first-in-first-out basis).  Thus, for <code>Id=`1</code>, I want the PNL to be <code>-6*(10.0) +5*(11.0) + 1*(11.0) = +$6.00</code>, for <code>Id=`3</code>, Pnl is <code>-4*(10.0)+2*(11.0)+(2*9.0) = $0</code>, etc.</p>
  
  <p>In layman's terms, For the first buy-order of size 6, I want to offset this by the first 6 sells, and for the second buy-order of size 4, offset this with the subsequent 4 sells that have not been included in the pnl computation for the buy-6 order.</p>
  
  <p>Any advice?</p>