Assign Maximum positions

if(InWatchListName("Commodity"))
								{
									if(staticVarGet("comm")<3)
									{
										Buy[i]=1;
										dayssincebuy=0;
										StaticVarSet("buysig"+Name(),C[i]);
										StaticVarSet("comm",staticVarGet("comm")+1);
										
									}
								}
								else
								{
									Buy[i]=1;
									dayssincebuy=0;
									StaticVarSet("buysig"+Name(),C[i]);

								}

I am writing an afl code to assign maximum 3 open position to commodity watchlist and maximum 10 open positions overall. My code takes 10 maximum positions correctly. However it fails to assign maximum 3 positions and goes beyond the limit. Any kind of help will be appreciated.