Keyboard Shortcuts
Likes
- Twsapi
- Messages
Search
Re: Order change does not get transmitted
Apologies for posting off topic, but this is an update to fun fact that I posted previously.?
When parent and child order has different quantity, TWS will display parent quantity for both, but will still trade correctly for child quantity whenever it is due. Kind of unexpected behaviour. |
Re: How to check order init margin (Java API)
TWS API uses Double.MAX_VALUE or Integer.MAX_VALUE to indicate a field/value that is not present, not known, or not initialized. A MAX_VALUE response for initMarginChange in orderStatus (not openOrder, right?) suggests that the order you are asking for is not tradable in that form at the time when you make the request. Keep in mind that the margin requirements you receive are specific to the account used for the request so that trading permissions need to exist. What happens when you use "Check Margins" for the same option in TWS? ´³¨¹°ù²µ±ð²Ô |
Re: Canceling one order in bracket order
Yes, it is. You can also verify this manually in TWS. Internally, TWS/IBKR adds all child orders of the bracket to a single group. You can see that in the TWS "Orders" tab or by inspecting the order objects returned to you by . We have not tried this, but you may be able to configure the individual order behavior with the ´³¨¹°ù²µ±ð²Ô On Thu, Jan 12, 2023 at 12:34 PM, GreenGreen wrote:Here is my question. I submit bracket order (main order, stop loss and take profit). Then later I cancel stop loss order order and for some reason take profit order gets cancelled as well. Is this expected behaviour? |
How to check order init margin (Java API)
Hi. I have to check option order margin requirements. For that purpose Im sending whatif order and gather info from return data. But sometimes Im recieving?openOrder responce:?m_initMarginChange=1.7976931348623157E308 As I understand, that is the max number. Why API returns such strange value? Is it possible somehow to get margin impact of my planed order correctly? Thanx ? |
Re: Order change does not get transmitted
Thank you again, Jurgen!
I will post lengthy code only in external links going forward. Fun fact about child order. When setting totalQuantity the value should be numeric (could not be empty), but numerical value does not matter, since it will be set equal to parent quantity. |
Re: Order change does not get transmitted
Please refrain from posting large chunks of code and log output. You cannot expect that the 6,000 members of the group spend a lot of time to understand and debug your code for you by email. And most of them use programming languages other than Python. Try to ask precise questions from the TWS API perspective and include only the most minimal amount of code and log output, if any. I suggest you carefully re-read the entire section of the and make some experiments in TWS and Python based upon that information. In that section you will find, for example
Quantities for bracket orders are somewhat linked so that it may be sufficient for you to change totalQuantity in the parent order. But for that you need to set the transmit flag to "true" or you will have to click "Transmit" in TWS as you describe. ´³¨¹°ù²µ±ð²Ô |
Re: Cannot transmit order (main order and stop loss orders)
The example code does sets the parentID, so it does correctly attempts to have a parent/child relation.
order.orderId = app.nextValidOrderId
stopLossOrder.orderId = app.nextValidOrderId + 1
stopLossOrder.parentId = app.nextValidOrderId I suspect the issue could be the use of both OrderType and orderType, in python you can dynamically add attributes in that way, but you should have only the expected orderType, what happens if you have both I don't know.? There is also an error on the limit price being a string (should be a float) order = Order()
order.OrderType = "LMT";???? <<<<<
order.action = 'BUY'
order.totalQuantity = 3
order.orderType = 'LMT'????? <<<<<
order.lmtPrice = '21'??????? <<<<<
order.transmit = False
|
Order change does not get transmitted
I hope there are less fat fingers mistake in this post then in previous one?
My goal is to submit parent/child order (main order and stop loss order) and then change quantity for both orders (from 17 to 2).? My initial submission goes fine, both main order and stop loss are getting submitted without any problems. But my change order does not seem to go through. After submitting change order in TWS I see my main order showing quantity of 2 and in Transmit column shows "Update" and my stop loss order only shows original quantity of 17. Only after I click manually on "Update" it changes to 2. Here is my code below:? from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
import time
import datetime
import threading
from ibapi.contract import Contract
from ibapi.order import Order
?
?
class TestApp(EWrapper, EClient):
? ? def __init__(self):
? ? ? ? EClient.__init__(self, self)
? ? ? ? self.nextValidOrderId = None
? ? ? ??
? ? # this is getting called upon establishing connection? ??
? ? def nextValidId(self, orderId: int):
? ? ? ? super().nextValidId(orderId)
? ? ? ? self.nextValidOrderId = orderId
? ? ? ? print("NextValidId:", orderId)
? ? ? ??
? ? def error(self, reqId, errorCode, errorString, advancedOrderRejectJson):
? ? ? ? print("Error: ", reqId, " ", errorCode, " ", errorString, "", advancedOrderRejectJson)
? ??
? ? def orderStatus(self, orderId, status, filled,
? ? ? ? ? ? ? ? ? ? ? ? ?remaining, avgFillPrice, permId,
? ? ? ? ? ? ? ? ? ? ? ? ?parentId, lastFillPrice, clientId,
? ? ? ? ? ? ? ? ? ? ? ? ?whyHeld, mktCapPrice):
? ? ? ? print("This is from orderStatus")
? ? ? ??
? ? ? ? super().orderStatus(orderId, status, filled, remaining,
? ? ? ? ? ? ? ? avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice)
? ? ? ? print("Order ID: ", orderId)
? ? ? ? print("---------------------/n")
? ? ? ? print("Status: ", status)
? ? ? ? print("---------------------/n")
? ? ? ? print("Filled: ", filled)
? ? ? ? print("---------------------/n")
? ? ? ? print("Remaining: ", remaining)
? ? ? ? print("=====================/n")
?
contract = Contract()
contract.symbol = "SPY"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "ARCA"
?
order = Order()
order.action = "BUY"
order.totalQuantity = 17
order.orderType = "LMT"
order.lmtPrice = 37
order.transmit = False
?
stopLossOrder = Order()
stopLossOrder.action = "SELL"
stopLossOrder.totalQuantity = 17
stopLossOrder.orderType = "STP"
stopLossOrder.auxPrice = 35
stopLossOrder.transmit = True
app = TestApp()
app.connect("127.0.0.1", 7497, 1)
?
api_thread = threading.Thread(target=app.run)
api_thread.start()
time.sleep(3)
?
order.orderId = app.nextValidOrderId
stopLossOrder.orderId = app.nextValidOrderId + 1
stopLossOrder.parentId = app.nextValidOrderId
?
app.placeOrder(app.nextValidOrderId, contract, order)
app.placeOrder(app.nextValidOrderId+1, contract, stopLossOrder)
time.sleep(10) # just to peek at TWS ?
print("+++++++++++++Now changing quantity+++++++++++++++++++++")
order.totalQuantity = 2
stopLossOrder.totalQuantity = 2
?
app.placeOrder(app.nextValidOrderId, contract, order)
app.placeOrder(app.nextValidOrderId+1, contract, stopLossOrder)
? ? ? ??And here is the output (Sorry it is very long, I am not sure why orderStatus gets triggered so many times) NextValidId: 53
Error:? -1? ?2104? ?Market data farm connection is OK:usfarm.nj??
Error:? -1? ?2104? ?Market data farm connection is OK:cashfarm??
Error:? -1? ?2104? ?Market data farm connection is OK:usfarm??
Error:? -1? ?2106? ?HMDS data farm connection is OK:ushmds??
Error:? -1? ?2158? ?Sec-def data farm connection is OK:secdefil??
Error:? 53? ?399? ?Order Message: BUY 17 SPY ARCA Warning: your order will not be placed at the exchange until 2023-01-11 09:30:00 US/Eastern??
This is from orderStatus
Order ID:? 53
---------------------/n
Status:? PreSubmitted
---------------------/n
Filled:? 0
---------------------/n
Remaining:? 17
=====================/n
Error:? 54? ?399? ?Order Message: SELL 17 SPY ARCA Warning: your order will not be placed at the exchange until 2023-01-11 09:30:00 US/Eastern??
This is from orderStatus
Order ID:? 54
---------------------/n
Status:? PreSubmitted
---------------------/n
Filled:? 0
---------------------/n
Remaining:? 17
=====================/n
This is from orderStatus
Order ID:? 54
---------------------/n
Status:? PreSubmitted
---------------------/n
Filled:? 0
---------------------/n
Remaining:? 17
=====================/n
This is from orderStatus
Order ID:? 53
---------------------/n
Status:? PreSubmitted
---------------------/n
Filled:? 0
---------------------/n
Remaining:? 17
=====================/n
+++++++++++++Now changing quantity+++++++++++++++++++++
This is from orderStatus
Order ID:? 54
---------------------/n
Status:? PreSubmitted
---------------------/n
Filled:? 0
---------------------/n
Remaining:? 17
=====================/n
|
Re: Cannot transmit order (main order and stop loss orders)
My apologies, GreenGreen. You are correct in that you have declared the callback. But you disconnect and exit immediately after placing the order, so that your program is gone well before the callback arrives. If you remove or comment the disconnect statement at the end you will receive an error callback such as: Error:? 3?? 321?? Error validating request.-'bN' : cause - Invalid order type was entered |
Re: Cannot transmit order (main order and stop loss orders)
Thank you for clarifying, ´³¨¹°ù²µ±ð²Ô?!?
I am not sure I understand you saying that my code does not handle errors from??IBApi::EWrapper::error.? I do have in my code:? def error(self, reqId, errorCode, errorString, advancedOrderRejectJson):
? ? ? ? print("Error: ", reqId, " ", errorCode, " ", errorString, "", advancedOrderRejectJson) Would not this be enough to get error message? I posted response from console and it had no error message returned (besides regular) |
Re: Cannot transmit order (main order and stop loss orders)
That is correct, because you are using a legitimate Python feature that has nothing to do with the TWS API. There is nothing "wrong" with your code (from a Python point of view), it just does not work the way you want it to work. Let me quote from a : "In general, any Python object can have an attribute of any name assigned to it at any time. Unlike in languages like Java, where an object's full memory layout needs to be known at compile time, in Python object attributes are stored in dictionaries. Dictionaries can hold any number of items, so there is nothing to keep you from assigning to any attribute at any time." So if you want, you can write legitimate Python such as: order.ToiToiToi= 'You can do it! Be profitable!". In your specific case, TWS API has no idea that you opted to add the extra attribute "OrderType" for your order objects. An attribute that is different from the attribute "orderType" TWS API as defined. But TWS API does realize that one of the two orders has no "orderType" attribute and you will get an . callback immediately after you call placeOrder. You just do not handle errors in the code segment in your original post. None of the other language allows the assignment to undefined class attributes and you would get errors well before you even start a program with such an assignment. You might want to consider some kind of Python Interactive Development Environment (IDE) that flags and color codes cases of misspelled class attributes, or a tools chain that checks your code before you run it. IDEs also give you convenient pull-down menus for all defined class attributes so that you never have to type long field names in the first place. ´³¨¹°ù²µ±ð²Ô I am sorry for keeping this chat grow, but I feel this is important. I confirmed that when I submit stopLossOrder.OrderType = "STP" (incorrect capitalization), there is neither error message in console nor record in log file. |
Re: Cannot transmit order (main order and stop loss orders)
toggle quoted message
Show quoted text
|
Re: Cannot transmit order (main order and stop loss orders)
Just a quick reminder. There are 6,000++ members listening and this post is becoming more like a chat. The error message (assuming it is related) is pretty clear. Please clean up the order type initialization for both orders:
order.OrderType = "LMT";
...
order.orderType = 'LMT'
... stopLossOrder.OrderType = 'STP' ´³¨¹°ù²µ±ð²Ô |
Re: Cannot transmit order (main order and stop loss orders)
As far as I understand, there is no particular designation for parent order. When we write stopLoss.parentId = some_integer, that is where parent child connection is established.?
For the sake of investigation, I will try to copy exactly what is posted in docs. |
Re: Cannot transmit order (main order and stop loss orders)
On Tue, Jan 10, 2023 at 06:34 PM, GreenGreen wrote:
I didn't see any parent order in your code... are we overlooking something? Did you post the same code you're using? Are you sure you don't want to follow the instructions from IB? Or do you still want to ignore the difference between the sample and your code? |
Re: Cannot transmit order (main order and stop loss orders)
I don't really know, but from docs: parent order should be submitted with transmit = False and child order (stop loss) with transmit = True. This way system ensures that both orders submitted simultaneously.
On related note, my logging is 10 minutes behind. It makes hard to troubleshoot.? |