Search code examples
mql4metatrader4mt4mql5metatrader5

How to include Fibonacci levels in Expert Advisor (EA)?


I want to have access to various Fibonacci levels like 23.6%, 38.2%, 50%, 61.8% and 100% in my expert advisor (EA). How can I define those in my EA so that traders can select them via the inputs?

I tried this

input double Fibo=23.6;

However, is this the common approach? Is it possible to set them as predefined?

Thank you for your help!


Solution

  • You can set predefined Fibonacci levels by using enumerations. Either you use enumerations provided by MQL5 or define your own, like this:

    //+------------------------------------------------------------------+
    //| Enumeration for Fibonacci levels                                 |
    //+------------------------------------------------------------------+
    enum ENUM_FIBO_LEVELS
      {
       fib0618 = 0618, // 61.8%
       fib1000 = 1000, // 100.0%
       fib1382 = 1382, // 138.2%
       fib1618 = 1618, // 161.8%
      };
    

    Note: If you place a single-line comment, it will be associated with the variable name, as shown in this example.

    input ENUM_FIBO_LEVELS FiboValue=fib1618; // Fibonacci level
    

    As a result, users are able to select their preferred Fibonacci level:

    Expert Advisors>Properties>Inputs

    To calculate potential support and resistance levels, convert the Fibonacci ENUM level:

    (double(FiboValue)/1000)
    

    If you have any further questions, please leave a comment below.