I haven't been able to successfully close a position using
ibc.closeAll("AAPL")
It only works if I don't specify a ticker. i.e
ibc.closeAll()
Also, If it is not too much to ask, I would like to suggest two additional functionalities that i think would be very useful.
Ability to submit orders with "Adaptive IBALGO" order type- This would help ensure better execution price.
Ability to query margin requirement before submitting orders- As IB changes it's margin requirement very often this would help to prevent margin calls.
fxshrat
January 3, 2019, 10:08am
#2
Please read the IBcontroller docs carefully !
To close all open positions of a symbol use
ticker = "AAPL";
// or
// ticker = Name();
// or ...
ibc.CloseAllOpenPositions( ticker );
CloseAllOpenPositions ( [optional] string Ticker )
(new in 1.0.8)
Sends orders to close all currently open positions.
sends MARKET SELL/BUY order for each and every LONG/SHORT position in the portfolio page - should effectively close all open positions (provided that closing orders will fill correctly), note however that it may happen that these closing orders may fail (for example when markets are closed)
New in 1.1.0: now function accepts optional parameter. When it is provided, the function closes positions only on specified ticker. If it is not specified - it operates as previously (closes positions on all symbols).
As aside as for canceling all pending orders
CancelAllPendingOrders ( [optional] string Ticker )
(new in 1.0.8)
Cancels all pending orders
Sends cancel for each and every order displayed in the Pending orders page (with the exception of orders with Errors)
New in 1.1.0: now function accepts optional parameter. When it is provided, the function cancels orders only of specified ticker. If it is not specified - it operates as previously (cancels all pending orders on all symbols).
Michael
January 14, 2019, 5:46pm
#3
I have The same problem as daredamy.
ibc.CancelAllPendingOrders()
works fine, but
ibc.CloseAllOpenPositions()
or
ibc.CloseAllOpenPositions(ticker)
doesnt do anything.
fxshrat:
CloseAllOpenPositions
Thanks, fxshrat
Did you try ibc.CloseAllOpenPositions( ticker )
and verify that it works?
Tomasz
January 15, 2019, 11:50am
#5
Your posts are missing crucial information required to help you, like for example TWS VERSION that you are using. IB has long "tradition" of breaking things that were working.
Background
Some people come here and ask single-line SMS-style question (plus sometimes some random code) and expect "solution" without showing any effort made from their side.
Then all the good souls on this forum hopelessly try to figure out "what the heck original poster meant?"
So please follow the tips below so your question is more likely to get proper answer.
How to ask a good question
Follow the forum rules first: How to use this site Please use the search field in this forum, theā¦
Michael
January 15, 2019, 2:07pm
#6
I am using TWS Build 972.1p Aug 17 2018 (Stable Version)
Java Version 1.8.0_152
OS Windows 10
I tried the following:
_SECTION_BEGIN("Settings");
SVKey = Name()+NumToStr(GetChartID(),1.0,False);
ChartID=GetChartID();
Transmit = True;
Contracts=1;
ibc = GetTradingInterface("IB");
BuyOrderID = StaticVarGetText(SVKey+"BuyOrderID");
SellOrderID = StaticVarGetText(SVKey+"SellOrderID");
ticker="FDAX MAR 19-DTB-FUT-EUR";
_SECTION_END();
_SECTION_BEGIN("Triggers");
Buytrigger=ParamTrigger("BuyTrigger","BuyTrigger");
SellTrigger=ParamTrigger("SellTrigger","SellTrigger");
CloseAll=ParamTrigger("CloseAll","CloseAll");
_SECTION_END();
_SECTION_BEGIN("Orders");
if( Buytrigger )
{
BuyOrderID= ibc.ModifyOrder( BuyOrderID, ticker, "Buy", Contracts, "MKT", 0, 0, "Day", Transmit);
StaticVarSetText(SVKey+"BuyOrderID",BuyOrderID);
}
if( SellTrigger )
{
SellORderID = ibc.ModifyOrder( SellORderID , ticker, "Sell", Contracts, "MKT", 0, 0, "Day", Transmit);
StaticVarSetText(SVKey+"SellOrderID",SellOrderID);
}
if(CloseAll)
{
ibc.CloseAllOpenPositions(); //1.Try
//ibc.CloseAllOpenPositions(ticker); //2.Try
//ibc.CloseAllOpenPositions("FDAX MAR 19-DTB-FUT-EUR"); //3.Try
//ibc.CloseAllOpenPositions("Name()"); //4.Try
}
_SECTION_END();
Buying and Selling with this code works, but not CloseAll.
Tomasz
January 15, 2019, 11:23pm
#7
From the code I can see that your are using DTB futures. Did you try on plain US stock symbols?
Michael
January 16, 2019, 3:41pm
#8
I tried with german stock symbols ("BMW-SMART-STK-EUR").
There it is the same. Buying and Selling via AB is no problem, but CloseAllPositions
doesnt work. I cant try with US stocks, because I dont have the data-subscription.