//+------------------------------------------------------------------+
//| speed.mq5 |
//| Copyright 2021, Vladimir M. |
//| mikh.vl@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Vladimir M."
#property link "mikh.vl@gmail.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 4
//--- plot Speed
#property indicator_label3 "Speed UP"
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_color3 clrGreen
#property indicator_style3 STYLE_SOLID
#property indicator_width3 2
#property indicator_level3 0.0
#property indicator_label4 "Speed DOWN"
#property indicator_type4 DRAW_HISTOGRAM
#property indicator_color4 clrOrange
#property indicator_style4 STYLE_SOLID
#property indicator_width4 2
#property indicator_level4 0.0
#property indicator_label1 "Speed MAX"
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_level1 0.0
#property indicator_label2 "Speed MIN"
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_color2 clrBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
#property indicator_level2 0.0
//--- input parameters
input int TimePeriod = 1; // Period, sec
//--- indicator buffers
double SpeedBufferUP[],
SpeedBufferDOWN[],
SpeedBufferMAX[],
SpeedBufferMIN[];
double LastSpeed = 0.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(2, SpeedBufferUP, INDICATOR_DATA);
SetIndexBuffer(3, SpeedBufferDOWN, INDICATOR_DATA);
SetIndexBuffer(0, SpeedBufferMAX, INDICATOR_DATA);
SetIndexBuffer(1, SpeedBufferMIN, INDICATOR_DATA);
EventSetTimer(TimePeriod);
//---
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
static bool fstart;
//---
if(!fstart)
{
ArrayInitialize(SpeedBufferUP, 0.0);
ArrayInitialize(SpeedBufferDOWN, 0.0);
ArrayInitialize(SpeedBufferMAX, 0.0);
ArrayInitialize(SpeedBufferMIN, 0.0);
fstart = true;
}
double sp = LastSpeed;
if(sp > 0)
SpeedBufferUP[rates_total - 1] = sp;
else if(sp < 0)
SpeedBufferDOWN[rates_total - 1] = sp;
if(sp > SpeedBufferMAX[rates_total - 1])
SpeedBufferMAX[rates_total - 1] = sp;
if(sp < SpeedBufferMIN[rates_total - 1])
SpeedBufferMIN[rates_total - 1] = sp;
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
static double LastPrice;
double CurrentPrice;
//---
CurrentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
if(LastPrice == 0)
LastPrice = CurrentPrice;
LastSpeed = (CurrentPrice - LastPrice) / _Point;
LastPrice = CurrentPrice;
}
//+------------------------------------------------------------------+
Comments