-1

I have mql4 programming code of MT4 terminal's tool of Fibonacci Retracement. It automatically draws accurate % lines on chart regardless of type of financial instrument. The problem with the first code, which I am entirely copy pasting below, is that Fibonacci line .7263 (76.4%) is missing so I added those lines in the SECOND code below (entirely copied pasted AFTER first code). Now I have two problems with second code.

First problem (second code): Both lines 100.0% and 0.0% are missing. They don't get drawn on chart any more.

Second problem (second code): Same % lines, as in first code, are NOT being drawn on correct and accurate market price level any more. Example (same symbol, same time frame): 61.8% equals to market price 0.98247 in second code which is wrong. In first code (correct) 61.8% equals to market price 0.98075

+------------------------------------------------------------------+
//|                                        fibonacci-retracement.mq4 |
//|        ©2011 Best-metatrader-indicators.com. All rights reserved |
//|                        http://www.best-metatrader-indicators.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011 Best-metatrader-indicators.com."
#property link      "http://www.best-metatrader-indicators.com"

#property indicator_chart_window
extern double FiboLevel1=0.000;
extern double FiboLevel2=0.236;
extern double FiboLevel3=0.382;
extern double FiboLevel4=0.500;
extern double FiboLevel5=0.618;
extern double FiboLevel6=1.000;
string Copyright="\xA9 WWW.BEST-METATRADER-INDICATORS.COM";  
string MPrefix="FI";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
ClearObjects(); 
Comment("");
//----
DL("001", Copyright, 5, 20,Gold,"Arial",10,0); 
return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
ClearObjects(); 
Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  //----

     int fibHigh = iHighest(Symbol(),Period(),MODE_HIGH,WindowFirstVisibleBar()-1,1);
     int fibLow  = iLowest(Symbol(),Period(),MODE_LOW,WindowFirstVisibleBar()-1,1);

     datetime highTime = Time[fibHigh];
     datetime lowTime  = Time[fibLow];

      if(fibHigh>fibLow){
      WindowRedraw();
      ObjectCreate(MPrefix+"FIBO_LAB",OBJ_FIBO,0,highTime,High[fibHigh],lowTime,Low[fibLow]);
      color levelColor = Red;
      }
      else{
      WindowRedraw();
      ObjectCreate(MPrefix+"FIBO_LAB",OBJ_FIBO,0,lowTime,Low[fibLow],highTime,High[fibHigh]);
      levelColor = Green;
      }

      double fiboPrice1=ObjectGet(MPrefix+"FIBO_LAB",OBJPROP_PRICE1);
      double fiboPrice2=ObjectGet(MPrefix+"FIBO_LAB",OBJPROP_PRICE2);

      double fiboPriceDiff = fiboPrice2-fiboPrice1;
      string fiboValue0 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel1,Digits);
      string fiboValue23 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel2,Digits);
      string fiboValue38 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel3,Digits);
      string fiboValue50 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel4,Digits);
      string fiboValue61 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel5,Digits);
      string fiboValue100 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel6,Digits);

     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIBOLEVELS,6);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+0,FiboLevel1);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+1,FiboLevel2);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+2,FiboLevel3);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+3,FiboLevel4);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+4,FiboLevel5);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+5,FiboLevel6);


     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_LEVELCOLOR,levelColor);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_LEVELWIDTH,1);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_LEVELSTYLE,STYLE_DASHDOTDOT);
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 0,fiboValue0+" --> "+DoubleToStr(FiboLevel1*100,1)+"%"); 
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 1,fiboValue23+" --> "+DoubleToStr(FiboLevel2*100,1)+"%"); 
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 2,fiboValue38+" --> "+DoubleToStr(FiboLevel3*100,1)+"%"); 
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 3,fiboValue50+" --> "+DoubleToStr(FiboLevel4*100,1)+"%");
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 4,fiboValue61+" --> "+DoubleToStr(FiboLevel5*100,1)+"%");
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 5,fiboValue100+" --> "+DoubleToStr(FiboLevel6*100,1)+"%");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| DL function                                                      |
//+------------------------------------------------------------------+
 void DL(string label, string text, int x, int y, color clr, string FontName = "Arial",int FontSize = 12, int typeCorner = 1)

