Scale-in Quantity to be sold in lumsum

Hello experts, i am from non programming background. i am using algoji api bridge to automate strategy . It collects the quantity through scale in and sell the whole quantity in once. But quantity is fixed in given afl to automate. I added the two for loops in given bridge program. Two different loops because some time when short and sell signal or buy or cover appear on the same candle it makes wrong calculation. i tried to calculate the number of scale ins through count variable and use exit variable to sell the whole quantity. But it worked only few times and skipped a lot with message in bridge that quantity is not valid. Kindly help with your kind and expert vision.
Regards
instr= ParamList("Instrument Name","|EQ|FUTIDX|FUTSTK|OPTIDX|OPTSTK|FUTCOM");
delay= ParamToggle("Trade Intrabar?", "YES|Candle Completion", 1);
stag= ParamStr("Strategy Tag", "STG1");
dlong= ParamToggle("Disable Long?", "NO|YES");
dshort= ParamToggle("Disable Short?", "NO|YES");
bp= sp= NumToStr(Close[BarCount-1],1.2, False);
if(dlong){Buy=Sell=0;}
if(dshort){Short=Cover=0;}
RequestTimedRefresh( 1, onlyvisible = False ) ;
qty= "75";
cl =0;
i=0;
count=0;
onbuy =False;
for(i=0; i < BarCount ; i++)

{
if(Buy[i] ) 
{
onbuy=1;
 
 	count++;

}
			exitqty[i] = (count[i])*75;
							 
if(Sell[i] )
{
Buy[i] =0;

onbuy =0;

Avg =0;
count =0;
cl =0;

}

}
exitqtyno = int(exitqty);
exit = NumToStr( exitqtyno, format = 1.0, separator=True, roundAndPad = False );

cls =0;
j=0;
counts=0;
onshort =False;
for(j=0; j < BarCount ; j++)

{
if(Short[j]) 
{
onshort=1;
 


counts++;

}
			exitqtysh[j] = (counts[j])*75;
					 
if(Cover[j])
{

Short[j]=0;
onshort =0;

counts =0;
cls=0;
}

}
exitqtynosh = int(exitqtysh);
exitshrt = NumToStr( exitqtynosh, format = 1.0, separator=True, roundAndPad = False );

if(delay)
{Buy=Ref(Buy,-1); Sell=Ref(Sell,-1); Short= Ref(Short,-1); Cover= Ref(Cover,-1);}

global algoji;
algoji = Name() + NumToStr( Interval() / 60, 1.0, False ) ;

procedure aStaticVarSet( SName, Svalue )
{
global algoji;

    StaticVarSet( Sname + algoji, Svalue );

}

function aStaticVarGet( SName )
{
global algoji;
Var = StaticVarGet( Sname + algoji );

if ( IsNull( Var = StaticVarGet( Sname + algoji ) ) )
    Var = 0;

return Var;

}

sym = Name();

//_TRACE("t"+t);

Checkdt=Nz(aStaticVarGet("lastdt"));
dt = LastValue( DateTime() );
Checkdtss=Nz(aStaticVarGet("lastdtss"));
dtss = LastValue( DateTime() );
Checkdtc=Nz(aStaticVarGet("lastdtc"));
dtc = LastValue( DateTime() );
Checkdts=Nz(aStaticVarGet("lastdts"));
dts = LastValue( DateTime() );
RTBuy = LastValue( Buy) AND Checkdt != dt;
RTShort = LastValue( Short) AND Checkdtss != dtss;
RTCover = LastValue( Cover) AND Checkdtc != dtc;
RTSell = LastValue( Sell) AND Checkdts != dts;

if ( RTCover )
{
aStaticVarSet("lastdtc",dtc );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
_TRACE( "#"+Nz(StaticVarGet("counter"))+",SX,"+sym+",,," +bp +","+exitshrt+","+instr+",,");
Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SX",sym,"M","",bp,exitshrt,instr,stag);
}

if ( RTSell )
{
aStaticVarSet("lastdts",dts );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
_TRACE( "#"+Nz(StaticVarGet("counter"))+",LX,"+sym+",,," +sp +","+exit+",,,");
Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",sym,"M","",sp,exit,instr,stag);
}

if ( RTBuy )
{
aStaticVarSet("lastdt",dt );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
_TRACE( "#"+Nz(StaticVarGet("counter"))+",LE,"+sym+",,," +bp +","+qty+","+instr+",,");
Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,"M","",bp,qty,instr,stag);
}

if ( RTShort )
{
aStaticVarSet("lastdtss",dtss );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
sp= NumToStr(Close[BarCount-1],1.2, False);
_TRACE( "#"+Nz(StaticVarGet("counter"))+",SE,"+sym+",,," +sp +","+qty+","+instr+",,");
Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,"M","",bp,qty,instr,stag);
}

