alienbrett / PyAlly

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

holdings() not working? #47

Closed zhh210 closed 4 years ago

zhh210 commented 4 years ago

Looks like 1.0.4 changed the interface, when I was trying to pull my account holdings, an error was displayed:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-c7a8b39d842f> in <module>()
----> 1 a.holdings()

5 frames
/usr/local/lib/python3.6/dist-packages/ally/utils.py in format_strike(strike)
     65         # Pad strike with zeros
     66         def format_strike (strike):
---> 67                 x       = str(int(strike)) + "000"
     68                 return "0" * (8-len(x)) + x
     69         # Assemble

ValueError: invalid literal for int() with base 10: '7.5'
anthonydgallo commented 4 years ago

Getting this issue as well

alienbrett commented 4 years ago

Right away I can see that the problem is with an option contract, and that the strike is not a round integer. Would you mind posting the contract details and I can look into this? I've only built it with the assumption of integer strikes

zhh210 commented 4 years ago

Sure, the complete exception stack:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-c7a8b39d842f> in <module>()
----> 1 a.holdings()

5 frames
/usr/local/lib/python3.6/dist-packages/ally/Account/holdings.py in holdings(self, dataframe, block)
    103                 auth            = self.auth,
    104                 account_nbr     = self.account_nbr,
--> 105                 block           = block
    106     ).request()
    107 

/usr/local/lib/python3.6/dist-packages/ally/Api.py in request(self, block)
    166                         RateLimit.normal_update ( dict(x.headers), self._type )
    167 
--> 168                         return self.extract ( x )
    169 
    170 

/usr/local/lib/python3.6/dist-packages/ally/Account/holdings.py in extract(self, response)
     72                 holdings = response['accountholdings']['holding']
     73 
---> 74                 return list( map( Holdings._flatten_holding, holdings ) )
     75 
     76 

/usr/local/lib/python3.6/dist-packages/ally/Account/holdings.py in _flatten_holding(holding)
     47                                 exp_date        = op['matdt'][:10], # '2020-06-19T00:00:00-04:00'
     48                                 strike          = op['strkpx'],
---> 49                                 direction       = 'P' if op['putcall'] == '0' else 'C'
     50             )
     51 

/usr/local/lib/python3.6/dist-packages/ally/utils.py in option_format(symbol, exp_date, strike, direction)
     70         return str(symbol).upper() +\
     71                 datetime.datetime.strptime(exp_date,"%Y-%m-%d").strftime("%y%m%d") +\
---> 72                 direction + format_strike(strike)
     73 
     74 def option_strike(name):

/usr/local/lib/python3.6/dist-packages/ally/utils.py in format_strike(strike)
     65         # Pad strike with zeros
     66         def format_strike (strike):
---> 67                 x       = str(int(strike)) + "000"
     68                 return "0" * (8-len(x)) + x
     69         # Assemble

ValueError: invalid literal for int() with base 10: '7.5'
alienbrett commented 4 years ago

This is fixed in 1.0.5