Could you please help me convert this pine script to mql4? Some of this like NZ function is very complex to me. - page 2

 
Vladislav Boyko #:

This is not an example of how to do it. It's just my funny story :)

Thanks .

Won't this be the same result (High-Low) unless there is a gap between the candles ?

return(MathMax(high - low, MathMax(MathAbs(high - prevClose), MathAbs(low - prevClose))));
 
Lorentzos Roussos #:

i see , what is src ? 

Edit : i assume it can only be price so here goes : 

Thank you so much, very much appreciated bro 
 
Runny Picasso #:
Thank you so much, very much appreciated bro 

anytime

 
Lorentzos Roussos #:

Thanks .

Won't this be the same result (High-Low) unless there is a gap between the candles ?

This has been copied from the documentation "as is". In order for the calculation results to match.

RETURNS
True range. It is max(high - low, abs(high - close[1]), abs(low - close[1]))


 
Vladislav Boyko #:

This has been copied from the documentation "as is". In order for the calculation results to match.


You are right actually , there were atr issues . Here is the atr i used :

      raw_atr[i]=(MathMax(High[i]-Low[i],MathMax(MathAbs(High[i]-Close[i+1]),MathAbs(Low[i]-Close[i+1])))+(((double)atr_period)-1.0)*raw_atr[i+1])/((double)atr_period);

