Files
mql5_indicators_mt5_part1/AB - indicator for MetaTrader 5/ab.mq5
T

123 lines
9.7 KiB
Plaintext

//+------------------------------------------------------------------+
//| AB.mq5 |
//| Copyright 2018, MetaQuotes Software Corp. |
//| https://mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link "https://mql5.com"
#property version "1.00"
#property description "Acceleration Bands indicator"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 2
//--- plot Top
#property indicator_label1 "Top"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot Bottom
#property indicator_label2 "Bottom"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrGreen
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- input parameters
input uint InpPeriod = 20; // Period
input double InpFactor = 0.001; // Factor
//--- indicator buffers
double BufferTop[];
double BufferBottom[];
double BufferUP[];
double BufferDN[];
//--- global variables
double factor;
int period_ind;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- set global variables
period_ind=int(InpPeriod<1 ? 1 : InpPeriod);
factor=fabs(InpFactor);
//--- indicator buffers mapping
SetIndexBuffer(0,BufferTop,INDICATOR_DATA);
SetIndexBuffer(1,BufferBottom,INDICATOR_DATA);
SetIndexBuffer(2,BufferUP,INDICATOR_CALCULATIONS);
SetIndexBuffer(3,BufferDN,INDICATOR_CALCULATIONS);
//--- setting indicator parameters
IndicatorSetString(INDICATOR_SHORTNAME,"Acceleration Bands ("+(string)period_ind+","+DoubleToString(factor,4)+")");
IndicatorSetInteger(INDICATOR_DIGITS,Digits());
//--- setting plot buffer parameters
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,period_ind);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,period_ind);
//--- setting buffer arrays as timeseries
ArraySetAsSeries(BufferTop,true);
ArraySetAsSeries(BufferBottom,true);
ArraySetAsSeries(BufferUP,true);
ArraySetAsSeries(BufferDN,true);
//---
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[])
{
//--- Установка массивов буферов как таймсерий
ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
//--- Проверка и расчёт количества просчитываемых баров
if(rates_total<fmax(period_ind,4) || Point()==0) return 0;
//--- Проверка и расчёт количества просчитываемых баров
int limit=rates_total-prev_calculated;
if(limit>1)
{
limit=rates_total-1;
ArrayInitialize(BufferTop,EMPTY_VALUE);
ArrayInitialize(BufferBottom,EMPTY_VALUE);
ArrayInitialize(BufferUP,0);
ArrayInitialize(BufferDN,0);
}
//--- Расчёт индикатора
for(int i=limit; i>=0 && !IsStopped(); i--)
{
BufferUP[i]=high[i]*(1.0+2.0*(2000.0*factor*(high[i]-low[i])/(high[i]+low[i])));
BufferDN[i]=high[i]*(1.0-2.0*(2000.0*factor*(high[i]-low[i])/(high[i]+low[i])));
BufferTop[i]=GetSMA(rates_total,i,period_ind,BufferUP);
BufferBottom[i]=GetSMA(rates_total,i,period_ind,BufferDN);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
double GetSMA(const int rates_total,const int index,const int period,const double &price[],const bool as_series=true)
{
//---
double result=0.0;
//--- check position
bool check_index=(as_series ? index<=rates_total-period-1 : index>=period-1);
if(period<1 || !check_index)
return 0;
//--- calculate value
for(int i=0; i<period; i++)
result=result+(as_series ? price[index+i]: price[index-i]);
//---
return(result/period);
}
//+------------------------------------------------------------------+