Search code examples
pythonkeyword-argument

Correct syntax for **params argument to API call


I am trying to use the python-binance wrapper to the Binance API to write a simple little trading bot, mostly for the sake of learning how. I am currently stuck trying to get the price of a particular trading pair using the client.get_symbol_ticker() function, which can be found here on line 827.

The string I need to send in is of the form "AAABBB" where AAA is the first ticker symbol and BBB is the symbol in which I want the price of AAA. However, I am not getting the syntax right. When I pass in a string:

client.get_symbol_ticker('ETHBTC')

I get TypeError: get_symbol_ticker() takes exactly 1 argument (2 given)

if I do

client.get_symbol_ticker(params = 'ETHBTC')

I get BinanceAPIException: APIError(code=-1104): Not all sent parameters were read; read '0' parameter(s) but was sent '1'.

Clearly I am just getting the syntax for the call wrong. The function prototype looks like this:

 def get_symbol_ticker(self, **params):
    """Latest price for a symbol or symbols.
    https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#24hr-ticker-price-change-statistics
    :param symbol:
    :type symbol: str

Can someone tell me the proper syntax to use when calling this function?


Solution

  • Your call should be client.get_symbol_ticker(symbol = 'ETHBTC'). The only way to know this is to look at the docstrings:

    :param symbol:
    :type symbol: str
    

    This tells you it expects a keyword parameter named symbol.

    **params means "take all keyword arguments and return them as a dictionary in the variable params". So in the case of the method call I provided above, params would be:

    {'symbol': 'ETHBTC'}