AntRobot/indicators/ITrendConfirmation.mq5

154 lines
6.2 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 14:40:14 +02:00
//+------------------------------------------------------------------+
//| TrendConfirmation.mq5 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots 4
// MACDline
#property indicator_label1 "MACDline"
#property indicator_type1 DRAW_LINE
#property indicator_color1 Green
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//signalLine
#property indicator_label2 "Signalline"
#property indicator_type2 DRAW_LINE
#property indicator_color2 Red
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//histogram
#property indicator_label4 "Histogram"
#property indicator_type4 DRAW_COLOR_HISTOGRAM
#property indicator_color4 clrOrange,clrGreen,clrRed
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//--- input parameters
input int FMA=12;
input int SMA=26;
input int SIGNAL=9;
input double CONFIRMATION_UMBRAL=10;
//--- indicator buffers
double MACDlineBuffer[];
double signallineBuffer[];
double histogramBuffer[];
double trendConfirmationBuffer[];
double histogramColors[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit() {
SetIndexBuffer(0,MACDlineBuffer,INDICATOR_DATA);
SetIndexBuffer(1,signallineBuffer,INDICATOR_DATA);
SetIndexBuffer(2,trendConfirmationBuffer,INDICATOR_DATA);
SetIndexBuffer(3,histogramBuffer,INDICATOR_DATA);
SetIndexBuffer(4,histogramColors,INDICATOR_COLOR_INDEX);
PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_NONE);
PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_NONE);
//PlotIndexSetInteger(4,PLOT_DRAW_TYPE,DRAW_NONE);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int ratesTotal, const int prevCalculated, const int begin, const double &price[]) {
int MACDhadling=iMACD(NULL,0,FMA,SMA,SIGNAL,PRICE_CLOSE);
CopyBuffer(MACDhadling,0,0,ratesTotal,MACDlineBuffer);
CopyBuffer(MACDhadling,1,0,ratesTotal,signallineBuffer);
for(int i=0; i<ratesTotal; i++) {
int trendDirection=getTrendDirection(i);
trendConfirmationBuffer[i]=trendDirection;
if(trendDirection==2||trendDirection==0) {
histogramColors[i]=0;
}
if(trendDirection==-1) {
histogramColors[i]=2;
}
if(trendDirection==1) {
histogramColors[i]=1;
}
}
return(ratesTotal);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int getTrendDirection(int position) {
int direction=2;
histogramBuffer[position]=MACDlineBuffer[position]-signallineBuffer[position];
if(histogramBuffer[position] <= CONFIRMATION_UMBRAL && histogramBuffer[position] >= -CONFIRMATION_UMBRAL) {
return 2;
}
if(position>1) {
if(isValidDownTrend(position)&& isValidDownTrend(position-1)) return -1;
if(isValidUpTrend(position)&& isValidUpTrend(position-1)) return 1;
if((closeUpTrend(position)&& closeUpTrend(position-1))||(closeDownTrend(position)&&closeDownTrend(position-1))) return 0;
}
return direction;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int isValidDownTrend(int position) {
histogramBuffer[position]=MACDlineBuffer[position]-signallineBuffer[position];
if(histogramBuffer[position] < histogramBuffer[position-1] && histogramBuffer[position] < histogramBuffer[position-2] && histogramBuffer[position-1] < histogramBuffer[position-2]) {
return true;
} else {
return false;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int isValidUpTrend(int position) {
histogramBuffer[position]=MACDlineBuffer[position]-signallineBuffer[position];
if(histogramBuffer[position] >= histogramBuffer[position-1] && histogramBuffer[position] >= histogramBuffer[position-2]&& histogramBuffer[position-1] >= histogramBuffer[position-2]) {
return true;
} else {
return false;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int closeUpTrend(int position) {
histogramBuffer[position]=MACDlineBuffer[position]-signallineBuffer[position];
if(histogramBuffer[position]<= histogramBuffer[position-1] && histogramBuffer[position-1] <= histogramBuffer[position-2]) {
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int closeDownTrend(int position) {
histogramBuffer[position]=MACDlineBuffer[position]-signallineBuffer[position];
if(histogramBuffer[position] > histogramBuffer[position-1]&& histogramBuffer[position-1] > histogramBuffer[position-2]) {
return true;
}
return false;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+