Indicators Used
0
Views
0
Downloads
0
Favorites
MACD Top Bottom
ÿþ//+------------------------------------------------------------------+
//| MACD Top Bottom.mq5 |
//| Copyright © 2019, Vladimir Karputov |
//| http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link "http://wmua.ru/slesar/"
#property version "1.000"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 2
//--- plot CurrentTop
#property indicator_label1 "Top"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot CurrentBottom
#property indicator_label2 "Bottom"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- input parameters
input int Inp_MACD_fast_ema_period= 12; // MACD: period for Fast average calculation
input int Inp_MACD_slow_ema_period= 26; // MACD: period for Slow average calculation
input int Inp_MACD_signal_period = 9; // MACD: period for their difference averaging
input ENUM_APPLIED_PRICE Inp_MACD_applied_price = PRICE_CLOSE; // MACD: type of price
//--- indicator buffers
double CurrentTopBuffer[];
double CurrentBottomBuffer[];
double MACDBuffer[];
double SignalBuffer[];
//---
int handle_iMACD; // variable for storing the handle of the iMACD indicator
//--- we will keep the number of values in the Moving Averages Convergence/Divergence indicator
int bars_calculated=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,CurrentTopBuffer,INDICATOR_DATA);
SetIndexBuffer(1,CurrentBottomBuffer,INDICATOR_DATA);
SetIndexBuffer(2,MACDBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(3,SignalBuffer,INDICATOR_CALCULATIONS);
//--- sets first bar from what index will be drawn
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,Inp_MACD_signal_period-1);
//--- name for Dindicator subwindow label
IndicatorSetString(INDICATOR_SHORTNAME,"MACD Top Bottom("+
string(Inp_MACD_fast_ema_period)+","+
string(Inp_MACD_slow_ema_period)+","+
string(Inp_MACD_signal_period)+")");
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
PlotIndexSetInteger(0,PLOT_ARROW,159);
PlotIndexSetInteger(1,PLOT_ARROW,159);
//--- setting the vertical shift of arrows in pixels
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-5);
PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,5);
//--- setting as an empty value
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//--- create handle of the indicator iMACD
handle_iMACD=iMACD(Symbol(),Period(),Inp_MACD_fast_ema_period,Inp_MACD_slow_ema_period,
Inp_MACD_signal_period,Inp_MACD_applied_price);
//--- if the handle is not created
if(handle_iMACD==INVALID_HANDLE)
{
//--- tell about the failure and output the error code
PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped early
return(INIT_FAILED);
}
//---
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[])
{
//--- number of values copied from the iMACD indicator
int values_to_copy;
//--- determine the number of values calculated in the indicator
int calculated=BarsCalculated(handle_iMACD);
if(calculated<=0)
{
PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
return(0);
}
//--- if it is the first start of calculation of the indicator or if the number of values in the iMACD indicator changed
//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)
if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
{
//--- if the MACDBuffer array is greater than the number of values in the iMACD indicator for symbol/period, then we don't copy everything
//--- otherwise, we copy less than the size of indicator buffers
if(calculated>rates_total)
values_to_copy=rates_total;
else
values_to_copy=calculated;
}
else
{
//--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()
//--- for calculation not more than one bar is added
values_to_copy=(rates_total-prev_calculated)+1;
}
//--- fill the arrays with values of the iMACD indicator
//--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation
if(!FillArraysFromBuffers(MACDBuffer,SignalBuffer,handle_iMACD,values_to_copy))
return(0);
//--- memorize the number of values in the Moving Averages indicator Convergence/Divergence
bars_calculated=calculated;
//---
int limit=prev_calculated-1;
if(prev_calculated==0)
limit=0;
if(limit<1)
{
limit=1;
CurrentTopBuffer[0]=0.0;
CurrentBottomBuffer[0]=0.0;
}
for(int i=limit;i<rates_total;i++)
{
CurrentTopBuffer[i]=CurrentTopBuffer[i-1];
CurrentBottomBuffer[i]=CurrentBottomBuffer[i-1];
if(MACDBuffer[i]>0.0)
{
CurrentBottomBuffer[i]=0.0;
if(MACDBuffer[i-1]!=EMPTY_VALUE)
{
if(MACDBuffer[i-1]<MACDBuffer[i])
if(CurrentTopBuffer[i]<high[i] || CurrentTopBuffer[i]==0.0)
CurrentTopBuffer[i]=high[i];
}
}
else
{
CurrentTopBuffer[i]=0.0;
if(MACDBuffer[i-1]!=EMPTY_VALUE)
{
if(MACDBuffer[i-1]>MACDBuffer[i])
if(CurrentBottomBuffer[i]>low[i] || CurrentBottomBuffer[i]==0.0)
CurrentBottomBuffer[i]=low[i];
}
}
}
//--- return the prev_calculated value for the next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Filling indicator buffers from the iMACD indicator |
//+------------------------------------------------------------------+
bool FillArraysFromBuffers(double &macd_buffer[], // indicator buffer of MACD values
double &signal_buffer[], // indicator buffer of the signal line of MACD
int ind_handle, // handle of the iMACD indicator
int amount // number of copied values
)
{
//--- reset error code
ResetLastError();
//--- fill a part of the iMACDBuffer array with values from the indicator buffer that has 0 index
if(CopyBuffer(ind_handle,0,0,amount,macd_buffer)<0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(false);
}
//--- fill a part of the SignalBuffer array with values from the indicator buffer that has index 1
if(CopyBuffer(ind_handle,1,0,amount,signal_buffer)<0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(false);
}
//--- everything is fine
return(true);
}
//+------------------------------------------------------------------+
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
---