@Tomasz - I spent good amount of time with IB tech support, they dont look at AB code though, but they did troubleshoot a bit and said my orders are within limit of API. and for API cancelled issue, they said it is your API client or execution software.
I am not sure what do to next now. would be great if you can please give me some clues or leads. I have given my complete code here..
_SECTION_BEGIN( "ALGO Initialization" );
// for kelter channel
staticVarPfx = "" + GetChartID() + Name();
PrevTN = StaticVarGet( staticVarPfx + "TimeNumber" );
TN = LastValue( TimeNum() );
NewBar = TN != PrevTN;
tn2 = tn / 100;
symbo=Name();
ibc = GetTradingInterface("IB");
StaticVarSet( staticVarPfx + "TimeNumber", TN );
BuyOrderID = StaticVarGetText( staticVarPfx + "BuyOrderID" );
SellOrderID = StaticVarGetText( staticVarPfx + "SellOrderID" );
szLog = StaticVarGetText( staticVarPfx + "szLog" );
bzLog = StaticVarGetText( staticVarPfx + "bzLog" );
IBPosSize = ibc.GetPositionSize( symbo );
// To avoid repeat of trace in each bar once buy/sell done
bzLogged = StaticVarGetText( staticVarPfx + "bzLogged" );
szLogged = StaticVarGetText( staticVarPfx + "szLogged" );
BuyPending = ibc.IsOrderPending( BuyOrderID );
SellPending = ibc.IsOrderPending( SellOrderID );
cPosSize = ibc.GetPositionSize( symbo );
_SECTION_END();
_SECTION_BEGIN( "ALGO LOGIC SECTION" );
lineX = EMA( Close, 21 ); //Middle Line
buy = C > lineX;
Sell = C < lineX;
Buy = Ref( Buy, -1 );
Sell = Ref( Sell, -1 );
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
if( NewBar )
{
if( NOT BuyPending ) StaticVarSetText( staticVarPfx + "BuyOrderID", "" );
if( NOT SellPending ) StaticVarSetText( staticVarPfx + "SellOrderID", "" );
StaticVarSet( staticVarPfx+"OrderAlreadyPlaced", false );
ibc.CancelAllPendingOrders( symbo );
StaticVarSetText( staticVarPfx + "szLog", "" );
StaticVarSetText( staticVarPfx + "bzLog", "" );
}
_SECTION_END();
_SECTION_BEGIN( "ALGO EXECUTION SECTION" );
if( LastValue( Buy ) AND bzLog == "" )
{
if( cPosSize == 0 )
{
_TRACE( "NO POSITION | Next STEP --> GO LONG" );
if( Nz( StaticVarGet(staticVarPfx+"OrderAlreadyPlaced") ) == False )
{
StaticVarSet( staticVarPfx+"OrderAlreadyPlaced", True );
BuyOrderID = ibc.PlaceOrder( symbo, "Buy", OrderSize, "MKT", 0, 0, "Day", True );
StaticVarSetText( staticVarPfx + "BuyOrderID", BuyOrderID );
BuyOrderID = StaticVarGetText( staticVarPfx + "BuyOrderID" );
_TRACE( "BUY ORDER ID >>>>>>>> " + BuyOrderID );
StaticVarSetText( staticVarPfx + "bzLog", "" );
}
}
if( cPosSize == OrderSize AND bzLog == "" )
{
_TRACE( "Already Long Position [" + cPosSize + "] | Next STEP --> NO ACTION " );
StaticVarSetText( staticVarPfx + "bzLog", "X" );
}
if( cPosSize < 0 )
{
newOrderSize = abs( cPosSize ) + OrderSize;
_TRACE( " [" + cPosSize + "] Short Position Found | Next STEP >> COVER AND GO LONG " );
if( Nz( StaticVarGet(staticVarPfx+"OrderAlreadyPlaced") ) == False )
{
StaticVarSet( staticVarPfx+"OrderAlreadyPlaced", True );
BuyOrderID = ibc.PlaceOrder( symbo, "Buy", newOrderSize, "MKT", 0, 0, "Day", True );
StaticVarSetText( staticVarPfx + "BuyOrderID", BuyOrderID );
_TRACE( "BUY ORDER ID is " + BuyOrderID );
StaticVarSetText( staticVarPfx + "bzLog", "" );
}
}
if( cPosSize > 0 AND cPosSize != OrderSize )
{
if( cPosSize > OrderSize )
{
newOrderSize = abs( cPosSize ) - OrderSize;
_TRACE( " Extra [" + newOrderSize + "] LONG Found | NEXT STEP --> SELL Extra Positions " );
if( Nz( StaticVarGet(staticVarPfx+"OrderAlreadyPlaced") ) == False )
{
StaticVarSet( staticVarPfx+"OrderAlreadyPlaced", True );
SellOrderID = ibc.PlaceOrder( symbo, "Sell", newOrderSize, "MKT", 0, 0, "Day", True );
StaticVarSetText( staticVarPfx + "SellOrderID", SellOrderID );
_TRACE( "SELL ORDER ID is " + SellOrderID );
}
}
if( cPosSize < OrderSize )
{
newOrderSize = OrderSize - abs( cPosSize );
_TRACE( "LONG Positions Mismatch | Actual [" + cPosSize + "] / Exected [" + OrderSize + "] NEXT STEP --> Buy Extra " );
if( Nz( StaticVarGet(staticVarPfx+"OrderAlreadyPlaced") ) == False )
{
StaticVarSet( staticVarPfx+"OrderAlreadyPlaced", True );
BuyOrderID = ibc.PlaceOrder( symbo, "Buy", newOrderSize, "MKT", 0, 0, "Day", True );
StaticVarSetText( staticVarPfx + "BuyOrderID", BuyOrderID );
_TRACE( "BUY ORDER ID is " + BuyOrderID );
}
}
StaticVarSetText( staticVarPfx + "bzLogged", "" );
}
bzLog = StaticVarSetText( staticVarPfx + "bzLog", "bzLog" );
}
// SELL ZONE ///////////////////////////
if( LastValue( Sell ) AND szLog == "" )
{
cPosSize = ibc.GetPositionSize( symbo );
if( cPosSize == 0 AND szLog == "" )
{
_TRACE( "No Position | Next STEP --> DO NOTHING ( LONG ONLY) " );
StaticVarSetText( staticVarPfx + "szLog", "Y" );
}
if( cPosSize > 0 )
{
_TRACE( "Closing [" + cPosSize + "] Positions AS SELL SIGNAL TRIGGERED " );
if( Nz( StaticVarGet(staticVarPfx+"OrderAlreadyPlaced") ) == False )
{
StaticVarSet( staticVarPfx+"OrderAlreadyPlaced", True );
SellOrderID = ibc.PlaceOrder( symbo, "Sell", cPosSize, "MKT", 0, 0, "Day", True );
StaticVarSetText( staticVarPfx + "SellOrderID", SellOrderID );
_TRACE( "SELL ORDER ID is " + SellOrderID );
StaticVarSetText( staticVarPfx + "szLog", "" );
}
}
if( cPosSize < 0 )
{
OrderSize = abs( cPosSize );
_TRACE( "SHORTS IN LONG ONLY FUND- COVER [" + cPosSize + "] POSITIONS " );
if( Nz( StaticVarGet(staticVarPfx+"OrderAlreadyPlaced") ) == False )
{
StaticVarSet( staticVarPfx+"OrderAlreadyPlaced", True );
BuyOrderID = ibc.PlaceOrder( symbo, "Buy", newOrderSize, "MKT", 0, 0, "Day", True );
StaticVarSetText( staticVarPfx + "BuyOrderID", BuyOrderID );
_TRACE( "short Cover Buy ORDER ID is " + BuyOrderID );
StaticVarSetText( staticVarPfx + "szLog", "" );
}
}
szLog = StaticVarSetText( staticVarPfx + "szLog", "szLog" );
}
_SECTION_END();
_SECTION_BEGIN("price default");
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "", colorDefault, styleCandle );
_SECTION_END();