Newbie needs help with indicator code

 

Hi,


This is my first indicator and I don't understand what is going on. Could someone highlight my the problem with my code please. Thanks.

I want to calculate 2 moving averages and a MACD + histogram. I don't want to display the results, just use them to make a decision and display an arrow on the chart when the condition is true. Under debug the debug session stops at the first MACD_main[pos] calculation and I don't know why. This piece of code was copied from another indicator. Nothing is displayed in the debug session output.

Also, under debug the MA_short and MA_long variables display 0.0 but it is the first time around the loop so this could be normal.

Perhaps, it's because the result of moving average calculation is 0.0 but it doesn't explain why it stops the debug session.


Here's my code:

#property version   "1.00"
#property strict
#property indicator_chart_window

#include <MovingAverages.mqh>

input int SMAShort      =30; 
input int SMALong       =200;
input int MACDFastEMA   =12; 
input int MACDSlowEMA   =26; 
input int MACDSignalSMA =12;

//--- right input parameters flag
bool      ExtParameters=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if((MACDFastEMA<=1 || MACDSlowEMA<=1 || MACDSignalSMA<=1 || MACDFastEMA>=MACDSlowEMA) && (SMALong<=SMAShort))
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
//--- initialization done
   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[])
  {
//---
   double MA_short, MA_long, MACD_main[], MACD_signal[], MACD_hist[];
   int    ExtCountedBars;

   if(Bars<=SMALong) return(0);
  
   ExtCountedBars = IndicatorCounted();
//---
   if (ExtCountedBars<0) return(-1);

//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;

   int pos = Bars - ExtCountedBars - 1;

   if(ArraySize(MACD_main)==0 && ArraySize(MACD_signal)==0 && ArraySize(MACD_hist)==0)
   {
      ArrayResize(MACD_main, pos, 0);
      ArrayResize(MACD_signal, pos, 0);
      ArrayResize(MACD_hist, pos, 0);
   }
  
   while(pos >= 0)
   {
      MA_short = iMA(NULL, 0, SMAShort, 0, MODE_SMA, PRICE_CLOSE, pos);
      MA_long = iMA(NULL, 0, SMALong, 0, MODE_SMA, PRICE_CLOSE, pos);
      MACD_main[pos] = iMA(NULL, 0, MACDFastEMA, 0, MODE_EMA, PRICE_CLOSE, pos) - iMA(NULL, 0, MACDSlowEMA, 0, MODE_EMA, PRICE_CLOSE, pos);
      SimpleMAOnBuffer(rates_total,prev_calculated,0,MACDSignalSMA,MACD_main,MACD_signal);
      MACD_hist[pos] = MACD_main[pos]-MACD_signal[pos];
     
      pos--;
   }
  
   ObjectCreate("maValue",OBJ_LABEL,0,Time[0],0);
   ObjectSetText("maValue","Hello World!",8,"Tahoma",Gold);
  
   ObjectCreate("Up", OBJ_ARROW, 0, Time[0], Low[0]-10*Point); //draw an up arrow
   ObjectSet("Up", OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet("Up", OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
   ObjectSet("Up", OBJPROP_COLOR,Red);
     
//--- return value of prev_calculated for next call
   return(rates_total);
  }

Thanks in advance.

 
Ответы новичкам в картинках
Ответы новичкам в картинках
  • www.mql5.com
Как наложить (прикрепить) индикатор на график. - - Категория: общее обсуждение
 

Apply:

#property strict

 In the terminal, in the "Experts" will be shown the error.

 

Hi,


Thanks for your suggestion but this doesn't display any errors and still stops as soon as it executes the MACD_main[pos] = iMA(NULL, 0, MACDFastEMA, 0, MODE_EMA, PRICE_CLOSE, pos) - iMA(NULL, 0, MACDSlowEMA, 0, MODE_EMA, PRICE_CLOSE, pos); line.


Any other ideas?


Thanks.

 
imamushroom:

Hi,


Thanks for your suggestion but this doesn't display any errors and still stops as soon as it executes the MACD_main[pos] = iMA(NULL, 0, MACDFastEMA, 0, MODE_EMA, PRICE_CLOSE, pos) - iMA(NULL, 0, MACDSlowEMA, 0, MODE_EMA, PRICE_CLOSE, pos); line.


Any other ideas?


Thanks.

Правильно вставляем код на форуме

error 

Error:

   while(pos>=0)
     {
      MA_short= iMA(NULL,0,SMAShort,0,MODE_SMA,PRICE_CLOSE,pos);
      MA_long = iMA(NULL,0,SMALong,0,MODE_SMA,PRICE_CLOSE,pos);
      MACD_main[pos]=iMA(NULL,0,MACDFastEMA,0,MODE_EMA,PRICE_CLOSE,pos)-iMA(NULL,0,MACDSlowEMA,0,MODE_EMA,PRICE_CLOSE,pos);
      SimpleMAOnBuffer(rates_total,prev_calculated,0,MACDSignalSMA,MACD_main,MACD_signal);
      MACD_hist[pos]=MACD_main[pos]-MACD_signal[pos];
      pos--;
     }

 

The array size MACD_main[pos] is equal to "0". Need to change the logic of the program