
I was working on some buttons (not like the above) acting as indicators when the idea came up to my mind. What if the button was blinking instead of just changing color? After some late night thinking I found a pretty elegant way to do so.
If you have been reading my posts for some time already, you know we can change the button color then wait some time then change it back.
The challenge is that during any use of the wait() function, the whole DAS is basically stuck, and you can not do much during that time which would cause some laggy behavior.
My workaround
As we know, the timer event script is running every second. Which is conveniently the cycle I am about to use for the blinking button.
First let’s create the button called b_blinker
and put a simple code in it that will enable or disable the blinking by storing 1 or 0 into the $enableblinker variable.
if($enableblinker==1)
{
$enableblinker=0;
}
else
{
$enableblinker=1;
}
It acts as a trigger only.
My 1-min chart window is named MY1MIN and the code for the blinker i save as a hotkey called blinker.
$MY1MIWINDOW=GetWindowObj("MY1MIN");
if($enableblinker==1)
{
if($blicked==1)
{
$MY1MINWINDOW.GetCustButObj("B_BLINKER").bkcolor=white;
$blicked=0;
}
else
{
$MY1MINWINDOW.GetCustButObj("B_BLINKER").bkcolor=orange;
$blicked=1;
}
}
The first line actually belongs to the chart update script but for this purpose it is ok to have it here so that you can copy and paste it for your testing.
How does the above code blink?
The logic is simple. First, I check if the blinking is enabled by reading the $enableblinker variable.
Then I check for the value of a new variable which stores the state of the button. If $blicked equals 1 then change color to white and set $blicked to 0.
In case $blicked is not 1 then change the button color to orange and set the $blicked to 1.
The magic comes from the fact that the Timer Event script will run this for me exactly every second, effectively changing the button color as on the video above.
I named my montage as MONTAGE1 and all that is left is to put the call for my blinker hotkey into the event timer script.
$MONTAGE=GetWindowObj("MONTAGE1");
$MY1MINWINDOW=getwindowobj("MY1MIN");
$montage.exechotkey("blinker");
Preventing errors
As the Timer Event script area is sensitive for errors, we can improve the code to run only if all the needed objects for the code are existing. We can put the check into the hotkey or to the Timer Event script.
For this functionality we are using
the montage object $MONTAGE
the chart window object $MY1MINWINDOW
the button object which we do not store in a variable
To do the checks we need to use the isobj() function and test if the function call returns 1 or 0. If it returns 1, it means the object exists. Here is the code to be put into the Timer Event script
if (isobj($MONTAGE)==1)
{
if (isobj($MY1MINWINDOW)==1)
{
if (isobj($MY1MINWINDOW.GetCustButObj("B_BLINKER"))==1)
{
$montage.exechotkey("BLINKER");
}
}
}
You can use the same technique for other object missing related errors you might have encountered.
More colors, more advanced stuff
We can retrieve the current color of the button like this
$COLOR=$my1minchart.GetCustButObj("B_BLINKER2").bkcolor;
DAS Trader Pro stores the color in 24-bit RGB Integer format so here are some colors to choose from
RED 255
GREEN 65280
BLUE 16711680
CYAN 16776960
PURPLE 16711935
WHITE 16777215
Now, if we want to change the color of the button to more colors in a sequence
BLUE→CYAN→PURPLE we can use following code for the blinker hotkey.
$my1minchart=GetWindowObj("MY1MIN");
if($enableblinker==1)
{
if($my1minchart.GetCustButObj("B_BLINKER").bkcolor==16711680)
{
$my1minchart.GetCustButObj("B_BLINKER").bkcolor=16776960;
return;
}
if($my1minchart.GetCustButObj("B_BLINKER").bkcolor==16776960)
{
$my1minchart.GetCustButObj("B_BLINKER").bkcolor=16711935;
return;
}
if($my1minchart.GetCustButObj("B_BLINKER").bkcolor==16711935)
{
$my1minchart.GetCustButObj("B_BLINKER").bkcolor=16711680;
return;
}
$my1minchart.GetCustButObj("B_BLINKER").bkcolor=16776960;
return;
}
The code is a bit different now as we call a color change then end of the current code with the return function. We can use that anytime we need to end the current code execution.
With the same technique we can blink named lines or any other objects having a color property.
Happy blinking everyone!