Re: Distance from a Level Code

 

Hello!
I am working on an EA (my first one) and I am stuck. I am wanting to enter long or short if price is within a certain level of pips from predefined levels (highs and lows). Not sure though how to go about the distance in pips part of the code? 
I thought defining this distance first and then just doing an if, else statement that tells my EA that if price is within this distance buy or sell. But not sure exactly how to do this nor if it's the best way to do this. 

Hopefully I have asked this question clearly enough. If not let me know but any help would be appretiated!

Thanks!

 
Anewr: if price is within a certain level of pips from predefined levels (highs and lows). Not sure though how to go about the distance in pips part of the code?
  1. Define in concrete terms how you get your levels.
  2. Define what a PIP is.
              How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018)

  3. Distance in PIPs is MathAbs(level - market) / PIP

 

Thank you William for the response! 

Just so I am clear when you say "define a pip" you mean to code it so that the EA recognizes when it is dealing with Yen and on-Yen pairs? Like the following code?

// Function to calculate the decimal places.
// Digits is a native variable in MetaTrader, which is assumes the value 
// of a number of digits after the point in a quote.
double CalculateNormalizedDigits()
{
   // If there are 3 or fewer digits (JPY, for example), then return 0.01, which is the pip value.
   if (Digits <= 3){
      return(0.01);
   }
   // If there are 4 or more digits, then return 0.0001, which is the pip value.
   else if (Digits >= 4){
      return(0.0001);
   }
   // In all other cases, return 0.
   else return(0);
}
Or am I still missing something? I have the other two parts of your three step process that you outlined figured out Thanks again!

 
Anewr #: Just so I am clear when you say "define a pip" you mean to code it so that the EA recognizes when it is dealing with Yen and on-Yen pairs? Like the following code?
  1. That works for all major pairs. It does not work on symbols where the spread is hundreds or thousands of points.
  2. You can simplify your code:
    double CalculateNormalizedDigits()
    {
       return Digits % 2 == 0 ? Point : 10*Point;
    }

 
William Roeder #:
  1. That works for all major pairs. It does not work on symbols where the spread is hundreds or thousands of points.
  2. You can simplify your code:



Thanks for the reply! 
The fact that that code works for just majors is just fine for me. That is all I want to trade with this EA anyway but good to know if I want to use it for other pairs in the future. 

Reason: