Что это за недокументированное возвращаемое значение "-nan" и почему я его получаю и так далее? - PullRequest
0 голосов
/ 10 февраля 2019

Начальная проблема

В конечном итоге я пытаюсь построить RSI индикатора "xxxxxxx.mq4" следующим образом:

//+------------------------------------------------------------------+
//|                                                   RSIxxxxxxx.mq4 |
//|                                  Copyright © 2019, Andy Thompson |
//|                                   mailto:andydoc1@googlemail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Andy Thompson"
#property link      "mailto:andydoc1@googlemail.com"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2

//---- buffers
double ExtMapBufferCustomIndicator[];
double ExtMapBufferRSICustomIndicator[];
int i;
string s="xxxxxxx";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBufferRSICustomIndicator);
   SetIndexLabel(0,"RSICustomIndicator");

   IndicatorShortName("RSI of xxx: RSICustomIndicator");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=15;
   ArrayResize(ExtMapBufferCustomIndicator,Bars);
//---- main loop
   for(i=0; i<limit; i++)
     {
      ExtMapBufferCustomIndicator[i]= iCustom(NULL,0,s,20,40,0,0);
     }
   for(i=0; i<limit; i++)
     {
      ExtMapBufferRSICustomIndicator[i]=iRSIOnArray(ExtMapBufferCustomIndicator,0,14,0);
      printf(DoubleToString(ExtMapBufferCustomIndicator[i]),", ",DoubleToString(ExtMapBufferRSICustomIndicator[i])); //debug
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

, но я получаю массив изошибка range ', как подсказывает xerx593 , из-за игнорирования необходимости явного изменения размера моего динамического массива при удалении его из списка индикаторных буферов ( Почему я получаю массив изОшибка диапазона в этом коде? ).Это привело к строке:

   ArrayResize(ExtMapBufferCustomIndicator,Bars);

NB. Исходный индикатор xxxxxxx.mq4 работает нормально.

Теперь код работает без ошибок, кроме оператора отладки printf:

      printf(DoubleToString(ExtMapBufferCustomIndicator[i]),", ",DoubleToString(ExtMapBufferRSICustomIndicator[i])); //debug

производит постоянную распечатку, без запятой и только одно число, без индикатора, но сбивает с толку ошибку времени выполнения в терминале или журналах:

screenshot 1

"nan«ошибка возникает из-за упрощения кода!

Удаление ссылки на внешний код и его замена на пересчет исходного индикатора приводит к возврату« nan »и постоянному возврату, который должен варьироваться:

#property copyright "Copyright © 2019, Andy Thompson"
#property link      "mailto:andydoc1@googlemail.com"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2

//---- buffers
double ExtMapBufferCustomIndicator[];
double ExtMapBufferRSICustomIndicator[];
int i;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBufferRSICustomIndicator);
   SetIndexLabel(0,"RSIxxx");
   IndicatorShortName("RSI of xxx: RSIxxx");
//----
//---- prepare custom indicator array
   ArraySetAsSeries(ExtMapBufferCustomIndicator,true);
   ArrayResize(ExtMapBufferCustomIndicator,Bars);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=15;
//---- main loop
   for(i=0; i<limit; i++)
     {
      ExtMapBufferCustomIndicator[i]=(34.38805726*MathPow(iClose("EURUSD",0,i),0.3155)*MathPow(iClose("EURJPY",0,i),0.1891)*MathPow(iClose("EURGBP",0,i),0.3056)*MathPow(iClose("EURSEK",0,i),0.0785)*MathPow(iClose("EURCHF",0,i),0.1113))/(50.14348112*MathPow(iClose("EURUSD",0,i),-0.576)*MathPow(iClose("USDJPY",0,i),0.136)*MathPow(iClose("GBPUSD",0,i),-0.119)*MathPow(iClose("USDCAD",0,i),0.091)*MathPow(iClose("USDSEK",0,i),0.042)*MathPow(iClose("USDCHF",0,i),0.036)); // calculate the original custom indicator internally
     }
   for(i=0; i<limit; i++)
     {
      ExtMapBufferRSICustomIndicator[i]=iRSIOnArray(ExtMapBufferCustomIndicator,0,14,0);

      printf(DoubleToString(ExtMapBufferCustomIndicator[i])); //debug
      printf(DoubleToString(ExtMapBufferRSICustomIndicator[i])); //debug
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

Я добавлю еще скриншоты позже.

...