Page 1 of 1

Trying to code an MA Touch indicator for MT4 - Please check my code

Posted: Thu Jul 11, 2024 12:28 pm
by boytoy
Heya guys I'm stuck on this old code which I'm trying to fix.

The indicator I'm trying to make is just a Moving Average which alerts every time price touches or comes close to the MA... but here are the issues I am having:
  • The Moving Average in the code does not match and seems to be shifted lower
  • It's not user friendly at all
I have added to fix NMC compatible using:

{
for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);


But now I've got all sorts of glitches and arrows happening.

Here is the full code:

Code: Select all

#property copyright "forex-station.com"
#property link      "https://www.forex-station.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 White
#property indicator_color3 Red

extern int Crossed_Pips = 2;
extern int MA_Period = 200;
extern int MA_Type = MODE_SMA;
extern int Shift_Bars=0;
extern int Bars_Count= 1000;
int state;

extern bool ShowAlert      = FALSE;
extern bool SendEmail      = TRUE;
extern bool Push           = TRUE;
string FileName="MATouch_Alert";

//---- buffers
double v1[];
double v2[];
double v3[];
  
int init()
{
   for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);

   IndicatorBuffers(3);
   SetIndexArrow(0,217);
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexDrawBegin(1,MA_Period * 2);
   SetIndexBuffer(0, v1);
   SetIndexLabel(0,"Buy");
   
   SetIndexArrow(1,218);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexDrawBegin(1,MA_Period * 2);
   SetIndexBuffer(1, v2);
   SetIndexLabel(1,"Sell");
   
 
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1);
   SetIndexDrawBegin(1,MA_Period * 2);
   SetIndexBuffer(2, v3);
   SetIndexLabel(2,"MA");
 
   return(0);
  }

int start()
 {
  double ma;
  int previous;
  int i;
  int shift;
  bool crossed_up, crossed_down;
  int totalBars = Bars - (MA_Period * 2);
  
  if (Bars_Count > 0 && Bars_Count <= totalBars)
  {
    i = Bars_Count;
  }else if(totalBars <= 0 ) {
    return(0);
  }else{
    i = totalBars;
  }
  
  while(i>=0)
   {
    shift = i + Shift_Bars;
    ma = iMA(Symbol(), Period(), MA_Period, 0, MA_Type, PRICE_LOW, shift);
    crossed_up = High[shift] >= (ma + (Crossed_Pips * Point));
    crossed_down = Low[shift] <= (ma - (Crossed_Pips * Point));

    v1[i] = NULL;
    v2[i] = NULL;    
    v3[i] = ma;
    if (crossed_up && previous != 1) {
      v1[i] = ma + (Crossed_Pips * Point);
      previous = 1;
    }else if(crossed_down && previous != 2){
      v2[i] = ma - (Crossed_Pips * Point);
      previous = 2;
    }
 
    i--;
   }
   
   ma = iMA(Symbol(), Period(), MA_Period, 0, MA_Type, PRICE_LOW, 0);
   if (Close[0] >= ma + (Crossed_Pips * Point) && state != 1) { 
      state = 1;
     // Alert(Symbol() + " - UP - Price has crossed up the moving average.");
      SendAlerts(i,"Price has crossed up the moving average","Buy");
   }
   else if (Close[0] <= ma - (Crossed_Pips * Point) && state != -1) {
      state = -1;
      //Alert(Symbol() + " - DOWN - Price has crossed down the moving average.");
   SendAlerts(i,"Price has crossed down the moving average","Sell");   
   }

   return(0);
 }
 
//+------------------------------------------------------------------+
 bool SendAlerts(int thisi,string action,string type){

	string header,body;
 	header= FileName+" "+type+" signal on "+ Symbol()+" "+GetPeriodName();
 	body  = FileName+" has detected that "+action+" on "+ Symbol()+" "+GetPeriodName()+" on ";
static datetime prevtime=0;
 if(prevtime == Time[0]) {
	return(0);
 }
 Alert("MA TOUCH "+Symbol());
SendNotification("PRICE has touched ma" + _Symbol);
 prevtime = Time[0];

if(ShowAlert){
  Alert(header);
  }
 if (!IsTesting()){ 
if(SendEmail){
 	SendMail(header,body);
 }
 }
 }


//+------------------------------------------------------------------+
string GetPeriodName(){
   switch(Period()){
       case PERIOD_D1:  return("D1");
       case PERIOD_H4:  return("H4");
       case PERIOD_H1:  return("H1");
       case PERIOD_M1:  return("M1");
       case PERIOD_M15: return("M15");
       case PERIOD_M30: return("M30");
       case PERIOD_M5:  return("M5");
       case PERIOD_MN1: return("MN1");
       case PERIOD_W1:  return("W1");
     }
  }

Can one of the experts please fix my pathetic attempt at making a simple indicator!? 🙏

Re: Trying to code an MA Touch indicator for MT4 - Please check my code

Posted: Thu Jul 11, 2024 8:13 pm
by wojtek
Regardless of the structure of the code, the MA should
apply PRICE_CLOSE and the variables 'previous' and 'state'
should be buffers. The loop 'for' would be enough instead of 'while',
I suppose, with the standard alert block within.

Re: Trying to code an MA Touch indicator for MT4 - Please check my code

Posted: Thu Jul 11, 2024 11:42 pm
by Jimmy
Unless one of our coders get to it first i'll take a look when I'm back on my desktop.

For starters that indicator is showing the correct Moving Average but it's set to show the "Low" and not the "Close" which is why it doesn't match your other MA line.

As Wojtek said change PRICE_LOW to PRICE_CLOSE within the code so and it will match your yellow Moving Average line.

Re: Trying to code an MA Touch indicator for MT4 - Please check my code

Posted: Fri Jul 12, 2024 3:16 am
by mrtools
boytoy wrote: Thu Jul 11, 2024 12:28 pm Heya guys I'm stuck on this old code which I'm trying to fix.

The indicator I'm trying to make is just a Moving Average which alerts every time price touches or comes close to the MA... but here are the issues I am having:
  • The Moving Average in the code does not match and seems to be shifted lower
  • It's not user friendly at all
I have added to fix NMC compatible using:

{
for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);


But now I've got all sorts of glitches and arrows happening.

Here is the full code:

Code: Select all

#property copyright "forex-station.com"
#property link      "https://www.forex-station.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 White
#property indicator_color3 Red

extern int Crossed_Pips = 2;
extern int MA_Period = 200;
extern int MA_Type = MODE_SMA;
extern int Shift_Bars=0;
extern int Bars_Count= 1000;
int state;

extern bool ShowAlert      = FALSE;
extern bool SendEmail      = TRUE;
extern bool Push           = TRUE;
string FileName="MATouch_Alert";

//---- buffers
double v1[];
double v2[];
double v3[];
  
int init()
{
   for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);

   IndicatorBuffers(3);
   SetIndexArrow(0,217);
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexDrawBegin(1,MA_Period * 2);
   SetIndexBuffer(0, v1);
   SetIndexLabel(0,"Buy");
   
   SetIndexArrow(1,218);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexDrawBegin(1,MA_Period * 2);
   SetIndexBuffer(1, v2);
   SetIndexLabel(1,"Sell");
   
 
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1);
   SetIndexDrawBegin(1,MA_Period * 2);
   SetIndexBuffer(2, v3);
   SetIndexLabel(2,"MA");
 
   return(0);
  }

int start()
 {
  double ma;
  int previous;
  int i;
  int shift;
  bool crossed_up, crossed_down;
  int totalBars = Bars - (MA_Period * 2);
  
  if (Bars_Count > 0 && Bars_Count <= totalBars)
  {
    i = Bars_Count;
  }else if(totalBars <= 0 ) {
    return(0);
  }else{
    i = totalBars;
  }
  
  while(i>=0)
   {
    shift = i + Shift_Bars;
    ma = iMA(Symbol(), Period(), MA_Period, 0, MA_Type, PRICE_LOW, shift);
    crossed_up = High[shift] >= (ma + (Crossed_Pips * Point));
    crossed_down = Low[shift] <= (ma - (Crossed_Pips * Point));

    v1[i] = NULL;
    v2[i] = NULL;    
    v3[i] = ma;
    if (crossed_up && previous != 1) {
      v1[i] = ma + (Crossed_Pips * Point);
      previous = 1;
    }else if(crossed_down && previous != 2){
      v2[i] = ma - (Crossed_Pips * Point);
      previous = 2;
    }
 
    i--;
   }
   
   ma = iMA(Symbol(), Period(), MA_Period, 0, MA_Type, PRICE_LOW, 0);
   if (Close[0] >= ma + (Crossed_Pips * Point) && state != 1) { 
      state = 1;
     // Alert(Symbol() + " - UP - Price has crossed up the moving average.");
      SendAlerts(i,"Price has crossed up the moving average","Buy");
   }
   else if (Close[0] <= ma - (Crossed_Pips * Point) && state != -1) {
      state = -1;
      //Alert(Symbol() + " - DOWN - Price has crossed down the moving average.");
   SendAlerts(i,"Price has crossed down the moving average","Sell");   
   }

   return(0);
 }
 
