146 lines
No EOL
8.7 KiB
MQL5
146 lines
No EOL
8.7 KiB
MQL5
|
|
//+------------------------------------------------------------------
|
|
#property copyright "© mladen, 2019"
|
|
#property link "mladenfx@gmail.com"
|
|
#property description "Simple Moving Average"
|
|
//+------------------------------------------------------------------
|
|
#property indicator_chart_window
|
|
#property indicator_buffers 4
|
|
#property indicator_plots 2
|
|
|
|
#property indicator_label1 "TrailingSL-upper"
|
|
#property indicator_type1 DRAW_COLOR_LINE
|
|
#property indicator_color1 clrOlive
|
|
#property indicator_style1 STYLE_SOLID
|
|
#property indicator_width1 2
|
|
|
|
//EMA
|
|
#property indicator_label2 "TrailingSL-lower"
|
|
#property indicator_type2 DRAW_COLOR_LINE
|
|
#property indicator_color2 clrOlive
|
|
#property indicator_style2 STYLE_SOLID
|
|
#property indicator_width2 2
|
|
|
|
|
|
//trigger buy
|
|
#property indicator_label7 "ColorArrow"
|
|
#property indicator_type7 DRAW_COLOR_ARROW
|
|
#property indicator_color7 clrGreen
|
|
#property indicator_style7 STYLE_SOLID
|
|
#property indicator_width7 10
|
|
|
|
//
|
|
//
|
|
//
|
|
|
|
input int InpAtrPeriod = 14; // Period
|
|
input double InpAtrMultiplier = 1.5;
|
|
|
|
double upper[],lower[];
|
|
|
|
int ExtPeriodATR;
|
|
|
|
//------------------------------------------------------------------
|
|
//
|
|
//------------------------------------------------------------------
|
|
//
|
|
//
|
|
//
|
|
|
|
int OnInit()
|
|
{
|
|
if(InpAtrPeriod<=0)
|
|
{
|
|
ExtPeriodATR=14;
|
|
printf("Incorrect input parameter InpAtrPeriod = %d. Indicator will use value %d for calculations.",InpAtrPeriod,ExtPeriodATR);
|
|
}
|
|
else ExtPeriodATR=InpAtrPeriod;
|
|
|
|
SetIndexBuffer(0,upper,INDICATOR_DATA);
|
|
SetIndexBuffer(2,lower,INDICATOR_DATA);
|
|
|
|
IndicatorSetString(INDICATOR_SHORTNAME,"TrailingSL ("+(string)InpAtrMultiplier+"*ATR)");
|
|
return (INIT_SUCCEEDED);
|
|
}
|
|
void OnDeinit(const int reason)
|
|
{
|
|
}
|
|
|
|
//------------------------------------------------------------------
|
|
//
|
|
//------------------------------------------------------------------
|
|
//
|
|
//
|
|
//
|
|
|
|
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=prev_calculated-1; if (i<0) i=0;
|
|
int limit=0;
|
|
|
|
//--- indicator buffers
|
|
static double ExtATRBuffer[];
|
|
static double ExtTRBuffer[];
|
|
static double ExtTrailingSLUpperBuffer[];
|
|
|
|
|
|
|
|
if(prev_calculated==0)
|
|
{
|
|
ArrayResize(ExtATRBuffer,rates_total);
|
|
ArrayResize(ExtTRBuffer,rates_total);
|
|
ArrayResize(ExtTrailingSLUpperBuffer,rates_total);
|
|
|
|
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;
|
|
|
|
for (; i<rates_total && !_StopFlag; i++)
|
|
{
|
|
//val[i] = _sma.calculate(getPrice(inpPrice,open,high,low,close,i),i,rates_total);
|
|
//valc[i] = (i>0) ? (val[i]>val[i-1]) ? 0 : (val[i]<val[i-1]) ? 1 : valc[i-1] : 0;
|
|
|
|
|
|
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;
|
|
|
|
if(Close[i]>Open[i]){
|
|
upper[i] = Close[i] + ExtATRBuffer[i]*InpAtrMultiplier;
|
|
lower[i] = Open[i] - ExtATRBuffer[i]*InpAtrMultiplier;
|
|
}else{
|
|
upper[i] = Open[i] + ExtATRBuffer[i]*InpAtrMultiplier;
|
|
lower[i] = Close[i] - ExtATRBuffer[i]*InpAtrMultiplier;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return (i);
|
|
} |