How to get Ratio of 2 Moving averages

 
How do I code and get the ratio of 2 moving averages? Anyone kindly shed some light please. I need to calculate the ratio of 2 Moving averages .
 
Seleucus :
How do I code and get the ratio of 2 moving averages ? Anyone kindly shed some light please. I need to calculate the ratio of 2 Moving averages .

What does "ratio" mean? Show formula...

 
The ratio just means how many times one value contains another. Like 4:3, 2:1 and so on. 
So in my particular case , I would like to calculate ratio of for example MA20 : MA50 or MA20/MA50
 
Seleucus # :
The ratio just means how many times one value contains another. Like 4:3, 2:1 and so on. 
So in my particular case , I would like to calculate ratio of for example MA20 : MA50 or MA20/MA50

Take the Difference Two iMA indicator and instead of the line

   for(int i=limit; i<rates_total; i++)
     {
      Diff_Two_iMABuffer[i]=FastBuffer[i]-SlowBuffer[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Filling indicator buffers from the MA indicator                  |
//+------------------------------------------------------------------+

put

   for(int i=limit; i<rates_total; i++)
     {
      Diff_Two_iMABuffer[i]=FastBuffer[i]/SlowBuffer[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Filling indicator buffers from the MA indicator                  |
//+------------------------------------------------------------------+
 
I tried to incorporate your suggestion in my code but I'm getting errors everywhere. Frustrating . 
My idea was to:
compute MA3
compute MA5
compute ratio MA3/MA5
 When ratio >1,  MA3 is above the MA5  and When ratio <1 the MA3 is below  MA5

Compute the % change of the ratio through time:

[(ratio H-1) - (ratio H-2)] / (ratio H-2)

In this example  I used hourly date between H-1 and H-2 but you also can compute the evolution over any time frame. It tells you if your Ma3/ Ma5 is it growing or not? Here you get a feeling of the dynamic, of the curves getting closer or not. 
I may be complicating things with my thought process here , do correct me if I'm wrong or show a better way :)

I'm trying to do all this  to get that specific point where the MAs touch but have not yet crossed each other. 

I tried using indexes of MA but they are Just not working , they are inefficient.  The comment comes  after the MAs have crossed already  , yet I want to get that moment where they meet before intersecting.
 

There is no difference: at least apply the "-" sign, at least apply the "/" sign, at least apply the "ratio" - the final form of the indicator will remain the SAME. See the example below: three windows - each with its own formula

  • fast-slow
  • fast/slow
  • (Fast-Slow)/Slow
 
Thanks for this. Incorporated it into my code and it works. I owe you one. 
One last thing I can't seem to figure out is how to get the value of "fast/slow" as a double or integer(need to use the values in an if statement to enter and exit positions) .  All the values  seem to have been converted to strings inorder to display in indicator window 
Tried using StringTodouble function but it just won't work.

 
Seleucus # :
Thanks for this. Incorporated it into my code and it works. I owe you one. 
One last thing I can't seem to figure out is how to get the value of " fast/slow" as a double or integer(need to use the values in an if statement to enter and exit positions) .  All the values  seem to have been converted to strings inorder to display in indicator window 
Tried using StringTodouble function but it just won't work.
 Amateur me.

You can get the corrected indicator here: Two iMA Formula

Two iMA Formula
Two iMA Formula
  • www.mql5.com
Study of the dependencies of two iMA (Moving Average, MA)
 
Vladimir Karputov #:

You can get the corrected indicator here: Two iMA Formula

The first indicator works well also. Can you kindly show me how to get real time values of the indicator and use it in a way to close trades . Something like if ( "slow/fast" > 0){trade.PositionClose(symbol());}
 
Seleucus # :
The first indicator works well also. Can you kindly show me how to get real time values of the indicator and use it in a way to close trades . Something like if ( "slow/fast" > 0){trade.PositionClose(symbol());}

I can recommend two steps: step #1 - Creating an iMA indicator handle, getting indicator values

Step #2 will be after studying lesson #1.

How to start with MQL5
How to start with MQL5
  • 2020.03.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Seleucus #:
The first indicator works well also. Can you kindly show me how to get real time values of the indicator and use it in a way to close trades . Something like if ( "slow/fast" > 0){trade.PositionClose(symbol());}

Example: Very Simple Intersection iMA Open Position

How to start with MQL5
How to start with MQL5
  • 2022.04.07
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Reason: