Keyboard Shortcuts
Likes
- Twsapi
- Messages
Search
upper and lower limit
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
|
Re: How to get position info in "points"
As ´³¨¹°ù²µ±ð²Ô mentioned, you need to keep track of PnL yourself based on the executions.
Reconciliating against reqPnl should be a governance / secondary check function and not be based on for trading.
?
As such it's not directly a TWS related question. For example, more advanced PnL engines will keep track of the following also:
- arrival time
- arrival price (what is current market price when you decide to go long)
- fill times
- fill prices
- average of fill prices
- slippage (fill price - arrival price)
- commisions
- market cost (fill price - mid price on each fill)
- price impact (mid price 3 minutes later vs fill price)
- MAE (in-trade)
- MFE (in-trade)
- draw-down across trades
- profit
- %win
- avg. win
etc...
?
You would need to pay alot of attention to the number handling (especially where you might have zeroes and a division - for example with 0 trades you would return something for %win even if the division by zero number of trades is undefined).
Depending on what you're doing you might want to optimize secondarily also for different parameters (like minimize price impact) or just use one of the existing TWS algos. |
Re: Purebasic and TWS
I am going to have a look on Python, but I am affraid that mastering all the subtleties of a new language could take a lot of time? (a bit lazy too...). ? --------------------------------------------------------------------------------------------------------- Here is the code generated by chatGPT for the "TWSLib.dll" VERSION. This code is only for getting started. --------------------------------------------------------------------------------------------------------- ; Load the TWSLib.dll (make sure it's in the same folder as the executable or in the system PATH) If OpenLibrary(0, "TWSLib.dll") ??? Debug "TWSLib.dll loaded successfully" ??? ??? ; Declare the functions from the DLL ??? ; The function signatures are derived from TWSLib documentation and can be different ??? ; Depending on what you're doing, the function names and arguments might change ??? ??? ; Connect to TWS ??? Define ConnectToTWS.i ??? ConnectToTWS = CallFunction(0, "Connect", 7496, "127.0.0.1", "clientId") ; The Connect function is for establishing a connection ??? ??? If ConnectToTWS = 1 ; Check if connected successfully (depends on how TWSLib.dll works) ??????? Debug "Connected to TWS" ??????? ??????? ; Now you can send a request. For example, let's request account data ??????? ; You would need to call the appropriate methods for account data or market data ??????? ??????? ; Example of account request (you'd need to look up exact method and params in TWSLib) ??????? Define requestAccountData.i ??????? requestAccountData = CallFunction(0, "reqAccountSummary", 9001, "All", "NetLiquidation") ; Example parameters ? ??????? ; Process response (usually you need a callback handler to get updates, ??????? ; but you can poll the data periodically) ??????? ; Placeholder code for data reception ? ??????? Debug "Requested account summary data" ? ??? Else ??????? Debug "Failed to connect to TWS" ??? EndIf ? ??? ; Don't forget to disconnect when done ??? CallFunction(0, "Disconnect") ; Disconnect from TWS ??? ??? ; Release the library when done ??? CloseLibrary(0) Else ??? Debug "Failed to load TWSLib.dll" EndIf?
---------------------------------------------------------------------------------------------------------
Here is the code generated by chatGPT for the SOCKET version. ---------------------------------------------------------------------------------------------------------
; PureBasic code to connect to the TWS API using socket ? ; Define TWS API socket port and host #TWS_PORT = 7496? ; Default TWS socket port #TWS_HOST = "127.0.0.1"? ; Default local host ? ; Initialize the socket connection If OpenNetworkConnection(#TWS_HOST, #TWS_PORT) ??? Debug "Connected to TWS API." ??? ??? ; Send a request to TWS (example: request account summary) ??? ; Example request string to get account summary ??? ; The following message is the format for sending API requests to TWS ??? ; This particular message is an Account Summary request ??? ??? Define requestString.s ??? requestString = "10|1|1|1|4|0|1" ??? ??? ; Send the message to TWS ??? WriteStringN(requestString) ??? ??? ; Wait for response from TWS (you can use a loop here to read the data) ??? Define response.s ??? response = "" ??? While AvailableNetworkData() ??????? response + ReadString(AvailableNetworkData()) ??? Wend ??? ??? ; Output the response ??? If Len(response) > 0 ??????? Debug "Received response from TWS: " + response ??? Else ??????? Debug "No response received." ??? EndIf ??? ??? ; Close the network connection ??? CloseNetworkConnection() Else ??? Debug "Failed to connect to TWS API." EndIf--------------------------------------------------------------------------------------------------------- |
Re: Purebasic and TWS
As Richard suggest with reasons, Python is most probably the best choice for a programmer who need to switch to another language
You can find a lot of support here in Python, and Java (and C/C++)
Python is one of supported language from IBKR (you need a reference point)
?
While IBKR is Asynchronous (that's the beauty of it), Python users seems happy with it and there are some decent provision for multi-threading in Python.
Just fight using any "sleep", there are always a good trigger or cv to listen to.
I never heard of anybody complaining about python performance for algo trading, before you experience a difference you will have to deal with a lot of unrelated issues (coloc, venues selection)
As per portability, switching from one platform to another involve so many details that code refactoring is not the top one of the list (even in C++ if you use C20 and standard RTL call, porting from/to Windows/Linux is a matter of days on first iteration)
?
As per code generated by ChatGPT, able to deal with IBKR TWS API, I am presuming here, but it could be a layer that obstruct understanding of some detail and make your debugging life a misery.
Out of curiosity I wonder what it looks like, can you post it as an attachement?
I am really intrigued!?
Knowing the IBKR API,how can GPT generate a working lib out of nowhere but specs ?
?
Last though:
I am under the impression that IBKR have a decent REST API implementation. This could be a simpler path for you if you don't want to use a lib. REST is now universally implemented you should not have issue in Basic
? |
Re: Purebasic and TWS
Thank you for your detailed response.
?
My first idea was to use VB6 that I have used in the past but it is no longer supported.
Switching to VB.NET seems to be a step as large as to any other language. ?
The trading algorithm is very simple. It uses only one instrument (future). I do not expect to use a large variety of TWS function calls.
But things always get much more complicated as expected... ?
Programming also involves psychology. I have some aversion for (semi)interpreted stuff.
I have worked professionally in embedded computing which means designing compact electronic boards and compact programs that fit into small microcontrollers. My trading configuration will use a tiny PC (working 24 hours a day) that consumes 16W. Power will be backed up directly with a battery (no inverter at all). Thats the KISS approach. The simpler, the more reliable it should be: a professional "obsession" ;-) ?
With Purebasic the only thing that I am affraid of is that with TWS everything seems to be asynchronous.
So some kind of multitasking or multithreading handling could be required. ?
By the way, I just made a request to chatGPT and in just a few microseconds it generated source code for both socket and TWSLib.dll choices.
So both options seem to work. Although the second seems to be simpler. ?
|
Re: Purebasic and TWS
¿ªÔÆÌåÓýYou need to decide what your priorities are. If you just want a large challenge, by all means push ahead with PureBasic, but it will be difficult to say the least because you¡¯ll be pretty much entirely on your own, working on a topic that you have no experience of. And for what benefit? ¡®compact¡¯, ¡®efficient¡¯, ¡®no dependencies¡¯ etc are simply irrelevant distractions. ? But if you want to actually get something useful (for trading) working in the shortest possible time, then just forget about PureBasic. ? If you¡¯re already familiar with some form of Basic, then you could use Visual Basic .Net, which will cost you nothing if you use the free Community Edition of Microsoft Visual Studio: if you haven¡¯t used Visual Studio before you¡¯ll be blown away by its massive capabilities, and ¡®free¡¯ doesn¡¯t mean ¡®cut down¡¯, it just means you can¡¯t use it for commercial software. ? Visual Basic .Net works perfectly well with IBKR¡¯s C# API (as does any other .Net language). But it¡¯s still a significant learning curve to make good use of the API, as it also is in other languages like Java. ? But by far the fastest way to be productive is to use Python (and especially the ib-async API from , which is a third-party API implementation that makes life a lot simpler in many ways). ? Don¡¯t be fooled into thinking that Python, being interpreted rather than compiled, is a poor choice: for the kind of programs we write using the API, that difference is entirely irrelevant. While it¡¯s true that a compiled API might get your order out of the door a couple of microseconds sooner than an interpreted one, that advantage is totally buried by the several milliseconds, or more likely several tens of milliseconds, that it takes for your order to reach the exchange. ? By the way, I¡¯m not a Python user (there was no Python API when I started on this back in 2003), but I¡¯m very much aware of the considerable productivity benefits it offers. I¡¯m just too hugely invested in more ¡®traditional¡¯ languages to make the switch. ? If you decide to go the Visual Basic (or C#) route, I can provide you with a simple (but meaningful) sample program that shows how to get started. Actually I¡¯ll link to it here ¨C it¡¯s a couple of years out of date and uses the 10.19 API version, but it still works fine: ? ? You¡¯ll find the executable in the bin/debug subfolder, so you can run it to see what it does. If you load the project into Visual Studio, you won¡¯t be able to compile it without first updating the reference to the C# API. ? I hope this isn¡¯t all too confusing¡ ? Richard |
Re: No Security definition has been found for the request when running cpp sample code
¿ªÔÆÌåÓýreqRealTimeBars is served by the historical data server. But IBKR does not maintain ¡®last price¡¯ historical data for forex pairs. So you will never get realtime bars for any forex pair, if you have WhatToShow=¡±TRADES¡±: you¡¯ll just get this error message 420: ? Invalid Real-time Query:No historical market data for EUR/CASH@FXSUBPIP Last 0 ? But if you set WhatToShow=¡±MIDPOINT¡±, it works fine: ? 20241203 09:49:47.068? id=131072 time=20241203-09:49:40 open=0.829655 high=0.829655 low=0.82962 close=0.82964 volume=-1 WAP=-1 count=-1 20241203 09:49:50.362? id=131072 time=20241203-09:49:45 open=0.82964 high=0.82968 low=0.82964 close=0.829665 volume=-1 WAP=-1 count=-1 20241203 09:49:55.533? id=131072 time=20241203-09:49:50 open=0.829665 high=0.829665 low=0.82962 close=0.82962 volume=-1 WAP=-1 count=-1 ? Richard ? |
Re: Is there generic method to format contract for reqContractDetails
Glad that this works for you. There is one caveat about requesting contracts by ConId. It may not apply to your use case but you should be aware (and you may already be). The ConId uniquely identifies a specific instrument, possibly even for a long time. But if the issuer makes any change to the instrument that requires a change to the secId (ISIN/CUSIP, ...) IBKR changes the ConId as well. Even if the Symbol or LocalSymbol of the instrument does not change. Examples are stock splits, change of primary listing exchange, and corporate mergers, but there may be others. SH just went through this in early November. Generally in these cases, the old ConIds become invalid and result in request errors the moment the instrument change takes place. The Contract option "includeExpired" only works for certain instruments (such as Futures) and does not work for Stocks/ETFs/ETNs etc. For many use cases, your "four field" contract lookup such as "SH:STK:SMART:USD" might still be the best bet. ´³¨¹°ù²µ±ð²Ô ?
On Mon, Dec 2, 2024 at 07:51 AM, Andy Sanders wrote:
|
Re: Purebasic and TWS
¿ªÔÆÌåÓýI haven't seen an API version for basic or purebasic.? There are versions for C++, C#, Python, java and others.? But I'm not sure if that would work well and you wouldn't have much for support as you progress. Perrhaps someone else knows more on this... -------- Original message -------- From: "Tradiator via groups.io" <yahoo@...> Date: 2024-12-02 7:02 a.m. (GMT-08:00) Subject: [TWS API] Purebasic and TWS 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.
|
Purebasic and TWS
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.
|
Re: Is there generic method to format contract for reqContractDetails
I wish that was true but it is not.?
Just double checked, having only ConID doesn't work for SPY stock, other security types work fine.?
Error validating request.-'bI' : cause - The symbol or the local-symbol or the security id must be entered |
Re: Is there generic method to format contract for reqContractDetails
The ConId by itself uniquely identifies the Contract. If you already have a ConId (as you said from orders, positions, or similar objects), SecType and LocalSymbol are not required to retrieve the corresponding ContractDetails object. ´³¨¹°ù²µ±ð²Ô ? ? On Mon, Dec 2, 2024 at 12:38 AM, Andy Sanders wrote:
|
Re: Is there generic method to format contract for reqContractDetails
Thank you for the explanation.?
After mentioning that contract can be "overspecified", I tried? to simplify definition instead of complicating it.?
Appeared to be that specifying only 3 fields is enough to specify the contract.?
?
This information is returned for all open orders and positions. Technically, ConId is not required, but without ConId, reqContractDetails can't find some contracts within 5 seconds, specifically those with options expiring daily, which is not acceptable. Final version cut down to 3 fields only, works fine for stocks, options, forex, futures, futures options.?
?
public static Contract GetContract(InstrumentModel instrument)
{ ? var basis = instrument.Basis; ? var derivative = instrument.Derivative; ? var contract = new Contract ? { ? ? //Symbol = basis?.Name, ? ? LocalSymbol = instrument.Name, ? ? //Multiplier = $"{instrument.Leverage}", ? ? //Exchange = instrument.Exchange ?? "SMART", ? ? SecType = GetInstrumentType(instrument.Type), ? ? ConId = int.TryParse(instrument.Id, out var id) ? id : 0, ? ? //Currency = instrument.Currency?.Name ?? nameof(CurrencyEnum.USD) ? }; ? //if (derivative is not null)
? //{ ? // ?contract.Strike = derivative.Strike ?? 0; ? // ?contract.LastTradeDateOrContractMonth = $"{derivative.Expiration:yyyyMMdd}"; ? // ?switch (derivative.Side)
? // ?{ ? // ? ?case OptionSideEnum.Put: contract.Right = "P"; break; ? // ? ?case OptionSideEnum.Call: contract.Right = "C"; break; ? // ?} ? //} ? return contract;
} |
Re: Is there generic method to format contract for reqContractDetails
The Contract object contains a lot of information about an instrument, but the various API request calls thatt take Contract object parameters transmit only a subset of those to TWS/IBGW. Which those are is not defined by the API specification, so the best practice is to always use complete Contract objects that you receive from IBKR through reqContractDetails calls. I understand that many code samples give you a different impression (e.g. they make contract objects up on the fly) but "home made" Contract objects do not result in reliable results over long periods of time. Think about reqContractDetails as a database query. Depending on what you want to see, you provide more or less attributes to the query:
But you would generally not call reqContractDetails with a complete Contract object you have received from IBKR. Chances are that those contracts over-specify the instrument which can indeed result in a "No security definition was found" error. One convenient way to design your reqContractDetails queries (you'd do that once) is ?Richard King's Another path are the complete TWS API client applications that ship with TWS API (such as the pre-compiled Java and C# apps). They also have screens where you quickly can experiment with reqContractDetails. But Richard's tool is more purpose built and might get you results quicker. Many of our members call reqContractDetails for the instruments they are interested in when their client applications start, or at least once per session, and use the resulting Contract object for all requests. The returned ContractDetails object(s) do not just contain the Contract you are looking for, but also the actual trading hours for the next few days. This is very helpful to detect short notice changes (emergencies, local events) and the exact trading hours around public holidays. You can also search our message archive, since there are several topics about how to specify contract queries for various instrument types. Hope this helps as a direction for a reliable approach. ´³¨¹°ù²µ±ð²Ô PS. When you request market data with a contract that specifies just ES:IND:CME:USD you get very likely data for the ES Index and not the ES Future. If you want to request data for the ES Future with a "home made" contract, specify the fields I mentioned above: type such as type FUT plus localName or lastTradeDateOrContractMonth or type CONTFUT. ? ?
On Sun, Dec 1, 2024 at 05:01 PM, Andy Sanders wrote:
|
Is there generic method to format contract for reqContractDetails
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?
?
|
Re: How to get position info in "points"
For the future readers.
Using average open price and the current price, you can get PnL in points.?
?
?
Average? double average_op = (op1 * lot1 + op2 * lot2 + opN * lotN) / (lot1 + lot2 + lotN);
Profit in points: double profit = Bid - average_op;
What I was expected to receive from IB API is an ability to see the PnL in points and dollars separately.?
Appeared that IB returns only average cost in dollars and only for one unit, so there is no need to divide it by number of contracts but it needs to be divided by multiplier.?
?
Examples?
?
What "reqOpenPositions" returns
After dividing by multiplier I can get average open price?
I know average cost in dollars that IB returns.?
In other words, there are always 2 calls.?
|