How to Limit scanner to defined number of signals

Hello everyone,

i am generating signals in scan on a portfolio of stocks.
it gives up many signals at each bar based on buy/sell rules.

i want:
if first entry signal (buy) is generated on any 1 script of portfolio, it should stop generating further signals of other scripts on same/upcoming bars and on upcoming bars, scan only that stock until sell signal is generated through sell condition/ applystop.

Tried STATICVARSET, but it is not passing value across scripts to stop signals of other scripts.

Hope, some one would have already experienced such need.

StarticVarSet should be work, can you post the code how do you use StarticVarSet?
Another way can be to write a flag file and read it. Write some content in it like "StopBuy" or "Startbuy".
You find this in the help of Amibroker, commands are fputs/fgets.
But the better way is to use StarticVarSet/StaticVarGet.

Here is the code...Tried through StaticVarset. But still not working.

Brief details of variables used:
Using "intrade" to capture information of trade taken.
Using "Bartime" to capture bar timestamp- (chart timeinterval is 1 minute, but scanner will be running every 5 secs to check whether stoploss hit or not. Therefore, want to avoid mulitple orders creation at same bar)
OpenPosname- is used to capture the symbol name, so that 1. no new short signals until previous one is closed. 2. cover rule can be used only for open position (for other portfolio stocks, cover rules can be skipped)

Short = 0;
Cover = 0;

Formula66MA = MA(C,18);
LessVolatility = HHV(High,60)/LLV(Low,60) < 1.1 ;
Uptrend = Close > MA(Close,200);
PrevDclose = TimeFrameGetPrice("Close", inDaily,-1);
TodayLow =  TimeFrameGetPrice("Close", inDaily,-1);


intrade = staticvarget("intrade");
Bartime = staticvarget("Bartime");
Samebar = Bartime == timenum();
StaticVarSet("SameBar", SameBar);
SameBar = StaticVarGet("SameBar");
SameBar = Nz(SameBar);

//Enter Short

If (Not Lastvalue(intrade) AND NOT LastValue(SameBar))
{
Formula66MA = MA(C,18);
LessVolatility = HHV(High,60)/LLV(Low,60) < 1.1 ;
Uptrend = Close > MA(Close,200);

ShortRule1 =  cross( Close,MA(Close,18))  ;

Short = ref(ShortRule1,-1);
Shortprice = open;

if(LastValue(Short))
{
Staticvarset("intrade", 1);
StaticVarSetText("OpenPosName", Name());
}
}

//Enter Cover
intrade = staticvarget("intrade");
OpenPosName = Staticvargettext("OpenPosName");
MatchPos = OpenPosName == Name();

If (Lastvalue(intrade)  and LastValue(MatchPos))
{
PrevDclose = TimeFrameGetPrice("Close", inDaily,-1);
TodayLow =  TimeFrameGetPrice("Close", inDaily,-1);
CoverRule1 = Cross(Formula66MA, Close) AND Close < PrevDClose ; 

Cover = ref(CoverRule1,-1) OR TimeNum() >= 152100 ; 
Coverprice = Open;

amount = 1.5; //Optimize("SL", .1, .1, 5, .1);  
//ApplyStop( stopTypeLoss, stopModePercent, amount, True ); 

amount = 0.1 ; //Optimize("SL", .1, .1, 5, .1);
ApplyStop( stopTypeLoss, stopModePercent, amount, True ); 

amount = 3.5 ; //Optimize("SL", 1, .5, 10, .5);
ApplyStop( stopTypeProfit, stopModePercent, amount, True );

period = 22; // ATR period
multiplier = 3; // ATR multiplier
ApplyStop(stopTypeTrailing, stopModePoint, multiplier*ATR( period ), True, True, 0, 120 );

if(Lastvalue(Cover) ==1 or Lastvalue(Cover) ==2 or Lastvalue(Cover) ==3 or Lastvalue(Cover) ==4 or Lastvalue(Cover) ==5) 
{
Staticvarremove("intrade");
Staticvarremove("OpenPosName");
intrade = StaticVarGet("intrade");
OpenPosName = StaticVarGet("OpenPosName");
}
}

wlLastsym = "Zeel-I.NFO";  // watchlist last symbol
CurrentSYMisLastSYM = wlLastsym == Name();


bartime = timenum();
StaticVarSet("bartime", LastValue(bartime));
bartime = StaticVarGet("bartime");

intrade = StaticVarGet("intrade");
OpenPosName = StaticVarGettext("OpenPosName");







//* Plot cover and Short Signal Arrows */
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
//PlotShapes(IIf(cover==2,       shapeHollowDownTriangle, shapeNone),4,0,ShortPrice,0);
Plot(Formula66MA, "MA18", colorBlue);
Plot(MA(Close,200), "MA200", colorYellow);

Filter = Short OR cover ; //CheckRuleA ;      


SetSortColumns( -3,-1 ); 
AddColumn( PrevDClose, "PrevDClose" ); 
AddColumn( Close, "Close" ); 
AddColumn( Short, "Short" );  
AddColumn( Cover, "Cover" ); 
AddColumn( intrade, "intrade");
AddTextColumn( OpenPosName, "OpenPosName");
AddColumn(Bartime, "Bartime");

I think, your formula don't will enter any trade. It will, but only if your short will enter on the first tick/bar-refresh of a new bar.
You get the LastValue( SameBar ) first and set it on the end of the formula to the new value. Then both values are the same and stop trades.
Have not tried this but maybe you can move this lines:

bartime = timenum();
StaticVarSet("bartime", LastValue(bartime));

to the if-statement:

if(LastValue(Short))
{
Staticvarset("intrade", 1);
StaticVarSetText("OpenPosName", Name());
}

You can see this behavior in the formula-window, if you add a line like this:

Title += "\n Lastvalue(SameBar): "  + LastValue( SameBar );

If you want to enter multiple orders with different shares on a same bar, but only one to a time, you have to build a static var for each share, set it and request it on every loop. Or you remove your already defined static var StaticVarSetText("OpenPosName", Name()); on a next bar and compare it. Maybe you will need then more than one.

Thank you htrader.. i was away from AFL for some time. Now, i am back.
Will check the solution.