A beauty K_MACD Indicator Went Crazy.If you don't believe in Ghost whatch it.[pic*2][Code]

To add comments, please log in or register
Ma Yuliang
294
Ma Yuliang  

This is My favorate MACD colored and with point Arrow.

It works well on the first load,buy it went crazy if you change it's time frame.

the code is blow. The arrow I set shoud be 0.01 or -0.01 or be null

but after the change of timeframe It's start to be random num from nowhere.

Like a ghost in my computer.Help me tell me why. I am new if MT5.

I start to use MT4 since last week, I learnd a lot of code, function and start to make Indicator for my own.

This K_MACD is first first coded on mql4, it work well.

Yesterday I know the exitence of MT5. Since there is a new one why use the old.

But new one is not always the best. The flexibility and complecity of MT5 almost make me crazy.

I want to know why the buffer could take data randomlly.

//+------------------------------------------------------------------+
//|                                                       K_MACD.mq4 |
//|                                   Killnight a coder on breadline |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Killnight a coder on breadline"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#include <MovingAverages.mqh>
//#property indicator_minimum -0.08           //The bottom scaling limit for a separate indicator window
//#property indicator_maximum 0.08          //The top scaling limit for a separate indicator window

input int InpFastEMA=12;   // Fast EMA Period
input int InpSlowEMA=26;   // Slow EMA Period
input int InpSignalSMA=9;  // Signal SMA Period
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price

#property indicator_buffers 7
#property indicator_plots   4

//--- plot Section

#property indicator_label1  "ZBL"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "NBL"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "Signal"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrWhite
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2

#property indicator_label4  "MACD"
#property indicator_type4   DRAW_COLOR_HISTOGRAM
#property indicator_color4  clrRed,0x000046,0x234200,clrGreenYellow
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2




//--- indicator buffers
double                   ExtMacdBuffer[];
double                   ExtSignalBuffer[];
double                   ExtFastMaBuffer[];
double                   ExtSlowMaBuffer[];
double                   ColorBuffer[];
double                   UPBuffer[];
double                   DWBuffer[];

//--- MA handles
int                      ExtFastMaHandle;
int                      ExtSlowMaHandle;
//+---------------------------------------

//clrGreenYellow,C'0,66,35',C'70,0,0',clrRed

bool ExtParameters;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,UPBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,DWBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtSignalBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,ExtMacdBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,ColorBuffer,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(5,ExtFastMaBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,ExtSlowMaBuffer,INDICATOR_CALCULATIONS);

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

   PlotIndexSetInteger(0,PLOT_ARROW,226);
   PlotIndexSetInteger(1,PLOT_ARROW,225);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0);
  
   IndicatorSetString(INDICATOR_SHORTNAME,"K_MACD("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalSMA)+")");
//--- get MA handles
   ExtFastMaHandle=iMA(NULL,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice);
   ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice);
