斐波那契回撤(线缺失)

时间:2019-01-17 09:36:18

标签: mql4 metatrader4 mql mt4

我有斐波那契回撤MT4终端工具的mql4编程代码。无论金融工具的类型如何,它都会在图表上自动绘制准确的百分比线。第一个代码的问题是我完全在下面复制粘贴的,因为斐波那契线.7263(76.4%)丢失了,因此我在下面的第二个代码中添加了这些行(在第一个代码之后完全复制粘贴)。现在第二个代码有两个问题。

第一个问题(第二个代码):缺少100.0%和0.0%的行。他们不再在图表上被吸引。

第二个问题(第二个代码):与第一个代码相同的%线不再在正确和准确的市场价格水平上绘制。示例(相同符号,相同时间范围):61.8%等于第二个代码中的市场价格0.98247,这是错误的。在第一个代码中(正确)61.8%等于市场价格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--; } 
}
//+------------------------------------------------------------------+

第二个代码:

//+------------------------------------------------------------------+
//|                                        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--; } 
}
//+------------------------------------------------------------------+

1 个答案:

答案 0 :(得分:0)

这是处理fibo对象的高级方法。如果您更喜欢底层函数,可以使用调试器逐步执行此代码,以查看MQL标准库如何处理它。

#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);
}