And there were multiple errors in the  code too , here is the full fix (although i'm skeptical about where the arrows appear) Thanks @Vladislav Boyko

edits for better search : 

r-trend for metatrader , rtrend for mt4 

//R-TREND FOR META TRADER
#property copyright "GO TO DISCUSSION"
#property link "https://www.mql5.com/en/forum/439178"
#property strict
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   2
//--- plot Label1
#property indicator_label1  "trend"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrCrimson
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//--- plot Label1
#property indicator_label2  "trend2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrYellowGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  3
enum _atr_mode{
atr_rolling=0,//rolling atr
atr_mt4=1//mt4 atr
};
input int hl_period=2;//HL Period (p1)
input _atr_mode atr_mode=atr_rolling;//Atr Mode
input int atr_period=2;//Atr Period (p2)
input int atr_ema_period=20;//Ema Atr Period
input double f=0.1;//coefficient
input ENUM_APPLIED_PRICE source_price=PRICE_CLOSE;//source price
input string note_a="<!>";//Arrow Settings 
input color buy_color=clrYellowGreen;//Buy color
input color sell_color=clrCrimson;//Sell color
input int arrow_width=2;//Arrow Width
//arrows https://docs.mql4.com/constants/objectconstants/wingdings
input int buy_arrowcode=233;//Buy arrow code
input int sell_arrowcode=234;//Sell arrow code

//--- indicator buffers
double         trend[],trend2[],raw_atr[],atr[],atrEma[],m[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int deflekt=0;
string system_tag="RTREND_ARROWS_";
uint arrowid=0;
int OnInit()
  {
//--- indicator buffers mapping
   ObjectsDeleteAll(ChartID(),system_tag);
   arrowid=0;
   SetIndexBuffer(0,trend);
   SetIndexBuffer(1,trend2);
   SetIndexEmptyValue(1,0.0);
   SetIndexBuffer(2,atr);SetIndexStyle(2,DRAW_NONE,EMPTY,EMPTY,clrNONE);
   SetIndexBuffer(3,atrEma);SetIndexStyle(3,DRAW_NONE,EMPTY,EMPTY,clrNONE);
   SetIndexBuffer(4,m);SetIndexStyle(4,DRAW_NONE,EMPTY,EMPTY,clrNONE);
   SetIndexBuffer(5,raw_atr);SetIndexStyle(5,DRAW_NONE,EMPTY,EMPTY,clrNONE);
   deflekt=(int)MathMax(atr_ema_period,MathMax(atr_period,hl_period));
//---
   return(INIT_SUCCEEDED);
  }
void reset(){
  ArrayFill(trend,0,ArraySize(trend),0.0);
  ArrayFill(trend2,0,ArraySize(trend2),0.0);
  ArrayFill(atr,0,ArraySize(atr),0.0);
  ArrayFill(raw_atr,0,ArraySize(raw_atr),0.0);
  ObjectsDeleteAll(ChartID(),system_tag);
  arrowid=0;
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
  int calcs=rates_total-prev_calculated;
  int from=calcs;
  int bottom=rates_total-deflekt*2;
  if(from>deflekt){
  reset();
  from=bottom;
  }
  for(int i=from;i>=0;i--)
  {
  //atr = 0.1 * nz(ta.atr(2)[1], ta.atr(2))
    /*
    if Not A Number it is replaced with the current atr otherwise use the previous atr
    But we have taken care of that by starting after 2 times the deflekt value so its hard to 
    get an empty value so , just get the atr
    */
    //mt4 mode 
      if(atr_mode==atr_mt4){
      atr[i]=f*iATR(_Symbol,_Period,atr_period,i+1);
      }else{
      raw_atr[i]=(MathMax(High[i]-Low[i],MathMax(MathAbs(High[i]-Close[i+1]),MathAbs(Low[i]-Close[i+1])))+(((double)atr_period)-1.0)*raw_atr[i+1])/((double)atr_period);
      atr[i]=f*raw_atr[i+1];
      }
  //atr_ma = ta.ema(atr, 20)
    /*
    we are using an ema on the atr buffer , we need to start when we have atr_ema_period amount of 
    atr stored so :
    */
    if(i<=(bottom-atr_ema_period)){
    atrEma[i]=maOnArray(atr,i,atr_ema_period,ma_ema,true,(bottom-atr_ema_period)-i,atrEma[i+1]);
    }
  //m = (ta.highest(2) + ta.lowest(2)) / 2
    /*
    the highest and lowest of the 2 last bars by 2
    */
    m[i]=(High[iHighest(_Symbol,_Period,MODE_HIGH,hl_period,i)]+Low[iLowest(_Symbol,_Period,MODE_LOW,hl_period,i)])/2.0;
  //what you want to plot was not clear but these are the calcuations for the components.
  //if you want to use them in a formula use the [i] index alongside them
  //example :
    //formula[i]=m[i]*atrEma[i]*atr[i];//this is an example i don't know what you wanted to do with these buffers
    double src=get_price(Open[i],High[i],Low[i],Close[i],source_price);
    trend[i]=(src>trend[i+1]+2.0*atr[i])?trend[i+1]+atr[i]:(src<trend[i+1]-2.0*atr[i])?trend[i+1]-atr[i]:trend[i+1];
    //trend is color red and always fills , if we find trend>trend[1] or equal to a previous green
    //we then display the green buffer which is trend2
      //so most likely case first : 
        if(trend[i]>trend[i+1]){
        trend2[i]=trend[i];//green buffer appears
        }else if(trend[i]==trend[i+1]){
        //and trend 2 is not 0.0 on [i+1]
          if(trend2[i+1]==trend[i+1]){
          trend2[i]=trend[i];
          }
        }
      //buy  arrow
        //was red is green
        if(trend[i+1]!=trend2[i+1]&&trend[i]==trend2[i]){
        create_arrow(system_tag,0,arrowid,buy_arrowcode,buy_color,arrow_width,Low[i],Time[i],ANCHOR_TOP);
        } 
      //sell arrow 
        //was green is red 
        else if(trend[i+1]==trend2[i+1]&&trend[i]!=trend2[i]){
        create_arrow(system_tag,0,arrowid,sell_arrowcode,sell_color,arrow_width,High[i],Time[i],ANCHOR_BOTTOM);
        }
    
  }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//create arrow 
void create_arrow(string _tag,
                  int    subwin,
                  uint   &_counter,
                  int    _arrowcode,
                  color  _col,
                  int    _width,
                  double _price,
                  datetime _time,
                  ENUM_ARROW_ANCHOR _arrow_anchor){
_counter++;
string name=_tag+IntegerToString(_counter);
ObjectCreate(ChartID(),name,OBJ_ARROW,subwin,_time,_price);
ObjectSetInteger(ChartID(),name,OBJPROP_ANCHOR,_arrow_anchor);
ObjectSetInteger(ChartID(),name,OBJPROP_COLOR,_col);
ObjectSetInteger(ChartID(),name,OBJPROP_WIDTH,_width);
ObjectSetInteger(ChartID(),name,OBJPROP_ARROWCODE,_arrowcode);
}
//get price
double get_price(double o,double h,double l,double c,ENUM_APPLIED_PRICE _src_price){
if(_src_price==PRICE_CLOSE){return(c);}
else if(_src_price==PRICE_HIGH){return(h);}
else if(_src_price==PRICE_LOW){return(l);}
else if(_src_price==PRICE_MEDIAN){return((h+l)/2.00);}
else if(_src_price==PRICE_OPEN){return(o);}
else if(_src_price==PRICE_TYPICAL){return((h+l+c)/3.0);}
else if(_src_price==PRICE_WEIGHTED){return((h+l+c+c)/4.0);}
return(0.0);
}
/* ma of array */
enum ma_method{
ma_sma=0,//sma
ma_ema=1,//ema
ma_smma=2,//smma
ma_lwma=3//lwma
};
double maOnArray(const double &which_array[],
                    int where,//start from where 
                    int period,
              ma_method mode,
                   bool is_series,
                    int calc_index,//this measures which calculation this is starting from 0
                 double previous_ma){//and this receives the previous value 
double result=0.0;
//starting point 
  int from=0;
  int step=0;
  int until=where;
  if(!is_series){
  from=where-period+1;
  step=1;
  }
  else{
  from=where+period-1;
  step=-1;
  }
if(until>=0&&until<ArraySize(which_array)&&from>=0&&from<ArraySize(which_array)){
  //sma or first ema or first smma 
    if(mode==ma_sma||(mode==ma_ema&&calc_index==0)||(mode==ma_smma&&calc_index==0)){
    int i=from-step;
    while(i!=until)
      {
      i+=step;
      result+=which_array[i];
      }
    result/=((double)period);
    }
  //ema non first 
    else if(mode==ma_ema){
    double w=2.00/((double)period+1.0);
    result=which_array[until]*w+previous_ma*(1-w);
    }  
  //smma non first 
    else if(mode==ma_smma){
    result=((previous_ma*((double)period))-previous_ma+which_array[until])/((double)period);
    }
  //lwma
    else if(mode==ma_lwma){
    int i=from-step;
    double divider=0.0;
    int weight=0.0;
    while(i!=until)
      {
      i+=step;
      weight+=1.0;
      result+=which_array[i]*weight;
      divider+=weight;
      }
    result/=divider;    
    }
  }
return(result);
}


Files:
 
Lorentzos Roussos #: Won't this be the same result (High-Low) unless there is a gap between the candles ?
return(MathMax(high - low, MathMax(MathAbs(high - prevClose), MathAbs(low - prevClose))));
  1. Yes, that is the definition of “true range” vs “range”. It takes gaps into account. You are computing ATR, not AR.

  2. Simplified
    return MathMax(high, prevClose) - MathMin(low, prevClose);

 
William Roeder #:
  1. Yes, that is the definition of “true range” vs “range”. It takes gaps into account. You are computing ATR, not AR.

  2. Simplified

I didn't know that . Thanks @William Roeder , happy new year .

Reason: