Amibroker buy/sell -> IBControler -> TWS (Interactive Brokers) /// too many orders submitted

#1
Hi,

I would like to improve my autotrading so I would need your Help. Any help will be appreciated.

My problem is , when my Amibroker system scans for a signals (buy or sell ) at the end of each candle (minute candle) there is a signal generated by AFL code.
But Amibroker (6.20) sends signal ( to buy/sell) stocks but it does not check every time if there is some existing (placed) order before (or an order just realized a few candles earlier).
In this way Amibroker (VIA IBControler) sends to many orders so it wants to buy or sell to many stocks.

To make my words simpler: If there is first signal ,TWS will buy (MKT order) for example 200 stocks but later when a signal will come again TWS should not buy another 200 stocks because it has just bought thise 200 stocks so TWS should wait until sell signal will come and then should sell thise stoks during first sell signal. If another sell signal comes it will not sell more stocks becouse all stock was sold during first signal.

I prepare a visualization.
Please check it here:




AFL code is here:

Buy = Close>Ref(Close,-10);
Sell = Close<Ref(Close,-10);

if( LastValue( Buy ) )
{
ibc = GetTradingInterface("IB");

// check if we are connected OK
if( ibc.IsConnected() )
{
// check if we do not have already open position on this stock
if( ibc.GetPositionSize( "ALE-WSE-STK-PLN" ) == 0)
{
// transmit order
ibc.PlaceOrder( "ALE-WSE-STK-PLN", "Buy", 200, "MKT", 0, 0, "Day", True );
}
}
}



if( LastValue( Sell ) )
{
ibc = GetTradingInterface("IB");

// check if we are connected OK
if( ibc.IsConnected() )
{
// check if we do not have already open position on this stock
if( ibc.GetPositionSize( "ALE-WSE-STK-PLN" ) == 0)
{
// transmit order
ibc.PlaceOrder( "ALE-WSE-STK-PLN", "Sell", 200, "MKT", 0, 0, "Day", True );
}
}
}



Short = Sell;
Cover = Buy;
 
#2
Code:
Buy  = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
Use ExRem to suppress multiple buy/short orders before a sell/cover order is triggered

.
 
#3
Code:
Buy  = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
Use ExRem to suppress multiple buy/short orders before a sell/cover order is triggered

.

Hi, Thank you for your reply.
I think it does not work.
I prepared a better visualization in excel (below).
I just want only greens and reds signals to be generated.
Would you be so kind to help me in this?


1234.jpg