¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io
Date

Re: How Can I Locate Hard to Borrow Shares to Short with TWS API?

 

Here are instructions for retrieving the daily file via ftp or doing one-time/one-ticker searches:
https://ibkr.info/article/2024


Re: How Can I Locate Hard to Borrow Shares to Short with TWS API?

 

I was referring to finding the borrow rate. I assume these borrow rates change daily.?


Re: How Can I Locate Hard to Borrow Shares to Short with TWS API?

 

Do you mean the borrow rate, or the price you can sell them at???

I believe there's an ftp somewhere with a daily file containing the borrow rates.? I'd think it was also a tick in the API somewhere but that may be wishful thinking.

As for what you can sell them for, that's up to the market to decide..

-Peter


Re: What is the best way to get the open price of today for some symbols

 

Why don't you use instead of historical data? You'd subscribe any time before the market opens and you'll receive automatically within milliseconds after market opens.

´³¨¹°ù²µ±ð²Ô


What is the best way to get the open price of today for some symbols

 

I am looking to get the open price of today for around 30 symbols as early as possible.

It can be done by the following process:

  • Wait until the market opens, time is after 9:30:00
  • Use reqHistoricalData to get one day's daily candle
  • Grap the open price of today

The problem is that if I reqHistoricalData within 5 seconds after the market is open, the daily candle will be the previous day's data.

I have to wait until the time is after 9:30:05 instead of 9:30:00 to request the daily data.

May I ask if you have any suggestions to get the open price data from IB avoiding waiting 5 seconds after the market is open?

import datetime
import pandas as pd
from ib_insync import *

def wait_until_market_open():
	
	#get open time
	now      = datetime.datetime.now()
	openTime = now.replace(hour=9, minute=30, second=5, microsecond=0)
	
	#return if market is open
	while True:
		now = datetime.datetime.now()
		if( now >= openTime ): 
			return
		time.sleep(0.3)


if __name__ == "__main__":

	ib = IB()
	ib.connect('127.0.0.1', 7497, clientId=10)
	
	wait_until_market_open()
	
	symbs = ['AAPL', 'TSLA', 'SPY']
	for symb in symbs:
		
		contract = Stock(symb, 'SMART', 'USD', primaryExchange='ISLAND')
		bars = ib.reqHistoricalData(contract, 
			                          endDateTime='', 
			                          durationStr='1 D', 
			                          barSizeSetting='1 day', 
			                          whatToShow='TRADES', 
			                          useRTH=True, 
			                          formatDate=1)
		df = util.df(bars)
		print(symb, df.iloc[0]['open'])
	
	ib.disconnect()


Re: TwsActiveX class not registered run-time error '-2147221164 (80040154)

 

Update/workaround:
  1. Uninstalled 64-bit version of Microsoft 365
  2. Restarted computer
  3. Installed 32-bit version of Microsoft 365
I realize that after v973.05 the API installer is supposed to work with 32-bit AND 64-bit Excel, but in my case it doesn't work with the latter and 32-bit is still required.


Excel DDE scanner - live market data

 

Hi all!? I am new to this community and have been reading a ton of useful info on the TWS API.? However, as my (basic) VBA skills have been left unused over the past ten years and much has been forgotten, I wanted to ask your help in getting a simple market scanner up and running in Excel.

I am trying to run an existing scanner - (take?TOP_PERC_GAIN as an example) - and have found out that while it will provide a list of instruments which ties in with what you would get in TWS, the Excel scanner does not provide corresponding market data (last price or anything else, columns are just blank).??

I tried to link symbols with live and historical market data as per IB guidance and tutorials, but it appears that you cannot use DDE links with cell references in them and as the scanner results would change through the course of the day, just copy-pasting them in live data tab won't work.

As I understand the workflow at the moment:

- start market scanner, results are populated in Top_Gain sheet and updated while subscription is active
- pull live and historical market data on the basis of the tickers that are provided
- repeat the market data query based on updated tickers every time the scanner is refreshed?
- stop both scanner and market data pulls when subscription is no longer active

What is not clear to me is how to integrate the market data pulls so that they don't just pull data once (and the data could be wrong a few seconds later as tickers change / move order), but the code keeps iterating while the scanner subscription is active.

Could you please advise how / where in VBA modules you would add corresponding macro to pull live data for the same tickers that come out from the market scanner??

This is just for stocks and am only looking at US stocks at the moment just to get an understanding of how the code works.

The file that I am using is TWS DDE sample Excel that comes with the API.

Thank you very much in advance!


Re: How Can I Locate Hard to Borrow Shares to Short with TWS API?

 

Peter, do you know if there is a way to retrieve the price to short these shares in advance of actually placing the order?


Re: How Can I Locate Hard to Borrow Shares to Short with TWS API?

 

If IB has shares available to short, it's automatic.? Otherwise your order will be rejected.

As martintanner notes, in release 974, there's a new tick type 236 which provides the shares available for shorting right now.??

-Peter


Re: Error: 1 321 Error validating request.-'bW' : cause - Please enter a local symbol or an expiry

Nick
 

¿ªÔÆÌåÓý

That's because there are a zillion ways to calculate continuous futures prices and the continuous contract price may be far from the actual futures contract price. If you trade based on continuous futures you're trying to navigate Chicago with a map of Detroit.


On 3/13/2022 7:30 PM, dent wrote:

