CojoneAlAire/TrailingStopATR.mq5

117 lines
4.3 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 14:46:52 +02:00
//+------------------------------------------------------------------+
//| ATR.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property description "Average True Range"
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 1
//EMA
#property indicator_label2 "TrailingStop"
#property indicator_type2 DRAW_COLOR_LINE
#property indicator_color2 clrOlive
#property indicator_style2 STYLE_SOLID
#property indicator_width2 10
//--- input parameters
input int InpAtrPeriod=14; // ATR period
//--- indicator buffers
double ExtATRBuffer[];
double ExtTRBuffer[];
double ExtTrailingSLUpperBuffer[];
//--- global variable
int ExtPeriodATR;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- check for input value
if(InpAtrPeriod<=0)
{
ExtPeriodATR=14;
printf("Incorrect input parameter InpAtrPeriod = %d. Indicator will use value %d for calculations.",InpAtrPeriod,ExtPeriodATR);
}
else ExtPeriodATR=InpAtrPeriod;
IndicatorSetString(INDICATOR_SHORTNAME,"TrailingSL");
//EMA1 & EMA2
SetIndexBuffer(0,ExtTrailingSLUpperBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ExtATRBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(2,ExtTRBuffer,INDICATOR_CALCULATIONS);
//--- sets first bar from what index will be drawn
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpAtrPeriod);
//---
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- initialization done
}
//+------------------------------------------------------------------+
//| Average True Range |
//+------------------------------------------------------------------+
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 &TickVolume[],
const long &Volume[],
const int &Spread[])
{
int i,limit;
//--- check for bars count
if(rates_total<=ExtPeriodATR)
return(0); // not enough bars for calculation
//--- preliminary calculations
if(prev_calculated==0)
{
ArrayInitialize(ExtTRBuffer, 0.0);
ArrayInitialize(ExtATRBuffer, 0.0);
ArrayInitialize(ExtTrailingSLUpperBuffer, 0.0);
ExtTRBuffer[0]=0.0;
ExtATRBuffer[0]=0.0;
//--- filling out the array of True Range values for each period
for(i=1;i<rates_total && !IsStopped();i++)
ExtTRBuffer[i]=MathMax(High[i],Close[i-1])-MathMin(Low[i],Close[i-1]);
//--- first AtrPeriod values of the indicator are not calculated
double firstValue=0.0;
for(i=1;i<=ExtPeriodATR;i++)
{
ExtATRBuffer[i]=0.0;
firstValue+=ExtTRBuffer[i];
}
//--- calculating the first value of the indicator
firstValue/=ExtPeriodATR;
ExtATRBuffer[ExtPeriodATR]=firstValue;
ExtTrailingSLUpperBuffer[ExtPeriodATR] = Close[ExtPeriodATR] + ExtATRBuffer[ExtPeriodATR]*1.5;
limit=ExtPeriodATR+1;
}
else limit=prev_calculated-1;
//--- the main loop of calculations
for(i=limit;i<rates_total && !IsStopped();i++)
{
ExtTRBuffer[i]=MathMax(High[i],Close[i-1])-MathMin(Low[i],Close[i-1]);
ExtATRBuffer[i]=ExtATRBuffer[i-1]+(ExtTRBuffer[i]-ExtTRBuffer[i-ExtPeriodATR])/ExtPeriodATR;
//ExtTrailingSLUpperBuffer[i] = MathMax(High[i],Close[i-1]) + ExtATRBuffer[i]*1.5;
ExtTrailingSLUpperBuffer[i]=1.2;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+