deephaven-examples / deephaven-ib

An Interactive Brokers integration for Deephaven
Apache License 2.0
64 stars 27 forks source link

`account_positions` table can have duplicate entries #111

Open chipkent opened 1 year ago

chipkent commented 1 year ago

As reported in https://deephavencommunity.slack.com/archives/C036HT73T43/p1694639036590999, it is possible for the account_positions table to have duplicate entries.

This appears to happen because position data is requested for the base account as well as all accounts. image

At the same time, the raw data is aggregated using .last_by(["RequestId", "Account", "ModelCode", "ContractId"]).

Supporting files: requests.csv Screenshot 2023-09-22 at 10 17 22 positions.csv Screenshot 2023-09-22 at 10 12 07

from ibapi.contract import Contract
from ibapi.order import Order
import deephaven_ib as dhib
from deephaven import time_table
from deephaven.plot import Figure
from deephaven.plot.selectable_dataset import one_click
from deephaven.plot import PlotStyle

print("========== Create a client and connect.=========")
# Socket Port: TWS:: live:7496 paper:7497 | IB:: live:4001 paper:4002
client = dhib.IbSessionTws(host="host.docker.internal", port=4002,client_id=0, download_short_rates=False, 
                           read_only=False)
print(f"IsConnected: {client.is_connected()}")
client.connect()
print(f"IsConnected: {client.is_connected()}")

# Setup
account = "XXXXXXXXXXXXXX"
max_position_dollars = 200000.0 #per security
ema_t = "00:02:00"

errors = client.tables["errors"]
requests = client.tables["requests"]
positions = client.tables["accounts_positions"].where("Account = account")
ticks_bid_ask = client.tables["ticks_bid_ask"]
orders_submitted = client.tables["orders_submitted"].where("Account = account")
orders_status = client.tables["orders_status"]
orders_exec_details = client.tables["orders_exec_details"].where( "Account = account")
pnl = client.tables["accounts_pnl"].where("Account = account")
tmp= client.tables["accounts_summary"].where("Account=account")
chipkent commented 1 year ago

To work around this problem in the short term:

  1. Check out the dh-ib plugin from github
  2. Edit line 590 of ./src/deephaven_ib/init.py to be .last_by(["Account", "ModelCode", "ContractId"])
  3. You also may need to remove the RequestId from other "accounts_*" tables in that section of code.
  4. Rebuild and use the modified code. An easy way to do this is: cd docker/dev , ./build.sh , docker compose up