csingley / ibflex

Python parser for Interactive Brokers Flex XML statements
MIT License
85 stars 42 forks source link

Serial Number #55

Closed raph1900 closed 1 year ago

raph1900 commented 1 year ago

hey. (I edited the questions that why the topic isn't right) I was wondering if there is a way to arrange trades by the open and close. Sometimes a trade is open few days ago meanwhile other trade activity happens, only then a the trade was closed. so I can not find a way to attach the open of a trade with there close.

==================================== O 0.0 BUY MBTV2 2022-10-03 18:05:41 O 0.0 BUY BITO 2022-10-03 18:12:20 O 0.0 BUY BITO 2022-10-03 18:12:20 O 0.0 BUY BITO 2022-10-03 18:12:20 O 0.0 BUY BITO 2022-10-03 18:12:20 O 0.0 BUY BITO 2022-10-03 18:12:40 O 0.0 SELL NQZ2 2022-10-04 06:52:08 C 145.7 BUY NQZ2 2022-10-04 07:10:05

O 0.0 SELL NQZ2 2022-10-04 07:25:09 C -99.3 BUY NQZ2 2022-10-04 07:26:44 C 1328.9 SELL BITO 2022-10-04 11:05:10

here I tried to sort the trades by dates the were executed but it seems the order isn't right (open BITO at 10-3-22 closed at 10-4-22 but the close of NQZ2 was before.)

hope that make sense. thanks!

csingley commented 1 year ago

This library just parses the broker data, it doesn't implement complete trade journal logic. Matching opening basis with closing proceeds is a complex task, and there isn't one right way to do it. I don't know about other jurisdictions, but in the US you can elect different algorithms (FIFO, average cost, specific identification). The best signal you get from IB is in the transaction notes, which will indicate if a different basis-matching algorithm was in effect at the time of the trade. That's already way better than you get from other brokers....

ibflex just gets your data into Python, the processing is up to you. Good luck!