{
   string labelIndicator = MPrefix + label;   
   if (ObjectFind(labelIndicator) == -1)
   {
      ObjectCreate(labelIndicator, OBJ_LABEL, 0, 0, 0);
  }

   ObjectSet(labelIndicator, OBJPROP_CORNER, typeCorner);
   ObjectSet(labelIndicator, OBJPROP_XDISTANCE, x);
   ObjectSet(labelIndicator, OBJPROP_YDISTANCE, y);
   ObjectSetText(labelIndicator, text, FontSize, FontName, clr);

}  

//+------------------------------------------------------------------+
//| ClearObjects function                                            |
//+------------------------------------------------------------------+
void ClearObjects() 
{ 
  for(int i=0;i<ObjectsTotal();i++) 
  if(StringFind(ObjectName(i),MPrefix)==0) { ObjectDelete(ObjectName(i)); i--; } 
}
//+------------------------------------------------------------------+

SECOND CODE:

//+------------------------------------------------------------------+
//|                                        fibonacci-retracement.mq4 |
//|        ©2011 Best-metatrader-indicators.com. All rights reserved |
//|                        http://www.best-metatrader-indicators.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011 Best-metatrader-indicators.com."
#property link      "http://www.best-metatrader-indicators.com"

#property indicator_chart_window
extern double FiboLevel1=0.000;
extern double FiboLevel2=0.236;
extern double FiboLevel3=0.382;
extern double FiboLevel4=0.500;
extern double FiboLevel5=0.618;
extern double FiboLevel6=0.764;
extern double FiboLevel7=1.000;
string Copyright="\xA9 WWW.BEST-METATRADER-INDICATORS.COM";  
string MPrefix="FI";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
ClearObjects(); 
Comment("");
//----
DL("001", Copyright, 5, 20,Gold,"Arial",10,0); 
return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
ClearObjects(); 
Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  //----

     int fibHigh = iHighest(Symbol(),Period(),MODE_HIGH,WindowFirstVisibleBar()-1,1);
     int fibLow  = iLowest(Symbol(),Period(),MODE_LOW,WindowFirstVisibleBar()-1,1);

     datetime highTime = Time[fibHigh];
     datetime lowTime  = Time[fibLow];

      if(fibHigh>fibLow){
      WindowRedraw();
      ObjectCreate(MPrefix+"FIBO_LAB",OBJ_FIBO,0,highTime,High[fibHigh],lowTime,Low[fibLow]);
      color levelColor = Red;
      }
      else{
      WindowRedraw();
      ObjectCreate(MPrefix+"FIBO_LAB",OBJ_FIBO,0,lowTime,Low[fibLow],highTime,High[fibHigh]);
      levelColor = Green;
      }

      double fiboPrice1=ObjectGet(MPrefix+"FIBO_LAB",OBJPROP_PRICE1);
      double fiboPrice2=ObjectGet(MPrefix+"FIBO_LAB",OBJPROP_PRICE2);

      double fiboPriceDiff = fiboPrice2-fiboPrice1;
      string fiboValue0 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel1,Digits);
      string fiboValue23 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel2,Digits);
      string fiboValue38 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel3,Digits);
      string fiboValue50 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel4,Digits);
      string fiboValue61 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel5,Digits);
      string fiboValue76 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel6,Digits);
      string fiboValue100 = DoubleToStr(fiboPrice2-fiboPriceDiff*FiboLevel7,Digits);

     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIBOLEVELS,6);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+0,FiboLevel1);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+1,FiboLevel2);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+2,FiboLevel3);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+3,FiboLevel4);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+4,FiboLevel5);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+5,FiboLevel6);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_FIRSTLEVEL+6,FiboLevel7);


     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_LEVELCOLOR,levelColor);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_LEVELWIDTH,1);
     ObjectSet(MPrefix+"FIBO_LAB",OBJPROP_LEVELSTYLE,STYLE_DASHDOTDOT);
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 0,fiboValue0+" --> "+DoubleToStr(FiboLevel1*100,1)+"%"); 
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 1,fiboValue23+" --> "+DoubleToStr(FiboLevel2*100,1)+"%"); 
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 2,fiboValue38+" --> "+DoubleToStr(FiboLevel3*100,1)+"%"); 
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 3,fiboValue50+" --> "+DoubleToStr(FiboLevel4*100,1)+"%");
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 4,fiboValue61+" --> "+DoubleToStr(FiboLevel5*100,1)+"%");
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 5,fiboValue76+" --> "+DoubleToStr(FiboLevel6*100,1)+"%");
     ObjectSetFiboDescription( MPrefix+"FIBO_LAB", 6,fiboValue100+" --> "+DoubleToStr(FiboLevel7*100,1)+"%");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| DL function                                                      |
