Do not want to run script on more than one chart

 

I am fed up of searching on Google and various forums. Any help w'd be greatly appreciated.

I have a script which I am attaching to any one of the open charts. I want to make sure the script does not run on two open charts. so, if someone tries to attach to another chart, it should not attach. However, if he tries to attach it to the same chart on which it was running, he can..

I tried to use global variables. here is my code in OnStart().

<SRC by Moderator: Ubzen>

int i = GlobalVariableGet("MT5ETRunning");
if (i == 1)GlobalVariableSet("SecondInst", 1);
 int j = GlobalVariableGet("SecondInst");
if (i == 1 && j == 1) {
    GlobalVariableSet("SecondInst", 0);
    return (0);
}
else {
    GlobalVariableSet("MT5ETRunning", 1);
    GlobalVariableSet("SecondInst", 0);
}

This runs OK, but I get two issues.

1. When I try to attach it to the chart on which it was running, it removes the running script and after that if I try to attach it to same chart or any other chart, it won't atttach.

2. Even if I close and start client terminal, I am not able to attach the script to any chart.

Any pointers will be life-saver..

Thanks,

Janak

 

try using using ChartID()

when you attach script do GlobalVariableCheck() to see if GV exists already

if it doesnt exist ceate it and set it to hold chart id

if it does exist check to see if it matches chart id

if it doesnt match return(0); //exit script

if it does match allow script to run

dont forget to detete GV before script unloads

you will probably need to make each script create its own version of the GV so when you unload one it deletes its own GV but the script must test for both GV's. Something like that to make sure if two have been attached and one unloaded the other one still has a GV. You can figure that part out.

 

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Once you create your global variable, it's created so of course you can't attach it again.
    Not compiled, not tested.
    void OnStart(){
       #define GV_ONE "MT5ETRunning"
       if( !GlobalVariableCheck(GV_ONE){       // Does the variable exist?
          Alert(GV_ONE + " one only"); return; // Yes
       }
       GlobalVariableSet(GV_ONE, 1.);          // Create a lock.
    
       // function
    
       GlobalVariableDel(GV_ONE);              // Allow others.
    }
    Not compiled, not tested.
    Note: This works for scripts started by humans. It maybe unsafe in EAs due to race conditions.
 
janakpatel3210:

I have a script which I am attaching to any one of the open charts. I want to make sure the script does not run on two open charts. so, if someone tries to attach to another chart, it should not attach. However, if he tries to attach it to the same chart on which it was running, he can..

You say "someone", implying that you are dealing with other users, not just yourself. Anything involving global variables is fine provided that it's okay if the user can disable the lock by editing the global variables. If you need something more secure (or something which wouldn't then cause problems if MT4 shut down abnormlly), then you need to consider things like https://www.mql5.com/en/forum/130970

 

... simple single-instance lock which works by having non-shared write access to a file:

   string strLockFileName = "mylock.lock";
   int lock = FileOpen(strLockFileName, FILE_BIN | FILE_WRITE);
   if (lock == INVALID_HANDLE) {
      // File cannot be opened. Almost certainly means that it is 
      // already in use, though could conceivably be triggered by
      // other causes such as low disk space
      MessageBox("Already in use (probably)");
      
   } else {
      // Okay, got lock
      MessageBox("Locked");

      // Close file (and delete it, which isn't actually necessary, just tidy)
      FileClose(lock);
      FileDelete(strLockFileName);
   }   

The new ability in MT4 to use FILE_COMMON means that this can also easily be extended to cover multiple copies of MT4 on the same computer.

 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Once you create your global variable, it's created so of course you can't attach it again.
    Not compiled, not tested. Not compiled, not tested.
    Note: This works for scripts started by humans. It maybe unsafe in EAs due to race conditions.


Thanks for the code. It works fine if I attempt to attach script on other charts..it does not attach to them. But the problem still exist when I try to attach script to same chart (by mistake), It removes existing script and then will not attach to it if I try again. Moreover if I close metatrader while script was running..on starting again, it will not get attached..

Note: I have a never ending while loop in my script in OnStart() so GlobalVariableDel(GV_ONE); will never be executed..

Any suggetions to crack this?

 
SDC:

try using using ChartID()

when you attach script do GlobalVariableCheck() to see if GV exists already

if it doesnt exist ceate it and set it to hold chart id

if it does exist check to see if it matches chart id

if it doesnt match return(0); //exit script

if it does match allow script to run

dont forget to detete GV before script unloads

you will probably need to make each script create its own version of the GV so when you unload one it deletes its own GV but the script must test for both GV's. Something like that to make sure if two have been attached and one unloaded the other one still has a GV. You can figure that part out.


Thanks SDC. your approach works fine for my requirement. Only one issue left now..

My script is never ending script. there is a while loop there..so I will not be able to delete GV before script unloads..

In this case, I am unable to attach script after restarting MetaTrader. Please help.

 
Not ideal, but you could always press F3 and delete it manually.
 
janakpatel3210:


Thanks for the code. It works fine if I attempt to attach script on other charts..it does not attach to them. But the problem still exist when I try to attach script to same chart (by mistake), It removes existing script and then will not attach to it if I try again. Moreover if I close metatrader while script was running..on starting again, it will not get attached..

Note: I have a never ending while loop in my script in OnStart() so GlobalVariableDel(GV_ONE); will never be executed..

Any suggetions to crack this?

You can't run more than one script on a chart at once, if you have a script running, any script, and you try to run another on the same chart you have two options, stop the first to run the second, or change your mind and don't run the second. The second doesn't run until you decide what to do . . . . so it cannot tell if there is already a script running because it hasn't started . . . .
 
janakpatel3210:


Thanks for the code. It works fine if I attempt to attach script on other charts..it does not attach to them. But the problem still exist when I try to attach script to same chart (by mistake), It removes existing script and then will not attach to it if I try again. Moreover if I close metatrader while script was running..on starting again, it will not get attached..

Note: I have a never ending while loop in my script in OnStart() so GlobalVariableDel(GV_ONE); will never be executed..

Any suggetions to crack this?


It looks like you let your script time out. Your endless loop should contain condition like below. Does it?

for(;!IsStopped();) {
   // code here
}
 
RaptorUK:
You can't run more than one script on a chart at once, if you have a script running, any script, and you try to run another on the same chart you have two options, stop the first to run the second, or change your mind and don't run the second. The second doesn't run until you decide what to do . . . . so it cannot tell if there is already a script running because it hasn't started . . . .


As I said, my script is never ending script. It has never ending while loop in it. It attaches to a chart like an EA or Indicator.

So with your solution I am unable to run it on a chart on which it is already running. (Though this will happen rarely...by mistake).

Following code works fine to solve that issue.

     #define GV_ONE "ChartID"
          if(GlobalVariableCheck(GV_ONE))    // Does the variable exist?
          {
            if (GlobalVariableGet(GV_ONE) != ChartID())
            {     
               Alert(GV_ONE + " one only"); 
               return(0); // Yes
            }
          }
          GlobalVariableSet(GV_ONE, ChartID());          // Create a lock.
   
   
   

My only issue left now is, I want to reset or delete my Global Variables when I close the MT4 / MT5 or when it crashes.

Any help w'd be great.

Thanks,

Janak

Reason: