MQL4设计MFI指标问题求解 新评论 jiangzd1991 2022.04.13 17:07 求大神指点: 以下代码运行没有错误,但运行时不显示指标。分析发现GetMFI函数的返回值计算正确,但是没有显示出来。但是如果输出sum1和sum2的加法,减法和乘法都是显示指标的,就包含sum1/sum2的除法不显示。哎~~ //+------------------------------------------------------------------+ //| MFI.mq4 | //| Copyright 2022, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict #property indicator_separate_window #property indicator_buffers 1 #property indicator_type1 DRAW_LINE #property indicator_style1 STYLE_SOLID #property indicator_color1 clrAliceBlue #property indicator_width1 1 //--- //--- input int period = 14; //--- double MFIBugger[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0, MFIBugger); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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 i, limit; //--- if(rates_total < period) { return(0); } //--- bool as_series_high = ArrayGetAsSeries(high); bool as_series_low = ArrayGetAsSeries(low); bool as_series_close = ArrayGetAsSeries(close); bool as_series_tick_volume = ArrayGetAsSeries(tick_volume); bool as_series_MFIBugger = ArrayGetAsSeries(MFIBugger); if(as_series_close) { ArraySetAsSeries(close, false); } if(as_series_high) { ArraySetAsSeries(high, false); } if(as_series_low) { ArraySetAsSeries(low, false); } if(as_series_MFIBugger) { ArraySetAsSeries(MFIBugger, false); } if(as_series_tick_volume) { ArraySetAsSeries(tick_volume, false); } //--- if(prev_calculated == 0) { limit = period; for(i = 0; i < limit; i++) { MFIBugger[i] = 0; } for(i = limit; i < rates_total; i++) { MFIBugger[i] = GetMFI(high, low, close, tick_volume, period, i); } } else { limit = prev_calculated - 1; for(i = limit; i < rates_total; i++) { MFIBugger[i] = GetMFI(high, low, close, tick_volume, period, i); } } //--- if(as_series_close) { ArraySetAsSeries(close, true); } if(as_series_high) { ArraySetAsSeries(high, true); } if(as_series_low) { ArraySetAsSeries(low, true); } if(as_series_MFIBugger) { ArraySetAsSeries(MFIBugger, true); } if(as_series_tick_volume) { ArraySetAsSeries(tick_volume, true); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ double GetMFI(const double& high[], const double& low[], const double& close[], const long& volume[], const int count, int begin) { double typ1, typ2, sum1 = 0, sum2 = 0, mfi; for(int i = 1; i < count; i++) { typ1 = (high[begin - i - 1] + low[begin - i - 1] + close[begin - i - 1]) / 3; typ2 = (high[begin - i] + low[begin - i] + close[begin - i]) / 3; if(typ1 > typ2) { sum1 += typ1 * volume[begin - i - 1]; } else { sum2 += typ2 * volume[begin - i]; } } mfi=100-100/(1+sum2/sum1); return(mfi); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ Discover new MetaTrader 5 opportunities with MQL5 community and services 2022.04.13www.mql5.com MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions 新人对MQL4和MQL5的任何问题,对算法和代码的帮助和讨论 初学者的问题 MQL5 MT5 MetaTrader 5 我将免费编写指标 Hung Wen Lin 2022.04.13 17:54 #1 第一次計算時sum1等於零 所以執行會出錯 除數絕對不能等於零 修改成下述就可以運行了 會影響最遠的那根K 或是最近的一根K 你自己要看著排除 if(sum1==0) sum1=sum2; mfi=100-100/(1+sum2/sum1); return(mfi); jiangzd1991 2022.04.14 02:41 #2 非常感谢! 新评论 您错过了交易机会: 免费交易应用程序 8,000+信号可供复制 探索金融市场的经济新闻 注册 登录 拉丁字符(不带空格) 密码将被发送至该邮箱 发生错误 使用 Google 登录 您同意网站政策和使用条款 如果您没有帐号,请注册 可以使用cookies登录MQL5.com网站。 请在您的浏览器中启用必要的设置,否则您将无法登录。 忘记您的登录名/密码? 使用 Google 登录
求大神指点:
以下代码运行没有错误,但运行时不显示指标。分析发现GetMFI函数的返回值计算正确,但是没有显示出来。但是如果输出sum1和sum2的加法,减法和乘法都是显示指标的,就包含sum1/sum2的除法不显示。哎~~
//+------------------------------------------------------------------+
//| MFI.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_color1 clrAliceBlue
#property indicator_width1 1
//---
//---
input int period = 14;
//---
double MFIBugger[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0, MFIBugger);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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 i, limit;
//---
if(rates_total < period)
{
return(0);
}
//---
bool as_series_high = ArrayGetAsSeries(high);
bool as_series_low = ArrayGetAsSeries(low);
bool as_series_close = ArrayGetAsSeries(close);
bool as_series_tick_volume = ArrayGetAsSeries(tick_volume);
bool as_series_MFIBugger = ArrayGetAsSeries(MFIBugger);
if(as_series_close)
{
ArraySetAsSeries(close, false);
}
if(as_series_high)
{
ArraySetAsSeries(high, false);
}
if(as_series_low)
{
ArraySetAsSeries(low, false);
}
if(as_series_MFIBugger)
{
ArraySetAsSeries(MFIBugger, false);
}
if(as_series_tick_volume)
{
ArraySetAsSeries(tick_volume, false);
}
//---
if(prev_calculated == 0)
{
limit = period;
for(i = 0; i < limit; i++)
{
MFIBugger[i] = 0;
}
for(i = limit; i < rates_total; i++)
{
MFIBugger[i] = GetMFI(high, low, close, tick_volume, period, i);
}
}
else
{
limit = prev_calculated - 1;
for(i = limit; i < rates_total; i++)
{
MFIBugger[i] = GetMFI(high, low, close, tick_volume, period, i);
}
}
//---
if(as_series_close)
{
ArraySetAsSeries(close, true);
}
if(as_series_high)
{
ArraySetAsSeries(high, true);
}
if(as_series_low)
{
ArraySetAsSeries(low, true);
}
if(as_series_MFIBugger)
{
ArraySetAsSeries(MFIBugger, true);
}
if(as_series_tick_volume)
{
ArraySetAsSeries(tick_volume, true);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
double GetMFI(const double& high[], const double& low[], const double& close[], const long& volume[], const int count, int begin)
{
double typ1, typ2, sum1 = 0, sum2 = 0, mfi;
for(int i = 1; i < count; i++)
{
typ1 = (high[begin - i - 1] + low[begin - i - 1] + close[begin - i - 1]) / 3;
typ2 = (high[begin - i] + low[begin - i] + close[begin - i]) / 3;
if(typ1 > typ2)
{
sum1 += typ1 * volume[begin - i - 1];
}
else
{
sum2 += typ2 * volume[begin - i];
}
}
mfi=100-100/(1+sum2/sum1);
return(mfi);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+