//--- enums
enum ENUM_INTERSECT_DIRECTION
{
INTERSECT_DIRECTION_NONE= 0, // no hay cruce
INTERSECT_DIRECTION_UP = 1, // cruce hacia arriba
INTERSECT_DIRECTION_DOWN=-1, // cruce hacia abajo
};
//--- input parameters
input uint InpPeriod = 10; // MA Period
input int InpShift = 0; // MA Shift
input ENUM_MA_METHOD InpMethod = MODE_SMA; // MA Method
input ENUM_APPLIED_PRICE InpPrice = PRICE_CLOSE; // MA Applied price
//--- global variables
int ExtMaHandle;
int ExtMaPeriod;
double ExtData[2];
MqlRates ExtRates[2];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- el periodo de cálculo de la media móvil será igual al valor por defecto (10) si se especifica cero en el parámetro de entrada
ExtMaPeriod=int(InpPeriod<1 ? 10 : InpPeriod);
//--- creamos un manejador del indicador Moving Average con los parámetros especificados
ExtMaHandle=iMA(Symbol(),PERIOD_CURRENT,ExtMaPeriod,InpShift,InpMethod,InpPrice);
ResetLastError();
if(ExtMaHandle==INVALID_HANDLE)
{
PrintFormat("Failed to create iMA() handle. Error code: %d",GetLastError());
return(INIT_FAILED);
}
//--- obtenemos la hora de la última actualización de precios
datetime tick_time=TickTime();
//--- obtenemos los datos de la media móvil y los datos de precio de las dos últimas barras
if(GetData(ExtMaHandle,ExtData,ExtRates) && tick_time!=0)
{
//--- si el precio está por encima de la media móvil
if(ExtRates[1].close>ExtData[1])
{
//--- creamos un texto de mensaje y mostramos Alert
string message=StringFormat("Bar time: %s. The price is above the moving average",TimeToString(ExtRates[1].time));
Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
/*
Resultado:
Alert: Bar time: 2024.02.16 18:00. The price is above the moving average at 2024.02.16 18:47:43
*/
}
else
{
//--- si el precio está por debajo de la media móvil
if(ExtRates[1].close<ExtData[1])
{
//--- creamos un texto de mensaje y mostramos Alert
string message=StringFormat("Bar time: %s. The price is below the moving average.",TimeToString(ExtRates[1].time));
Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
/*
Resultado:
Alert: Bar time: 2024.02.16 19:00. The price is below the moving average at 2024.02.16 19:33:14
*/
}
else
{
//--- creamos un texto de mensaje y mostramos Alert
string message=StringFormat("Bar time: %s. The price and moving average are equal.",TimeToString(ExtRates[1].time));
Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
/*
Resultado:
Alert: Bar time: 2024.02.16 20:00. The price and moving average are equal at 2024.02.16 20:12:22
*/
}
}
}
//--- сon éxito
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
ResetLastError();
//--- obtenemos los datos de la media móvil y los datos de precio de las dos últimas barras
if(!GetData(ExtMaHandle,ExtData,ExtRates))
return;
//--- obtenemos la dirección en que el precio cruza la media móvil en la barra actual
ENUM_INTERSECT_DIRECTION intersect=GetIntersectDirection(ExtData,ExtRates);
//--- variable para guardar el mensaje anterior
static string message_prev="";
//--- si el precio ha cruzado la media móvil en la barra actual de abajo hacia arriba
if(intersect==INTERSECT_DIRECTION_UP)
{
//--- obtenemos la hora del tick en el que se ha producido el cruce
datetime tick_time=TickTime();
if(tick_time==0)
return;
//--- creamos un texto de mensaje
string message=StringFormat("Bar time: %s. The price crossed the MA from bottom to top",TimeToString(ExtRates[1].time));
//--- si el mensaje pasado es igual al actual, mostramos Alert con el mensaje y la hora del tick
if(message!=message_prev)
{
Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
message_prev=message;
/*
Resultado:\
Alert: Bar time: 2024.02.16 09:00. The price crossed the MA from bottom to top at 2024.02.16 09:20:35
*/
}
}
//--- si el precio ha cruzado la media móvil en la barra actual de arriba hacia abajo
if(intersect==INTERSECT_DIRECTION_DOWN)
{
//--- obtenemos la hora del tick en el que se ha producido el cruce
datetime tick_time=TickTime();
if(tick_time==0)
return;
//--- creamos un texto de mensaje
string message=StringFormat("Bar time: %s. The price crossed the MA from top to bottom",TimeToString(ExtRates[1].time));
//--- si el mensaje pasado es igual al actual, mostramos Alert con el mensaje y la hora del tick
if(message!=message_prev)
{
Alert(message+" at "+TimeToString(tick_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
message_prev=message;
/*
Resultado:\
Alert: Bar time: 2024.02.16 10:00. The price crossed the MA from top to bottom at 2024.02.16 10:42:15
*/
}
}
}
//+------------------------------------------------------------------+
//| Obtiene los datos del precio y las medias móviles en arrays |
//+------------------------------------------------------------------+
bool GetData(int handle,double &ma_data[],MqlRates &price_data[])
{
ResetLastError();
//--- obtenemos los datos de la media móvil de las dos últimas barras
if(CopyBuffer(handle,0,0,2,ma_data)!=2)
{
PrintFormat("CopyBuffer() failed. Error code: %d",GetLastError());
return(false);
}
//--- obtenemos los datos de precio de las dos últimas barras
if(CopyRates(Symbol(),PERIOD_CURRENT,0,2,price_data)!=2)
{
PrintFormat("CopyRates() failed. Error code: %d",GetLastError());
return(false);
}
return(true);
}
//+------------------------------------------------------------------+
//| Retorna la dirección en que el precio cruza una media móvil |
//+------------------------------------------------------------------+
ENUM_INTERSECT_DIRECTION GetIntersectDirection(double &ma_data[],MqlRates &price_data[])
{
double ma0=ma_data[1];
double ma1=ma_data[0];
double close0=price_data[1].close;
double close1=price_data[0].close;
if(close1<=ma1 && close0>ma0)
return(INTERSECT_DIRECTION_UP);
else
{
if(close1>=ma1 && close0<ma0)
return(INTERSECT_DIRECTION_DOWN);
else
return(INTERSECT_DIRECTION_NONE);
}
}
//+------------------------------------------------------------------+
//| Retorna la hora del tick en segundos |
//+------------------------------------------------------------------+
datetime TickTime()
{
MqlTick tick={};
ResetLastError();
if(!SymbolInfoTick(Symbol(),tick))
{
PrintFormat("SymbolInfoTick() failed. Error code: %d",GetLastError());
return(0);
}
return(tick.time);
}
|