133 lines
4.8 KiB
MQL5
133 lines
4.8 KiB
MQL5
|
//+------------------------------------------------------------------+
|
||
|
|
||
|
//+------------------------------------------------------------------+
|
||
|
//| ICrossAverage.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_chart_window
|
||
|
#property indicator_applied_price PRICE_CLOSE
|
||
|
#property indicator_buffers 4
|
||
|
#property indicator_plots 4
|
||
|
#property indicator_type2 DRAW_LINE
|
||
|
#property indicator_color2 clrBlue
|
||
|
#property indicator_type3 DRAW_LINE
|
||
|
#property indicator_color3 clrAqua
|
||
|
#property indicator_type4 DRAW_LINE
|
||
|
#property indicator_color4 clrRed
|
||
|
|
||
|
#include <MovingAverages.mqh>
|
||
|
|
||
|
input int FEMA = 16;
|
||
|
input int MEMA = 26;
|
||
|
input int SEMA = 34;
|
||
|
input int N_CANDLES = 3;
|
||
|
|
||
|
double bufferFEMA[];
|
||
|
double bufferMEMA[];
|
||
|
double bufferSEMA[];
|
||
|
double trendBuffer[];
|
||
|
|
||
|
//+------------------------------------------------------------------+
|
||
|
//| Custom indicator initialization function |
|
||
|
//+------------------------------------------------------------------+
|
||
|
int OnInit() {
|
||
|
SetIndexBuffer(0,trendBuffer,INDICATOR_DATA);
|
||
|
SetIndexBuffer(1,bufferFEMA,INDICATOR_CALCULATIONS);
|
||
|
SetIndexBuffer(2,bufferMEMA,INDICATOR_CALCULATIONS);
|
||
|
SetIndexBuffer(3,bufferSEMA,INDICATOR_CALCULATIONS);
|
||
|
return(INIT_SUCCEEDED);
|
||
|
}
|
||
|
//+------------------------------------------------------------------+
|
||
|
//| Custom indicator iteration function |
|
||
|
//+------------------------------------------------------------------+
|
||
|
int OnCalculate(const int ratesTotal, const int prevCalculated, const int begin, const double &price[]) {
|
||
|
|
||
|
int start,i;
|
||
|
ArraySetAsSeries(price, true);
|
||
|
|
||
|
if(ratesTotal<SEMA) {
|
||
|
return(0);
|
||
|
}
|
||
|
|
||
|
if(prevCalculated==0) {
|
||
|
start=SEMA;
|
||
|
} else {
|
||
|
start=prevCalculated-1;
|
||
|
}
|
||
|
ExponentialMAOnBuffer(ratesTotal,prevCalculated,begin,FEMA,price,bufferFEMA);
|
||
|
ExponentialMAOnBuffer(ratesTotal,prevCalculated,begin,MEMA,price,bufferMEMA);
|
||
|
ExponentialMAOnBuffer(ratesTotal,prevCalculated,begin,SEMA,price,bufferSEMA);
|
||
|
|
||
|
for(i=start; i<ratesTotal; i++) {
|
||
|
trendBuffer[i]=0;
|
||
|
int trend=trendDetector(i);
|
||
|
if(isLastNCandlesTrendUp(price)) {
|
||
|
trendBuffer[i]=1;
|
||
|
} else if(isLastNCandlesDonw(price)) {
|
||
|
trendBuffer[i]=-1;
|
||
|
}
|
||
|
}
|
||
|
return(ratesTotal);
|
||
|
}
|
||
|
|
||
|
//+------------------------------------------------------------------+
|
||
|
//| |
|
||
|
//+------------------------------------------------------------------+
|
||
|
bool isLastNCandlesTrendUp(const double& price[]) {
|
||
|
int priceSize = ArraySize(price);
|
||
|
|
||
|
for(int i=0; i<=N_CANDLES; i++) {
|
||
|
if(price[i+1]>=price[i+2]) {
|
||
|
continue;
|
||
|
} else return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
//+------------------------------------------------------------------+
|
||
|
//| |
|
||
|
//+------------------------------------------------------------------+
|
||
|
bool isLastNCandlesDonw(const double& price[]) {
|
||
|
|
||
|
int priceSize = ArraySize(price);
|
||
|
|
||
|
for(int i=0; i<=N_CANDLES; i++) {
|
||
|
if(price[i+1]<=price[i+2]) {
|
||
|
continue;
|
||
|
} else return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
//+------------------------------------------------------------------+
|
||
|
//| |
|
||
|
//+------------------------------------------------------------------+
|
||
|
int trendDetector(int position) {
|
||
|
int trendDirection=0;
|
||
|
if(bufferFEMA[position] > bufferMEMA[position] && bufferMEMA[position] > bufferSEMA[position]) {
|
||
|
trendDirection=1;
|
||
|
} else if(bufferFEMA[position] < bufferMEMA[position] && bufferFEMA[position] > bufferSEMA[position]) {
|
||
|
trendDirection=-1;
|
||
|
}
|
||
|
return(trendDirection);
|
||
|
}
|
||
|
|
||
|
//int trendDetector(int position) {
|
||
|
// int trendDirection=0;
|
||
|
// if(bufferFEMA[position] > bufferMEMA[position] && bufferMEMA[position] > bufferSEMA[position]) {
|
||
|
// trendDirection=1;
|
||
|
// } else if(bufferFEMA[position] < bufferMEMA[position] && bufferFEMA[position] > bufferSEMA[position]) {
|
||
|
// trendDirection=2;
|
||
|
// } else if(bufferFEMA[position] < bufferMEMA[position] && bufferMEMA[position] < bufferSEMA[position]) {
|
||
|
// trendDirection=-1;
|
||
|
// } else if(bufferFEMA[position] > bufferMEMA[position] && bufferMEMA[position] < bufferSEMA[position]) {
|
||
|
// trendDirection=-2;
|
||
|
// }
|
||
|
// return(trendDirection);
|
||
|
// }
|