egh / ledger-autosync

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

Security with cusip, but no ticker #90

Closed jeanralphaviles closed 4 years ago

jeanralphaviles commented 4 years ago

Importing a security from an InvestmentTransaction? which doesn't have a TICKER tag in the security list leads to the following error.

Traceback (most recent call last):
  File "ledgerautosync/cli.py", line 359, in <module>
    run()
  File "ledgerautosync/cli.py", line 355, in run
    import_ofx(ledger, args)
  File "ledgerautosync/cli.py", line 178, in import_ofx
    print_results(converter, ofx, ledger, txns, args)
  File "ledgerautosync/cli.py", line 74, in print_results
    print(converter.convert(txn).format(args.indent))
  File "/home/root/.local/lib/python3.8/site-packages/ledgerautosync/converter.py", line 138, in format
    retval += posting.format(indent, assertions)
  File "/home/root/.local/lib/python3.8/site-packages/ledgerautosync/converter.py", line 158, in format
    len(self.account) - len(self.amount.format())
  File "/home/root/.local/lib/python3.8/site-packages/ledgerautosync/converter.py", line 191, in format
    if re.search(r'[\s0-9]', self.currency):
  File "/usr/lib/python3.8/re.py", line 199, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

In my case, an empty currency argument here from self.maybe_get_ticker gets through and fails when trying to print later on.

I'm importing OFX from Vanguard. Here's what the security list section looks like.

<SECLIST>                                                                                                                                                                                                                                                                                  
  <MFINFO>                                                                      
    <SECINFO>                                                                   
      <SECID>                                                                   
        <UNIQUEID>92204E852</UNIQUEID>                                          
        <UNIQUEIDTYPE>CUSIP</UNIQUEIDTYPE>                                      
      </SECID>                                                                  
      <SECNAME>Vanguard Target Retirement 2060 Trust Select</SECNAME>           
      <FIID>1685</FIID>                                                         
      <UNITPRICE>44.11</UNITPRICE>                                              
      <DTASOF>20200116160000.000[-5:EST]</DTASOF>                               
      <MEMO>Price as of date based on closing price</MEMO>                      
    </SECINFO>                                                                  
  </MFINFO>                                                                     
</SECLIST>  

Inserting the following tag gets it working.

<TICKER>VTTSX</TICKER>

Would it be possible to fallback to printing the cusip if no ticker is found? Seems like a bug from https://github.com/egh/ledger-autosync/pull/12.

I'd be willing to make a jab at making the fix if you think it would be proper to fallback to printing the cusip.

Aside: Thanks for your work on ledger-autosync, it's really great 👍.

egh commented 4 years ago

Thanks for the report @jeanralphaviles ! It seems reasonable to me to print out the CUSIP if their is no ticker information. If you can file a PR that would be very much appreciated!