Make negative value into positive...

 
Hello friends, how are you? I hope well.

I'm trying to do a simple rule of 3 where the final result should be plotted on the histogram. It is about measuring the distance from a moving average to the closing price of the candle. And the price should be returned in percentage, in histogram format.
However, one of the formulas can sometimes return a negative value, when candles are bearish. And this generates a conflict, because we can't divide a number when the divisor is negative.

The formula is simple.

Variable 1 = Closing price - opening price
Variable 2 = Closing price - moving average closing price
Variable 3 = Variable 2 * 100

Variable 4 = Variable 3 / Variable 1 // result in percentage, however when the number of variable 1 is negative, the program does not perform the division and returns the histogram all black.


I didn't write the code, as I'm very new to MQL4. I'm just trying to adapt it, to do what I want. But I'm stuck on this, for days, without finding any solution. So, if someone can give me a little help, I be very greatful.


Here is the code.

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  LimeGreen
#property indicator_color2  Orange
#property indicator_width1  5
#property indicator_width2  5
//
//
//
//
//
extern int MaPeriod =  9;
extern int MaMethod =  MODE_EMA;
extern int Price_cls  =  PRICE_CLOSE;
//
//
//
//
//
double up[];
double dn[];
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,up);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,dn);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   return(0);
  }
int deinit() { return(0); }
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars<0)
      return(-1);
   if(counted_bars>0)
      counted_bars--;
   int limit = MathMin(Bars-counted_bars,Bars-1);
   double pipMultiplier = MathPow(10,MathMod(Digits,5));
//
//
//
//
//
//
   for(int i=limit; i >= 0; i--)
     {
      double price = iClose(NULL, 0, i) - iOpen(NULL, 0, i);
      double emaprice = iClose(NULL, 0, i) - iMA(NULL,0,MaPeriod,0,MaMethod,Price_cls,i);
      double math1 = emaprice*100;
      double math2 = math1/price;
//
      if(math2>0)
        { up[i] = math2; dn[i] = EMPTY_VALUE; }
      else
        { dn[i] = math2; up[i] = EMPTY_VALUE; }
     }
   return(0);
  }
//+------------------------------------------------------------------+


 
taij1987:
.. So, if someone can give me a little help, I be very greatful.

Hello,

You can use this function: https://docs.mql4.com/math/mathabs

Regards.

MathAbs - Math Functions - MQL4 Reference
MathAbs - Math Functions - MQL4 Reference
  • docs.mql4.com
MathAbs - Math Functions - MQL4 Reference
 
Yohana Parmi #:

Hello,

You can use this function: https://docs.mql4.com/math/mathabs

Regards.

Hello Yohana, thank you!

Could you help me a little further?


I changed the code, but still histogram returns all black screen. So, I look at the de Expert Advisors tab, and I saw there is an Zero Divider error on this line


2022.10.21 11:24:06.538 Ma distance_2 EURJPY,M5: zero divide in 'Ma distance_2.mq4' (79,31)

The exact line where I make the division between variable emaprice and price, to get the percentage number of the distance I want to calculate.

Do you know how I can get rid of this Zero Divide error? I tried some solutions from other topics, but then always i get the wrong results in percentage.


    double price = iClose(NULL, 0, i) - iOpen(NULL, 0, i);
      price = MathAbs(price);
      double emaprice = (iClose(NULL, 0, i) - iMA(NULL,0,MaPeriod,0,MaMethod,Price_cls,i))*100;
      double math1 = emaprice/price;
//
      if(math1>0)
        { up[i] = math1; dn[i] = EMPTY_VALUE; }
      else
        { dn[i] = math1; up[i] = EMPTY_VALUE; }
     }
   return(0);
  }


 
taij1987 #:

Hello Yohana, thank you!
Could you help me a little further?
I changed the code, but still histogram returns all black screen. So, I look at the de Expert Advisors tab, and I saw there is an Zero Divider error on this line
2022.10.21 11:24:06.538 Ma distance_2 EURJPY,M5: zero divide in 'Ma distance_2.mq4' (79,31)
The exact line where I make the division between variable emaprice and price, to get the percentage number of the distance I want to calculate.
Do you know how I can get rid of this Zero Divide error? I tried some solutions from other topics, but then always i get the wrong results in percentage.


Hello,

Here is your code with a few fixes as necessary:

   double math1, price, emaprice;
   for(int i=limit; i >= 0; i--)
     {
      price = MathAbs(iClose(NULL, 0, i) - iOpen(NULL, 0, i));
      emaprice = (iClose(NULL, 0, i) - iMA(NULL,0,MaPeriod,0,MaMethod,Price_cls,i))*100;
      //--
      if(price>0) math1=emaprice/price;
      else continue;
      //--
      if(math1>0)
        { up[i] = math1; dn[i] = EMPTY_VALUE; }
      else
        { dn[i] = math1; up[i] = EMPTY_VALUE; }
     }

Result:


If you need further assistance, please use freelance.
You will find great programmers even better than me there.

Happy coding :)

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2022.10.22
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Yohana Parmi #:


Hello,

Here is your code with a few fixes as necessary:

Result:


If you need further assistance, please use freelance.
You will find great programmers even better than me there.

Happy coding :)

Thank you very much! God bless you! =)