Uh OH just found this



```

Continuous futures are available from the API with TWS v971 and higher. Continuous futures cannot be used with real time data or to place orders, but only for historical data.

```


On 3/13/22 18:28, ´³¨¹°ù²µ±ð²Ô Reinold via groups.io wrote:
No problem. has an overview of most (if not all) SecTypes as well as the contract object fields you want to set to properly describe the instrument you are interested in.


Re: Error: 1 321 Error validating request.-'bW' : cause - Please enter a local symbol or an expiry

 

¿ªÔÆÌåÓý

Excellent I will get on that immediately, seems like a GREAT solution and thank you so much for taking the time.

cp


On 3/13/22 18:51, ´³¨¹°ù²µ±ð²Ô Reinold via groups.io wrote:

We do use CONTFUT with real time data, But not directly.

As a matter of practice we follow this work flow:

  • Make a sparse contract object that describes the instruments we are interested in (such as ES, GLOBEX, CONTFUT)
  • Use reqContractDetails to request a complete contract object from IBKR
  • Use the complete contract object for all API calls that require contracts

For CONTFUT contracts, reqContractDetails returns the contract for the actual current front-month future (such as ESH2 today). That contract has a SecType of FUT and reqMktData is happy.

´³¨¹°ù²µ±ð²Ô


Re: Error: 1 321 Error validating request.-'bW' : cause - Please enter a local symbol or an expiry

 

We do use CONTFUT with real time data, But not directly.

As a matter of practice we follow this work flow:

  • Make a sparse contract object that describes the instruments we are interested in (such as ES, GLOBEX, CONTFUT)
  • Use reqContractDetails to request a complete contract object from IBKR
  • Use the complete contract object for all API calls that require contracts

For CONTFUT contracts, reqContractDetails returns the contract for the actual current front-month future (such as ESH2 today). That contract has a SecType of FUT and reqMktData is happy.

´³¨¹°ù²µ±ð²Ô


Re: Error: 1 321 Error validating request.-'bW' : cause - Please enter a local symbol or an expiry

 

¿ªÔÆÌåÓý

Uh OH just found this



```

Continuous futures are available from the API with TWS v971 and higher. Continuous futures cannot be used with real time data or to place orders, but only for historical data.

```


On 3/13/22 18:28, ´³¨¹°ù²µ±ð²Ô Reinold via groups.io wrote:

No problem. has an overview of most (if not all) SecTypes as well as the contract object fields you want to set to properly describe the instrument you are interested in.


Re: Error: 1 321 Error validating request.-'bW' : cause - Please enter a local symbol or an expiry

 

No problem. has an overview of most (if not all) SecTypes as well as the contract object fields you want to set to properly describe the instrument you are interested in.


Re: Error: 1 321 Error validating request.-'bW' : cause - Please enter a local symbol or an expiry

 

¿ªÔÆÌåÓý

Hi jurgen

I think you've spotted the flaw in my cunning plan. I hadn't AND when I did it I got

```

Error: 1 321 Error validating request.-'bW' : cause - Please enter a valid security type
```

?I didn't know that security type existed so I'm going to go through the code to see if I can actually send that. Thanks for the headsup on CONTFUT.

cp



On 3/13/22 18:10, ´³¨¹°ù²µ±ð²Ô Reinold via groups.io wrote:

Are you setting SecType = "CONTFUT" somewhere ?

´³¨¹°ù²µ±ð²Ô


Re: Error: 1 321 Error validating request.-'bW' : cause - Please enter a local symbol or an expiry

 

Are you setting SecType = "CONTFUT" somewhere ?

´³¨¹°ù²µ±ð²Ô


Error: 1 321 Error validating request.-'bW' : cause - Please enter a local symbol or an expiry

 

I want to get continuous futures and don't want to mess around with expiration et al. Basically what I see in the TWS watchlist.

I tried

reqMktData 

with 
Symbol  = "ES"
Exchange = "GLOBEX"
Currency = "USD"

and got the error in the subject line. Is there ANY variant of reqMktData that might work OR do I have to resort to reqRealTimeBars and pick the last bar?

No point in posting code as I am writing in julia. The calls to the TWS api are populated the same so ANY help would be appreciated.

thanks

and got the error in the subject line.


Re: bunch of reqMktData requests for options

 

Do you also get the message that they will stop the support for older versions after 31st March 2022? I'm still unsure what this actually means. It could be that starting from this day, this client will not work anymore. It could also mean that they would just don't process any tickets regarding this version anymore.

So hopefully, they will fix it asap.


Re: bunch of reqMktData requests for options

 

Sorry for my late reply, But I've opened several tickets about it.
The IB development is investigating it.?
Hope they will patch it soon.
I opened my first tickets about this issue more than 2 years ago.
We still use TWS 9.73 as it was the last release before the 'regression/bug' was introduced in their code.


Re: OCA Group Orders being placed, and executed, but the stop loss orders or Take profit order are not being cancelled after one of the bracket orders are executed

Nick
 

Probably because you are not placing a bracket order, you're just sending 3 independent orders.

See bracket order info here:

On 3/13/2022 8:46 AM, alcatrazam wrote:
Hey everyone, so I am placing the bracket orders, they are going to IB, being placed, and executed, but once one part is triggered, the remaining ordered is hanging, and not being canceled, then my python program crashes as it sees the remaining fragment of the order.