¿ªÔÆÌåÓý

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

Re: Building an Advanced Historical Stock Screener

 

¿ªÔÆÌåÓý

I have noticed that as well.? If I just do one or two requests, I get the data fast.? But then after about the third or so request, it slows down.? I think IB deprioritizes the request when they realize that it is complex and likely the input to an algorithm rather than a human looking at a chart.

If you are doing this, I highly recommend caching your data.? I have an updater utility that will pull the entire history (daily bars) for a new symbol and stores it in a file.? Then the next time I request the symbol, it only pulls from the last day in the history forward and then updates the cache each time.? I did that to save time, but also to minimize load on IB's servers.


From: [email protected] <[email protected]> on behalf of ´³¨¹°ù²µ±ð²Ô Reinold via groups.io <TwsApiOnGroupsIo@...>
Sent: Wednesday, April 6, 2022 12:14 PM
To: [email protected] <[email protected]>
Subject: Re: [TWS API] Building an Advanced Historical Stock Screener
?
I can confirm Richard's measurements, In fact some of my "1 day RTHOnly for 50 years" requests get served even faster.
  • 12.676s --> 9,070 bars for MSFT RTHonly 1day 19860313 to 20220304
  • 12.903s --> 10,604 bars for IBM RTHonly 1day 19800317 to 20220406
  • 12.997s --> 10,601 bars for F RTHonly 1day 19800317 to 20220406
  • 7.470s --> 7,349 bars for SPY RTHonly 1day 19930129 to 20220406
  • 5.096s --> 10,603 bars for MMM RTHonly 1day 19800317 to 20220406
I get a similar total times when I make back-to-back 10 year requests instead. The interesting thing is, that the first two requests consistently get served very fast (~500ms) while requests take a little more than 5s from the third on:
  • 0.534s --> 2,517 bars for INTC. Have 2,517 bars now.
  • 0.688s --> 2,520 bars for INTC. Have 5,037 bars now.
  • 5.507s --> 2,517 bars for INTC. Have 7,554 bars now.
  • 5.690s --> 2,530 bars for INTC. Have 10,084 bars now.
  • 5.766s --> 523 bars for INTC. Have 10,607 bars now.
  • Total: 18.185s for INTC RTHonly 1day 19800317 to 20220406.
´³¨¹°ù²µ±ð²Ô

On Wed, Apr 6, 2022 at 12:30 PM, Richard L King wrote:

As an example, you can download the entire daily history of Microsoft from 13 March 1986 in a single call, returning 9091 bars (today) in about 30 seconds. Use 50 Y as the duration.


What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)

 

¿ªÔÆÌåÓý

The latest TWS API for Java added a new required parameter to the existing method EClient::cancelOrder.? It now takes a string for manualOrderCancelTime which can be left null.? What does this parameter do?? Cancel an order if not already filled at a specific time in the future?? It's not documented:



Re: Building an Advanced Historical Stock Screener

 

I can confirm Richard's measurements, In fact some of my "1 day RTHOnly for 50 years" requests get served even faster.
  • 12.676s --> 9,070 bars for MSFT RTHonly 1day 19860313 to 20220304
  • 12.903s --> 10,604 bars for IBM RTHonly 1day 19800317 to 20220406
  • 12.997s --> 10,601 bars for F RTHonly 1day 19800317 to 20220406
  • 7.470s --> 7,349 bars for SPY RTHonly 1day 19930129 to 20220406
  • 5.096s --> 10,603 bars for MMM RTHonly 1day 19800317 to 20220406
I get a similar total times when I make back-to-back 10 year requests instead. The interesting thing is, that the first two requests consistently get served very fast (~500ms) while requests take a little more than 5s from the third on:
  • 0.534s --> 2,517 bars for INTC. Have 2,517 bars now.
  • 0.688s --> 2,520 bars for INTC. Have 5,037 bars now.
  • 5.507s --> 2,517 bars for INTC. Have 7,554 bars now.
  • 5.690s --> 2,530 bars for INTC. Have 10,084 bars now.
  • 5.766s --> 523 bars for INTC. Have 10,607 bars now.
  • Total: 18.185s for INTC RTHonly 1day 19800317 to 20220406.
´³¨¹°ù²µ±ð²Ô


On Wed, Apr 6, 2022 at 12:30 PM, Richard L King wrote:

As an example, you can download the entire daily history of Microsoft from 13 March 1986 in a single call, returning 9091 bars (today) in about 30 seconds. Use 50 Y as the duration.


Re: Building an Advanced Historical Stock Screener

 

¿ªÔÆÌåÓý

No, I manually (and quite tediously) scraped the symbols from a website and then edited them into a file in the format that my downloader command line program can use for input.

?

You can get such a list of all US stocks from . This I better than what I used because you can ask it to display them all on one page. My approach was to copy the table, paste it into Excel, remove the unwanted columns, export it to a text file, then use NotePad++ to add the missing bits.

?

Just for interest, I've attached the actual file that I input to the program. The boilerplate comments at the start show the most common commands that the program recognises.

?

?

From: [email protected] <[email protected]> On Behalf Of tagerrish@...
Sent: 06 April 2022 17:44
To: [email protected]
Subject: Re: [TWS API] Building an Advanced Historical Stock Screener

?

Richard, did you manually enter every symbol into a list for the contract.symbol = ¡°ticker¡±

I don¡¯t know if there is a way to scan all stocks in nasdaq in one go.


Re: Building an Advanced Historical Stock Screener

 

It's vastly improved. There are still combinations of bar size and duration that cause lengthy waits, and my implementation takes care to avoid them. Though I've just realised that at least some of the limits are now much larger than I've coded for. For example, for 1-minute bars I was using 6 D as the longest duration (and making multiple requests if I needed more bars than that), but I just tried 20 D and that returns pretty much instantly; 30 D took 18 seconds. 50D was 30 seconds. And a proportion of that time was formatting the received results and writing them to the logfile. So I need to revise all my rules - it may even be that there are no restrictions now.

As an example, you can download the entire daily history of Microsoft from 13 March 1986 in a single call, returning 9091 bars (today) in about 30 seconds. Use 50 Y as the duration.

I'm pretty pleased with it, because I always hated the historical data implementation right from the start in about 2005. I still hate the API function, but at least it works well now!

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Nick
Sent: 06 April 2022 14:12
To: [email protected]
Subject: Re: [TWS API] Building an Advanced Historical Stock Screener

Yikes. Things have changed for the better. I know they relaxed the pacing
limits but didn't think it was so dramatic for daily bars.

Thanks for the info!


Re: Building an Advanced Historical Stock Screener

 

Richard, did you manually enter every symbol into a list for the contract.symbol = ¡°ticker¡±

I don¡¯t know if there is a way to scan all stocks in nasdaq in one go.


Re: Building an Advanced Historical Stock Screener

Nick
 

Yikes. Things have changed for the better. I know they relaxed the pacing limits but didn't think it was so dramatic for daily bars.

Thanks for the info!

On 4/6/2022 7:16 AM, Richard L King wrote:
Nick, I think you're dismissing the historical data mechanism too easily.

I downloaded 10 daily bars for each of the S&P 500 stocks using my downloader program, and it did the lot in just 30 seconds.


Re: Building an Advanced Historical Stock Screener

 

Nick, I think you're dismissing the historical data mechanism too easily.

I downloaded 10 daily bars for each of the S&P 500 stocks using my downloader program, and it did the lot in just 30 seconds.

