Keyboard Shortcuts
Likes
- Twsapi
- Messages
Search
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” |
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.
toggle quoted message
Show quoted text
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----- |
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.
toggle quoted message
Show quoted text
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. |
Re: Building an Advanced Historical Stock Screener
Nick, I think you're dismissing the historical data mechanism too easily.
toggle quoted message
Show quoted text
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----- |
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.
toggle quoted message
Show quoted text
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ó:
|
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, |
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: Pairs trading: Parent vs Child order placement
Thank you very much both.
With trial and error, I found that Transmit=False is of no use in this case, as I need the orders transmitted; the child order got blocked in the current format as not on the same contract. I did not manage to use?hedgeType='P', hedgeParams=f'ratio = {self.ratio}', it seems like whatever I put in hedgeParam(s?) I get an "invalid ratio" error in return. How would I place these conditional orders manually, that's an excellent question and I honestly don't know. Algos seem interesting and I would love to do some profiling with them at a later stage, but these are not available for testing on paper money as I understand, so no use at this time for me. Execution Conditions seem to offer every possible condition, except the condition of another order getting executed, or maybe I am missing something. Combo orders might be closest to what I am looking for, but I spent a few hours on it thus far without understanding the concept.. Apparently I need to define a "BAG" contract like "AAPL, AMZN", put an action "BUY"/"SELL" and a ratio on each leg, and then append both legs to a?contract.comboLegs object. What is not clear at all from? is what to do next with this contract.comboLegs object? According to??I need to define an order with the orderComboLegs properties in there, but it suggests that this order must also have an action, and orderType e.g. "LMT", a quantity and/or a limit price? what does that mean? the quantity of what? the limit price of what exactly?
Also how does the guaranteed flag work? if I need both legs or nothing, I should simply not mention it? |
Re: TWS order has reqId=0; how to cancel it from API?
开云体育Thanks for the reply. I found the root cause of this.
I used ‘reqAllOpenOrders’ instead of ‘reqOpenOrders’ and that does not receive the ‘orderBound’ callback. The confusing thing is that it seems that only on new orders that were submitted in TWS after a last API call to ‘reqOpenOrders’ the orderId is 0 when you retrieve via ‘reqAllOpenOrders’ later. On orders that were submitted before a last call to ‘reqOpenOrders’, even when you retrieve them via ‘reqAllOpenOrders’ later, still a valid (negative) orderId is returned.? The API doc indeed only mentions the orderBound callback for ‘reqOpenOrders’ and says to use ‘reqAllOpenOrders’ to ‘obtain those orders created via the API regardless of client application’. So while it does retrieve orders from TWS also, their orderId will be zero if not bound prior. Bart On Mar 27, 2022, 1:28 PM -0700, 闯ü谤驳别苍 Reinold via groups.io <TwsApiOnGroupsIo@...>, wrote:
|