Hi guys, I'm learning how to handle with GUI objects, in particular the toggle buttons, but there's something I'm missing.
I've studied the manual, the tutorial and some forum posts without finding my answers.
The idea I'm working on is really simple: I would like to have 2 toggles, one to plot the MA20 and one to plot the MA100 of the close prices. My questions are:
-
If I open 2 or more scripts at the same time, then they became linked due the fact their function point to the same static vars. What is in this case the best way to handle multiple contemporary instances of the same interface?
-
To learn how to handle queued events I've added a fake procedure that's time demanding. I'm expecting that it slowes down the script only when an event occurs, but I'm experiencing that it alwasy slow down my code: for example when I move the bar line indicator without changing the price window. Why this beahavior?
This is the test code:
// GUI_test_2.afl
delay = Param("Time intensive function's iterations", 0, 0, 20, 1);
PlotOHLC(O,H,L,C, "", colorBlack, styleBar);
SetChartOptions(0, chartShowDates);
idToggle1 = 1;
idToggle2 = 2;
procedure createGUI()
{
GuiToggle("TGGL 1", idToggle1, 20, 35, 100, 50, notifyClicked);
GuiToggle("TGGL 2", idToggle2, 120, 35, 100, 50, notifyClicked);
}
procedure initGUI()
{
if (StaticVarGet("toggle1Status") == 1)
GuiSetCheck(idToggle1, 1);
if (StaticVarGet("toggle1Status") == 0)
GuiSetCheck(idToggle1, 0);
if (StaticVarGet("toggle2Status") == 1)
GuiSetCheck(idToggle2, 1);
if (StaticVarGet("toggle2Status") == 0)
GuiSetCheck(idToggle2, 0);
}
procedure handleEvents()
{
newvalue = 0;
for ( n = 0; id = GuiGetEvent(n, 0); n++) // il 2° parametro = 0 => restituisce l'ID del controllo.
{
code = GuiGetEvent(n, 1); // il 2° parametro = 1 => restituisce il notification code.
switch (id)
{
case idToggle1:
{
value = StaticVarGet("toggle1Status", True);
if (value == 0)
newvalue = 1;
if (value == 1)
newvalue = 0;
StaticVarSet("toggle1Status", newvalue, False, False);
text = "Toggle 1 value = " + NumToStr(newvalue) + " Delay = " + NumToStr(delay, 1.0);
PopupWindow(text, "TEST TGGL1", 1,-1, -1, -1, -1, True);
break;
}
case idToggle2:
{
value = StaticVarGet("toggle2Status", True);
if (value == 0)
newvalue = 1;
if (value == 1)
newvalue = 0;
StaticVarSet("toggle2Status", newvalue, False, False);
text = "Toggle 2 value = " + NumToStr(newvalue) + " Delay = " + NumToStr(delay, 1.0);
PopupWindow(text, "TEST TGGL2", 1,-1, -1, -1, -1, True);
break;
}
default:
break;
}
}
}
procedure timeIntensiveFakeProcedure(iterations)
{
for (i = 0; i <= iterations; i++)
{
for (bi = 0; bi <= BarCount-1; bi++)
{
handle = fopen("test.csv", "w", False);
fputs(NumToStr(C[bi], 1.3), handle);
fclose(handle);
}
}
}
createGUI();
initGUI();
handleEvents();
// Toggle 1
tggl1Val = StaticVarGet("toggle1Status");
if (tggl1Val)
{
Plot(MA(C,20), "MA20", colorRed, styleLine|styleThick);
timeIntensiveFakeProcedure(delay);
}
// Toggle 2
tggl2Val = StaticVarGet("toggle2Status");
if (tggl2Val)
{
Plot(MA(C,100), "MA100", colorBlue, styleLine|styleThick);
timeIntensiveFakeProcedure(delay);
}
Thank you in advance for any insight !