Button = ParamToggle( "Enable Button Trading", "YES|NO" );
expiry= ParamStr("Expiry","");
strike= ParamStr("Strike","");
type= ParamStr("Option Type", "");
sym = Name()+ "|"+expiry+ "|" +strike+ "|" +type;

function GetSecondNum()
{
Time = Now( 4 );
Seconds = int( Time % 100 );
Minutes = int( Time / 100 % 100 );
Hours = int( Time / 10000 % 100 );
SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
return SecondNum;
}

function PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
{
displayText = bodytext + captiontext;
if ( ( StaticVarGetText( "prevPopup" + popupID ) != displayText) OR ( StaticVarGet( "prevPopupTime" + popupID ) < GetSecondNum() ) )
{
StaticVarSetText( "prevPopup" + popupID, displayText);
StaticVarSet( "prevPopupTime" + popupID, GetSecondNum() + timeout );
PopupWindow( bodytext, Captiontext + popupID, timeout, Left, top );
}
}

x1= Status( "pxchartleft" )+10;
y1= Status( "pxcharttop" )+20;

if ( Button == 0 )
{
click = GetCursorMouseButtons() == 9;
Px = GetCursorXPosition( 1 );
Py = GetCursorYPosition( 1 );

x2 = x1 + 60;
y2 = y1 + 60;
GfxSelectSolidBrush(  ColorRGB( 0, 102, 0 ) ); //buy
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
GfxRoundRect( x1, y1, x2, y2 , 7, 7 ) ;
GfxTextOut( "LE", x1 + 20, y1 + 20 );

sx1 = x2;
sy1 = y1;
sx2 = sx1 + 60;
sy2 = sy1 + 60;
GfxSelectSolidBrush(  ColorRGB( 255, 204, 204 ) );//sell
GfxRoundRect( sx1, sy1, sx2, sy2 , 7, 7 ) ;
GfxSetTextColor( ColorRGB( 153, 0, 0 ) );
GfxTextOut( "SE", sx1 + 20, sy1 + 20 );

ex1 = x1;
ey1 = y1+60;
ex2 = ex1 + 60;
ey2 = ey1 + 60;
GfxSelectSolidBrush(  ColorRGB( 255, 204, 204 ) );//sell
GfxRoundRect( ex1, ey1, ex2, ey2 , 7, 7 ) ;
GfxSetTextColor( ColorRGB( 153, 0, 0 ) );
GfxTextOut( "LX", ex1 + 20, ey1 + 20 );

GfxSelectSolidBrush(  ColorRGB( 0, 102, 0 ) );//sell
GfxRoundRect( ex2, ey1, ex2+60, ey2 , 7, 7 ) ;
GfxSetTextColor( colorWhite );
GfxTextOut( "SX", ex2 + 20, ey1 + 20 );

if ( px > x1 AND px<x2 AND py>y1 AND py < y2 AND Click )
{
    _TRACE( "# ," + NumToStr(Nz(StaticVarGet("counter")),0,False) + ", BUY triggered from button, "  );
	AlertIf( 1, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 1, 2, 1 );    
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
	PopupWindowEx( "ID:1", "BUY", "Buy Triggered from Button "+Name(), 1, -1, -1 );
AlgoJi_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,"M","",sp,qty,instr,stag);
}

//https://algoji.com/
if ( px > sx1 AND pxsy1 AND py < sy2 AND Click )
{
_TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", SHORT triggered from button, " );
AlertIf( 2, "SOUND C:\Windows\Media\tada.wav", "Audio alert", 2, 2, 1 );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
PopupWindowEx( "ID:3", "SHORT", "Short Triggered from Button "+Name(), 1, -1, -1 );
AlgoJi_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,"M","",sp,qty,instr,stag);
}
//https://algoji.com/
if ( px > ex1 AND pxey1 AND py<ey2 AND Click )
{
_TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", SELL triggered from button, " );
AlertIf( 3, "SOUND C:\Windows\Media\tada.wav", "Audio alert", 2, 2, 1 );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
PopupWindowEx( "ID:3", "SELL", "Sell Triggered sel "+Name()+ "sss" + exit, 1, -1, -1 );
AlgoJi_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",sym,"M","",sp,exit,instr,stag);
}
//https://algoji.com/
if ( px > ex2 AND px<(ex2+60) AND py>ey1 AND py < ey2 AND Click )
{
_TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", Cover triggered from button, " );
AlertIf( 4, "SOUND C:\Windows\Media\tada.wav", "Audio alert", 2, 2, 1 );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
PopupWindowEx( "ID:3", "Cover", "Cover Triggered from Button "+Name(), 1, -1, -1 );
AlgoJi_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SX",sym,"M","",sp,exitshrt,instr,stag);
}

}

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

