Limit number of trades per day in a backtest

Dear Friend,

i try to limit the number of trades made per day per symbol. Like as this Limit number of trades per day in a backtest.

now i try to add BuyPrice / SellPrice / ShortPrice / CoverPrice. see the below code fyr.

tradeCount = onBuy = onShort = onSell = onCover = onBuyPrice = onShortPrice = onSellPrice = onCoverPrice = 0;
	 
for( i = 0; i < BarCount; i++ )
{
    // reset trade counter on the new day
    if( new_day[ i ] ) tradeCount = 0;

    // keep buy signal if there is no trade and trade count did not hit the limit
    if( Buy[ i ] AND tradeCount < N AND NOT onBuy )
    {
        OnBuy = 1;
		TradeCount++;				
    }
    else
		Buy[ i ] = 0; // ignore other buy signals
		
	if( onBuy AND Sell[ i ] )
	{
		onBuy = 0; // reset onBuy flag on exit
	}
	if( onBuy AND SellPrice[ i ] )
	{
		onBuy = 0; // reset onBuy flag on exit
	}
	
    if( BuyPrice[ i ] AND tradeCount < N AND NOT onBuyPrice )
    {
        OnBuyPrice = 1;
		TradeCount++;				
    }
    else
		BuyPrice[ i ] = 0; // ignore other BuyPrice signals

	if( onBuyPrice AND Sell[ i ] )
	{
		onBuyPrice = 0; // reset onBuyPrice flag on exit
	}
	if( onBuyPrice AND SellPrice[ i ] )
	{
		onBuyPrice = 0; // reset onBuyPrice flag on exit
	}
		
	if( Short[ i ] AND tradeCount < N AND NOT onShort )
	{
		onShort = 1;
		TradeCount++;		
	}
	else
		Short[ i ] = 0; // ignore other Short signals

	if( onShort AND Cover[ i ] )
	{
		onShort = 0; // reset onShort flag on exit
	}
	if( onShort AND CoverPrice[ i ] )
	{
		onShort = 0; // reset onShort flag on exit
	}

	if( ShortPrice[ i ] AND tradeCount < N AND NOT onShortPrice )
	{
		onShortPrice = 1;
		TradeCount++;		
	}
	else
		ShortPrice[ i ] = 0; // ignore other ShortPrice signals
								
	if( onShortPrice AND Cover[ i ] )
	{
		onShortPrice = 0; // reset ShortPrice flag on exit
	}
	if( onShortPrice AND CoverPrice[ i ] )
	{
		onShortPrice = 0; // reset ShortPrice flag on exit
	}

}

i am facing problem is excess signal

and also How to add Reverse Trade Long or Short in Sell/SellPrice or Cover/CoverPrice.

dear firends,

please help to above code solve.

@Ganesan,

Look into Static Variables for each stock. Will probably be similar logic to what you already have with just referencing the stock's specific trade count.