-
Bars is unreliable (a refresh/reconnect can change number of bars on chart)
volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
- I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
- A function should do one thing. Move your new bar code to OnTick; it doesn't belong in the ATR calculation.

- www.mql5.com
Do not use volume to detect a new bar, if the first tick of a new bar is missed, the code is broken.
How to make the function not to return 0 val when Vol >1 ??
double MTRd()
{
int i;
int n= MathRound((MTRperiod-1)/2);
double c;
if (Bars!=prevbars) // calculate only when the new bar appears
{
prevbars=Bars;
double Ext[];
ArrayResize(Ext,0); //reset old values
ArrayResize(Ext,MTRperiod); // MTRperiod input val =7
for(i=0; i<=MTRperiod; i++)
{
Ext[i]=High[i+1]-Low[i+1];
ArraySort(Ext);
c= ((Ext[n]+Ext[n-1]+Ext[n+1])/3);
}
}
return(c);
}
double MTRd()
{
int i;
int n= MathRound((MTRperiod-1)/2);
double c;
if (Bars!=prevbars) // calculate only when the new bar appears
{
prevbars=Bars;
double Ext[];
ArrayResize(Ext,0); //reset old values
ArrayResize(Ext,MTRperiod); // MTRperiod input val =7
for(i=0; i<=MTRperiod; i++)
{
Ext[i]=High[i+1]-Low[i+1];
ArraySort(Ext);
c= ((Ext[n]+Ext[n-1]+Ext[n+1])/3);
}
}
return(c);
}
Bars is unreliable to detect new bar. Please listen to other advices and don't give wrong example to people who are learning.
And if you start to argue about this you will be banned immediately.
Bars is unreliable to detect new bar. Please listen to other advices and don't give wrong example to people who are learning.
And if you start to argue about this you will be banned immediately.
Thank you for comment. I manage to deal with the "0"problem but i have new one. I build it on a base of my little ea helper ea that checks visually if the Mtr (media true range) function works fine. But it doesnt. I have checked it on M1 chart. It shows value much lower that it supposed to... plz help
extern double Risk = 1;
extern string Note_1 = "0 - Balance/1 - Equity/2 - Free Margin";
extern int Risk_Basis = 0;
input double MTRperiod=7;
//+------------------------------------------------------------------+
bool NewBar()//
{
static datetime lastbar = 0;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
//+------------------------------------------------------------------+
int
D_Factor = 1,
LotDigits = 1;
double
Pip,
Stoploss;
string
Obj_Prefix = "VOE_";
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Pip=Point;
if(Digits==3||Digits==5){D_Factor=10;Pip*=10;}
if (MarketInfo(Symbol(),MODE_LOTSTEP) == 1)LotDigits = 0;
CreateTextLable(Obj_Prefix+"Risk_Label","R%= ",Orange,1,10); //
CreateTextLable(Obj_Prefix+"Risk_Val",DoubleToStr(Risk,1),Orange,30,10);
int handle=FileOpen("Visual_Order_EA_Settings.csv",FILE_CSV|FILE_WRITE,";");
FileWrite(handle,Risk, Risk_Basis);
FileClose(handle);
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
CreateTextLable("MTR","MTR= ",Orange,1,55);
if (NewBar())
{
CreateTextLable("MTRval",DoubleToStr (MTR() ,5) ,Orange,60,55);
}
return(0);
}
//+------------------------------------------------------------------+
void CreateTextLable(string TextLableName,string Text,color l_Text_Color,int X,int Y)
{
int
TextSize = 10,
Corner = 0;
string
Font = "Times New Roman";
ObjectCreate(TextLableName, OBJ_LABEL, 0, TimeCurrent(), 0);
ObjectSet(TextLableName, OBJPROP_CORNER, Corner);
ObjectSet(TextLableName, OBJPROP_XDISTANCE, X);
ObjectSet(TextLableName, OBJPROP_YDISTANCE, Y);
ObjectSetText(TextLableName,Text,TextSize,Font,l_Text_Color);
}
//-----------------------------------------------------------------+
double MTR()
{
int i;
int n= MathRound((MTRperiod-1)/2); ////MTRperiod is odd val
double c;
double Ext[];
ArrayResize(Ext,0); //reset old values
ArrayResize(Ext,MTRperiod);
for(i=0; i<=MTRperiod; i++)
{
Ext[i]=High[i+1]-Low[i+1];
ArraySort(Ext);
c= ((Ext[n]+Ext[n-1]+Ext[n+1])/3);
}
return(c);
}
{
Ext[i]=High[i+1]-Low[i+1];
ArraySort(Ext);
c= ((Ext[n]+Ext[n-1]+Ext[n+1])/3);
- Do you understand that True Range is not H - L? That is just the bar's range.
- Why are you repeatedly sorting after adding each value? That is why the value you get is wrong. First sort moves random array values and then the next iterator you clobber something.
- Do you understand that the median is either the central value or the average of the two.
- Write functions that do one thing.double MTR(int length=0, int iBar=0){
if(length == 0) length = MTRperiod;
double Ext[]; ArrayResize(Ext,length);
while(length > 0){ double C = Close[iBar+1];
Ext[--length] = MathMax(High[iBar], C)
- MathMin( Low[iBar], C);
++iBar;
}
return Median(Ext);
}
//+------------------------------------------------------------------+
//| Median value |
//+------------------------------------------------------------------+
double MedianConstArray(const double&array[],int length=WHOLE_ARRAY,int iBeg=0){
double copied[]; length = ArrayCopy(copied, array, 0, iBeg, length);
return Median(copied, length, 0);
}
double Median(double& values[], int length=WHOLE_ARRAY, int iBeg=0){
if (length == WHOLE_ARRAY) length = ArraySize(values) - iBeg;
ArraySort(values, length, iBeg); // Even 0,1,2,3 Len=4 iMed=4/2=(2 and 1)/2
int iMed = length/2+iBeg; // Odd 0,1,2,3,4 Len=5 iMed=5/2=2
return length%2 == 0 ? (values[iMed] + values[iMed-1]) * 0.5 : values[iMed];
}

- en.wikipedia.org

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
How to make the function not to return 0 val when Vol >1 ??
{
int i;
int n= MathRound((MTRperiod-1)/2);
if (Volume[0]<1) // calculate only when the new bar appears
{
double c;
double Ext[];
ArrayResize(Ext,0); //reset old values
ArrayResize(Ext,MTRperiod); // MTRperiod input val =7
for(i=0; i<=MTRperiod; i++)
{
Ext[i]=High[i+1]-Low[i+1];
ArraySort(Ext);
c= ((Ext[n]+Ext[n-1]+Ext[n+1])/3);
}
}
return(c);
}