Get Moving Average Waves / Triangles (MQL4)

 

Hello forum, good day.

I need help to get the Moving Average waves by obtaining three values for each wave: the beginning, the highest point and the end of a wave. How can these three points be obtained to draw a triangle? I'll attach an image so it is better exemplified.


So far, this is what I've got but I can't make it work yet: 

input int Waves     = 2;
input int Threshold = 5;

double    wave_up_start[],
          wave_up_peak[],
          wave_up_end[];

datetime  wave_up_start_time[],
          wave_up_peak_time[],
          wave_up_end_time[];

//+------------------------------------------------------------------+
//| Get Moving Average waves function                                |
//+------------------------------------------------------------------+
void fnGetMAWaves()
  {
    int i,
        i_up_start = 0,
        i_up_peak  = 0,
        i_up_end   = 0,
        wave_count = 0,
        limit      = MathMin( 100, iBars( NULL, 0 ) );

    double ma_val[]; ArrayResize( ma_val, limit ); ArrayInitialize( ma_val, 0 );

    double ma_up_start[]; ArrayResize( ma_up_start, limit ); ArrayInitialize( ma_up_start, EMPTY_VALUE );
    double ma_up_peak[];  ArrayResize( ma_up_peak, limit );  ArrayInitialize( ma_up_peak, EMPTY_VALUE );
    double ma_up_end;     ArrayResize( ma_up_end, limit );   ArrayInitialize( ma_up_end, EMPTY_VALUE );

    ArrayResize( wave_up_start, Waves );      ArrayResize( wave_up_peak, Waves );        ArrayResize( wave_up_end, Waves );
    ArrayResize( wave_up_start_time, Waves ); ArrayResize( wave_up_peak_time, Waves );   ArrayResize( wave_up_end_time, Waves );

    for ( i = 0; i < limit; i++ )
      {
        ma_val[i] = iMA( NULL, 0, MA_1_Period, MA_1_Shift, MA_1_Method, MA_1_Price, i );
      }

    for ( i = 2; i < limit - 1; i++ )
      {
        // Start of the wave up
        if ( ma_value[i] <= ma_value[i - 1] && ma_value[i] < ma_value[i + 1] )
          {
            ma_up_start[i] = ma_val[i];
            i_up_start     = i;
          }

        // Peak of the wave up
        if ( ma_up_start[i] > 0 && ma_value[i] >= ma_value[i - 1] && ma_value[i] > ma_value[i + 1] )
          {
            ma_up_peak[i] = ma_val[i];
            i_up_peak     = i;
          }

        // End of the wave up
        if ( ma_up_peak[i] > 0 && ma_value[i] <= ma_value[i - 1] && ma_value[i] < ma_value[i + 1] )
          {
            ma_up_end[i] = ma_val[i];
            i_up_end     = i;
          }

        // Check Threshold/Magnitude of the wave
        if ( Threshold >= MathAbs( NormalizeDouble( ( ma_up_peak[i] - ma_up_start[i] ) / Point, Digits ) / PipFactor ) 
          && Threshold >= MathAbs( NormalizeDouble( ( ma_up_peak[i] - ma_up_end[i] ) / Point, Digits ) / PipFactor ) )
          {
            wave_up_start_time[i] = Time[i];
            wave_up_peak_time[i] = Time[i];
            wave_up_end_time[i] = Time[i];

            wave_up_start[i] = ma_up_start[i];
            wave_up_peak[i]  = ma_up_peak[i];
            wave_up_end[i]   = ma_up_end[i];

            wave_count++;

            ObjectCreate( "Wave Up", OBJ_TRIANGLE, 0, wave_up_start_time[0], wave_up_start[0], wave_up_peak_time[0], wave_up_peak[0], wave_up_end_time[0], wave_up_end[0] );
            ObjectSetInteger( 0, "Wave Up", OBJPROP_COLOR, clrGreen );
            ObjectSetInteger( 0, "Wave Up", OBJPROP_STYLE, STYLE_SOLID );
            ObjectSetInteger( 0, "Wave Up", OBJPROP_WIDTH, 1 );
          }

        if ( wave_count > 2 * ( Waves - 1 ) ) break;
      }
  } //--- end fnGetMAWaves()


Your help will be much appreciated!


Best regards and thank you in advance,

Gerardo 

Files:
 
Gerardo Bustos:

Hello forum, good day.

I need help to get the Moving Average waves by obtaining three values for each wave: the beginning, the highest point and the end of a wave. How can these three points be obtained to draw a triangle? I'll attach an image so it is better exemplified.


So far, this is what I've got but I can't make it work yet: 


Your help will be much appreciated!


Best regards and thank you in advance,

Gerardo 

Hehe, I just solved this myself! xD

It just needs some tweaking but it is working. 

 

Gerardo.