sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.97k stars 2.19k forks source link

Creating an order in Binance Features Testnet results in KeyError: 'code' #915

Closed barkntuncer closed 3 years ago

barkntuncer commented 3 years ago

Dear friends, I am trying to send an order to the Binance Features Testnet with the code below. However, there is an error I couldn't understand why. I searched online but couldn't find any solutions. Code:

import json, config
from binance.client import Client
from binance.enums import *

lev = 5
client = Client(config.API_KEY_TESTNET2, config.SECRET_KEY_TESTNET2, testnet=True)
client.API_TESTNET_URL = "https://testnet.binancefuture.com"
try:
    client.futures_change_leverage(symbol='ETHUSDT',leverage = lev)
    client.futures_change_margin_type(symbol='ETHUSDT',marginType = 'ISOLATED')
    print('The futures type is set to ISOLATED')
except:
    print('The futures type is set to ISOLATED!!')

symbol = 'ETHUSDT'
side = 'BUY'
order_type = 'MARKET'
quantity = 0.5
order = client.create_order(symbol = symbol,
                                            side = side,
                                            type = order_type,
                                            quantity = quantity)

and the output is:

The futures type is set to ISOLATED!!
Traceback (most recent call last):

  File "<ipython-input-34-aeae8d721130>", line 1, in <module>
    runfile('/home/barkntuncer/BinanceBot/dnm.py', wdir='/home/barkntuncer/BinanceBot')

  File "/usr/lib/python3/dist-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "/usr/lib/python3/dist-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/barkntuncer/BinanceBot/dnm.py", line 22, in <module>
    order = client.create_order(symbol = symbol,

  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 1387, in create_order
    return self._post('order', True, data=params)

  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 368, in _post
    return self._request_api('post', path, signed, version, **kwargs)

  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 328, in _request_api
    return self._request(method, uri, signed, **kwargs)

  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 309, in _request
    return self._handle_response(self.response)

  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 318, in _handle_response
    raise BinanceAPIException(response, response.status_code, response.text)

  File "/usr/local/lib/python3.8/dist-packages/binance/exceptions.py", line 14, in __init__
    self.code = json_res['code']

KeyError: 'code'

I believe the api keys are correct. I took them from the Binance Futures testnet page. Thank you for your help.

Edit 1: Changing the parameters to enums like SIDE_BUY did not help

barkntuncer commented 3 years ago

Apparently, you have to use client.futures_create_order to achieve what I am trying for future references. :)