alpacahq / alpaca-trade-api-python

Python client for Alpaca's trade API
https://pypi.org/project/alpaca-trade-api/
Apache License 2.0
1.73k stars 534 forks source link

[Bug]: "TypeError: string indices must be integers" with incoming error messages #633

Closed critopadolf closed 2 years ago

critopadolf commented 2 years ago

Is there an existing issue for this?

Current Behavior

When I place a limit order for BCHUSD I get an error from alpaca. However, I do not know what the error is, because I get the message shown below:

alpaca_trade_api/rest.py", line 53, in init super().init(error['message']) TypeError: string indices must be integers

Expected Behavior

When I have an error in my order submission, the error handler should display the error response from alpaca. It appears that the 'error' variable is a string, not a dictionary. If this is possible then the type of the 'error' var should be checked and handled accordingly

SDK Version I encountered this issue in

alpaca-trade-api version 2.2.0

Steps To Reproduce

return api.submit_order(
            symbol='BCHUSD',
            side='buy',
            type='limit',
            limit_price = '117.307',
            qty='15.555',
            time_in_force='day',
            order_class='simple'
    )

Filled out the Steps to Reproduce section?

Anything else?

No response

critopadolf commented 2 years ago
def overrideRestAPI(cls):
    __class__ = cls
    # api method overide #
    def APIErrorInitOverride(self, error, http_error=None):
        try:
            super().__init__(error['message'])
        except:
            super().__init__(error)

        self._error = error
        self._http_error = http_error
    cls.__init__ = APIErrorInitOverride
overrideRestAPI(tradeapi.rest.APIError)

Here is a fix if anyone else comes across this issue, just paste this below your module import and you will be good to go. I am now getting the actual error which is "alpaca_trade_api.rest.APIError: limit price increment must be > 0.025"