alienbrett / PyAlly

Ally Invest API Module for Python3
https://alienbrett.github.io/PyAlly/
MIT License
57 stars 28 forks source link

Accounts fails to parse when multiple accounts exist #68

Open garygause opened 3 years ago

garygause commented 3 years ago

I have three ally stock accounts and when running:

a.accounts()

the json data returned by Ally is not parsed properly into a dataframe. It contains only one row/column with all the json data in it. Also, I might expect it to only return info for the account number listed when instantiating the ally object, rather than all accounts.

Works fine with accounts(dataframe=False) but then has to be parsed manually. No big deal, but thought I would point it out.

LaikaN57 commented 3 years ago

I took a quick look at this. It looks like it is "working", but maybe not(?). Let me try to explain a bit...

The GET Accounts API shows that we will end up with JSON like this:

{
  "response": {
    "accounts": {
      "accountsummary": [
        {
          "account": ... ,
          "accountbalance": { ... },
          "accountholdings": { ... }
        },
        ...
      ]
    }
  }
}

In our code, we extract the response and accounts keys but we do not extract the accountsummary key. I am not a Pandas expert but I believe this leads to a Pandas DataFrame that has a single row in a single named column accountsummary with the only cell containing an object-type. I would guess that you could then just cast the value of that cell to another Pandas DataFrame to get a Nx3 matrix where N is the number of accounts you have and the datatypes for the three columns are int, object, object (or similar).

... but wait...

I also saw that it looks like we solve for this by trying to flatten the JSON. When I did my quick investigation, it looked like this flattening was not happening(?). We should build out the unit tests for this function to verify that it is working correctly. I also found a good reference article about this type of scenario (referenced below).

Ref:

  1. https://medium.com/swlh/converting-nested-json-structures-to-pandas-dataframes-e8106c59976e