2
Views
0
Downloads
0
Favorites
ADX_SimpleMA
//+------------------------------------------------------------------+
//| ADX.mq5 |
//| Copyright 2009-2017, MetaQuotes Software Corp., SEM |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009-2017, MetaQuotes Software Corp., SEM"
#property link "http://www.mql5.com"
#property description "Average Directional Movement Index"
#include <MovingAverages.mqh>
#property indicator_separate_window
#property indicator_buffers 11
#property indicator_plots 4
#property indicator_label1 "D"
#property indicator_type1 DRAW_COLOR_HISTOGRAM2
#property indicator_color1 clrLime,clrRed,clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 5
#property indicator_label2 "D+"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrLime
#property indicator_style2 STYLE_SOLID
#property indicator_width2 3
#property indicator_label3 "D-"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
#property indicator_style3 STYLE_SOLID
#property indicator_width3 3
#property indicator_label4 "ADX"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrMediumBlue
#property indicator_style4 STYLE_SOLID
#property indicator_width4 3
//#property indicator_type3 DRAW_ARROW
//#property indicator_color3 clrRed
//#property indicator_style3 STYLE_SOLID
//#property indicator_width3 1
#property indicator_level1 15
#property indicator_level2 20
#property indicator_level3 25
#property indicator_level4 50
//#property indicator_label3 "-DI"
//--- input parameters
input int InpPeriodADX=16; // Period
input int simple=5; //
//---- buffers
double ExtPDIBuffer[];
double ExtNDIBuffer[];
double colorbufer[];
double ExtPDIBufferLine[];
double ExtNDIBufferLine[];
double ExtADXBuffer[];
double ExtPDBuffer[];
double ExtNDBuffer[];
double ExtTmpBuffer[];
double ExtP2[];
double ExtN2[];
//--- global variables
int ExtADXPeriod;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- check for input parameters
if(InpPeriodADX>=100 || InpPeriodADX<=0)
{
ExtADXPeriod=14;
printf("Incorrect value for input variable Period_ADX=%d. Indicator will use value=%d for calculations.",InpPeriodADX,ExtADXPeriod);
}
else
ExtADXPeriod=InpPeriodADX;
//---- indicator buffers
SetIndexBuffer(0,ExtPDIBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ExtNDIBuffer,INDICATOR_DATA);
SetIndexBuffer(2,colorbufer,INDICATOR_COLOR_INDEX);
PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);
SetIndexBuffer(3,ExtPDIBufferLine,INDICATOR_DATA);
SetIndexBuffer(4,ExtNDIBufferLine,INDICATOR_DATA);
SetIndexBuffer(5,ExtADXBuffer,INDICATOR_DATA);
SetIndexBuffer(6,ExtPDBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(7,ExtNDBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(8,ExtTmpBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(9,ExtP2,INDICATOR_CALCULATIONS);
SetIndexBuffer(10,ExtN2,INDICATOR_CALCULATIONS);
//--- indicator digits
IndicatorSetInteger(INDICATOR_DIGITS,2);
//--- set draw begin
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod<<1);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod);
PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXPeriod);
//--- indicator short name
string short_name="ADX Simple("+string(ExtADXPeriod)+","+string(simple)+")";
//string short_name="_";
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- change 1-st index label
PlotIndexSetString(0,PLOT_LABEL,short_name);
PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);
//---- end of initialization function
}
//+------------------------------------------------------------------+
//| 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[])
{
//--- checking for bars count
if(rates_total<ExtADXPeriod)
return(0);
//--- detect start position
int start;
if(prev_calculated>1)
start=prev_calculated-1;
else
{
start=1;
ExtPDIBuffer[0]=0.0;
ExtNDIBuffer[0]=0.0;
ExtADXBuffer[0]=0.0;
}
//--- main cycle
for(int i=start; i<rates_total && !IsStopped(); i++)
{
//--- get some data
double Hi =high[i];
double prevHi=high[i-1];
double Lo =low[i];
double prevLo=low[i-1];
double prevCl=close[i-1];
//--- fill main positive and main negative buffers
double dTmpP=Hi-prevHi;
double dTmpN=prevLo-Lo;
if(dTmpP<0.0)
dTmpP=0.0;
if(dTmpN<0.0)
dTmpN=0.0;
if(dTmpP>dTmpN)
dTmpN=0.0;
else
{
if(dTmpP<dTmpN)
dTmpP=0.0;
else
{
dTmpP=0.0;
dTmpN=0.0;
}
}
//--- define TR
double tr=MathMax(MathMax(MathAbs(Hi-Lo),MathAbs(Hi-prevCl)),MathAbs(Lo-prevCl));
//---
if(tr!=0.0)
{
ExtPDBuffer[i]=100.0*dTmpP/tr;
ExtNDBuffer[i]=100.0*dTmpN/tr;
}
else
{
ExtPDBuffer[i]=0.0;
ExtNDBuffer[i]=0.0;
}
ExtP2[i]=ExponentialMA(i,ExtADXPeriod,ExtP2[i-1],ExtPDBuffer);
ExtN2[i]=ExponentialMA(i,ExtADXPeriod,ExtN2[i-1],ExtNDBuffer);
//--- fill smoothed positive and negative buffers
ExtPDIBuffer[i]=SimpleMA(i,simple,ExtP2);
ExtNDIBuffer[i]=SimpleMA(i,simple,ExtN2);
ExtPDIBufferLine[i]=ExtPDIBuffer[i];
ExtNDIBufferLine[i]=ExtNDIBuffer[i];
//--- fill ADXTmp buffer
double dTmp=ExtPDIBuffer[i]+ExtNDIBuffer[i];
if(dTmp!=0.0)
dTmp=100.0*MathAbs((ExtPDIBuffer[i]-ExtNDIBuffer[i])/dTmp);
else
dTmp=0.0;
ExtTmpBuffer[i]=dTmp;
//--- fill smoothed ADX buffer
ExtADXBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtADXBuffer[i-1],ExtTmpBuffer);
};
for(int i=start; i<rates_total-1 && !IsStopped(); i++)
{
if((ExtPDIBuffer[i]<ExtPDIBuffer[i+1] && ExtNDIBuffer[i]<ExtPDIBuffer[i]) || (ExtPDIBuffer[i]<ExtPDIBuffer[i+1] && ExtNDIBuffer[i]>ExtNDIBuffer[i+1]))
{colorbufer[i]=0.0;}
else
if((ExtNDIBuffer[i]<ExtNDIBuffer[i+1] && ExtNDIBuffer[i]>ExtPDIBuffer[i]) ||(ExtNDIBuffer[i]<ExtNDIBuffer[i+1] && ExtPDIBuffer[i]>ExtPDIBuffer[i+1]))
{ colorbufer[i]=1.0; }
else
{ colorbufer[i]=2.0; };
};
//---- OnCalculate done. Return new prev_calculated.
return(rates_total);
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---