Search code examples
pythonjavainteractive-brokers

320 Error after IBApi.EClient.placeOrder() in Python & Interactive Brokers


I am trying to place an order through Interactive Brokers' Python API but receive the error:

ERROR 1 320 Error reading request: Unable to parse data. java.lang.NumberFormatException: For input string: "1.7976931348623157e+308"

Connecting and retrieving data works fine but when submitting an order, one of my parameters seems to be wrong and I simply can't figure out what it is. I was closely following IB's documentation, so it really comes as a bit of a surprise to me.

The error code (320) is not really telling, unfortunately, as IB merely describes it as a "Server error".

The only related question I found online, links the error to an invalid ID but I checked mine and it should be fine.

The code:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order

import threading

class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)

def run_loop():
    app.run()

app = IBapi()
app.connect('127.0.0.1', 7496, 1)

api_thread = threading.Thread(target = run_loop, daemon = True)
api_thread.start()

ctr = Contract()
ctr.symbol = 'AAPL'
ctr.secType = 'STK'
ctr.exchange = 'SMART'
ctr.currency = 'USD'

ord = Order()
ord.action = 'BUY'
ord.orderType = 'LMT'
ord.totalQuantity = 1
ord.lmtPrice = 150

app.reqIds(-1)
id = app.nextValidOrderId
print(id)
print(isinstance(id, int))

app.placeOrder(id, ctr, ord)

returns:

1
True
ERROR 1 320 Error reading request: Unable to parse data. java.lang.NumberFormatException: For input string: "1.7976931348623157e+308"

My TWS version is 10.20.1d, which is the latest as of now (since this fixed a somewhat related question).

Can someone help me with what I am doing wrong, please?


Solution

  • Using TWS 10.20.1d and API_Version=10.20.01 I find your code works with only a minor change with nextValidOrderId.

    Suggest checking API version, and upgrading if not latest version.