Problema codigo de ema

 

Hola gente buenos dias, estoy aprendiendo mql4 pero me esta costando me parece. La solucion que queria implementar es super sencilla pero no me esta saliendo...

Lo que quiero es cargar una EMA de 100 perdiodos 0 desplazamiento al cierre. La condicion seria que cuando el precio este debajo de la ema y suba hasta tocar la EMA envie una alerta (al mt4 movil) y cuando el precio este por encima, haga lo mismo en viceversa. 

NOTA: la alerta tiene que aparecer cuando el precio toque la ema, no cuando el precio cierre por encima/debajo. Solo debe tocar. Espero me puedan ayudar.

Muchas gracias a todos genios.

 
agugood100:
  • Por lo general, las personas que no pueden codificar no reciben ayuda gratuita en este foro, aunque podría suceder si tiene suerte. Sea paciente.
  • Si muestra sus intentos y describe su problema con claridad, probablemente recibirá una respuesta de la comunidad.
  • Si no quiere aprender a codificar, eso no es un problema. Puede mirar la sección CodeBase donde encontrara código libre y gratuito, o en el Market para productos de pago (también a veces gratis). Por último, también tiene la opción de contratar a un programador en la sección Freelance.
 
agugood100:

Hola gente buenos dias, estoy aprendiendo mql4 pero me esta costando me parece. La solucion que queria implementar es super sencilla pero no me esta saliendo...

Lo que quiero es cargar una EMA de 100 perdiodos 0 desplazamiento al cierre. La condicion seria que cuando el precio este debajo de la ema y suba hasta tocar la EMA envie una alerta (al mt4 movil) y cuando el precio este por encima, haga lo mismo en viceversa. 

NOTA: la alerta tiene que aparecer cuando el precio toque la ema, no cuando el precio cierre por encima/debajo. Solo debe tocar. Espero me puedan ayudar.

Muchas gracias a todos genios.

#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 2
#property indicator_color1 clrGreenYellow
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 2
#property indicator_color2 clrCrimson
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern int Periods = 100;
datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;//Audible Alerts
extern bool Push_Notifications = true;//Push Notifications
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | EMA Alert @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | EMA Alert @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Push_Notifications) SendNotification(type+" | EMA Alert @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(High[1+i] > iMA(NULL, PERIOD_CURRENT, Periods, 0, MODE_SMA, PRICE_CLOSE, i)
      && High[1+i+1] < iMA(NULL, PERIOD_CURRENT, Periods, 0, MODE_SMA, PRICE_CLOSE, i+1) //Candlestick High crosses above Moving Average
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
         if(i == 0) myAlert("indicator", "Buy"); //Instant alert, unlimited
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(Low[1+i] < iMA(NULL, PERIOD_CURRENT, Periods, 0, MODE_SMA, PRICE_CLOSE, i)
      && Low[1+i+1] > iMA(NULL, PERIOD_CURRENT, Periods, 0, MODE_SMA, PRICE_CLOSE, i+1) //Candlestick Low crosses below Moving Average
      )
        {
         Buffer2[i] = High[i]; //Set indicator value at Candlestick High
         if(i == 0) myAlert("indicator", "Sell"); //Instant alert, unlimited
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Sirva esto como base para lo que quieres hacer. Un saludo.

 
Enrique Enguix #:

Aunque lo hayas modificado estaría bien nombrar la fuente del ejemplo. Un saludo.

https://www.mql5.com/es/code/23545

MACD Candle Indicator for Mt4
  • www.mql5.com
Displays colored bars for MACD indicator above or below the zero level. Configurable with alerts.
 
Miguel Angel Vico Alba #:

Aunque lo hayas modificado estaría bien nombrar la fuente del ejemplo. Un saludo.

https://www.mql5.com/es/code/23545

La fuente de ejemplo es esta:



El código original, creado por mi para responder esta pregunta, usando EAbuilder es el siguiente:


//+------------------------------------------------------------------+
//|                                         Indicator: EMA Alert.mq4 |
//|                                       Created with EABuilder.com |
//|                                        https://www.eabuilder.com |
//+------------------------------------------------------------------+
#property copyright "Created with EABuilder.com"
#property link      "https://www.eabuilder.com"
#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern int Periods = 14;
datetime time_alert; //used when sending alert
bool Audible_Alerts = true;
bool Push_Notifications = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | EMA Alert @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | EMA Alert @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Push_Notifications) SendNotification(type+" | EMA Alert @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(High[1+i] > iMA(NULL, PERIOD_CURRENT, Periods, 0, MODE_SMA, PRICE_CLOSE, i)
      && High[1+i+1] < iMA(NULL, PERIOD_CURRENT, Periods, 0, MODE_SMA, PRICE_CLOSE, i+1) //Candlestick High crosses above Moving Average
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
         if(i == 0) myAlert("indicator", "Buy"); //Instant alert, unlimited
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(Low[1+i] < iMA(NULL, PERIOD_CURRENT, Periods, 0, MODE_SMA, PRICE_CLOSE, i)
      && Low[1+i+1] > iMA(NULL, PERIOD_CURRENT, Periods, 0, MODE_SMA, PRICE_CLOSE, i+1) //Candlestick Low crosses below Moving Average
      )
        {
         Buffer2[i] = High[i]; //Set indicator value at Candlestick High
         if(i == 0) myAlert("indicator", "Sell"); //Instant alert, unlimited
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
 
Enrique Enguix #:

Entiendo....entonces por lo visto todo el mundo usa EAbuilder dado que tienen el mismo patrón como el EA de CodeBase que adjunte...interesante.

 
Miguel Angel Vico Alba #:

Entiendo....entonces por lo visto todo el mundo usa EAbuilder dado que tienen el mismo patrón como el EA de CodeBase que adjunte...interesante.

El EAbuilder debería llamarse IndiBuilder porque la estructura que genera está bastante bien. Para generar Asesores Expertos es más flojo, las estructuras tienen algunos fallos y el código está muy poco optimizado. Para generar bocetos rápidos puede servir. Un saludo Miguel Angel
 
Muchas gracias amigo!!