Bar Replay automatically 'click' my GuiButton

I have the following code, basically a continuation from my previous question.

I'm trying to use this on Bar Replay.
The button works as expected, except when the bar selection is at the last value (hide it by clicking the Date Axis). Once I click Count+ button at this position, the button seems to be 'auto-clicked' for every future bars.
Please see the screen grab below. The first 3 clicks is on the middle of the chart, button working fine. After that I click the Date Axis to put selection to the last value (vertical line hidden). Then, the button seems to be 'auto-clicked'.

Appreciate any advice on this.
Thanks.

//https://forum.amibroker.com/t/transferring-staticvar-data-to-array-elements/15489/3

idCount = 1;
idReset = 2;
idchangeText = 3;

bi = BarIndex();
sbi = SelectedValue(bi);

count = Nz(StaticVarGet("countStaticVar"));
data = Nz(StaticVarGet("dataStaticVar"));
myArray = 0;
printf("\ndata = " + data);

function CreateButtonGUI()
{
	GuiButton( "Count+", idCount, 100, 10, 70, 30, notifyClicked );
	GuiButton( "Reset", idReset, 160, 10, 70, 30, notifyClicked );
	GuiButton( "Change Count Button", idChangeText, 100, 40, 130, 30, notifyClicked );
}

function Counter()
{
	count++;
	StaticVarSet("countStaticVar", count);
	data = 10;
	printf("\ndata = :" + data);

	StaticVarSet("dataStaticVar", data);
	
	myArray = Nz(StaticVarGet("myArray"));
	myArray[sbi] = data;
	StaticVarSet("myArray", myArray);	
}

function Reset()
{
	StaticVarRemove("countStaticVar");
	StaticVarRemove("dataStaticVar");
	StaticVarRemove("myArray");
}

function ChangeButtonText()
{
	GuiSetText("Counter", idCount ); 
}

function HandleEvents()
{
    for( n = 0; id = GuiGetEvent( n, 0 ); n++ )
    {
		switch ( id )
		{
			case idCount:
				Counter();
			break;
			
			case idReset:
				Reset();
			break;
			
			case idChangeText:
				ChangeButtonText();
			break;
		}
	}
}

CreateButtonGUI();
HandleEvents();

printf("\ncount: " + count);

myArray = StaticVarGet("myArray");
cumMyArray = Cum(myArray);

Plot( myArray, "myArray", colorBrightGreen, styleNoLabel );
Plot( cumMyArray, "cumMyArray", colorWhite, styleNoLabel );

fvbi = Status( "FirstVisibleBar" );
lvb = Min(Barcount-1, Status("lastvisiblebar"));

printf("\ncumMyArray: " + cumMyArray);

Title = "";

bar%20replay%20-%20auto%20click%20button

It does not auto click. SelectedValue becomes LastValue at last bar. It is just kept previous value.

Here is one way to prevent your results at last bar:

//https://forum.amibroker.com/t/transferring-staticvar-data-to-array-elements/15489/3
//https://forum.amibroker.com/t/bar-replay-automatically-click-my-guibutton/15630
idCount = 1;
idReset = 2;
idchangeText = 3;

bi = BarIndex();
sbi = SelectedValue(bi);
dt = DateTime();

static_dt = StaticVarGet("static_dt");
count = Nz(StaticVarGet("countStaticVar"));
data = Nz(StaticVarGet("dataStaticVar"));
myArray = 0;

printf("\ndata = " + data);

function CreateButtonGUI()
{
	GuiButton( "Count+", idCount, 100, 10, 70, 30, notifyClicked );
	GuiButton( "Reset", idReset, 160, 10, 70, 30, notifyClicked );
	GuiButton( "Change Count Button", idChangeText, 100, 40, 130, 30, notifyClicked );
}

function Counter()
{
	count++;
	StaticVarSet("countStaticVar", count);

	data = 10;
	printf("\ndata = :" + data);

	StaticVarSet("dataStaticVar", data);
	
	mydt = StaticVarGet("static_dt");
	mydt[sbi] = SelectedValue(dt); 
	StaticVarSet("static_dt", mydt);
	
	myArray = Nz(StaticVarGet("myArray"));
	myArray[sbi] = data; 
	StaticVarSet("myArray", myArray);	
}

function Reset()
{
	StaticVarRemove("countStaticVar");
	StaticVarRemove("dataStaticVar");
	StaticVarRemove("myArray");
	StaticVarRemove("static_dt");
}

function ChangeButtonText()
{
	GuiSetText("Counter", idCount ); 
}

function HandleEvents()
{
    for( n = 0; id = GuiGetEvent( n, 0 ); n++ )
    {
		switch ( id )
		{
			case idCount:
				Counter();
			break;		

			case idReset:
				Reset();
			break;		

			case idChangeText:
				ChangeButtonText();
			break;
		}
	}
}

CreateButtonGUI();
HandleEvents();

printf("\ncount: " + count);

myArray = IIf(dt == static_dt, StaticVarGet("myArray"), 0);
cumMyArray = Cum(myArray);

Plot( myArray, "myArray", colorBrightGreen, styleNoLabel );
Plot( cumMyArray, "cumMyArray", colorWhite, styleNoLabel );

fvbi = Status( "FirstVisibleBar" );
lvb = Min(Barcount-1, Status("lastvisiblebar"));

printf("\ncumMyArray: " + cumMyArray);

Title = "";

3

3 Likes

Thanks heaps @fxshrat. First time heard about this:

Great discovery.