//+------------------------------------------------------------------+
 void DL(string label, string text, int x, int y, color clr, string FontName = "Arial",int FontSize = 12, int typeCorner = 1)

{
   string labelIndicator = MPrefix + label;   
   if (ObjectFind(labelIndicator) == -1)
   {
      ObjectCreate(labelIndicator, OBJ_LABEL, 0, 0, 0);
  }

   ObjectSet(labelIndicator, OBJPROP_CORNER, typeCorner);
   ObjectSet(labelIndicator, OBJPROP_XDISTANCE, x);
   ObjectSet(labelIndicator, OBJPROP_YDISTANCE, y);
   ObjectSetText(labelIndicator, text, FontSize, FontName, clr);

}  

//+------------------------------------------------------------------+
//| ClearObjects function                                            |
//+------------------------------------------------------------------+
void ClearObjects() 
{ 
  for(int i=0;i<ObjectsTotal();i++) 
  if(StringFind(ObjectName(i),MPrefix)==0) { ObjectDelete(ObjectName(i)); i--; } 
}
//+------------------------------------------------------------------+
halfer
  • 19,824
  • 17
  • 99
  • 186
Puppy00
  • 1
  • 1

1 Answers1

0

Here is the high-level way of working with fibo objects. If you prefer to get down-and-dirty in the low-level functions you can use the debugger to step through this code in order to see how it's handled by the MQL standard library.

#property strict
#property indicator_chart_window
#include <chartobjects/chartobjectsfibo.mqh>

CChartObjectFibo g_fibo;

int OnInit()
{
   if(!g_fibo.Create(0, "fibo", 0, 0, 0.0, 0, 0.0))
      return INIT_FAILED;
   double levels[] = {0.0, 0.10, 0.236, 0.328, 0.5, 0.618, 0.764, 0.9, 1.0};
   int total = ArraySize(levels);
   g_fibo.LevelsCount(total);
   for(int i=0; i<total; i++) {
      g_fibo.LevelValue(i, levels[i]);
      g_fibo.LevelDescription(i, DoubleToString(levels[i] * 100, 1));
   }
   return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int ihigh = ArrayMaximum(high, 10);
   int ilow = ArrayMinimum(low, 10);
   g_fibo.Time(0, time[ihigh]);
   g_fibo.Time(1, time[ilow]);
   g_fibo.Price(0, high[ihigh]);
   g_fibo.Price(1, low[ilow]);
   return(rates_total);
}
nicholishen
  • 2,602
  • 2
  • 9
  • 13
  • Nicholishen thank you for your reply but your code doesn't work. It doesn't even draw appropriate lines on chart. No market prices mentioned. Nothing. 95% of chart area is ignored using your tool. Also incorrect calculations. – Puppy00 Jan 17 '19 at 22:21
  • i changed previous code to your one and clicked on Compile. Then when i added the tool with your code to the chart, the lines where loaded to only small piece of entire trend. Without defined market price (as in first code in my first message above) on Fibonacci levels. Also without alerts upon touch/cross up/cross down. – Puppy00 Jan 18 '19 at 01:17