mql4 - 斐波那契回撤(线缺失)
问题描述
我有 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--; }
}
//+------------------------------------------------------------------+
解决方案
这是使用 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);
}
推荐阅读
- java - 将属性值替换为 DTO
- flutter - 我可以使用 ChangeNotifierProxyProvider 根据另一个 ChangeNotifier 的状态有条件地提供依赖的 ChangeNotifier 吗?
- mailchimp - 添加布尔自定义字段
- angular - Keycloak 12,REST API(来自 JS)提供 CORS
- c# - 通过链接检测进行 CSOM 共享
- vue.js - Jest + Vue 测试 utlis TypeError:无法设置属性“内容”为空
- python - 将非结构化日志文件导入熊猫
- python - 使用 DateTimeIndex 仅插入 pandas 数据帧中的短间隙
- node.js - npm install 在 docker 容器中输出“这是 npm 本身的错误 - cb() 从未调用过”
- z3 - 有没有办法在 SMTLIB 中表达“当且仅当”?