Daily bar downloads are so simple and quick that, certainly for small numbers of days, there is no suggestion of historical data API rate limiting.. (Whether the daily bars contain enough information to satisfy the OP's needs is another matter.)

What there still is, of course, is the overall API input message rate limit 0f 50 per second. When I ran it again, I hit this limit and TWS broke the connection. My downloader doesn't implement explicit API message rate limiting, because it's normally only used for a handful or a few dozen of requests. For example it will reliably retrieve 1-minute bars for all S&P100 stocks for the current and previous session in around 9 seconds, and here the API message rate is quite low: one contract request and one hist data request per contract, so about 200 requests in 9 seconds.

But in this case it had actually gone through all 500 contract requests and 266 of the historical data requests before TWS broke the connection, in just 5 seconds, which is about 150 input API messages per second. So it looks like the 50 messages per second API limit isn't very precise!

So anyway, I think if the input rate were limited to say 40 per second, there should be no problem retrieving daily bars for all 6000 stocks in about 300 seconds, which doesn't seem too long to wait. But it may be that if you keep pumping in requests for that many stocks, even within the limiting rate, TWS might eventually balk, though I've no reason to suspect that it will.

I should be able to test this fairly easily. My historical data mechanism still has an implementation of the original historical data pacing rules (remember the 60 requests per 10 minutes, etc?), but it's turned off by default: I could easily turn it back on and adjust the parameters suitably to fit within the required limit (or use the API connection parameter rate-limiter option).

Richard

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Nick
Sent: 06 April 2022 01:00
To: [email protected]
Subject: Re: [TWS API] Building an Advanced Historical Stock Screener

Your best bet is the built-in scanners since you probably can't download
history for those markets (around 6,000 stocks) in a timely manner with the
tws api. The pacing limits are a moving target so you never know how many
requests you can make per minute.

reqMktData with frozen data might work you would have to see the timings
for yourself.

Note that there is no bulk symbol download in tws api. You have to request
one symbol at a time which is not fast.

If the available scanners don't have the conditions you want then you'll
probably need a different data provider.


On 4/5/2022 7:30 PM, tagerrish@... wrote:
I am trying to build an advanced stock screener using tws API and I am
not sure if what I am trying to do is possible. I want to scan the
entire NYSE and NASDAQ for a set of historical criteria to check if
these criteria were met within the last 3 days.




Re: Building an Advanced Historical Stock Screener

Nick
 

Your best bet is the built-in scanners since you probably can't download history for those markets (around 6,000 stocks) in a timely manner with the tws api. The pacing limits are a moving target so you never know how many requests you can make per minute.

reqMktData with frozen data might work you would have to see the timings for yourself.

Note that there is no bulk symbol download in tws api. You have to request one symbol at a time which is not fast.

If the available scanners don't have the conditions you want then you'll probably need a different data provider.

On 4/5/2022 7:30 PM, tagerrish@... wrote:
I am trying to build an advanced stock screener using tws API and I am not sure if what I am trying to do is possible. I want to scan the entire NYSE and NASDAQ for a set of historical criteria to check if these criteria were met within the last 3 days.


Building an Advanced Historical Stock Screener

 

I am trying to build an advanced stock screener using tws API and I am not sure if what I am trying to do is possible. I want to scan the entire NYSE and NASDAQ for a set of historical criteria to check if these criteria were met within the last 3 days. For example, I want to output a list of stocks that gained x% between today's close and the close 3 days ago. Is there a way to do this?

I found an example using the contract() object. In the example below the contract.symbol is defined as a single stock such as AAPL. Im trying to figure out if there is a way to scan all symbols against a list of custom conditions and then obtain the output for any tickers that match.?

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum


class TestApp(EWrapper, EClient):
??? def __init__(self):
??????? EClient.__init__(self, self)

??? def error(self, reqId, errorCode, errorString):
??????? print("Error: ", reqId, " ", errorCode, " ", errorString)

??? def tickPrice(self, reqId, tickType, price, attrib):
??????? print("Tick Price. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType), "Price:", price, end=' ')

??? def tickSize(self, reqId, tickType, size):
??????? print("Tick Size. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType), "Size:", size)


def main():
??? app = TestApp()

??? app.connect("127.0.0.1", 7497, 0)

??? contract = Contract()
??? contract.symbol = "AAPL"
??? contract.secType = "STK"
??? contract.exchange = "SMART"
??? contract.currency = "USD"
??? contract.primaryExchange = "NASDAQ"

??? app.reqMarketDataType(4)? # switch to delayed-frozen data if live is not available
??? app.reqMktData(1, contract, "", False, False, [])

??? app.run()


if __name__ == "__main__":
??? main()


Re: TWS API for FUTURE response 200 No security definition has been found for the request

 

The required fields are incorrect.? Take a look at the samples in the API, in the ContractSample.cpp file from the C++ project, and the? FutureWithLocalSymbol() function.

??? contract.secType = "FUT";
?? ?contract.exchange = "GLOBEX";
?? ?contract.currency = "USD";
?? ?contract.localSymbol = "ESZ6";


Re: TWS API for FUTURE response 200 No security definition has been found for the request

 

The financial futures trade calendar quarter-end deliveries: March, June, September, December
Commodity futures deliveries coincide with production or consumption cycles (e.g. growing season in agriculture, usage in gold/silver)
[rwk]


TWS API for FUTURE response 200 No security definition has been found for the request

 

Hi,
I am trying to get the FUTURE data of some indexes like ES,?NQ, so I write the following code:

contract = Contract()
contract.symbol = "ES"
contract.secType = "FUT"
contract.exchange = "GLOBEX"
contract.lastTradeDateOrContractMonth = "202204"
reqMarketDataType(1)
reqMktData(2, contract, "", False, False, [])

But its responses --> 200 No security definition has been found for the request

Can anyone help me to figure out what is the exact reason for my code so it does not return any data?

- Thanks
Ihtesham


Re: Conditions on twsactivex API.... AGAIN :)

 

Just for whoever finds this thread and because they had the same problem as me, to circumvent this issue I had to cancel the order and send it again with a new order Id, every day. Hope there will be a solution to prevent?this from happening again.


El s¨¢b, 2 abr 2022 a las 12:57, joanmarcel119 via (<joanmarcel=[email protected]>) escribi¨®:
First of all, I hope this message finds you well.

Here we go again with an issue related to price conditions. It seems that I'm the only one person in the world using these conditions on the TwsActiveX API... I need to be able to send the order changes to the TWS every day at the first hour in the morning, so I'm not keeping the TWS and the excel always connected nor the computer turned on.

I think we have found another bug on IB, because the price condition doesn't get updated if you turn off the TWS and restart it again!

For the tests, I used the Excel sample IB provides:

1) On the sheet "Conditional Orders" and after connecting to TWS I sent a MKT order on IBM stock with price condition:
"last of bid/ask Price of 37018770(SMART) is >= 30" (conid 37018770 is T stock)

After placing the focus on the row and clicking on the "Place /Modify Order" button, the order is sent and the Order Id is set on T column "Id" so we can resend the order and update the price condition. The result was as expected on TWS.

2) Update the price condition to:
"last of bid/ask Price of 37018770(SMART) is >= 32"

