# How to make the function not to return 0?

132

How to make the function not to return 0 val when Vol >1 ??

double MTRd()
{
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);
}
Moderator
10651

Do not use volume to detect a new bar, if the first tick of a new bar is missed, the code is broken.
15925

1. 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
2. I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
3. A function should do one thing. Move your new bar code to OnTick; it doesn't belong in the ATR calculation.
New candle
• www.mql5.com
Anyone got a good piece of code for build 600+ that will determine if a tick that came in is the start of a new candle or not...
132

Keith Watford:
Do not use volume to detect a new bar, if the first tick of a new bar is missed, the code is broken.
thank you for your advice, i have already change the code, but it doesnt chage the result. If isnewbar()!=true c=0, while i would like to have previous val >0
7295

Declare the c at the beginning of the function, e.g. right below int n= ...
27
eevviill6
Brt88:

How to make the function not to return 0 val when Vol >1 ??

...

int prevbars;

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);
}
Moderator
33341

eevviill6:

int prevbars;

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.

132

Alain Verleyen:

Bars is unreliable to detect new bar. Please listen to other advices and don't give wrong example to people who are learning.

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);
}
15925

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);
1. Do you understand that True Range is not H - L? That is just the bar's range.
2. 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.
3. Do you understand that the median is either the central value or the average of the two.
4. 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];
}

Average true range - Wikipedia
• en.wikipedia.org
The range of a day's trading is simply . The true range extends it to yesterday's closing price if it was outside of today's range. The true range is the largest of the: Most recent period's high minus the most recent period's low Absolute value of the most recent period's high minus the previous close Absolute value of the most recent...