I am trying to enter short trades based on number of long open trades on any day

Suppose there are 4 long trades open on any day and I have 5 short trade signals out of which only 4 short trades will be opened.

SetCustomBacktestProc(""); 
if (Status("action") == actionPortfolio) { 
    bo = GetBacktesterObject();    //  Get backtester object 
    bo.PreProcess();    //  Do pre-processing (always required) 
	
    for (i = 0; i < BarCount; i++)    //  Loop through all bars 
    { 
		L_entrycount=0;	
		shortcount=0;    	
        for (sig = bo.GetFirstOpenPos(i); sig; sig = bo.GetNextOpenPos(i)) 
        {  
            if (sig.IsLong()) 
            { 
				L_entrycount=L_entrycount+1;//sig.GetPositionValue(); 
            } 
        } 
              
        for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
        {    //  Loop through all signals at this bar
                if (sig.IsEntry() && sig.IsLong()==False)
                {
					if (L_entrycount==0)
						sig.PosSize = 0;
                    else if (L_entrycount>0 && shortcount>L_entrycount)
						sig.PosSize = 0; 
					else
						shortcount=shortcount+1;
                            //  Set position size to zero to prevent purchase  
                }   
        }    //  End of for loop over signals at this bar

        bo.ProcessTradeSignals(i);    //  Process trades at bar (always required) 
    }  
    bo.PostProcess();    //  Do post-processing (always required) 
} 

Setting position size to zero is wrong. If you want to skip signal, set sig.Price = -1; as this is a way to tell AmiBroker to ignore signal. This technique is presented in the Knoledge Base: http://www.amibroker.com/kb/2014/10/23/how-to-exclude-top-ranked-symbols-in-rotational-backtest/