again after clicking on the button to send this modification, the result was right on TWS.

3) Now turn off the TWS and the excel

4) turn on again the TWS and the excel

5) Update the price condition to:
"last of bid/ask Price of 37018770(SMART) is >= 30"
?
After clicking on the button the modification is not sent, well I've taken a look at the API log and it is sent but the order is not modified on TWS and no error returned, so it is not working as expected.

Any clues on this? What can I do or?test?

I attach the excel demo workbook with the test row as explained here and the api log which shows the order is sent to the TWS and an answer comes from it with the price unchanged

Thank you very much


Conditions on twsactivex API.... AGAIN :)

 

First of all, I hope this message finds you well.

Here we go again with an issue related to price conditions. It seems that I'm the only one person in the world using these conditions on the TwsActiveX API... I need to be able to send the order changes to the TWS every day at the first hour in the morning, so I'm not keeping the TWS and the excel always connected nor the computer turned on.

I think we have found another bug on IB, because the price condition doesn't get updated if you turn off the TWS and restart it again!

For the tests, I used the Excel sample IB provides:

1) On the sheet "Conditional Orders" and after connecting to TWS I sent a MKT order on IBM stock with price condition:
"last of bid/ask Price of 37018770(SMART) is >= 30" (conid 37018770 is T stock)

After placing the focus on the row and clicking on the "Place /Modify Order" button, the order is sent and the Order Id is set on T column "Id" so we can resend the order and update the price condition. The result was as expected on TWS.

2) Update the price condition to:
"last of bid/ask Price of 37018770(SMART) is >= 32"

again after clicking on the button to send this modification, the result was right on TWS.

3) Now turn off the TWS and the excel

4) turn on again the TWS and the excel

5) Update the price condition to:
"last of bid/ask Price of 37018770(SMART) is >= 30"
?
After clicking on the button the modification is not sent, well I've taken a look at the API log and it is sent but the order is not modified on TWS and no error returned, so it is not working as expected.

Any clues on this? What can I do or?test?

I attach the excel demo workbook with the test row as explained here and the api log which shows the order is sent to the TWS and an answer comes from it with the price unchanged

Thank you very much


Re: Pairs trading: Parent vs Child order placement

 

I want to add my experience with combo orders. I also was at the beginning worried because they were not guaranteed. However it happened extremely seldom that not both legs filled almost simultaneously. I was usually satisfied with the executions I got. It might be different if one side is illiquid.


Re: Pairs trading: Parent vs Child order placement

 

Thank you for your support, I had a read at this page:??
which enlightened me a bit; essentially pairs are what I am looking after, however for the two stocks I am after, it cannot be guaranteed, therefore I do not see any interest in using this.
Moreover it only allows a diff trading for convenience, but say you have a stock priced $1 and the other 9$, your pair is co-integrated, you want to buy 9 shared of the first type and sell 1 of the second type.
Well with this tool you only know that you will take a $10 position for your pair, but if you had the 9$ one at a fair price, and the 1$ one you paid a disgusting spread, well your pairs trade is worth much less now because you will have to pay that spread 9 times as opposed to 1 time for the $9 one.

Not sure if the above makes sense, but anyways I will stick to submitting orders 1 after the other, first take position on the $1 at a good limit price, then hedge it immediately with the $9 at any price.


Re: Pairs trading: Parent vs Child order placement

 

I do use this type of orders for one special effect, which excludes trading in different securities (it's more of a simpler reversion trade). There are a some quirks to all of varieties of IB's bracket order, this one being no exception. Although my code for managing this was hacked a long time ago and I couldn't find any clear comments or side notes, I found in my code a check giving a strong warning which concerns automatically assigning the calculated values of 'hedgeParam' below 1.0. In particular the only accepted values for the apper to be 0.1, 0.2 and 0.5, which I probably found experientially. Other than that it likely should work.

Hope that helps.

§Ó§ä, 29 §Þ§Ñ§â. 2022 §Ô., 19:09 John <serorjb@...>:

For context, the goal is pairs trading, but one of the two securities is somewhat less liquid than the other,
hence the desired prioritization/conditionality of the most liquid order on the execution of the least liquid one first.


Problem with specific symbols

 

Hi all,

I am using ib_insync to buy/sell NYSE stocks, and generally the script works fine.

In some cases, when I buy or sell specific symbols - orders are not filled.
This happens with those symbols evey single time.
When using TWS GUI- there is no issue, orders are carried out the same as any other symbol.

Can you help understnading what makes those symbols 'special'??
Any idea how to handle this?

Examples for problematic symbols: SOFI, ABEV
Symbol with correct functioning: PLUG, AAPL

Thanks,
Kobi


Re: TWS order has reqId=0; how to cancel it from API?

 

This is weird for sure.? I just call reqAllOpenOrders() and reqOpenOrders() and store everything that gets returned in the callbacks.? That seems to grab them all and I then have the correct orderId.

Mike