Trigger an Alert/Action when the candle closes - page 3

 
Ahmad Zuhairdi Noh:

So? did you try it? does it work if you put it above OnInit?

You should try it yourself. Try and error.

You'll learn faster that way.

Ok, I tested this, it doesn't help. I was curious about what exactly global scope means. It shouldn't be this difficult to add a simple logical OR statement.

I suppose there is always the option of running the working code (the one without the logical OR statement) on 2 seperate EAs on 2 seperate open charts. One for the Low alert the other for the High alert.

 
datetime barMark = TimeCurrent(); //put this line above OnInit to initialize the mark in a global scope
Global and static variables work exactly the same way in MT4/MT5/C/C++.
  1. They are initialized once on program load.
  2. They don't update unless you assign to them.
  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary. Think symbol/TF change.
              external static variable - Inflation - MQL4 programming forum
 
Elixium:

Ok, I tested this, it doesn't help. I was curious about what exactly global scope means. It shouldn't be this difficult to add a simple logical OR statement.

I suppose there is always the option of running the working code (the one without the logical OR statement) on 2 seperate EAs on 2 seperate open charts. One for the Low alert the other for the High alert.

Not necessary. Just use more static/global variables.

extern double TargetHigh=52.75;
extern double TargetLow=42.75;
input ENUM_TIMEFRAMES Timeframe=PERIOD_M5;
//---
void OnTick()
  {
   static datetime time0=0;
   static bool newBarHigh=false,newBarLow=false;
//---
   if(time0==iTime(_Symbol,Timeframe,1))
      newBarHigh=newBarLow=true;
   if(newBarHigh && Close[0]>TargetHigh)
     {
      Alert("High exceeded");
      newBarHigh=false;
     }
//---

//---
   time0=iTime(_Symbol,Timeframe,0);   
  }
 
Ernst Van Der Merwe:

Not necessary. Just use more static/global variables.


Hi. Thanks for the code example. Your code has the exact same problem as Ahmad Zuhairdi Noh's code when he added a second price target, it executes an alert immediately instead of waiting for the candle to close.

 
Elixium:


Hi. Thanks for the code example. Your code has the exact same problem as Ahmad Zuhairdi Noh's code when he added a second price target, it executes an alert immediately instead of waiting for the candle to close.

That's because it's looking at the current price.

extern double TargetHigh=52.75;
extern double TargetLow=42.75;
input ENUM_TIMEFRAMES Timeframe=PERIOD_CURRENT;
//---
void OnTick()
  {
   static datetime time0=0;
//---
   if(time0==iTime(_Symbol,Timeframe,1))
     { 
      if(Close[1]>TargetHigh)
         Alert("High target breached");
      if(Close[1]<TargetLow)
         Alert("Low target breached");
     }
//---
   time0=iTime(_Symbol,Timeframe,0);   
  }
 
Elixium:


Hi. Thanks for the code example. Your code has the exact same problem as Ahmad Zuhairdi Noh's code when he added a second price target, it executes an alert immediately instead of waiting for the candle to close.

Oh, so you want to delay the alert until the candle close even though the price has breach the target?

then you should change Close[0] to Close[1] as suggested by Ernst.

Close[0] means current price while Close[1] is a close price of previous candle.

 
Ahmad Zuhairdi Noh:

Oh, so you want to delay the alert until the candle close even though the price has breach the target?

then you should change Close[0] to Close[1] as suggested by Ernst.

Close[0] means current price while Close[1] is a close price of previous candle.


I see.
It's working well now after testing it.
What would be the code for automatically halting the EA once the first alert has been triggered?