How to ensure that the EA only works on the chart where it was first dropped - page 10

 
gip:


Well, you could have read all three options and understood them, couldn't you? But you didn't. The second option solves your problem.

Also, code should not base its work on the value of local variables.

We're going around in circles, getting lost in the middle of nowhere.

My option seems to me more reliable, somehow strategically. Although yours is noticeably more succinct.
 
To ensure that an EA (Expert Advisor) only works on the chart where it was first dropped, you can use the ChartID property in MQL4 or MQL5.

When the EA is first dropped on the chart, you can store the chart ID in a variable. Then, in the OnTick() function or any other function that you want to restrict to the original chart, you can check if the current chart ID matches the stored chart ID. If they match, the function can proceed as normal. If they don't match, you can simply return without taking any action.

Here's an example of how to store the chart ID in MQL4:

csharp
Copy code
int originalChartID;

int OnInit()
{
   // Store the chart ID
   originalChartID = ChartID();

   return(INIT_SUCCEEDED);
}

void OnTick()
{
   // Check if the current chart ID matches the stored chart ID
   if (ChartID() == originalChartID)
   {
      // Proceed with the EA's normal operation
      // ...
   }
   else
   {
      // Do nothing if the current chart is not the original chart
      return;
   }
}
You can modify this code as necessary to suit your specific EA's requirements.
 
jongore #:
To ensure that an EA (Expert Advisor) only works on the chart where it was first dropped, you can use the ChartID property in MQL4 or MQL5.

When the EA is first dropped on the chart, you can store the chart ID in a variable. Then, in the OnTick() function or any other function that you want to restrict to the original chart, you can check if the current chart ID matches the stored chart ID. If they match, the function can proceed as normal. If they don't match, you can simply return without taking any action.

Here's an example of how to store the chart ID in MQL4:

csharp
Copy code
int originalChartID;

int OnInit()
{
   // Store the chart ID
   originalChartID = ChartID();

   return(INIT_SUCCEEDED);
}

void OnTick()
{
   // Check if the current chart ID matches the stored chart ID
   if (ChartID() == originalChartID)
   {
      // Proceed with the EA's normal operation
      // ...
   }
   else
   {
      // Do nothing if the current chart is not the original chart
      return;
   }
}
You can modify this code as necessary to suit your specific EA's requirements.

What's the point to post in a 13 years old topic with a wrong solution ?

 
  1. jongore #: Here's an example of how to store the chart ID in MQL4:

    Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. The title says: How to ensure that the EA only works on the chart where it was first dropped
    Which your attempt solves. But the OPs question was:
    Sergey Kravchuk:

    I need to make the EA work with only "one instance". I.e. I attach it to any chart for the first time and it works. If I then accidentally or intentionally attach it again to another chart, the second attachment should recognize that it's already running and do nothing. 

    Which is not what your code fixes.

  3. Do you really expect that people are still watching this thread after thirteen (13) years?
    Don't resurrect old threads without a very good reason. A lot has changed since Build 600 and Higher. (2014)