Sorry, i am revising this code. kindly help to find solutions.
Regards,

 instr= ParamList("Instrument Name","|EQ|FUTIDX|FUTSTK|OPTIDX|OPTSTK|FUTCOM"); 
delay= ParamToggle("Trade Intrabar?", "YES|Candle Completion", 1);
stag= ParamStr("Strategy Tag", "STG1");
dlong= ParamToggle("Disable Long?", "NO|YES");
dshort= ParamToggle("Disable Short?", "NO|YES");
    bp= sp= NumToStr(Close[BarCount-1],1.2, False);
if(dlong){Buy=Sell=0;}
if(dshort){Short=Cover=0;}
RequestTimedRefresh( 1, onlyvisible = False ) ;
qty= "75";
cl =0;
i=0;
count=0;
onbuy =False;
for(i=0; i < BarCount ; i++)
	
	{
	if(Buy[i] ) 
	{
	onbuy=1;
	 
	 	count++;
	
	}
				exitqty[i] = (count[i])*75;
								 
	if(Sell[i] )
	{
	Buy[i] =0;
	
	onbuy =0;
   Avg =0;
   count =0;
   cl =0;
 
}

	}
	exitqtyno = int(exitqty);
	exit = NumToStr( exitqtyno, format = 1.0, separator=True, roundAndPad = False );

cls =0;
j=0;
counts=0;
onshort =False;
for(j=0; j < BarCount ; j++)
		
	{
	if(Short[j]) 
	{
	onshort=1;
	 
	
	
	counts++;
	
	}
				exitqtysh[j] = (counts[j])*75;
						 
	if(Cover[j])
	{

	Short[j]=0;
	onshort =0;
   
   counts =0;
   cls=0;
   }

	}
	exitqtynosh = int(exitqtysh);
	exitshrt = NumToStr( exitqtynosh, format = 1.0, separator=True, roundAndPad = False );

if(delay)
{Buy=Ref(Buy,-1); Sell=Ref(Sell,-1); Short= Ref(Short,-1); Cover= Ref(Cover,-1);}

global algoji;
algoji = Name() + NumToStr( Interval() / 60, 1.0, False ) ;

procedure aStaticVarSet( SName, Svalue )
{
    global algoji;

        StaticVarSet( Sname + algoji, Svalue );
}

function aStaticVarGet( SName )
{
    global algoji;
    Var = StaticVarGet( Sname + algoji );

    if ( IsNull( Var = StaticVarGet( Sname + algoji ) ) )
        Var = 0;

    return Var;
}

sym = Name();

//_TRACE("t"+t);

Checkdt=Nz(aStaticVarGet("lastdt"));
dt = LastValue( DateTime() );
Checkdtss=Nz(aStaticVarGet("lastdtss"));
dtss = LastValue( DateTime() );
Checkdtc=Nz(aStaticVarGet("lastdtc"));
dtc = LastValue( DateTime() );
Checkdts=Nz(aStaticVarGet("lastdts"));
dts = LastValue( DateTime() );
RTBuy = LastValue( Buy) AND Checkdt != dt;
RTShort = LastValue( Short) AND Checkdtss != dtss;
RTCover = LastValue( Cover) AND Checkdtc != dtc;
RTSell = LastValue( Sell) AND Checkdts != dts;

if ( RTCover )
{
	 aStaticVarSet("lastdtc",dtc );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
            _TRACE( "#"+Nz(StaticVarGet("counter"))+",SX,"+sym+",,," +bp +","+exitshrt+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SX",sym,"M","",bp,exitshrt,instr,stag);
}

if ( RTSell )
{
	 aStaticVarSet("lastdts",dts );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
            _TRACE( "#"+Nz(StaticVarGet("counter"))+",LX,"+sym+",,," +sp +","+exit+",,,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",sym,"M","",sp,exit,instr,stag);
}

if ( RTBuy )
{
	 aStaticVarSet("lastdt",dt );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
            _TRACE( "#"+Nz(StaticVarGet("counter"))+",LE,"+sym+",,," +bp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,"M","",bp,qty,instr,stag);
}

if ( RTShort )
{
	 aStaticVarSet("lastdtss",dtss );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    sp= NumToStr(Close[BarCount-1],1.2, False);
            _TRACE( "#"+Nz(StaticVarGet("counter"))+",SE,"+sym+",,," +sp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,"M","",bp,qty,instr,stag);
}

Button = ParamToggle( "Enable Button Trading", "YES|NO" );
expiry= ParamStr("Expiry","");
strike= ParamStr("Strike","");
type= ParamStr("Option Type", "");
sym = Name()+ "|"+expiry+ "|" +strike+ "|" +type;


function GetSecondNum()
{
    Time = Now( 4 );
    Seconds = int( Time % 100 );
    Minutes = int( Time / 100 % 100 );
    Hours = int( Time / 10000 % 100 );
    SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
    return SecondNum;
}
 
function PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
{
    displayText = bodytext + captiontext;
    if ( ( StaticVarGetText( "prevPopup" + popupID ) != displayText) OR ( StaticVarGet( "prevPopupTime" + popupID ) < GetSecondNum() ) )
    {
        StaticVarSetText( "prevPopup" + popupID, displayText);
        StaticVarSet( "prevPopupTime" + popupID, GetSecondNum() + timeout );
        PopupWindow( bodytext, Captiontext + popupID, timeout, Left, top );
    }
}



x1= Status( "pxchartleft" )+10;
y1= Status( "pxcharttop" )+20;

if ( Button == 0 )
{
    click = GetCursorMouseButtons() == 9;
    Px = GetCursorXPosition( 1 );
    Py = GetCursorYPosition( 1 );

    x2 = x1 + 60;
    y2 = y1 + 60;
    GfxSelectSolidBrush(  ColorRGB( 0, 102, 0 ) ); //buy
    GfxSelectFont( "Tahoma", 13, 100 );
    GfxSetBkMode( 1 );
    GfxSetTextColor( colorWhite );
    GfxRoundRect( x1, y1, x2, y2 , 7, 7 ) ;
    GfxTextOut( "LE", x1 + 20, y1 + 20 );

    sx1 = x2;
    sy1 = y1;
    sx2 = sx1 + 60;
    sy2 = sy1 + 60;
    GfxSelectSolidBrush(  ColorRGB( 255, 204, 204 ) );//sell
    GfxRoundRect( sx1, sy1, sx2, sy2 , 7, 7 ) ;
    GfxSetTextColor( ColorRGB( 153, 0, 0 ) );
    GfxTextOut( "SE", sx1 + 20, sy1 + 20 );

    ex1 = x1;
    ey1 = y1+60;
    ex2 = ex1 + 60;
    ey2 = ey1 + 60;
    GfxSelectSolidBrush(  ColorRGB( 255, 204, 204 ) );//sell
    GfxRoundRect( ex1, ey1, ex2, ey2 , 7, 7 ) ;
    GfxSetTextColor( ColorRGB( 153, 0, 0 ) );
    GfxTextOut( "LX", ex1 + 20, ey1 + 20 );

    GfxSelectSolidBrush(  ColorRGB( 0, 102, 0 ) );//sell
    GfxRoundRect( ex2, ey1, ex2+60, ey2 , 7, 7 ) ;
    GfxSetTextColor( colorWhite );
    GfxTextOut( "SX", ex2 + 20, ey1 + 20 );

    if ( px > x1 AND px<x2 AND py>y1 AND py < y2 AND Click )
    {
        _TRACE( "# ," + NumToStr(Nz(StaticVarGet("counter")),0,False) + ", BUY triggered from button, "  );
		AlertIf( 1, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 1, 2, 1 );    
    StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    	PopupWindowEx( "ID:1", "BUY", "Buy Triggered from Button "+Name(), 1, -1, -1 );
	AlgoJi_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,"M","",sp,qty,instr,stag);
	}
//https://algoji.com/
    if ( px > sx1 AND px<sx2 AND py>sy1 AND py < sy2 AND Click )
    {
        _TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", SHORT triggered from button, " );
		AlertIf( 2, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 2, 2, 1 );
    StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
	PopupWindowEx( "ID:3", "SHORT", "Short Triggered from Button "+Name(), 1, -1, -1 );
	AlgoJi_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,"M","",sp,qty,instr,stag);
    }
//https://algoji.com/
    if ( px > ex1 AND px<ex2 AND py>ey1 AND py<ey2 AND Click )
    {
        _TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", SELL triggered from button, " );
		AlertIf( 3, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 2, 2, 1 );
    StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );    
	PopupWindowEx( "ID:3", "SELL", "Sell Triggered  sel "+Name()+ "sss" + exit, 1, -1, -1 );
	AlgoJi_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",sym,"M","",sp,exit,instr,stag);
    }
//https://algoji.com/
    if ( px > ex2 AND px<(ex2+60) AND py>ey1 AND py < ey2 AND Click )
    {
        _TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", Cover triggered from button, " );
		AlertIf( 4, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 2, 2, 1 );
    StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
	PopupWindowEx( "ID:3", "Cover", "Cover  Triggered from Button "+Name(), 1, -1, -1 );
	AlgoJi_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SX",sym,"M","",sp,exitshrt,instr,stag);
    }

}