//------------------------------------------------------------------ #property indicator_chart_window #property indicator_buffers 6 #property indicator_plots 3 //--- plot Upwards #property indicator_label1 "Upwards" #property indicator_type1 DRAW_ARROW #property indicator_color1 clrSteelBlue #property indicator_style1 STYLE_SOLID #property indicator_width1 2 //--- plot Topdown #property indicator_label2 "Topdown" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrRed #property indicator_style2 STYLE_SOLID #property indicator_width2 2 #property indicator_label3 "Double smoothed EMA" #property indicator_type3 DRAW_COLOR_LINE #property indicator_color3 clrDarkGray,clrDeepPink,clrLimeGreen #property indicator_width3 2 //--- input parameters input double inpPeriod = 25; // Period input ENUM_APPLIED_PRICE inpPrice = PRICE_CLOSE; // Price input int arr_code_up = 172; // Arrow Up Code input int arr_code_dn = 172; // Arrow Down Code //--- indicator buffers double UpwardsBuffer[]; double TopdownBuffer[]; double val[],valc[],work[],atr[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping //--- setting a code from the Wingdings charset as the property of PLOT_ARROW PlotIndexSetInteger(0,PLOT_ARROW,arr_code_up); PlotIndexSetInteger(1,PLOT_ARROW,arr_code_dn); //--- Set the vertical shift of arrows in pixels PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,10); PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-10); SetIndexBuffer(0,UpwardsBuffer,INDICATOR_DATA); SetIndexBuffer(1,TopdownBuffer,INDICATOR_DATA); PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE); PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE); SetIndexBuffer(2,val,INDICATOR_DATA); SetIndexBuffer(3,valc,INDICATOR_COLOR_INDEX); SetIndexBuffer(4,work,INDICATOR_CALCULATIONS); SetIndexBuffer(5,atr,INDICATOR_CALCULATIONS); //--- indicator short name assignment IndicatorSetString(INDICATOR_SHORTNAME,"ATR adaptive double smoothed EMA ("+(string)inpPeriod+")"); //--- return (INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator de-initialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| 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[]) { if(Bars(_Symbol,_Period)=0; k++) atr[i] += MathMax(high[i-k],close[i-k-1])-MathMin(low[i-k],close[i-k-1]); atr[i] /= inpPeriod; int _start = MathMax(i-(int)inpPeriod+1,0); double _max = atr[ArrayMaximum(atr,_start,(int)inpPeriod)]; double _min = atr[ArrayMinimum(atr,_start,(int)inpPeriod)]; double _coeff = (_min!=_max) ? 1-(atr[i]-_min)/(_max-_min) : 0.5; double _alpha = 2.0/(1.0+MathSqrt(MathMax((1+inpPeriod*(_coeff+1.0)/2.0),1))); work[i] = (i>0) ? work[i-1] + _alpha*(price-work[i-1]) : price; val[i] = (i>0) ? val[i-1] + _alpha*(work[i]-val[i-1]) : work[i]; valc[i] = (i>0) ? (val[i]>val[i-1]) ? 2 : (val[i]rates_total-15; j--) { _atr += high[j] - low[j]; } _atr /= 14.0; if (i > 1) { TopdownBuffer[i-1] = (valc[i] < valc[i-1]-0.1) ? MathMax(val[i],high[i-1])+_atr*0.2 : EMPTY_VALUE; UpwardsBuffer[i-1] = (valc[i] > valc[i-1]+0.1) ? MathMin(val[i],low[i-1]) -_atr*0.2 : EMPTY_VALUE; } } return(rates_total); } //+------------------------------------------------------------------+ //| Custom functions | //+------------------------------------------------------------------+ void MakeArrows(int rates_total) { if (IsNewBar()) { for (int i=rates_total-1; i>1; i--) { UpwardsBuffer[i-1] = (valc[i] > valc[i-1]+0.1) ? val[i] : EMPTY_VALUE; } } UpwardsBuffer[rates_total-1] = EMPTY_VALUE; TopdownBuffer[rates_total-1] = EMPTY_VALUE; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Custom functions | //+------------------------------------------------------------------+ double getPrice(ENUM_APPLIED_PRICE tprice,const double &open[],const double &close[],const double &high[],const double &low[],int i,int _bars) { if(i>=0) switch(tprice) { case PRICE_CLOSE: return(close[i]); case PRICE_OPEN: return(open[i]); case PRICE_HIGH: return(high[i]); case PRICE_LOW: return(low[i]); case PRICE_MEDIAN: return((high[i]+low[i])/2.0); case PRICE_TYPICAL: return((high[i]+low[i]+close[i])/3.0); case PRICE_WEIGHTED: return((high[i]+low[i]+close[i]+close[i])/4.0); } return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Bar refreshing function | //| true - new bar opened, false - not opened | //+------------------------------------------------------------------+ bool IsNewBar(bool reinitialize=false) { static datetime SavedTime=iTime(NULL,PERIOD_CURRENT,1); // TimeCurrent(); // iTime(NULL,PERIOD_CURRENT,0); datetime curTime =iTime(NULL,PERIOD_CURRENT,0); if (reinitialize) {SavedTime=0; return true;} if (curTime>SavedTime) { SavedTime=curTime; Print(" *** New Bar opened."); return(true); } else return(false); } //+------------------------------------------------------------------+