Download MetaTrader 5

MACD greater than 0 bug

To add comments, please log in or register
hpr
23
hpr  

Dear people,


I have been struggling with this problem the whole day yesterday. It seems such an easy task, but apparently it is not. I just want to look at 3 MACD values and when they all 3 are above zero, I want to plot blue dots on the 0 line. And when all 3 are below zero I want to plot red dots on the 0 line.


Have the following code:


int start()
   {
      int i, counted_bars;
      counted_bars = IndicatorCounted();
      //---- check for possible errors
      if (counted_bars<0) return(-1);
      //---- last counted bar will be recounted
      if (counted_bars>0) counted_bars--;
      i = Bars - counted_bars - 1;
    
      double wave1, wave2, wave3, zero=0.0;
    
      while(i >= 0)
      {
         wave1 = iMA(NULL,0,FastMAPeriod1,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod1,0,MODE_EMA,PRICE_CLOSE,i);
         wave2 = iMA(NULL,0,FastMAPeriod2,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod2,0,MODE_EMA,PRICE_CLOSE,i);
         wave3 = iMA(NULL,0,FastMAPeriod3,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod3,0,MODE_EMA,PRICE_CLOSE,i);
         bool upTrend = wave1 > 0.0 && wave2 > 0.0 && wave3 > 0.0;
         bool downTrend = wave1 < 0.0 && wave2 < 0.0 && wave3 < 0.0;
        
         if(upTrend)
         {
            pos[i] = 0.0;
            neg[i] = EMPTY_VALUE;
           
         }
        
         else if(downTrend)
         { 
            pos[i] = EMPTY_VALUE;
            neg[i] = 0.0;
                    
         }
        
         else
         {
            pos[i] = EMPTY_VALUE;
            neg[i] = EMPTY_VALUE;
           
         }
         i--;
      }
      

      return;

  }


But it only plots the pos[i] line, and if you look further in the history it sometimes shows really different results from other charting software. Weird thing is at some point (think when I put the "wave1 > 0.0  && wave2 > 0.0 && wave3 > 0") it only plotted the neg[i] line.


What am I missing?


Thank you for your time!


hamama

hpr
23
hpr  
(think when I put the "wave1 > 0.0  && wave2 > 0.0 && wave3 > 0" in the if-satatement)
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  
Actually 0 line is always moving, so, you might not be able to achieve what you are trying to achieve.
hpr
23
hpr  

hmm but why is the 0 line always moving? Isn't that simply a static value in the separate indicator window? if...then plot a dot with value 0. I don't see how the these dots would always be moving.


Could you elaborate?

hpr
23
hpr  
Think there is something horribly wrong with the data at mt4 brokers, with 1 broker it shows everything, with one it does not and the one that shows changes its results when you change timeframes compared to when you open mt4 starting on that timeframe....
Simon Gniadkowski
Moderator
18003
Simon Gniadkowski  
hamama:

Dear people,


I have been struggling with this problem the whole day yesterday. It seems such an easy task, but apparently it is not. I just want to look at 3 MACD values and when they all 3 are above zero, I want to plot blue dots on the 0 line. And when all 3 are below zero I want to plot red dots on the 0 line.


Have the following code:

SNIP

Please use the SRC button to post code: How to use the SRC button.

hpr
23
hpr  
RaptorUK:

Please use the SRC button to post code: How to use the SRC button.



#property copyright "Maggot"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 2
#property indicator_buffers 2
#property indicator_plots   2
//--- plot upTrend
#property indicator_label1  "upTrend"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//--- plot downTrend
#property indicator_label2  "downTrend"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  3
//--- indicator buffers
double         upTrendBuffer[];
double         downTrendBuffer[];
double         macd1[];
double         macd2[];
double         macd3[];

//-- parameters

extern int       FastMAPeriod1=20;
extern int       SlowMAPeriod1=41;
extern int       FastMAPeriod2=34;
extern int       SlowMAPeriod2=89;
extern int       FastMAPeriod3=55;
extern int       SlowMAPeriod3=89;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  
   IndicatorDigits(Digits+1);
//--- indicator buffers mapping
   SetIndexBuffer(0,upTrendBuffer);
   SetIndexBuffer(1,downTrendBuffer);
   SetIndexBuffer(2, macd1);
   SetIndexBuffer(3, macd2);
   SetIndexBuffer(4, macd3);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   
//---
   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 limit=rates_total-prev_calculated;
//---
   if(rates_total<=SlowMAPeriod3)
      return(0);
//---
   if(prev_calculated>0)
      limit++;
   for(int i=0; i<limit; i++)
   {
      macd1[i] = iMA(NULL,0,FastMAPeriod1,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod1,0,MODE_EMA,PRICE_CLOSE,i);
      macd2[i] = iMA(NULL,0,FastMAPeriod2,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod2,0,MODE_EMA,PRICE_CLOSE,i);
      macd3[i] = iMA(NULL,0,FastMAPeriod3,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod3,0,MODE_EMA,PRICE_CLOSE,i);
      
      upTrendBuffer[i] = EMPTY_VALUE;
      downTrendBuffer[i] = EMPTY_VALUE;
      
      if(macd1[i] > 0.0 && macd2[i] > 0.0 && macd3[i] > 0.0)
         upTrendBuffer[i] = 1;
         
      if(macd1[i] < 0.0 && macd2[i] < 0.0 && macd3[i] < 0.0)
         downTrendBuffer[i] = 1;
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
hpr
23
hpr  
tried like this, now it doesnt plot anything at all
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  
Use Print() and see if anything comes out from it. We always use Print() for debugging purposes.
hpr
23
hpr  
Can't seem to make it work, the weird thing is, sometimes it shows both blue and red dots and sometimes when you restart it only shows blue dots. It is really really buggy
Keith Watford
Moderator
9642
Keith Watford  
int OnInit()
  {
  
   IndicatorDigits(Digits+1);
   IndicatorBuffers(5);
//--- indicator buffers mapping
   SetIndexBuffer(0,upTrendBuffer);
   SetIndexBuffer(1,downTrendBuffer);
   SetIndexBuffer(2, macd1);
   SetIndexBuffer(3, macd2);
   SetIndexBuffer(4, macd3);
12
To add comments, please log in or register