API dev available
Hello, all, I am available for API dev work if anyone needs an extra hand
|
Best Practices for Pulling Stock Data Efficiently with APIs
6
Hi everyone, I¡¯m working on a project that requires pulling historical data on hundreds of stocks using the Interactive Brokers TWS API. The data includes daily OHLC (Open, High, Low, Close) values and trading volume. My current method involves sequential API calls in a loop, which is slow and not efficient for a large dataset. Here¡¯s my current setup: Sending requests for individual stocks sequentially using the TWS API. Saving the returned data locally after each request for further analysis. I¡¯d like to optimize this process to handle large batches of stocks more efficiently.I¡¯m interested in advice on: Batch Requests: Does the TWS API support batch requests, and if so, how can I implement them? API Limitations: Are there known rate limits or best practices with the TWS API to avoid throttling or data loss? Caching Strategies: Are there effective ways to cache or save data to avoid redundant requests during retries or updates? If anyone has experience optimizing the Interactive Brokers API or similar APIs for high-volume data retrieval, I¡¯d appreciate your insights. Also, if this topic has been covered before, I¡¯d be grateful if you could point me to the relevant discussions in the archives. Thanks in advance!
|
IB Gateway freezes sometimes.
15
Not sure if this is the right place to ask the question, but since I can't find anywhere else looking for help for those kinds of questions, I suppose I might just post it here or someone could probably redirect me to the right place. So I'm basically running the IB Gateway(the linux stable version) on a linux virtual machine, and sometimes the application just freezes, and when I try to click anything, it doesn't respond, and the api client connected to the gateway receiving data feeds also can't receive any data. when I try to connect new client, it also fails, I have to manually kill the process and restart the application. So looks like an issue related to the IB Gateway itself? Checking the api log doesn't give any useful information, just some normal messages. I'm wondering if anyone experience the same issue before or any bug report related to this kind of scenario? Any suggestions what I can do?
|
Generic ticks
Hi, there are several Market Data column types available in TWS for which I don't see corresponding generic tick types. Are the tick types supported by the API only a subset of the TWS types? The data points I am trying to request are Market Cap and 1 Wk Change. Thanks.
|
Retrieving last trade quotation side from api
I am trying to subscribe to the following last trade information from the API. - last price - last size - last timestamp - last quotation side I have only managed to receive the first 3 information from the callbacks as shown in logs: Tick Price. Ticker Id: 1013, Field: 68, Price: 42.35, CanAutoExecute: 0, PastLimit: 0, PreOpen: 0 // Delayed last Tick Size. Ticker Id: 1013, Field: 71, Size: 500 // Delayed last size Tick String. Ticker Id: 1013, Type: 88, Value: 1734076776 // Delayed last timestamp Is there any way of subscribing to the quotation side of last trade from the API?
|
No Security definition has been found for the request when running cpp sample code
7
Hi, I'been trying to run the cpp sample code to reqRealTimeBars for some contract. but the ib server returns no security defintionn found even though I was using the sample contract.
|
Discrepancies between real-time and historical market data
2
Attached are the logs from my subscription to real-time and historical bar data yesterday. As you can see, there are discrepancies between the 'high' and 'Wap' values in the two datasets. I'm wondering if anyone can explain why this might be occurring. Any insights would be helpful. RealTimeBars: JD,Time:1733404940,Open:36.28,High:36.28,Low:36.28,Close:36.28,Volume:500000000000,Wap:36280000000,Count:1 HistoricalBars: JD,BarData(date=datetime.datetime(2024, 12, 5, 8, 22, 20, tzinfo=backports.zoneinfo.ZoneInfo(key='US/Eastern')), open=36.28, high=36.29, low=36.28, close=36.28, volume=500.0, average=36.282, barCount=1) Thanks xb
|
what time EST is it safe to assume that IB have printed the final close for all symbols EOD
5
Hi all In the groups experience what is the earliest time , eastern standard time, when it's reasonable to say that IB have printed ( immutable) the close for all symbols that will be the number of record in perpetuity. So market close at 16:00 but there's going to be a period where the numbers are still being populated. I'm certain that someone has figured out what that "indeterminate" period is to a margin of safety. I asked IB sometime back but the support desk has never responded, I asked 3 time. thank you for any help OR thoughts. cp
|
Multiple strategies in one account
4
Hello, I need to run several strategies in one account. How do I best keep them apart, unknowing of each other? I can tag orders using the orderRef field, but as of now, all I can think of is to keep a local database with all information (and maybe reconcile it with IB now and then). How else can I know which position belongs to which strategy? Do you have any best practices, or tips, to share? Thanks, Peter
|
reqRealTimeBars: data stops receiving for after-hour session
4
isn't this exactly what useRTH=False supposed to mean?
|
Purebasic and TWS
20
Hi, I am looking for Purebasic source code that uses TWS API. I am a begineer in Purebasic and TWS but not in programming but dont really know where to start. For example do I have to: 1 - Load a DLL such as TWSAPI.DLL or TWSLib.dll or something else, and then call the included functions? 2 - Open a windows socket connection and then send command strings ? I tryed to find out exploring source code in other languages, but nothing appeared clearly to me. My feeling is that Purebasic is a good language for this use as it generates a very compact and efficient executable file with no depencies. I just need to get started, then I can manage the rest. However if I could find Purebasic source code I might save a lot of time. Thanking you in advance for any help.
|
TWS API - TICK Historical Candles issue AFTER November 25th
Hi, I am trying to retrieve historical price data of a few FOREX currency pairs: mainly AUDCAD, EURUSD and EURGBP. Since November 25th, I am unable to retrieve historical TICK data via API. Before that date, I was able to download any price even going back to 2022. When trying to download TICK data, I am getting the following error: Historical Market Data Service error message:HMDS query returned no data: <SYMBOL@TYPE OF PRICE> I get the error regardles if it's: CASH or CFD IDEAPRO or SMART ASK BID BID_ASK_MIDPOINT price What I tried: 1. I tried and I CAN download 30 SEC candles meaning that it is not a problem with the code, connection or ports. 2. I used another account and another IP. 3. I tried different DATE formats, e.g. yyyyMMdd-HH:mm:ss I am running out of options. Can you please help me understand what might be the problem?
|
How do I find out the margin requirements for a US stock through the TWS API?
9
Hi, I'd like to find out the margin requirements for US stocks via the TWS API in python. On the Trader Workstation, of I look the Description of a Stock, it has a tab about margins, and lists the initial and maintenance margin requirements for long and short positions - see screenshot attached. I'd like to get the same information via the TWS API in python. When requesting the contract details via the API, this information is not contained in the returned ContractDetails object. Here's a sample code using the ib_async wrapper: qqq = Stock(symbol='META', exchange='SMART', currency='USD') qqq_details = ib.reqContractDetails(qqq) pprint(qqq_details) here's the result of the printout: [ContractDetails(contract=Contract(secType='STK', conId=107113386, symbol='META', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='META', tradingClass='NMS'), marketName='NMS', minTick=0.01, orderTypes='ACTIVETIM,AD,ADJUST,ALERT,ALGO ,ALLOC,AON,AVGCOST,BASKET,BENC HPX,CASHQTY,COND,CONDORDER,DAR KONLY,DARKPOLL,DAY,DEACT,DEACT DIS,DEACTEOD,DIS,DUR,GAT,GTC,G TD,GTT,HID,IBKRATS,ICE,IMB,IOC ,LIT,LMT,LOC,MIDPX,MIT,MKT,MOC ,MTL,NGCOMB,NODARK,NONALGO,OCA ,OPG,OPGREROUT,PEGBENCH,PEGMID ,POSTATS,POSTONLY,PREOPGRTH,PR ICECHK,REL,REL2MID,RELPCTOFS,R PI,RTH,SCALE,SCALEODD,SCALERST ,SIZECHK,SMARTSTG,SNAPMID,SNAP MKT,SNAPREL,STP,STPLMT,SWEEP,T RAIL,TRAILLIT,TRAILLMT,TRAILMI T,WHATIF', validExchanges='SMART,AMEX,NYSE,CBOE,PHLX,ISE, CHX,ARCA,NASDAQ,DRCTEDGE,BEX,B ATS,EDGEA,BYX,IEX,EDGX,FOXRIVE R,PEARL,NYSENAT,LTSE,MEMX,IBEO S,OVERNIGHT,TPLUS0,PSX', priceMagnifier=1, underConId=0, longName='META PLATFORMS INC-CLASS A', contractMonth='', industry='Communications', category='Internet', subcategory='Internet Content-Entmnt', timeZoneId='US/Eastern', tradingHours='20241016:0400-20241016:2000;20241017:0400-20241017:2000;20241018:0400-20241018:2000;20241019:CLOSED;20241020:CLOSED;20241021:0400-20241021:2000', liquidHours='20241016:0930-20241016:1600;20241017:0930-20241017:1600;20241018:0930-20241018:1600;20241019:CLOSED;20241020:CLOSED;20241021:0930-20241021:1600', evRule='', evMultiplier=0, mdSizeMultiplier=1, aggGroup=1, underSymbol='', underSecType='', marketRuleIds='26,26,26,26,26,26,26,26,26,26, 26,26,26,26,26,26,26,26,26,26, 26,26,26,26,26', secIdList=[TagValue(tag='ISIN', value='US30303M1027')], realExpirationDate='', lastTradeTime='', stockType='COMMON', minSize=0.0001, sizeIncrement=0.0001, suggestedSizeIncrement=100.0, cusip='', ratings='', descAppend='', bondType='', couponType='', callable=False, putable=False, coupon=0, convertible=False, maturity='', issueDate='', nextOptionDate='', nextOptionType='', nextOptionPartial=False, notes='')]
|
upper and lower limit
2
Hello all, I would like to set an upper and lower limit for selling stocks I hold. For example, let's say that I hold 120 shares of SMCI and the current price is 32.50. I would like to SELL those 120 shares if it reaches 34.71 OR if it went down and reached 31.20: SMCI-STK--0.0---SMART-Nasdaq-USD-----SELL-120-STP-34.71 SMCI-STK--0.0---SMART-Nasdaq-USD-----SELL-120-STP-31.2 The question is, if I only hold 120 shares, can I still do that since I sent 2 calls of 120 shares each (240 in total)? Will it be rejected with "you are trying to sell 240 SMCI shares but you only hold 120"? Thank you
|
How to get position info in "points"
5
What is the most efficient way to get information about position including the fields below. - open time - open price - unrealized PnL Request "reqPositions" and "reqPositionsMulti" return these - reqId - account - contract - avgCost 1. Instead of average cost, how do I get price of the stock / option / future when position was opened? 2. How do I get time when it was open? 3. There is a separate request for PnL but even if I get PnL, there are some missing pieces, e.g. I can't calculate the range in points between open and current prices of the asset.
|
Is there generic method to format contract for reqContractDetails
7
To get stock contract, it requires only 4 fields. Name = "SPY" Exchange = "SMART" SecType = "STK" Currency = "USD" For stock options, the 4 fields above + 3 more Right = "C" Strike = 650 LastTradeDateOrContractMonth = "20241231" When it comes to futures and futures options, it gets messy. For example, after reading couple of posts in this group, this is the contract definition that I used to subscribe to live data feed for ES futures. Of course, "SecType" = "FUT" and "Exchange" = "SMART" would be more appropriate but here we go. Name = "ES" Exchange = "CME" SecType = "IND" Currency = "USD" I have no idea why it has to be "IND" instead of "FUT" but somehow it worked for "reqMktData". Meanwhile, when I try to pass this to "reqContractDetails", I get "No security definition was found for the request". The same happens when I try to request contract details for futures options. The funny part is that I don't populate these parameters manually, I just request all open orders and positions, copy contract object from there and use it in reContractDetails but IB API returns "No security found" error for it. In other words, IB API returns INCORRECT data that can't be used to make other requests. Hence the question, Is there a map or table that clearly defines which security type and exchange need to be used with specific securities, e.g. if I have these assets, how do I create method that correctly populates properties for them, so I could use their contract definitions in other API calls, like reqMktData? SPY SPY call option ES futures ES futures call option EURUSD
|
The difference between realtimebar vs reqHistoricalData with barSizeSetting = "5S" and keepUpToDate = True
Hi All, do you guys know what is the difference between realtimebar vs reqHistoricalData with barSizeSetting = "5S" and keepUpToDate = True? Thanks xb
|
TWS API Stopped accepting incoming connections sometimes
2
TWS sometimes suddenly cannot connect, and it must be re-verified and logged in again to fix it. Belows are gateway logs happened before my connection failure today 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsServicePingS9-286] - Farm ushmds/NATIVE: Closing dormant connection type:HISTORICAL_DATA 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsServicePingS9-286] - Disconnecting cdc1.ibllc.com:4000 [disconnectDetails=DisconnectDetails[sessionID=9,endPoint=cdc1.ibllc.com:4000,reason=DISCONNECT_ON_INACTIVITY,cause=null,systemMessage=null,keepSocketOpen=false]]... 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsServicePingS9-286] - Socket closed. 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsServicePingS9-286] - SocketTracker[name=name=ushmds(cdc1.ibllc.com:4000),usesCcpConMan=false,lastReadTimestamp=1732869470298,lastReadDateTime=2024-11-29T08:37:50.298,lastFixSendTime=]. 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsServicePingS9-286] - Interrupting dispatcher [sessionID=9]... 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsServicePingS9-286] - Interrupting listener [sessionID=9,disconnectSocket=true]... 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsServicePingS9-286] - AuthTimeoutMonitor-ushmds: deactivate 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsDispatcherS9-282S9-283] - Dispatcher thread terminating [sessionID=9,interrupted=true]... 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsListenerS9-281] - Socket or stream for connection cdc1.ibllc.com:4000 was closed by another thread. 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsServicePingS9-286] - Terminating ping thread 2024-11-29 08:37:59.254 [EY] INFO [JTS-ushmdsListenerS9-281] - Listener thread terminating [sessionID=9] [seen=36342294,totalShifted=36342294,moreAvailable=0] 2024-11-29 08:37:59.254 [EY] INFO [JTS-DisconnectedS9-551] - Farm ushmds/NATIVE: Lost active connection with disconnect status DISCONNECT_ON_INACTIVITY 2024-11-29 08:37:59.254 [EY] INFO [JTS-DisconnectedS9-551] - Farm ushmds/NATIVE: Not resetting 2024-11-29 08:37:59.255 [EY] INFO [JTS-AsyncNonLocked-33] - [50:176:176:1:0:4:2:DET] Sending error. 2024-11-29 08:37:59.255 [EY] INFO [JTS-AsyncNonLocked-33] - [50:176:176:1:0:4:2:DET] [4;2;-1;2107;HMDS data farm connection is inactive but should be available upon demand.ushmds;] 2024-11-29 08:37:59.255 [EY] INFO [JTS-AsyncNonLocked-33] - [50:176:176:1:0:4:2:DET] Error sent. 2024-11-29 08:38:31.130 [EY] INFO [JTS-CCPPingS2-28] - Resetting long time mode 2024-11-29 08:39:32.134 [EY] INFO [JTS-CCPPingS2-28] - Resetting long time mode 2024-11-29 08:40:11.939 [EY] INFO [JTS-DeadlockMonitor-2] - Memory:total=786,432KB free=277,852KB HeapUsage: Max=786,432KB used=506,764KB committed=786,432KB NonHeapUsage: Max=0KB used=172,722KB committed=176,432KB 2024-11-29 08:40:12.940 [EY] INFO [JTS-DeadlockMonitor-2] - CPU:cur=12.52% avg=0.38% 30 min avg=4.46% 10 min avg=12.51% 5 min avg=12.50% 1 min avg=12.52% GC:called=953 times CPU used=0.03% Finalizer:cur=0.00% avg=0.00% 30 min avg=0.00% 10 min avg=0.00% 5 min avg=0.00% 1 min avg=0.00% Threads Count:curr live=72 curr daemon=33 2024-11-29 08:40:33.139 [EY] INFO [JTS-CCPPingS2-28] - Resetting long time mode 2024-11-29 08:41:34.144 [EY] INFO [JTS-CCPPingS2-28] - Resetting long time mode 2024-11-29 08:42:35.148 [EY] INFO [JTS-CCPPingS2-28] - Resetting long time mode 2024-11-29 08:43:36.153 [EY] INFO [JTS-CCPPingS2-28] - Resetting long time mode 2024-11-29 08:44:37.158 [EY] INFO [JTS-CCPPingS2-28] - Resetting long time mode 2024-11-29 08:45:11.944 [EY] INFO [JTS-DeadlockMonitor-2] - Memory:total=786,432KB free=273,763KB HeapUsage: Max=786,432KB used=510,860KB committed=786,432KB NonHeapUsage: Max=0KB used=172,732KB committed=176,432KB 2024-11-29 08:45:12.945 [EY] INFO [JTS-DeadlockMonitor-2] - CPU:cur=12.52% avg=0.49% 30 min avg=6.54% 10 min avg=12.51% 5 min avg=12.51% 1 min avg=12.52% GC:called=953 times CPU used=0.03% Finalizer:cur=0.00% avg=0.00% 30 min avg=0.00% 10 min avg=0.00% 5 min avg=0.00% 1 min avg=0.00% Threads Count:curr live=72 curr daemon=33 2024-11-29 08:45:38.162 [EY] INFO [JTS-CCPPingS2-28] - Resetting long time mode 2024
|
No real-time data after IB nightly server reset
17
I have been using IB Gateway (v10.19.2o) inside a docker container. After the nightly reset of IB servers at around 0430 UTC, I get the following message on some days: Connectivity between IB and Trader Workstation has been restored - data maintained. The following farms are connected: eufarm; euhmds; secdefeu. The following farms are not connected: ushmds. Error Code: 1102 After this I don't receive any real-time data even if I resubscribe to it using reqRealTimeBars. I have tried resetting the data connection as well as account & the reset seems to be done fine but I still can't get the data. The only way to get the data is to re-login which requires 2FA. Curiously, on some days it just works fine after the nightly reset. In such cases, I get the following message (same error code as before but all farms are connected including ushmds): Connectivity between IBKR and Trader Workstation has been restored - data maintained. All data farms are connected: eufarm; euhmds; ushmds; secdefeu. Error Code: 1102 I was wondering if anyone else has also encountered this problem & found a solution for it that doesn't require re-login. Thanks, Farhan
|
Best practice for cancelmktdata
5
Hello I am currently have this in a foreach loop using if (hasCompleteData || hasPartialData) { ClientSocket.cancelMktData(cancelTickerId); MaxSubscriptioncancelledTickerIds.Add(cancelTickerId); logBuilder.AppendLine(hasCompleteData ? $"Max Subscription Data Cancel. Complete Data. Ticker Id: {cancelTickerId}" : $"Max Subscription Data Cancel. Partial Data. Ticker Id: {cancelTickerId}"); return true; // Ticker was successfully canceled My question is should i add sleep between each canceltickerid, I am batching about 300 canceltickerids at once and the tick call backs for each canceltickerids keeps firing, On the log for example I could call canclemktdata for a specific tickerid and for up to 2 minutes I may still get tick call backs for that partciular tickerid, Will adding sleep allow for faster processing or should i do smaller batches?
|