//--- initialization done
//ChartRedraw();
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int getIndexOfColor(int i)
  {


   if(ExtMacdBuffer[i]>0)
     {
      if(ExtMacdBuffer[i-1]<ExtMacdBuffer[i])return(0);//如果左柱<右柱即MACD上升趋势应为亮红否则为暗红
      else return(1);

     }
   else
     {

      if(ExtMacdBuffer[i-1]>ExtMacdBuffer[i]) return(2);
      else return(3);

     }

  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---

//--- check for data
   if(rates_total<InpSignalSMA)
      return(0);
//--- not all data may be calculated
   int calculated=BarsCalculated(ExtFastMaHandle);
   if(calculated<rates_total)
     {
      Print("Not all data of ExtFastMaHandle is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
   calculated=BarsCalculated(ExtSlowMaHandle);
   if(calculated<rates_total)
     {
      Print("Not all data of ExtSlowMaHandle is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
//--- get Fast EMA buffer
   if(IsStopped()) return(0); //Checking for stop flag

   if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0)
     {
      Print("Getting fast EMA is failed! Error",GetLastError());
      return(0);
     }
//--- get SlowSMA buffer
   if(IsStopped()) return(0); //Checking for stop flag
   if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)
     {
      Print("Getting slow SMA is failed! Error",GetLastError());
      return(0);
     }

   int limit;
   if(prev_calculated==0)
      limit=0;
   else limit=prev_calculated-1;
//--------------------------------------------------------------------  
//--- calculate MACD
   for(int i=limit;i<rates_total && !IsStopped();i++)
      ExtMacdBuffer[i]=ExtFastMaBuffer[i]-ExtSlowMaBuffer[i];
//--- calculate Signal
   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,ExtMacdBuffer,ExtSignalBuffer);

//macd填色

   for(int i=limit+1;i<rates_total ;i++)
     {
      ColorBuffer[i]=getIndexOfColor(i);
     }

//---------------------------------------------------
   for(int i=limit+1;i<rates_total ;i++)
     {
      if(ExtMacdBuffer[i]>0)
        {
         if(ExtMacdBuffer[i-1]<ExtMacdBuffer[i] && close[i-1]>close[i])
           {
            UPBuffer[i]=-0.001;

           }
        }
      else
        {
         if(ExtMacdBuffer[i-1]>ExtMacdBuffer[i] && close[i-1]<close[i])
           {
            DWBuffer[i]=+0.001;
           }
        }
     }

//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);



  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0);  

   Print(__FUNCTION__,"_Uninitalization reason code = ",reason);

}
Alain Verleyen
37290
Alain Verleyen  

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

For a complete code, I suggest you to use "Attach file", otherwise your post if too big.
Ma Yuliang
294
Ma Yuliang  
Alain Verleyen:
For a complete code, I suggest you to use "Attach file", otherwise your post if too big.

Thanks. Do you have any idea with these crazy Arrow thing?

Alain Verleyen
37290
Alain Verleyen  
killnight:

Thanks. Do you have any idea with these crazy Arrow thing?

Sorry I don't have time to check your code, maybe someone else.

Just a quick overview, I think maybe you missed the point the array indexing is not "as timeseries" by default on mql5. You may have to use ArraySetAseries().

Ma Yuliang
294
Ma Yuliang  
Alain Verleyen:

Sorry I don't have time to check your code, maybe someone else.

Just a quick overview, I think maybe you missed the point the array indexing is not "as timeseries" by default on mql5. You may have to use ArraySetAseries().

Alain Verleyen:

Sorry I don't have time to check your code, maybe someone else.

Just a quick overview, I think maybe you missed the point the array indexing is not "as timeseries" by default on mql5. You may have to use ArraySetAseries().

ArraySetAseries() I tried it but still not work.

I tried to solve this bug by several way only three can make it btter but not solve it totally.

1. not just set the point I want show the arrow. I also set NULL to all the poit I don't want to show the arrow.It not work.

   for(int i=limit+1;i<rates_total;i++)
     {
      if(ExtMacdBuffer[i]>0)
        {
         if(ExtMacdBuffer[i-1]<ExtMacdBuffer[i] && close[i-1]>close[i]) UPBuffer[rates_total-i]=-0.005;
         else UPBuffer[i]=NULL;
        }

      if(ExtMacdBuffer[i]<0)
        {
         if(ExtMacdBuffer[i-1]>ExtMacdBuffer[i] && close[i-1]<close[i]) DWBuffer[rates_total-i]=0.005;
         else DWBuffer[i]=NULL;
        }
     }

2.use the line below in init. to set the arrow buffer with empty value. It works if i just got one arrow buffer. By two arrow buffer it not work.

PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

3. Change ArrowsBuffer type from Indicator_data to INDICATOR_CALCULATIONS it do make it looks better.But the Buffer value still jumped to some other number randomly.

SetIndexBuffer(0,ArrowsBuffer,INDICATOR_DATA);


I had test it for about 20 times. and I am sure it's a bug in mql5. If I set a arrow buffer, without set any data to it , the arrow will stick to the nearest line.

It is convenient if you just want see some arrow on the screen, but it is stupit to lose accuracy for conveniece.

I hope some one could tell me "you are wrong" and show me the right way or just tell me "you are right" .So that I will not wast time on it.

COSMOS4U - D. Vezeris & Co, L.P.
2573
Dimitrios Vezeris  
Εventually is a bug or you solved it?
To add comments, please log in or register