Files
mql5_indicators_mt5_part3/MPO - indicator for MetaTrader 5/mpo.mq5
T

132 lines
11 KiB
Plaintext

//+------------------------------------------------------------------+
//| MPO.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 "Midpoint Oscillator"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2
//--- plot MP
#property indicator_label1 "Midpoint"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot SM
#property indicator_label2 "Signal"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- input parameters
input uint InpPeriod = 26; // Period
input uint InpPeriodSm = 9; // Smoothing period
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Smoothing method
//--- indicator buffers
double BufferMP[];
double BufferSM[];
//--- global variables
int period_mp;
int period_sm;
int weight_sum;
//--- includes
#include <MovingAverages.mqh>
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- set global variables
period_mp=int(InpPeriod<1 ? 1 : InpPeriod);
period_sm=int(InpPeriodSm<2 ? 2 : InpPeriodSm);
//--- indicator buffers mapping
SetIndexBuffer(0,BufferMP,INDICATOR_DATA);
SetIndexBuffer(1,BufferSM,INDICATOR_DATA);
//--- setting indicator parameters
IndicatorSetString(INDICATOR_SHORTNAME,"MPO ("+(string)period_mp+","+(string)period_sm+")");
IndicatorSetInteger(INDICATOR_DIGITS,Digits());
//--- setting buffer arrays as timeseries
ArraySetAsSeries(BufferMP,true);
ArraySetAsSeries(BufferSM,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);
ArraySetAsSeries(close,true);
//--- Проверка и расчёт количества просчитываемых баров
if(rates_total<4) return 0;
//--- Проверка и расчёт количества просчитываемых баров
int limit=rates_total-prev_calculated;
if(limit>1)
{
limit=rates_total-1;
ArrayInitialize(BufferMP,EMPTY_VALUE);
ArrayInitialize(BufferSM,EMPTY_VALUE);
}
//--- Расчёт MP
for(int i=limit; i>=0 && !IsStopped(); i--)
{
int bl=Lowest(period_mp,i);
int bh=Highest(period_mp,i);
if(bl==WRONG_VALUE || bh==WRONG_VALUE)
continue;
double min=low[bl];
double max=high[bh];
BufferMP[i]=(max!=min ? 100.0*(2.0*close[i]-max-min)/(max-min) : 0);
}
//--- Сглаживание
switch(InpMethod)
{
case MODE_EMA : ExponentialMAOnBuffer(rates_total,prev_calculated,period_mp,period_sm,BufferMP,BufferSM); break;
case MODE_SMMA : SmoothedMAOnBuffer(rates_total,prev_calculated,period_mp,period_sm,BufferMP,BufferSM); break;
case MODE_LWMA : LinearWeightedMAOnBuffer(rates_total,prev_calculated,period_mp,period_sm,BufferMP,BufferSM,weight_sum); break;
//---MODE_SMA
default : SimpleMAOnBuffer(rates_total,prev_calculated,period_mp,period_sm,BufferMP,BufferSM); break;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Возвращает индекс максимального значения таймсерии High |
//+------------------------------------------------------------------+
int Highest(const int count,const int start)
{
double array[];
ArraySetAsSeries(array,true);
return(CopyHigh(Symbol(),PERIOD_CURRENT,start,count,array)==count ? ArrayMaximum(array)+start : WRONG_VALUE);
}
//+------------------------------------------------------------------+
//| Возвращает индекс минимального значения таймсерии Low |
//+------------------------------------------------------------------+
int Lowest(const int count,const int start)
{
double array[];
ArraySetAsSeries(array,true);
return(CopyLow(Symbol(),PERIOD_CURRENT,start,count,array)==count ? ArrayMinimum(array)+start : WRONG_VALUE);
return WRONG_VALUE;
}
//+------------------------------------------------------------------+