Search code examples
mql4

How to draw a Fibonacci indicator automatically in MQL4?


I wish to analyze a chart using Fibonacci indicator. Are there any methodologies ( or libraries ) that can draw of a Fibonacci indicator automatically on a chart with MQL4?


Solution

  • Use the MQL4 "Object Functions" ( Ref. to GUI Objects' manipulations )

    Among many graphical elements, MetaTrader Terminal 4 can place also the few of Fibonacci based technical indicators ( let's go ahead with the Fibonacci retracement tool, other are similar in the use and can apply the same to them ).

    First, one has to create such object, using:

    ObjectCreate( chart_ID,                           // ref. doc
                  anInterimObjNAME,                   //      a name to be used
                  OBJ_FIBO,                           //      a type of GUI-obj to draw
                  0,                                  //      sub-window
                  time1, price1,                      //      where to start
                  time2, price2                       //      where to finish
                  );
    

    Next, one can adapt many attributes of the already created object - colour, line-type / line-thickness, texts and also the starting time, starting price, ending time, ending price - to such values that are pleasing one's taste.

    //--- set how many levels you want to paint
          ObjectSetInteger( chart_ID,
                            anInterimObjNAME,
                            OBJPROP_LEVELS,
                            levels
                            );
    
    //--- set the properties of each of the level
       for (  int i = 0; i <  levels; i++ )
       {      ObjectSetDouble(  chart_ID, anInterimObjNAME, OBJPROP_LEVELVALUE, i, values[i] ); // level value
              ObjectSetInteger( chart_ID, anInterimObjNAME, OBJPROP_LEVELCOLOR, i, colors[i] ); // level color
              ObjectSetInteger( chart_ID, anInterimObjNAME, OBJPROP_LEVELSTYLE, i, styles[i] ); // level style
              ObjectSetInteger( chart_ID, anInterimObjNAME, OBJPROP_LEVELWIDTH, i, widths[i] ); // level width
              ObjectSetString(  chart_ID, anInterimObjNAME, OBJPROP_LEVELTEXT,  i, DoubleToString( 100 * values[i], 1 ) ); // level description
              }