You are on page 1of 3

//+------------------------------------------------------------------+

//| TrendTracker.mq5 |
//| Copyright 2020, Phillipe S. Scofield |
//| phillipe.s.scofield@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Phillipe S. Scofield"
#property link "phillipe.s.scofield@gmail.com"
#property version "1.00"
#property indicator_separate_window
//------------------------------------------------------------------
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_label1 "TrendTracker"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrSilver
#property indicator_width1 2

//
//---
//

input int inpBars = 5; // Barras contadas


input int inpPeriod = 21; // Periodo da MA
input ENUM_APPLIED_PRICE inpPrice = PRICE_CLOSE; // Preço utilizado nas barras
input ENUM_MA_METHOD inpMaMode = MODE_EMA; // Tipo da MA

//
//---
//

double trend[];

double ma_Buffer[],stdDev_Buffer[];
int ma_Handle,stdDev_Handle;
//------------------------------------------------------------------
// Custom indicator initialization function
//------------------------------------------------------------------

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnInit()
{
//
//---- indicator buffers mapping
//
SetIndexBuffer(0,trend,INDICATOR_DATA);
SetIndexBuffer(1,ma_Buffer,INDICATOR_CALCULATIONS);
IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- sets first bar from what index will be drawn
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,MathMax(inpPeriod,inpBars)-1);
//--- name for DataWindow and indicator subwindow label
//
//----
//
ma_Handle=iMA(_Symbol,_Period,inpPeriod,0,inpMaMode,PRICE_CLOSE);
stdDev_Handle=iStdDev(_Symbol,_Period,inpPeriod,0,inpMaMode,PRICE_CLOSE);
IndicatorSetString(INDICATOR_SHORTNAME,"Trend Tracker ("+(string)inpPeriod+","+
(string)inpBars+")");
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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[])
{

int i,limit;
//--- check for bars count
if(rates_total<2*MathMax(inpPeriod+1,inpBars+1))
return(0);// not enough bars for calculation
//--- not all data may be calculated
int calculated=BarsCalculated(ma_Handle);
if(calculated<rates_total)
{
Print("Not all data of ma_Handle is calculated (",calculated,"bars ).
Error",GetLastError());
return(0);
}
calculated=BarsCalculated(stdDev_Handle);
if(calculated<rates_total)
{
Print("Not all data of stdDev_Handle is calculated (",calculated,"bars ).
Error",GetLastError());
return(0);
}
//--- we can copy not all data
int to_copy;
if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
else
{
to_copy=rates_total-prev_calculated;
if(prev_calculated>0) to_copy++;
}
//---- get ma buffers
if(IsStopped()) return(0); //Checking for stop flag
if(CopyBuffer(ma_Handle,0,0,rates_total,ma_Buffer)<=0 ||
CopyBuffer(stdDev_Handle,0,0,rates_total,stdDev_Buffer)<=0)
{
Print("getting ma_Handle or stdDev_Handle is failed! Error",GetLastError());
return(0);
}
//--- first calculation or number of bars was changed
if(prev_calculated<MathMax(inpPeriod+1,inpBars+1))
limit=MathMax(inpPeriod+1,inpBars+1);
else limit=prev_calculated-1;
double price=0;
//--- the main loop of calculations
for(i=limit; i<rates_total && !IsStopped(); i++)
{
trend[i]=0;//close[i]-ma_Buffer[i];
for(int j=i; j>i-inpBars; j--)
{
switch(inpPrice)
{
case PRICE_CLOSE:
price = close[j];
break;

case PRICE_OPEN:
price = open[j];
break;

case PRICE_HIGH:
price = high[j];
break;

case PRICE_LOW:
price = low[j];
break;

case PRICE_MEDIAN:
price = (high[j]+low[j])/2.0;
break;

case PRICE_TYPICAL:
price = (high[j]+low[j]+close[j])/3.0;
break;

case PRICE_WEIGHTED:
price = (high[j]+low[j]+close[j]+close[j])/4.0;
break;
default :
price = 0;
}
trend[i]=trend[i] + (price-ma_Buffer[j]);
}
trend[i]=trend[i]/(stdDev_Buffer[i]);
}
//--- return value of prev_calculated for next call
return(rates_total);
}

//+------------------------------------------------------------------+

You might also like