Order status randomly becomes APIpending, Presubmitted

ib%20controller

I am trying to submit the order from Amibroker to TWS but the order status in Ibcontroller randomly becomes APIpending, (Not success) Presubmitted (Not Success) but sometimes it is "filled" (success). It is very annoying. Can anyone help? Please find the AFL code below:

    BUY11OID = ibc.PlaceOrder( IBName, "Buy", 1, "LMT", 20000, TickSize, 
                          "DAY", SubmitOrders, TickSize/0.0001, "outsideRTH");
    for (dummy=0; dummy<40; dummy++)
      //  ibc.Sleep(10);  

Thanks for your help!

try using round( TickSize * 10000 ) instead of TickSize/0.0001

also it depends on what you are trading. If the TickSize itself is less than 0.0001 then you have a problem and best use 0.5 or 1 instead of round( TickSize * 10000 ). If your TickSize is 0.0001 or greater then round( TickSize * 10000 ) works good.

Thank you so much! It seems the order can be transferred to TWS 90% of the time now. Previously it was like 30%...

The latest situation is that although the order transmitted to TWS successfully (90% of the time), but the order status in IBcontroller is still shown as "Presubmitted" or "Pending submit"

Is there any code to update the order status in IBcontroller?
Thanks again!

not sure, what are you trading? Forex? Because there is a problem when your ticksize is less than 0.0001. In this case like I said best to use 0.5 or 1 instead of round( TickSize * 10000 ).

personally I do not have this issue with the order status. I currently trade futures only and the ticksize is always greater than 0.0001

I can give you code I use below. This is basically copied from what Tomasz put in http://www.amibroker.com/at/

see code below. So this code I call for instance to sell 1 future I call:

    ibc = GetTradingInterface( "IB" );
    ps = ibc.GetPositionSize( StaticVarGetText( "symbol" + cid ) );
    sendOrder_proc( StaticVarGetText( "symbol" + cid ), StaticVarGet( "transmitOnOffFlag" + cid ),
                    ps - 1, StaticVarGet( "tz" + cid ), LastValue( C ), LastValue( C ), cid, StaticVarGetText( "MKToLMT" + cid ) );
   Say( "Sell 1 Future" );

to add 1 future I call:

    ibc = GetTradingInterface( "IB" );
    ps = ibc.GetPositionSize( StaticVarGetText( "symbol" + cid ) );
    sendOrder_proc( StaticVarGetText( "symbol" + cid ), StaticVarGet( "transmitOnOffFlag" + cid ),
                    ps + 1, StaticVarGet( "tz" + cid ), LastValue( C ), LastValue( C ), cid, StaticVarGetText( "MKToLMT" + cid ) );
    Say( "Add 1 Future" );

to close the position I call:

    ibc = GetTradingInterface( "IB" );
    ps = ibc.GetPositionSize( StaticVarGetText( "symbol" + cid ) );
    sendOrder_proc( StaticVarGetText( "symbol" + cid ), Nz( StaticVarGet( "transmitOnOffFlag" + cid ) ),
                    0, StaticVarGet( "tz" + cid ), LastValue( C ), LastValue( C ), cid, StaticVarGetText( "MKToLMT" + cid ) );
    Say( "Close Position" );

so this just as an example what I do

procedure sendOrder_proc( symb, transmitOnOffFlag, pstn, tz, ask_pp, bid_pp, cidi, orderType )
{
    //_TRACE( "tz: " + tz );
    // launch IB controller
    ibc = GetTradingInterface( "IB" );

    // check if we are connected OK
    if( ibc.IsConnected() )
    {
        ps = ibc.GetPositionSize( symb );
        // calculate number of shares
        nos = pstn - ps;
        // transaction price
        ask_price = ask_pp;
        bid_price = bid_pp;

        if( orderType == "MKT" )
        {
            ask_price = 0;
            bid_price = 0;
        }
        else
            if( orderType == "LMT" )
            {
                ask_price = ask_pp;
                bid_price = bid_pp;
            }

        if( nos > 0 )
        {
            // retrieve orderID from previous run, will be empty if no order was placed before
            OrderID = StaticVarGetText( "OrderID" + cidi );

            // place order
            if( ibc.IsOrderPending( OrderID ) )
            {
                OrderID = ibc.ModifyOrder( OrderID, symb, "BUY", nos, orderType, ask_price, 0, "Day", transmitOnOffFlag, tz );
            }
            else
            {
                OrderID = ibc.PlaceOrder( symb, "BUY", nos, orderType, ask_price, 0, "Day", transmitOnOffFlag, tz );
            }

            // store orderID for next run so we know which order to modify
            StaticVarSetText( "OrderID" + cidi, OrderID );
        }
        else
            if( nos < 0 )
            {
                // retrieve orderID from previous run, will be empty if no order was placed before
                OrderID = StaticVarGetText( "OrderID" + cidi );

                // place order
                if( ibc.IsOrderPending( OrderID ) )
                {
                    OrderID = ibc.ModifyOrder( OrderID, symb, "SELL", abs( nos ), orderType, bid_price, 0, "Day", transmitOnOffFlag, tz );
                }
                else
                {
                    OrderID = ibc.PlaceOrder( symb, "SELL", abs( nos ), orderType, bid_price, 0, "Day", transmitOnOffFlag, tz );
                }

                // store orderID for next run so we know which order to modify
                StaticVarSetText( "OrderID" + cidi, OrderID );
            }
    }
}
5 Likes