I am attempting to use the autotrading interface for Interactive
Brokers and am trying to keep the system from entering a new order if
there is already an order in IB that is pending/partially filled/etc.
I wrote a quick loop to check for pending orders. The loop iterates
through the Pending list, and checks each symbol against the symbol
for the current trade. If the current symbol if found in the pending
list then it sets the "check" flag to 1. For some reason this does
not work as intended.
This is the relevant bit of code:
check = 0;
symbols = ibc.GetPendingList( 1, "" );
for( i = 0; ( symbol = StrExtract( symbols, i ) ) != ""; i++ )
{
//printf("Symbol: " + symbol + "\n" );
if (Name() == symbol) {check = 1;}
}
if( ibc.GetPositionSize( Name() ) == 0 && check == 0)
{
...[Enter Order]..
}
It seems that the value of "symbol" does not equal the value of
"Name()" even when the current ticker is in the symbol list, though
I'm not sure why. Any guesses? Alternatively, is there a "grep" type
command in AFL?
///////////FULL CODE///////////////////////////
if( LastValue( Buy ) )
{
ibc = GetTradingInterface("IB");
// check if we are connected OK
if( ibc.IsConnected() )
{
//Check if current symbol already has an order pending
check = 0;
symbols = ibc.GetPendingList( 1, "" );
for( i = 0; ( symbol = StrExtract( symbols, i ) ) != ""; i++ )
{
//printf("Symbol: " + symbol + "\n" );
if (Name() == symbol) {check = 1;}
}
// place order only if we do not have already open position on
this symbol and no pending orders
if( ibc.GetPositionSize( Name() ) == 0 && check == 0)
{
OrderID = ibc.PlaceOrder(Name(), "BUY", LastValue(
Shares ),
"STOPLMT", LastValue( BuyPrice ) + Buy_slip, LastValue( BuyPrice ),
"Day", False, 100 ); // place order but do not transmit yet
//Enter protective stop on buy order
ibc.PlaceOrder(Name(), "SELL", LastValue( Shares ),
"STOPLMT",
LastValue(BuyPrice) - LastValue( stop ) - Sell_slip,
LastValue(BuyPrice) - LastValue( stop ), "GTC", False, 100, "",
OrderID );
}
}
}