Max OpenPositions

Hi,
I would like to ask you, how can I tell to IBController that I want open just 2 positions.
I use basic afl code to place order.For example I´m looking for signals in SP100.
I found 10 entry signals, I ordered them by PositionScore but I need open just 2 Longs.
When I do it IBController sends all founded signals to TWS. Not 2 but all.
honzasek

You can't tell IB that you want Max open positions.

Once you RANK your signals, keep a count of active trades and only fire those many as needed.

Use static variables if required.

Thank a lot but I don ´t know how.

Well then you can either search the Forum and Manual for static variables or post your working code.

Even I don't know how much you know and don't.

Here is may code:

Max_positions = 2; 
Pocatecni_ucet = 5000; 
Start_equity = 50;

SetOption("AllowSameBarExit", False); 
SetOption("MaxOpenPositions", Max_positions); 
SetPositionSize(Percent_equity, spsPercentOfEquity );
//SetPositionSize(Max_pozic,spsPercentOfEquity);
SetOption("AllowPositionShrinking",True);
SetOption("AccountMargin", 100);
SetOption("InitialEquity", Start_ucet );


mashort = Optimize("Prumer kratsi",25,5,50,1);
malong = Optimize("Prumer delsi",50,10,100,1);

TimeFrameSet( inWeekly );
MA1=MA(C,mashort);
MA2=MA(C,malong);

TimeFrameRestore(); 
 
MA1D = TimeFrameExpand( MA1, inWeekly ); 
MA2D = TimeFrameExpand( MA2, inWeekly ); 


PositionScore = ROC(C,60);


Buy = RSI(2)< 10 AND MA1D>MA2D AND MA(Close,mashort)>MA(Close,malong) AND Close > MA2D;

BuyPrice = Close;
 
Sell = Close>MA(C,5); 
SellPrice = Close; 



/*
=============================================
AUTO TRADING PART
==============================================
*/
Symbol = Name();
SetOption("MaxOpenPositions", Max_pozic); 
PercentageOfAvailableFunds = 100; //% of Available Funds
if (LastValue(Buy) || LastValue(Sell)) {
ibc = GetTradingInterface( "IB" );
if (ibc.IsConnected() ) {
pos = ibc.GetPositionSize(symbol);
if (LastValue(Sell)) {
if (pos > 0) {
orderID = ibc.PlaceOrder( symbol, "Sell", pos, "LMT", LastValue(C), 0, "GTC", True );
_TRACE("Sell "+symbol+", orderID "+orderID);
}
}
else if (LastValue(Buy)) {
if (pos == 0) {
CashBalance = StrToNum(ibc.GetAccountValue("CashBalance"));
Quantity = CashBalance * PercentageOfAvailableFunds/100/LastValue(C);
_TRACE("CashBalance "+CashBalance+", Closing price "+LastValue(C)+", Quantity "+Quantity);
Quantity = Max(1, round(Quantity));
orderID = ibc.PlaceOrder( symbol, "Buy", Quantity, "LMT", LastValue(C), 0, "GTC", True );
_TRACE("Buy "+symbol+", orderID "+orderID);
}
else {
_TRACE("Order for "+symbol+" already exists!");
}
}
}
else {
Error("IB Controller Interface is not connected");
}
}
if (LastValue(Buy)) {
}

I See one wrong in code. 2nd line in AUTO TRADE PART does not belong there. I apologize..