csingley / ofxtools

Python OFX Library
Other
301 stars 68 forks source link

Request for suggestion on how to get positions #152

Closed emin63 closed 2 years ago

emin63 commented 2 years ago

Thank you for writing ofxtools. I am hoping to use it to pull together my positions across brokerage accounts.

Do you have any hints or suggestions on how to do that?

From poking around, it seems like I should do something like the following:

  1. Download ofx via something like
    • ofxget stmt vanguard --all -u $MY_USERNAME > /tmp/vanguard.ofx
  2. Start python via python -i, import ofx stuff and convert the statement via something like
    • from ofxtools.Parser import OFXTree
    • parser = OFXTree()
    • parser.parse('/tmp/vanguard.ofx')
    • ofx = parser.convert()
      1. Then I should pull out a statement (e.g., the first one) and look at invposlist via something like
    • stmt = ofx.statements[0]
    • positions = stmt.invposlist
      1. Finally, I should pick a position (e.g., the first one) and show info about it like:
    • my_pos = positions[0]
    • print(f'{my_pos.postype} {my_pos.units} of {my_pos.secid.uniqueid}')

Does that look right or am I missing something? Partly the reason I ask is because I didn't see any mention of invposlist in the docs. Maybe you intend to mainly work with the transactions and then the positions just come out right without having to extract them?

Thanks again.

csingley commented 2 years ago

It sounds like you’ve got the gist! Just follow the data structures and you’ll be fine. I encourage you to inspect the investment model classes… I went to some trouble to make the class definitions for INVPOSLIST et al. as clear as I could, including abundant references to the OFX spec.

You might be worrying about a perceived disconnect with the docs. In the docs I mainly focused on deviations from the spec. These mainly involve some convenience functions I added to the object model. If the docs are silent, it means I just followed the spec.

csingley commented 2 years ago

Maybe you intend to mainly work with the transactions and then the positions just come out right without having to extract them?

Certainly not!! I just wasn’t trying to write a primer on financial accounting for securities.

The foundational technique of accounting is to calculate the same quantity via 2 different independent methods and check if they agree… think balancing your check book, or those cop shows where the police question different suspects in isolation to see if their stories line up.

In this case, you keep your own record of portfolio positions. You “roll forward” the opening balances by applying the transactions reported by the custodian (a broker in this case), and then check that the closing balances you compute match those reported by the broker. N.b. We’re here accounting for units, e.g. shares of common stock, not dollars

csingley commented 2 years ago

You will find that the OFX data model for securities transactions is terribly lacking, and then badly implemented by the brokers. Double checking against the INVPOSLIST helps narrow down flaws in the transaction data. You will spend a lot of time parsing key data (e.g. CUSIPs) out of the transaction memo field, using methods that vary from broker to broker.

emin63 commented 2 years ago

Thanks for the pointers. I found INVPOSLIST very helpful. I'm closing this ticket but may later post some samples of how I used this in case it will be helpful to others.