I want to assign a variable to the stop loss and take profit in the following phrase. According to the exchange documentation, these values are constants as follows(for example: 102217.88):
paramsMap = {
"symbol": symbol,
"side": side,
"positionSide": positionSide,
"type": "MARKET",
"quantity": amount,
"stopLoss": "{\"type\": \"STOP_MARKET\", \"stopPrice\": 102217.88,\"workingType\":\"MARK_PRICE\"}",
"takeProfit": "{\"type\": \"TAKE_PROFIT_MARKET\", \"stopPrice\": 102363.2178,\"price\": 102363.2178,\"workingType\":\"MARK_PRICE\"}"
}
When I use the string.format() function as below:
"stopLoss": "{\"type\": \"STOP_MARKET\", \"stopPrice\": {0},\"workingType\":\"MARK_PRICE\"}".format(str(sl)),
I get this error:
KeyError: '"type"'
So, how can I pass the sl variable value to this string?
Edit: I put the whole of my function that places an order in the exchange for more clarification.
def PlaceOrder(symbol, side, positionSide, amount, tp, sl):
payload = {}
path = '/openApi/swap/v2/trade/order'
method = "POST"
paramsMap = {
"symbol": symbol,
"side": side,
"positionSide": positionSide,
"type": "MARKET",
"quantity": amount,
"stopLoss": "{\"type\": \"STOP_MARKET\", \"stopPrice\": {},\"workingType\":\"MARK_PRICE\"}".format(str(sl)),
"takeProfit": "{\"type\": \"TAKE_PROFIT_MARKET\", \"stopPrice\": {},\"price\": 102363.2178,\"workingType\":\"MARK_PRICE\"}".format(str(tp))
}
paramsStr = request_client.parseParam(paramsMap)
response = request_client.send_request(method, path, paramsStr, payload)
return response.json()
According to JonSG's comment, I use the double {}
; however, I get this error again:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[4], line 5
3 tp, sl = Bingx.RiskReward(symbol, buy=True, risk=0.12, reward=0.06,leverage=30)
4 print(f"tp:{tp} sl:{sl}")
----> 5 response = Bingx.PlaceOrder(symbol, 'BUY', 'LONG', 0.0001, tp, sl)
6 response
Cell In[1], line 465, in Bingx.PlaceOrder(symbol, side, positionSide, amount, tp, sl)
457 path = '/openApi/swap/v2/trade/order'
458 method = "POST"
459 paramsMap = {
460 "symbol": symbol,
461 "side": side,
462 "positionSide": positionSide,
463 "type": "MARKET",
464 "quantity": amount,
--> 465 "stopLoss": "{\"type\": \"STOP_MARKET\", \"stopPrice\": {{}},\"workingType\":\"MARK_PRICE\"}".format(str(sl)),
466 "takeProfit": "{\"type\": \"TAKE_PROFIT_MARKET\", \"stopPrice\": {{}},\"workingType\":\"MARK_PRICE\"}".format(str(tp))
467 }
468 paramsStr = request_client.parseParam(paramsMap)
469 response = request_client.send_request(method, path, paramsStr, payload)
KeyError: '"type"'
Don't use string formatting to create nested JSON; use the json
library. Then you simply pass the variable as a value in the dictionary.
import json
paramsMap = {
"symbol": symbol,
"side": side,
"positionSide": positionSide,
"type": "MARKET",
"quantity": amount,
"stopLoss": json.dumps({"type": "STOP_MARKET", "stopPrice": sl,"workingType":"MARK_PRICE"}),
"takeProfit": json.dumps({"type": "TAKE_PROFIT_MARKET", "stopPrice": tp,"workingType":"MARK_PRICE"})
}