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_buffers6#property indicator_plots2//--- plot Label1#property indicator_label1"trend"#property indicator_type1DRAW_LINE#property indicator_color1clrCrimson#property indicator_style1STYLE_SOLID#property indicator_width13//--- plot Label1#property indicator_label2"trend2"#property indicator_type2DRAW_LINE#property indicator_color2clrYellowGreen#property indicator_style2STYLE_SOLID#property indicator_width23enum _atr_mode{
atr_rolling=0,//rolling atr
atr_mt4=1//mt4 atr
};
inputint hl_period=2;//HL Period (p1)input _atr_mode atr_mode=atr_rolling;//Atr Modeinputint atr_period=2;//Atr Period (p2)inputint atr_ema_period=20;//Ema Atr Periodinputdouble f=0.1;//coefficientinputENUM_APPLIED_PRICE source_price=PRICE_CLOSE;//source priceinputstring note_a="<!>";//Arrow Settings inputcolor buy_color=clrYellowGreen;//Buy colorinputcolor sell_color=clrCrimson;//Sell colorinputint arrow_width=2;//Arrow Width//arrows https://docs.mql4.com/constants/objectconstants/wingdingsinputint buy_arrowcode=233;//Buy arrow codeinputint sell_arrowcode=234;//Sell arrow code//--- indicator buffersdouble trend[],trend2[],raw_atr[],atr[],atrEma[],m[];
//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+int deflekt=0;
string system_tag="RTREND_ARROWS_";
uint arrowid=0;
intOnInit()
{
//--- indicator buffers mappingObjectsDeleteAll(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 |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &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 buffersdouble 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
}elseif(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 greenif(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 elseif(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 callreturn(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 pricedouble get_price(double o,double h,double l,double c,ENUM_APPLIED_PRICE _src_price){
if(_src_price==PRICE_CLOSE){return(c);}
elseif(_src_price==PRICE_HIGH){return(h);}
elseif(_src_price==PRICE_LOW){return(l);}
elseif(_src_price==PRICE_MEDIAN){return((h+l)/2.00);}
elseif(_src_price==PRICE_OPEN){return(o);}
elseif(_src_price==PRICE_TYPICAL){return((h+l+c)/3.0);}
elseif(_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(constdouble &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 0double 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 elseif(mode==ma_ema){
double w=2.00/((double)period+1.0);
result=which_array[until]*w+previous_ma*(1-w);
}
//smma non first elseif(mode==ma_smma){
result=((previous_ma*((double)period))-previous_ma+which_array[until])/((double)period);
}
//lwmaelseif(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);
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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 ?
i see , what is src ?
Edit : i assume it can only be price so here goes :
Thank you so much, very much appreciated bro
anytime
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.
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 :
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
Yes, that is the definition of “true range” vs “range”. It takes gaps into account. You are computing ATR, not AR.
Yes, that is the definition of “true range” vs “range”. It takes gaps into account. You are computing ATR, not AR.
I didn't know that . Thanks @William Roeder , happy new year .