egh / ledger-autosync

Synchronize your ledger-cli files with your bank.
GNU General Public License v3.0
272 stars 42 forks source link

Bug in expired short option position from Vanguard #105

Open emin63 opened 4 years ago

emin63 commented 4 years ago

I think there is a bug in processing expired short option positions from Vanguard.

What happened is I sold 20 puts for $0.25 and they were assigned to me (i.e., the person who bought the puts exercised them early and sold me 2000 shares of stock). The buy of 2000 shares shows up correctly but the short puts never go away.

Below is an example of what ofxclient is downloading which ledger-autosync is having trouble parsing:

<INVTRAN><FITID>112283661<DTTRADE>20200430160000.000[-5:EST]
<DTSETTLE>20200501160000.000[-5:EST]<MEMO>SELL</INVTRAN>
<SECID><UNIQUEID>Z9713494Z<UNIQUEIDTYPE>CUSIP</SECID>
<UNITS>-20.0<UNITPRICE>0.25<COMMISSION>20.0<FEES>0.01
<TOTAL>479.99<SUBACCTSEC>CASH<SUBACCTFUND>CASH</INVSELL><OPTSELLTYPE>SELLTOCLOSE<SHPERCTRCT>0</SELLOPT>
... lots of random stuff
<OPTINFO>
  <SECINFO><SECID><UNIQUEID>Z9713494Z<UNIQUEIDTYPE>CUSIP</SECID>
  <SECNAME>PUT QEP RESOURCES INC $1<TICKER>Z9713494Z<MEMO>SELL</SECINFO>
  <OPTTYPE>PUT<STRIKEPRICE>0.0<DTEXPIRE>20200515160000[-5:EST]
  <SHPERCTRCT>0
</OPTINFO>

I end up seeing something like the following in the output:

2020/04/30=2020/05/01 SELL
    Vanguard:27467766                           -20.0 "Z9713494Z" @ $0.25
    ; ofxid: 15103.27467766.112283661
    Assets:Unknown                                          $5.00

which is the opening position where I sell the puts but the "Z9713494Z" position never gets closed out in the output of ledger-autosync.

I can't follow the python code very well but it seems like the second "Z9713494Z" transaction is ignored in converter.py.

This may well be a bug in Vanguard's OFX output but it seems odd that converter.py isn't even seeing the second "Z9713494Z" transaction.

egh commented 4 years ago

Probably we need to add something here: https://github.com/egh/ledger-autosync/blob/master/ledgerautosync/converter.py#L487

To be honest that kind of transaction is beyond me. What output would you expect to see for this kind of transaction?

emin63 commented 4 years ago

To be honest that kind of transaction is beyond me.

Fair enough. Selling puts is often a stupid thing to do anyway so you could argue not supporting it is doing people a favor. :)

What output would you expect to see for this kind of transaction?

I want a transaction so that when the assignment happens the option position goes away. The first transaction below is what ledger-autosync produces for buying the stock (which is correct). The second transaction is what I manually added to fix things to note that the option position goes away.

2020/05/15=2020/05/19 BUY
    Vanguard Brokerage Account                      2000.0 QEP @ $1.0
    ; ofxid: 15103.27467766.586242039
    Assets:Unknown                                      -$2000.00

2020/05/15=2020/05/19 OPTION ASSIGNED
    Vanguard Brokerage Account                   20.00 "Z9713494Z" @ $0.0
    ; manual fix due to bug discussed in github issue 105 of ledger-autosync
    Assets:Unknown                                       $0.00
egh commented 4 years ago

Thanks for the example! Is the only information about the put available in the SECINFO part? Is there only the one sell transaction in the OFX file? I'm not sure if l-a is not getting enough information from the https://github.com/jseutter/ofxparse library, or if it just isn't doing the right thing with the data it's getting.