Dear all,
I am trying to place a multiple order composed of a market order and a trailing stop loss order of the opposite side.
I fail in putting the parameter batchOrders in JSON format. I get the error message:
TypeError: Object of type Decimal is not JSON serializable
I suspect it is because one of my parameter (quantity) is in Decimal format. Any idea how can I solve this? Many thanks in advance.
please find below the code used:
def PlaceFutureBatchOrder(self, log_filename, symbol:str, side:str, Order_type:str, quantity:float=0, price:float=0):
params1 = {
'symbol': symbol,
'side': side, # BUY or SELL
'type': Order_type, # MARKET, LIMIT, STOP_LOSS etc
'quantity': quantity,
}
params2 = {
'symbol': symbol,
'type': "TRAILING_STOP_MARKET",
'quantity': quantity,
'callbackRate': 0.9
}
if params1['side'] == "SELL":
params2['side'] = "BUY"
else:
params2['side'] = "SELL"
if Order_type != 'MARKET' and Order_type != 'STOP_LOSS' and Order_type != 'TAKE_PROFIT':
params1['timeInForce'] = 'GTC'
params1['price'] = self.floatToString(price)
if Order_type == 'STOP_LOSS' or Order_type == 'TAKE_PROFIT':
params1['stopPrice'] = self.floatToString(price)
param_head = [params1, params2]
json.dumps(param_head)
params = {
'batchOrders': param_head,
'recvWindow': 5000,
'timestamp': int(round(time.time()*1000))
}
self.signRequest(params)
url = self.base + self.endpoints['BatchOrder']
try:
response = requests.post(url, params=params, headers=self.headers)
data = response.text
except Exception as e:
print(" Exception occurred when trying to place order on "+url)
print(e)
data = {'code': '-1', 'msg':e}