Wokring out the length of the body of the candle....

 

Hi

I have this code in my EA but it just keeps returning 0.0000 can only help?

int Range(int shift){
double Range;

   if(Close[shift]<Open[shift]) Range = Open[shift]-Close[shift]*Point*mno;
   if(Close[shift]>Open[shift]) Range = Close[shift]-Open[shift]*Point*mno;

   return(0);
}

The "Range" is then used in a condition statement, but I have the Range measurement shown in the comment area and it just returns 0.0000

Thanks

Antony

 

TJ

See this EA https://www.mql5.com/en/code/8714

for body & other candle-based calculations

-BB-

 
BarrowBoy:

TJ

See this EA https://www.mql5.com/en/code/8714

for body & other candle-based calculations

-BB-


Hi

Thanks for the reply, I just had a look but it doesn't explain my problem :( My code seems to use the same logic.

Thanks

Antony

 
tonyjms2005:
Thanks for the reply, I just had a look but it doesn't explain my problem :( My code seems to use the same logic.
Your code contains at least two problems. Firstly, you are doing Open[shift]-Close[shift]*Point. Because there are no brackets, MT4 is not interpreting this in the order which you appear to be expecting. For example, if the figures were 5 - 2 * 10, then MT4 would give the answer -15, not +30.

Secondly, I'm almost certain that you're actually intending to divide by Point, not multiply by Point, in order to convert the size of the range into a number of pips. Otherwise, you're taking a difference between open and close such as 0.440 and multiplying by a value such as 0.00001, which would give you 0.0000044.

However, neither of these explains the fact that you're seeing 0.0000 values. You may have a third problem as well where the value you are using for shift is beyond the number of available bars, and Open[shift] and Close[shift] are both returning 0.

EDIT: of course... reading more closely, it looks like you've misunderstood how functions work, and/or how localisation of variables works within functions. The Range() function is always returning 0, regardless of what value you calculate during it. In addition, the return type of the function is an int which may well not be suitable.
 
jjc:
Your code contains at least two problems. Firstly, you are doing Open[shift]-Close[shift]*Point. Because there are no brackets, MT4 is not interpreting this in the order which you appear to be expecting. For example, if the figures were 5 - 2 * 10, then MT4 would give the answer -15, not +30.

Secondly, I'm almost certain that you're actually intending to divide by Point, not multiply by Point, in order to convert the size of the range into a number of pips. Otherwise, you're taking a difference between open and close such as 0.440 and multiplying by a value such as 0.00001, which would give you 0.0000044.

However, neither of these explains the fact that you're seeing 0.0000 values. You may have a third problem as well where the value you are using for shift is beyond the number of available bars, and Open[shift] and Close[shift] are both returning 0.

EDIT: of course... reading more closely, it looks like you've misunderstood how functions work, and/or how localisation of variables works within functions. The Range() function is always returning 0, regardless of what value you calculate during it. In addition, the return type of the function is an int which may well not be suitable.


Hi

Thanks for teh reply, I think I will have to go back the drawing board, can anyone give any pionters as I am a novice, I did try replacing:

return(0)

with:

return(Range)

But that just returns a constant amount of 2.0000

Thanks

Antony

 
tonyjms2005:

[...] But that just returns a constant amount of 2.0000

...because, as I noted above, you have declared the function as an int. Therefore, the value will be getting rounded down to the nearest whole number.

I don't know what value "mno" has, but the full calculation as MT4 sees it is going to be as follows:

  • Multiply Close[shift] by Point. For example, on EURUSD at current prices that's going to give a value such as 0.00001441.
  • This value is then multipled by mno. Don't know what mno is. Let's say that the result is still pretty small.
  • The resulting value is then subtracted from Open[shift]. This is going to give a result which is very similar to Open[shift], i.e. the end result of the Range calculation is more or less the same as either the open price or the close price, e.g. 1.44392
  • The value is then converted to an int to be returned from the Range() function. This will turn 1.44392 into 1.00000
 
tonyjms2005:

Thanks for teh reply, I think I will have to go back the drawing board, can anyone give any pionters as I am a novice

I assume from your use of Point that you want to get the range in terms of number of pips. If so, the following may help:

// Returns the open-close range of the nth bar in pip terms
double Range(int shift)
{
   // Get the difference between open and close. Not interested
   // in the direction of the bar; just get the absolute difference.
   double RangeInPriceTerms = MathAbs(Open[shift] - Close[shift]);
   
   // Do a basic conversion of this price range such as 0.0123 into a 
   // number of pips such as 123
   double RangeInPipTerms = RangeInPriceTerms / Point;
   
   // If the broker quotes prices to 3/5 digits, then division by Point 
   // will say that a price movement of 0.0001 is 10 pips, not 1 pip.
   // So, to keep in line with most people's definition of what a "pip" is,
   // we'll divide the number of pips by 10 on such brokers.
   switch (MarketInfo(Symbol(), MODE_DIGITS)) {
      case 3:
      case 5:
         RangeInPipTerms = RangeInPipTerms / 10;           
         break;
   }
   
   return (RangeInPipTerms);
} 
 
jjc:

I assume from your use of Point that you want to get the range in terms of number of pips. If so, the following may help:


Excellent! works a treat, Thank you for your help!!

Thanks again

Antony

Reason: