cullen-b / Tradovate-Python-Client

This library makes it easy to trade with the Tradovate API using Python!
MIT License
23 stars 9 forks source link

JSON Error With PlaceLimitOrder.py #2

Closed Wmang46 closed 1 year ago

Wmang46 commented 1 year ago

When I run the Placelimitorder I get the following error. Any ideas?

Limit Order Failed Invalid JSON: illegal number, offset: 0x00000090 400 Bad Request 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 "C:\Users\jk\Documents\Tradovate-Python-Client-main\placelimitorder.py", line 62, in print(placeLimitOrder(False, 1, "Buy", 3700, token)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\jk\Documents\Tradovate-Python-Client-main\placelimitorder.py", line 56, in placeLimitOrder 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)

cullen-b commented 1 year ago

Hard to tell based off of the message you sent. I would double check that your access token is valid and that your json message is error free.

If you send some of your code i can take a look.

I hope the rest of the repo has been useful! Cullen


From: Wmang46 @.> Sent: Thursday, March 2, 2023 5:46:14 PM To: supahcullen/Tradovate-Python-Client @.> Cc: Subscribed @.***> Subject: [supahcullen/Tradovate-Python-Client] JSON Error With PlaceLimitOrder.py (Issue #2)

When I run the Placelimitorder I get the following error. Any ideas?

Limit Order Failed Invalid JSON: illegal number, offset: 0x00000090 400 Bad Request 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\jsoninit.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 "C:\Users\jk\Documents\Tradovate-Python-Client-main\placelimitorder.py", line 62, in print(placeLimitOrder(False, 1, "Buy", 3700, token)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\jk\Documents\Tradovate-Python-Client-main\placelimitorder.py", line 56, in placeLimitOrder 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)

— Reply to this email directly, view it on GitHubhttps://github.com/supahcullen/Tradovate-Python-Client/issues/2, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASPBBQB7OCIOX3473NRPQATW2EWMNANCNFSM6AAAAAAVN76KQM. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Wmang46 commented 1 year ago

I fixed this issue. The values I supplied were not correct or in the right place. Once fixed, I created a new script to place an OCO order with a Take Profit and Stop Loss. The endpoint changes from "placeorder" to "placeOSO" the new order structure that works for this is:

# bracket1 = { "action": ocoDirection, "orderType": "Limit", "price": takeProfit } bracket2 = { "action": ocoDirection, "orderType": "Stop", "stopPrice": stoploss }

order = {
      "accountSpec": USER_NAME,
      "accountId": accId,
      "action": buysell,
      "symbol": SYMBOL,
      "orderQty": Qty,
      "orderType": "StopLimit",
      "price": orderPrice,
      "stopPrice": orderPrice,
      "isAutomated": True,
      "bracket1": bracket1,
      "bracket2": bracket2
    }

This will place a Stop Limit order with Take Profit and Stop Loss above or below the current market price.

Where: ocoDirecion = The opposite of the value buysell direction //The variable name should be osoDireection, but it can have any name. takeProfit = The Take Profit price stoploss = The Stop Loss price accid = The Account ID SYMBOL = The Market being traded Qty = The quantity orderPrice = The Entry price isAutomated = This was in "" and needed to not have quotes -Flag for automated trading

exp: token = getAccessToken(False)[0] print(placeStopLimitOrder(False, 1, "Buy", 3977, 3976.50, 3977.50, token))

ENTRY = 3977 STOPLOSS = 39766.50 TAKE PROFIT = 3977.50 Your code has been very helpful!! I have been searching for something that makes working with the API easy and this does the trick!!

cullen-b commented 1 year ago

Glad to hear you fixed it! Good luck!

On Mar 3, 2023, at 8:31 AM, Wmang46 @.***> wrote:

I fixed this issue. The problem was that my order was not structured correctly. The new order structure that works looks like this:

bracket1 = { "action": ocoDirection, "orderType": "Limit", "price": takeProfit } bracket2 = { "action": ocoDirection, "orderType": "Stop", "stopPrice": stoploss }

order = { "accountSpec": USER_NAME, "accountId": accId, "action": buysell, "symbol": SYMBOL, "orderQty": Qty, "orderType": "StopLimit", "price": orderPrice, "stopPrice": orderPrice, "isAutomated": True, "bracket1": bracket1, "bracket2": bracket2 } This will place a Stop Limit order with Take Profit and Stop Loss above or below the current market price.

Where: ocoDirecion = The opposite of the value buysell direction takeProfit = The Take Profit price stoploss = The Stop Loss price accid = The Account ID SYMBOL = The Market being traded Qty = The quantity orderPrice = The Entry price isAutomated = This was in "" and needed to not have quotes -Flag for automated trading

Your code has been very helpful!! I have been searching for something that makes working with the API easy and this does the trick!!

— Reply to this email directly, view it on GitHub https://github.com/supahcullen/Tradovate-Python-Client/issues/2#issuecomment-1453617045, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASPBBQH6IJ4Q7ZSK46MSXMLW2H6CNANCNFSM6AAAAAAVN76KQM. You are receiving this because you commented.