//+------------------------------------------------------------------+
 bool SendAlerts(int thisi,string action,string type){

	string header,body;
 	header= FileName+" "+type+" signal on "+ Symbol()+" "+GetPeriodName();
 	body  = FileName+" has detected that "+action+" on "+ Symbol()+" "+GetPeriodName()+" on ";
static datetime prevtime=0;
 if(prevtime == Time[0]) {
	return(0);
 }
 Alert("MA TOUCH "+Symbol());
SendNotification("PRICE has touched ma" + _Symbol);
 prevtime = Time[0];

if(ShowAlert){
  Alert(header);
  }
 if (!IsTesting()){ 
if(SendEmail){
 	SendMail(header,body);
 }
 }
 }


//+------------------------------------------------------------------+
string GetPeriodName(){
   switch(Period()){
       case PERIOD_D1:  return("D1");
       case PERIOD_H4:  return("H4");
       case PERIOD_H1:  return("H1");
       case PERIOD_M1:  return("M1");
       case PERIOD_M15: return("M15");
       case PERIOD_M30: return("M30");
       case PERIOD_M5:  return("M5");
       case PERIOD_MN1: return("MN1");
       case PERIOD_W1:  return("W1");
     }
  }

Can one of the experts please fix my pathetic attempt at making a simple indicator!? 🙏
Moving Average Touch Alerts

Try, seems okay.

Re: Trying to code an MA Touch indicator for MT4 - Please check my code

Posted: Fri Jul 12, 2024 3:57 am
by mrtools
mrtools wrote: Fri Jul 12, 2024 3:16 am Try, seems okay.
Moving Average Touch Alert for newer MT4 builds with Arrow Signals

And this one should work on newer builds and can change the arrow code and arrow distance from the ma.

Re: Trying to code an MA Touch indicator for MT4 - Please check my code

Posted: Fri Jul 12, 2024 11:34 am
by boytoy
mrtools wrote: Fri Jul 12, 2024 3:57 am And this one should work on newer builds and can change the arrow code and arrow distance from the ma.
Nothing beats hand coded by Mrtools... Chatgpt 4 struggled to figure it out and I tried for hours with it before posting here man.... thank you I can now update my countertrend template 🙏

Re: Trying to code an MA Touch indicator for MT4 - Please check my code

Posted: Fri Jul 12, 2024 7:42 pm
by moey_dw
mrtools wrote: Fri Jul 12, 2024 3:57 am And this one should work on newer builds and can change the arrow code and arrow distance from the ma.
PERFECT....

Funny I downloaded tried many MA ALERTS TOUCH indicators over the years but every single one all had bugs this is the only one that works and pings alerts as price gets near or hits the moving average lines......

Also I am using the Windings code 158 for the arrows from the MT4 Windings table thx to make it adjustable now........ 👍👍👍

Re: Trying to code an MA Touch indicator for MT4 - Please check my code

Posted: Fri Jul 12, 2024 8:22 pm
by Jimmy
mrtools wrote: Fri Jul 12, 2024 3:57 am And this one should work on newer builds and can change the arrow code and arrow distance from the ma.
This is the only MA touch indicator that actually works properly for MT4. As Moey said there's a few other versions on FF and on here from years ago but they all had problems.

Thank you Boytoy and Mrtools for the the simple, yet indispensable indicator.