I am not sure if my OCO order has all the correct fields. I am trying to test this, but I get an Invalid JSON: illegal number, offset: 0x00000114 JSON error. I am using Python examples from supahcullen/**[Tradovate-Python-Client]. I have this one script I created for a StopOrder with a StopLoss and TakeProfit for testing.
Here is the code. Some variable values are imported and not shown.
#Place TV Stop limit order
import requests
from GetAccessToken import getAccessToken
from config import *
import time
Be careful with this one! It will submit an order!
buysell must be either “Sell” or “Buy”
def placeStopLimitOrder(live, Qty, buysell, orderPrice, stoploss, takeProfit, token):
“”"Places StopLimit order with quantity “Qty”, direction “buysell”, price “orderPrice”, and includes StopLoss and TakeProfit
Args:
live (bool): True = live, False = demo
Qty (int): Quantity of contracts you want to buy
buysell (string): "Buy" or "Sell"
orderPrice (int): Price you want the order at
stoploss (double): Stop Price
takeProfit (double): Take Profit Price
token (string): User's access token
Returns:
int: orderId of order you just placed
"""
ocoDirection = ""
if buysell == "Buy":
ocoDircetion = "Sell"
else:
ocoDirection = "Buy"
header = {
'Authorization': f'Bearer ${token}',
'Accept': 'application/json'
}
if live:
url = "https://live.tradovateapi.com/v1/order/placeorder"
accId = LIVE_ACCOUNT_ID
else:
url = "https://demo.tradovateapi.com/v1/order/placeorder"
accId = DEMO_ACCOUNT_ID
order = {
"accountSpec": USER_NAME, //imported value
"accountId": accId, //imported value
"action": buysell,
"symbol": SYMBOL, //imported value
"orderQty": Qty,
"orderType": "StopLimit",
"Price": orderPrice,
"timeInForce": "GTC",
"isAutomated": "true",
"bracket1": {
"action": ocoDirection, //For some reason this is empty
"orderType": "Stop",
"price": stoploss,
"timeInForce": "GTC"
},
"bracket2": {
"action": ocoDirection, //For some reason this is empty
"orderType": "Stop",
"price": takeProfit,
"timeInForce": "GTC"
}
}
# Make request
res = requests.post(url, headers=header, data=order)
# Error stuff
if res.status_code != 200:
print("StopLimit Order Failed")
print(res.text)
print(res.status_code)
print(res.reason)
print(order)
orderId = res.json()['orderId']
return orderId
This is the error I get back.
StopLimit Order Failed
Invalid JSON: illegal number, offset: 0x00000114
400
Bad Request
{‘accountSpec’: ‘HIDDEN’, ‘accountId’: ‘HIDDEN’, ‘action’: ‘Buy’, ‘symbol’: ‘ESH3’, ‘orderQty’: 1, ‘orderType’: ‘StopLimit’, ‘Price’: 3943, ‘timeInForce’: ‘GTC’, ‘isAutomated’: ‘true’, ‘bracket1’: {‘action’: ‘’, ‘orderType’: ‘Stop’, ‘price’: 3942, ‘timeInForce’: ‘GTC’}, ‘bracket2’: {‘action’: ‘’, ‘orderType’: ‘Stop’, ‘price’: 3944, ‘timeInForce’: ‘GTC’}}
Traceback (most recent call last):
File “C:\Python311\Lib\site-packages\requests\models.py”, line 960, in json
return complexjson.loads(self.content.decode(encoding), **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Python311\Lib\json_init_.py”, line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Python311\Lib\json\decoder.py”, line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Python311\Lib\json\decoder.py”, line 355, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “HIDDEN\Tradovate-Python-Client-main\placestoplimitorder.py”, line 84, in
print(placeStopLimitOrder(False, 1, “Buy”, 3943, 3942, 3944, token))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “HIDDEN\Tradovate-Python-Client-main\placestoplimitorder.py”, line 78, in placeStopLimitOrder
orderId = res.json()[‘orderId’]
^^^^^^^^^^
File “C:\Python311\Lib\site-packages\requests\models.py”, line 968, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
What am I missing??