Re: One cancel all, trailing stop loss OR exit on close
Below is the code I use - Its a bracket order with a parent stop order, and 3 child orders - a profit taker, a stop loss, and a flatten order. the Flatten order does what you want - close the order at a certain time. The below is based on the ib_insync python library, but should give you an idea of how to mimic in your code. You basically place each orders one after the other ( the stoploss is the last order to be placed, as it has Transmit=True)
parent = StopOrder(
? ? ? ? action,
? ? ? ? quantity,
? ? ? ? stopPrice,
? ? ? ? orderId=getOrderId(),
? ? ? ? transmit=False,
? ? ? ? outsideRth=True,
? ? ? ? tif="GTD",
? ? ? ? goodTillDate=util.formatIBDatetime(flattenTime if gtd is None else gtd),
? ? ? ? **kwargs,
? ? )
? ? takeProfit = LimitOrder(
? ? ? ? reverseAction,
? ? ? ? quantity,
? ? ? ? takeProfitPrice,
? ? ? ? orderId=getOrderId(),
? ? ? ? transmit=False,
? ? ? ? parentId=parent.orderId,
? ? ? ? tif="GTC",
? ? ? ? outsideRth=True,
? ? ? ? **kwargs,
? ? )
? ? flatten = MarketOrder(
? ? ? ? reverseAction,
? ? ? ? quantity,
? ? ? ? orderId=getOrderId(),
? ? ? ? transmit=False,
? ? ? ? parentId=parent.orderId,
? ? ? ? tif="GAT",
? ? ? ? goodAfterTime=util.formatIBDatetime(flattenTime), ?# "17:50:00"
? ? ? ? outsideRth=True,
? ? ? ? **kwargs,
? ? )
? ? stopLoss = StopOrder(
? ? ? ? reverseAction,
? ? ? ? quantity,
? ? ? ? stopLossPrice,
? ? ? ? orderId=getOrderId(),
? ? ? ? transmit=True,
? ? ? ? parentId=parent.orderId,
? ? ? ? outsideRth=True,
? ? ? ? tif="GTC",
? ? ? ? **kwargs,
? ? )
|
Re: One cancel all, trailing stop loss OR exit on close
How do I group the orders into the oca groups?
|
Re: How to get real time data for INDEX and FUTURES using TWS API reqMktData()?
You might want to reread the TWS API Reference, specifically the section on , Tareq.
|
Re: How to get real time data for INDEX and FUTURES using TWS API reqMktData()?
@Ted Penner: No, I am using TWS API for Python, not Rstudio.
|
Re: How to get real time data for INDEX and FUTURES using TWS API reqMktData()?
Is rstudio used for this?
toggle quoted message
Show quoted text
Hi, Normally I use the following format of code to get real-time market data for a Symbol, which works fine: ``` contract = Contract() contract.symbol = sym contract.secType = "STK" contract.currency = "USD" contract.exchange = "SMART"
reqMarketDataType(1) reqMktData(x, contract, "", False, False, []) ```
So I want to write a similar code to get real-time data for FUTURES and INDEX. But the problem is, I have a little bit of doubt about how to write the symbol of an INDEX/FUTURES to receive actual data. Because the following code is not working: ``` contract = Contract() contract.symbol = "/ES" contract.secType = "IND" contract.currency = "USD" contract.exchange = "SMART"
reqMarketDataType(1) reqMktData(x, contract, "", False, False, []) ```
Does anyone have any idea how to write the code for INDEX/FUTURES to receive real-time data?
Thanks - Tareq
--
|
How to get real time data for INDEX and FUTURES using TWS API reqMktData()?
Hi, Normally I use the following format of code to get real-time market data for a Symbol, which works fine: ``` contract = Contract() contract.symbol = sym contract.secType = "STK" contract.currency = "USD" contract.exchange = "SMART"
reqMarketDataType(1) reqMktData(x, contract, "", False, False, []) ```
So I want to write a similar code to get real-time data for FUTURES and INDEX. But the problem is, I have a little bit of doubt about how to write the symbol of an INDEX/FUTURES to receive actual data. Because the following code is not working: ``` contract = Contract() contract.symbol = "/ES" contract.secType = "IND" contract.currency = "USD" contract.exchange = "SMART"
reqMarketDataType(1) reqMktData(x, contract, "", False, False, []) ```
Does anyone have any idea how to write the code for INDEX/FUTURES to receive real-time data?
Thanks - Tareq
|
Re: Volume and Float and Outstanding Shares
Finally getting close to getting
historical market data using Ross Hemingway,?
HHS Software. This is primarily for Delphi and C++ Builder
developers.
I don't do Java, C#, Visual C or Python.
On 2/17/2022 5:53 PM, Colin B Maharaj via groups.io wrote:
toggle quoted message
Show quoted text
Hey gals/guys,
Is it possible to get this type of information on a daily basis
for a handful of stocks.
Also, wrt, fibs, is that something we need to calculate and draw
for ourselves, from market data,? or can that be included in a
query.
Thanks again for your help.
|
Re: Reqpositions for 1 account
The short answer is yes. In other words, you can set that up anyway you need it to work.
Take another look at the documentation where you can find details for the reqPositions() and reqPositionsMulti( account, model ) subscriptions.
闯ü谤驳别苍
|
Reqpositions for 1 account
Hi Guys,
I read the docs on this, but still I am unclear. Hopefully someone can answer.
So, lets say I have 2 linked accounts and 2 algos. One algo for each account. If algo1 request positions using reqpositions, then would it receive only the positions in account 1? Or would it receive positions from all the linked accounts?
Let me know if more details are needed.
Thanks!!!
|
Re: Conditions on twsactivex api
Dear Richard,
thank you very much again, as always you were right 100%, after dividing the condition price using the price magnifier, it worked as expected. Thank you. It's a bit annoying having to get this data and applying?the computations to the price condition to finally when looking to the tws order condition it is like nothing has happened, so it feels a bit strange this procedure.
Again thank you for your explanation and guidance!! Have a great weekend
toggle quoted message
Show quoted text
El vie, 25 feb 2022 a las 0:35, Richard L King (< rlking@...>) escribió: Joan ? I think what you're seeing is the result of this contract having a priceMagnifier of 100 (see contract details below). ? There is a very poor description of the priceMagnifier here: ? It's too late at night to try and make sense of this, but I think that what it boils down to is that when setting conditions involving a contract with a price magnifier that is not 1, you'll have to divide the price you think you want to specify by the price magnifier and put that in the condition instead. ? And also, if you're using market rules to determine the minimum tick size for a contract at the current trading price (UK stocks for example have minimum tick sizes that depend on the price – it really is a nightmare), then you have to multiply all the edge prices and increments by the price magnifier. It's all jolly good fun really… ? Richard ? ? ---- Contract Details Begin ---- Contract ??? conId = 374571803 ??? symbol = KE ??? secType = FUT ??? lastTradeDate = 20220314 ??? strike = 0 ??? Right = None ??? multiplier = 5000 ??? exchange = ECBOT ??? primaryExchange = ????Currency = USD ??? localSymbol = KE?? MAR 22 ??? tradingClass = KE ? Details ??? marketName = KE ??? minTick = 0.25 ??? priceMagnifier = 100 ??? OrderTypes = ACTIVETIM,AD,ADJUST,ALERT,ALGO,ALLOC,AVGCOST,BASKET,BENCHPX,COND,CONDORDER,DAY,DEACT,DEACTDIS,DEACTEOD,GAT,GTC,GTD,GTT,HID,ICE,IOC,LIT,LMT,LTH,MIT,MKT,MTL,NGCOMB,NONALGO,OCA,PEGBENCH,SCALE,SCALERST,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF ??? validExchanges = ECBOT,QBALGO ??? underConId = 43627882 ??? longName = Hard Red Winter Wheat -KCBOT- ??? contractMonth = 202203 ??? industry = ????category = ????subcategory = ????timeZoneId = US/Central ??? tradingHours = ????????20220223:1900-20220224:0745 ??????? 20220224:0830-20220224:1320 ??????? 20220224:1900-20220225:0745 ??????? 20220225:0830-20220225:1320 ??????? 20220226:CLOSED ??????? 20220227:1900-20220228:0745 ??????? 20220228:0830-20220228:1320 ??????? 20220228:1900-20220301:0745 ??????? 20220301:0830-20220301:1320 ??????? 20220301:1900-20220302:0745 ??????? 20220302:0830-20220302:1320 ??? liquidHours = ????????20220224:0830-20220224:1320 ??????? 20220225:0830-20220225:1320 ??????? 20220226:CLOSED ??????? 20220227:CLOSED ??????? 20220228:0830-20220228:1320 ??????? 20220301:0830-20220301:1320 ??????? 20220301:1900-20220302:0745 ??????? 20220302:0830-20220302:1320 ??? evRule = ????evMultiplier = 0 ??? mdSizeMultiplier = 1 ??? aggGroup = 2147483647 ??? underSymbol = KE ??? underSecType = Index ??? marketRuleIds = 151,151 ??? realExpirationDate = 20220314 ??? lastTradeTime = 12:01 ? secIdList=() ---- Contract Details End ---- ?
|
Re: Conditions on twsactivex api
Joan ? I think what you're seeing is the result of this contract having a priceMagnifier of 100 (see contract details below). ? There is a very poor description of the priceMagnifier here: ? It's too late at night to try and make sense of this, but I think that what it boils down to is that when setting conditions involving a contract with a price magnifier that is not 1, you'll have to divide the price you think you want to specify by the price magnifier and put that in the condition instead. ? And also, if you're using market rules to determine the minimum tick size for a contract at the current trading price (UK stocks for example have minimum tick sizes that depend on the price – it really is a nightmare), then you have to multiply all the edge prices and increments by the price magnifier. It's all jolly good fun really… ? Richard ? ? ---- Contract Details Begin ---- Contract ??? conId = 374571803 ??? symbol = KE ??? secType = FUT ??? lastTradeDate = 20220314 ??? strike = 0 ??? Right = None ??? multiplier = 5000 ??? exchange = ECBOT ??? primaryExchange = ????Currency = USD ??? localSymbol = KE?? MAR 22 ??? tradingClass = KE ? Details ??? marketName = KE ??? minTick = 0.25 ??? priceMagnifier = 100 ??? OrderTypes = ACTIVETIM,AD,ADJUST,ALERT,ALGO,ALLOC,AVGCOST,BASKET,BENCHPX,COND,CONDORDER,DAY,DEACT,DEACTDIS,DEACTEOD,GAT,GTC,GTD,GTT,HID,ICE,IOC,LIT,LMT,LTH,MIT,MKT,MTL,NGCOMB,NONALGO,OCA,PEGBENCH,SCALE,SCALERST,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF ??? validExchanges = ECBOT,QBALGO ??? underConId = 43627882 ??? longName = Hard Red Winter Wheat -KCBOT- ??? contractMonth = 202203 ??? industry = ????category = ????subcategory = ????timeZoneId = US/Central ??? tradingHours = ????????20220223:1900-20220224:0745 ??????? 20220224:0830-20220224:1320 ??????? 20220224:1900-20220225:0745 ??????? 20220225:0830-20220225:1320 ??????? 20220226:CLOSED ??????? 20220227:1900-20220228:0745 ??????? 20220228:0830-20220228:1320 ??????? 20220228:1900-20220301:0745 ??????? 20220301:0830-20220301:1320 ??????? 20220301:1900-20220302:0745 ??????? 20220302:0830-20220302:1320 ??? liquidHours = ????????20220224:0830-20220224:1320 ??????? 20220225:0830-20220225:1320 ??????? 20220226:CLOSED ??????? 20220227:CLOSED ??????? 20220228:0830-20220228:1320 ??????? 20220301:0830-20220301:1320 ??????? 20220301:1900-20220302:0745 ??????? 20220302:0830-20220302:1320 ??? evRule = ????evMultiplier = 0 ??? mdSizeMultiplier = 1 ??? aggGroup = 2147483647 ??? underSymbol = KE ??? underSecType = Index ??? marketRuleIds = 151,151 ??? realExpirationDate = 20220314 ??? lastTradeTime = 12:01 ? secIdList=() ---- Contract Details End ---- ?
|
Re: Conditions on twsactivex api
Hi Richard! How are you doing? hope this message finds you well
Sorry to bother you but here we go again with the same price condition issue but now related to a futures contract... Wheat futures contract.?
I tried the previous tests we did and they work fine but with this one the?price condition loses its decimal point as you can see in the following image: After sending this Condition: "last Price of 374571803(ECBOT) is <= 880.25"
... and after sending this Condition: "last Price of 374571803(ECBOT) is <= 880" => The price condition is set to 88000
When I try directly on Tws the?order condition displays as:
I also tried sending "last Price of 374571803(ECBOT) is <= 880 1/4" but this?got?filled immediately and I suppose it set the wrong condition price?
What do you think? Maybe it's a matter of the?1/4 format min tick?
I can send you the excel file with the?tests if you want to try
Thank you again
toggle quoted message
Show quoted text
El mar, 8 feb 2022 a las 20:46, Richard L King (< rlking@...>) escribió: I'm glad it worked. When you get to my age, every time you send out something like this, you half expect a torrent of complaints because of all the things you've forgotten! ? Interestingly, looking into this has brought up some other issues that I'm not happy about (this happens every time I look at IB's API code – I always want to rewrite it all…). In particular I'm very disappointed with the way they've dealt with the recent change of sizes to decimal. I've made what I think are considerable improvements, and I'll submit them to IB for inclusion as well as the fixes for the conditions. But they may prefer to keep their kludge, since it has been published – though this decimal stuff is only in the 'latest' version, and that's relatively easy to change without upsetting too many people, so perhaps they'll accept it. ? Any, I'm glad I got to the bottom of the conditions problem. It was rather stupid of me not to realise sooner what was going on, but not to worry… ? Richard ? ? ? It works Richard!! You are the best!!! Wow... Thank you?so much for helping me and fixing this. I've just sent the?original Conditional price order successfully: " Price of 376589407(NYBOT) is >= 1.2111" which after more than 40 emails has been solved?by you. ? El mar, 8 feb 2022 a las 19:21, Richard L King (<rlking@...>) escribió: This is a link to the fixes to correct the problem described in this thread: ?
? This link should take you to a page showing a zip file called CSharpClient.zip. This zip contains copies of the files changed in this fix (unchanged files are not included). ? Download the zip, and copy each file to overwrite the corresponding existing file in your TWS API folder structure (make sure Excel is not running while you do this). ? Files changed are: ? - Source files that have changed in the CSharp API: these are the files that implement the various condition classes: you only need to copy these files if you intend to build the CSharp and/or ActiveX libraries yourself
- Compiled TWSLib.dll - this is the ActiveX control library. Note that both 'debug' and 'release' versions are included: don't copy these files if you intend to build the ActiveX library yourself. There should be no need to register these files
- Compiled CSharpAPI.dll – this is the basic CSharp ?API library: don't copy these files if you intend to build the CSharp library yourself
? If you want to build the ActiveX and/or CSharp API libraries yourself, you can do so using the TWSLib.sln and CSharpAPI.sln solution files in the ActiveX and Client subfolders. Note that to build TWSLib.sln you need to run Visual Studio with Administrator privileges. ? Any problems, let me know! ? Richard ? ? Just an update on this. ? I've discovered the cause of the problem. It occurred to me while lying in bed last night, that although the Invariant culture is used when parsing the condition, perhaps it isn't used when serialising it to send it to TWS. That hadn't occurred to me before, because I assumed it used the same routines as when sending any other API object's fields, which do you the invariant culture for numeric values. But in fact it doesn't use those for this price value, and indeed it uses the current culture of the computer. So if your computer is currently set to culture that uses a comma as the decimal separator, then a price of 1.15 is sent as 1,15. This throws the validation at the TWS end. The same applies in a couple of other places in the conditions code, for example a percent change condition. ? I've done a fix for this, and I'm just testing it at the moment. ? When I'm sure it works properly, I'll make an updated TWSLib available (and/or instructions for how to build it yourself). And I'll submit a pull request to IB so they can fix it. ? Richard ?
|
Re: How can I pull my account value in as an object to help calculate my contract quantity size?
Hi,
Thanks for the nice solution that you have provided. Yes, whenever I executed NAVs - it provided me a result like this:
Testing IB's API as an imported library: Waiting for IB's API response for accounts positions requests... Columns: [Symbol, Quantity, Average Cost, Sec Type] Waiting for IB's API response for NAVs requests... ? ? ? ? ? ?reqId? ? Account? ? ? ? ? ? ?Tag? ? ?Value Currency DUXX70XXX? ? ? 0? DUXX70XXX? TotalCashValue? 5000.04? ? ? USD
But what if I request more than one Value with the reqAccountSummary() - is there any way that I can capture all of the values? Such as (NetLiquidation, TotalCashValue, etc., etc.)
? ? ? ? 0, "All", "NetLiquidation, TotalCashValue"
On Sun, Jul 25, 2021 at 5:08 AM Rational-IM < ds@...> wrote: Maybe you are looking for something like this:?i
There is a code to get positions and NAVs for your account(s)
|
Re: How to print / return size of position for a given contract
I managed to solve it by using identifying features of the contract
e.g.
if contract.symbol == 'APPL':
|
Re: Retrieving past trades in an account
Thank you very much.? Changing it to 7 days provided what I was looking for.??
|
Re: Retrieving past trades in an account
Thank you very much!? I think the first option you proposed should work for me.
|
Re: How to print / return size of position for a given contract
That's a good idea, and I haven't heard of this before, thanks for letting me know about it! I'll look into it
|
Re: TWS on Windows 7: autoupdate fails, won't load
I only use the offline installer so I have control of when I update plus I have installers for several versions in case I need a specific version.
|
Re: TWS on Windows 7: autoupdate fails, won't load
Just a follow-up. the response from tech support stated that Win 8.1 is the minimum operating system required for TWS. They suggested using the offline TWS installer rather than the Updateable TWS installer.
|
Re: Retrieving past trades in an account
ds-avatar summarized this nicely.
In a similar topic last year, Richard King pointed out a tweak in TWS that allows you to retrieve execution reports for up to seven days. This does not work for IB Gateway, though. In TWS go to File -> New Window -> Trade Log and change the pull-down "Show trades for" from "Today" to a longer period. The maximum is "Last 7 Days".
闯ü谤驳别苍
toggle quoted message
Show quoted text
On Sun, Feb 20, 2022 at 11:09 PM, ds-avatar wrote:
In TWS API you can only retrieve trades for the current trading day.
You can use two approaches:
The first one is to request executions, i.e. reqExecutions. Pros: it is well known and seems to be widely used, and can consume executions filters (including contracts traded). Cons: even with a filter it could still be pretty verbose because it can return numerous partial fills per order which the client needs to accumulate and collate.
The second one is to get completed orders, i.e. reqCompletedOrders. Pros: it returns concise trade data per each order. Cons: despite being in API releases for at least 2 years it's still poorly documented, and it spews all completed orders after each request so any filtering must be carried out by the client.
For trades going back beyond 1 day, reports should be downloaded from the account management website (or set up for automatic delivery by email).
|