sammchardy / python-binance

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

Can't post OCO order #1217

Closed Karlheinzniebuhr closed 2 years ago

Karlheinzniebuhr commented 2 years ago

I've tried two methods of posting a margin OCO order.

params = {
        'symbol': SYMBOL_TO_TRADE,
        'side': tp_sl_side,
        'quantity': str(quantity),
        'price': str(tp_price),
        'stopPrice': str(tp_stop_price),
        'stopLimitPrice': str(sl_price),
        'stopLimitTimeInForce':'GTC',
        'isIsolated': isIsolated,
        'sideEffectType': 'MARGIN_BUY',
        }
oco_order = cls.client._post('margin/order/oco', True, data=params)

This one gives me the following error

raise BinanceAPIException(response, response.status_code, response.text)
binance.exceptions.BinanceAPIException: APIError(code=0): Invalid JSON error message from Binance: <html><body><h2>404 Not found</h2></body></html>

params = {
        'symbol': SYMBOL_TO_TRADE,
        'side': tp_sl_side,
        'quantity': str(quantity),
        'price': str(tp_price),
        'stopPrice': str(tp_stop_price),
        'stopLimitPrice': str(sl_price),
        'stopLimitTimeInForce':'GTC',
        'isIsolated': isIsolated,
        'sideEffectType': 'MARGIN_BUY',
        }
params_json = json.dumps(params)
oco_order = cls.client._post('margin/order/oco', True, data=params_json)

And this one also gives an error

kwargs = self._get_request_kwargs(method, signed, force_params, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\binance\client.py", line 269, in _get_request_kwargs
    kwargs['data']['timestamp'] = int(time.time() * 1000 + self.timestamp_offset)
TypeError: 'str' object does not support item assignment

How can I manage to place the order correctly?

The content of the json looks like this: {'symbol': 'BTCUSDT', 'side': 'BUY', 'quantity': '0.001', 'price': '18988.1', 'stopPrice': '20043.0', 'stopLimitPrice': '22152.8', 'stopLimitTimeInForce': 'GTC', 'isIsolated': 'TRUE', 'sideEffectType': 'MARGIN_BUY', 'timestamp': 1657923917979, 'signature': '9b6e1b6851c6dcb497d2...fc5dc19dcb'}

Karlheinzniebuhr commented 2 years ago

Managed to create an order using the OCO function, didn't realize it was already supported as it isn't mentioned in the docmentation

oco_order = cls.client.create_margin_oco_order(
    symbol=SYMBOL_TO_TRADE,
    side=tp_sl_side,
    quantity=quantity,
    price=tp_price,
    stopPrice=sl_price,
    stopLimitPrice=sl_price,
    stopLimitTimeInForce=TIME_IN_FORCE_GTC,
    isIsolated=isIsolated,
    sideEffectType="MARGIN_BUY")