Download MetaTrader 5

Mathematical Functions

To add comments, please log in or register
fly7680
567
fly7680  
Hello, I performed a series of calculations to arrive at a final percentage. The code has no errors but the calculation does not work.

example:

Total excursion Body candle = 36 tenths
Total
excursion shadows      = 11 tenths
mathematical expression (100 * 11) / 36 = 30.55%

Can you help me?

Thank you


bool c1 = Open[1+i] < Close[1+i] && MathAbs(Close[1+i]-Open[1+i]) > 2  * myPoint;
bool c2 = MathAbs (High [1+i] - Close[1+i]); //shadow up                          
bool c3 = MathAbs (Open [1+i] - Low  [1+i]); //shadow donw                          
bool c4 = MathAbs (High [1+i] - Low  [1+i]); //total escursion                    
bool c5 = MathAbs (c2+c3); //sum shadow                                                
bool c6 = (100*c5)/ c4;  //result as a percentage                                                                
bool c7 = c6 < 3.9;                                                               
 
honest_knave
Moderator
2403
honest_knave  

You're working exclusively with bool variables... true / false (yes / no). Their values can only be 1 or 0. You need to be using some doubles.

Data Types 

fly7680
567
fly7680  
I modified the code and no errors but still does not work.

Help please?

      bool   c1 =  Open[1+i] < Close [1+i] && MathAbs(Close[1+i]-Open[1+i]) > 2  * myPoint; //Candela Verde
      double c2 = MathAbs (High [1+i] - Close[1+i]); //shadow up                          
      double c3 = MathAbs (Open [1+i] - Low  [1+i]); //shadow donw                          
      double c4 = MathAbs (High [1+i] - Low  [1+i]); //total escursion
      double c5 = MathAbs (c2+c3);        //sum shadow                    
      double c6 = MathAbs (100*c5)/c4;  //result as a percentage                                                                
      bool   c7 = c6 < 3.9; 
whroeder1
15086
whroeder1  
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Print out your variables, and find out why.
  3. Drop the c1 as you don't use it. Rename your variables to meaningful names. Your code should be able to be read out loud and make sense.
  4. Drop your MathAbs as those are unnecessary (except c1 lline.)
  5. Your indicator for loop computes the value of bar "i," drop those "1+".
Fernando Carreiro
2232
Fernando Carreiro  

You are assuming that all candles have Close > Open. Also, use variable names that are more meaningful and self documenting.

// Attention - uncompiled/untested!
int
   BarShift       = i + 1; // Depends on what you are doing (as "WHRoeder" pointed out)
double
   CandleOpen     = Open[  BarShift ],
   CandleClose    = Close[ BarShift ],
   CandleHigh     = High[  BarShift ],
   CandleLow      = Low[   BarShift ],
   CandleRange    = CandleHigh - CandleLow,
   BodyHigh       = fmax( CandleOpen, CandleClose ),
   BodyLow        = fmin( CandleOpen, CandleClose ),
   WickUpperRange = CandleHigh - BodyHigh,
   WickLowerRange = BodyLow - CandleLow,
   WickRange      = WickUpperRange + WickLowerRange,
   WickRatio      = ( CandleRange != 0.0  ) ? WickRange / CandleRange : 0.0, // Prevent "divide by zero" error
   WickPercentage = WickRatio * 100.0;
fly7680
567
fly7680  
Thanks to everyone for the great suggestions and lines of code that occasionally posted for us beginners!
I was able to integrate your code to mine, it all works.

Thanks again
To add comments, please log in or register