Download MetaTrader 5
To add comments, please log in or register
Find out how to install MetaTrader on Mac OS
fxalert
19
fxalert 2015.04.24 13:06 

Hi - I'm very new to coding and have drowned in google searches and reading instruction manuals trying to find the correct way to code for high/low alerting.

---

For the new high criteria, I want to generate an alert when the past 5 bars have the high being  from bar 3. e.g.:

double HighBar1 = High[1], repeat variable up to  double HighBar5 = High[5]

Alert when HighBar3 is > HighBar1 && HighBar2 && HighBar4 && HighBar5

---

For the new low criteria, I want to generate an alert when the past 5 bars have the low being  from bar 3. e.g.:

double LowBar1 = Low[1], repeat variable up to  double LowBar5 = Low[5]

Alert when LowBar3 is > LowBar1 && LowBar2 && LowBar4 && LowBar5 

---

The best help I've found so far was in this forum under various posts including: https://forum.mql4.com/38196, but I'm still unsuccessful in creating a good working piece of code.

I've kinda cracked the alerting code and just need the correct way to state my expression and action the result . Any help suggestions would be greatly appreciated, as I'm going round in circles and confusing myself more and more....

-- Specifically:

in the int Start() function do I need to use an IF expression or a For Expression (apologies if this shows my lowly coding level)

eg

IF ( HighBar3 > HighBar1 && HighBar3 > HighBar2 && HighBar3 > double HighBar4 && HighBar3 > HighBar5)

SendAlert = Alert("text and price detail) ;

Or

For  (HighBar3 > HighBar2 && HighBar3 > HighBar1; HighBar3 > HighBar4 && HighBar3 > HighBar5; SendAlert etc);

--

As I type this I realise I'm not making much sense so apologies and just hope there is a charitable champion out there that can help

FYI - I;ve struggled with this code using the iHigh & iHighest functions but again just ended up with lots of alerts but not when wanted/expected..!

Yours desperately - MR Greenhorn 

whroeder1
13593
whroeder1 2015.04.24 14:23  
MQLGreenHorn:

Hi - I'm very new to coding and have drowned in google searches and reading instruction manuals trying to find the correct way to code for high/low alerting.

double HighBar1 = High[1], repeat variable up to  double HighBar5 = High[5]

Alert when HighBar3 is > HighBar1 && HighBar2 && HighBar4 && HighBar5

  1. learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem. Was it so hard you couldn't even attempt it?
  2. if(iHighest(NULL,0, MODE_HIGH, 5, 1) == 3) Alert("New high fractal");
  3. int iHH = 5; for(int iBar = 4; iBar >= 1; --iBar) if(High[iBar] > High[iHH]) iHH = iBar;
    if(iHH == 3) Alert("New high fractal");
  4. double HighBar1=High[1], HighBar2=High[2], HighBar3=High[3], HighBar4=High[4], HighBar=High[5];
    if( MathMax(MathMax(HighBar1, HighBar2), 
                MathMax(HighBar4, HighBar5) ) < HighBar3) Alert("New high fractal");
    
  5. double HighBar1=High[1], HighBar2=High[2], HighBar3=High[3], HighBar4=High[4], HighBar=High[5];
    if(HighBar1 < HighBar3
    && HighBar2 < HighBar3
    && HighBar4 < HighBar3
    && HighBar5 < HighBar3) Alert("New high fractal");
fxalert
19
fxalert 2015.04.24 14:28  
WHRoeder:
MQLGreenHorn:

Hi - I'm very new to coding and have drowned in google searches and reading instruction manuals trying to find the correct way to code for high/low alerting.

double HighBar1 = High[1], repeat variable up to  double HighBar5 = High[5]

Alert when HighBar3 is > HighBar1 && HighBar2 && HighBar4 && HighBar5

  1. learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem. Was it so hard you couldn't even attempt it?

Thanks, I will post my attempts soon (e.g. when they don't work again), and I'm only looking for pointers not someone to write my code.

The reason for using the forum is because I want to learn to code ..! Everyone has to start somewhere and I am at the stage of using forums for help - so thank you for your welcoming reply.

Keith Watford
Moderator
7870
Keith Watford 2015.04.24 21:37  

There is no need to check more than once per bar, otherwise you will keep getting alerts.

Use a static or globalscope datetime variable initialised as 0

If Time[]0 is not equal to the variable, then do the check and update the variable 

fxalert
19
fxalert 2015.04.25 12:03  
Thank you GumRai - that was the next part of my quest as alerts were being generated every tick.
fxalert
19
fxalert 2015.04.25 14:03  
//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+


double ActivePrice=High[0];
extern bool SoundAlert = true; // will use in alerting sub function
extern bool PopupAlert = true; // will use in alerting sub function
extern bool EmailAlert = true; // will use in alerting sub function
//extern string "UP";
//extern string "DOWN";

#define HIGH 1
#define LOW 0

datetime LastHighAlert = D'1970.01.01';
datetime LastLowAlert = D'1970.01.01';

//--------------------------------------------------------------------
//--------------------------------------------------------------------

 int init()                                      // This is simply to ensure that Alerts do pop up when this code is running
   {
   
   Alert ("High Low Alert started: ", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES), " ", Symbol(), " With Price at :", ActivePrice);// Alert
   

   return 0;                                     // Exit init()
   }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
int start() 
{
  /* is this needed??? Is this just a sanity check to ensure the charts are loaded and working as expected?
   int counted = IndicatorCounted();

   if (counted < 0) return(-1);

   if (counted > 0) counted--;
   
   int limit = Bars - counted;

   for (int i = 0; i <= limit; i++)
    {
      If needed, I can move the  if & else if expression in to here
    }   
 */
   // check for new high or low 
  
  if(iHighest(NULL,0, MODE_HIGH, 5, 1) == 3 && (LastLowAlert != Time[0])) Alert("***NEW HIGH*** For: " + Symbol() + " at: " + TimeToStr(TimeCurrent(), TIME_MINUTES) + "Active Price: " + ActivePrice);
  else if(iLowest(NULL,0, MODE_LOW, 5, 1) == 3 && (LastLowAlert != Time[0])) Alert("***NEW LOW*** For: " + Symbol() + " at: " + TimeToStr(TimeCurrent(), TIME_MINUTES) + "Active Price: " + ActivePrice);  
 
 return (0);    // Exit Start                                  
 }
fxalert
19
fxalert 2015.04.25 14:04  
Above is the code that I hope will work, I can only test once the market opens again.
Thanks to WHroeder & GumRai for supporting this post. The code is mostly copy & paste from the help given on this forum and the many, many other sources google searches provide. 
But is is one step into the world of MQL coding...

whroeder1
13593
whroeder1 2015.04.25 17:24  
   if (counted > 0) counted--;
   int limit = Bars - counted;
   for (int i = 0; i <= limit; i++)
  1. First time counted = 0, your loop goes to i=Bars which does not exist.
  2. No need for the decrement Contradictory information on IndicatorCounted() - MQL4 forum
  3. Get in the habit of always counting down. Indicators can't use future values.
  4. Handle look back properly
    #define lookback 1 // High[i+1] for example
    for(int i = Bars - 1 - MathMax(lookback,counted); i >= 0; --i)
    



fxalert
19
fxalert 2015.04.25 17:25  
Appreciated. Many thanks.
/
To add comments, please log in or register