//+------------------------------------------------------------------+
//|                                                      IMA_250.mq4 |
//|                                                 GGG Macon France |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                      IMA_250.mq4 |
//|                                                              GGG |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "GGG"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
#property indicator_buffers 7
#property indicator_color1 PaleGreen // DarkOrange
#property indicator_color2  DarkOrange
#property indicator_color3 LightBlue
#property indicator_color4  Yellow //Orange
#property indicator_color5 PaleGreen // DarkOrange
#property indicator_color6 DarkOrange //LightBlue
#property indicator_color7 Gold//Orange

#property indicator_width1 4
#property indicator_width2 4
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 2
#property indicator_width7 2

extern int     i250_Prix    = 4;
extern int     i250_Lengh    = 12;
extern int     i250_Filter   = 10;
extern double i250_x_atr = 6;
// IMA_250[x][i] = iCustom(NULL,0,"IMA_250",i250_Prix ,i250_Lengh,i250_Filter,i250_x_atr,x,i);  

double arrow_up[];
double arrow_down[];

double ma_250[];
double haut[];
double bas[];
double ma_courte[];
double Atr_Long[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, arrow_up );
   SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID);   
   SetIndexArrow(0,159);
   SetIndexEmptyValue(0, 0.0); 
 //-------   
   SetIndexBuffer(1, arrow_down );
   SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID);   
   SetIndexArrow(1,159);
   SetIndexEmptyValue(1, 0.0); 
//--------
   SetIndexBuffer(2, ma_250 );
   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 2);   
//-------   
   SetIndexBuffer(3, haut );
   SetIndexStyle(3, DRAW_LINE, STYLE_SOLID);   
   // SetIndexEmptyValue(1, 0.0); 
//-------   
   SetIndexBuffer(4, bas );
   SetIndexStyle(4, DRAW_LINE, STYLE_SOLID, 2);   
   SetIndexEmptyValue(4, 0.0); 
//-------   
   SetIndexBuffer(5, ma_courte );
   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID, 2);  
   SetIndexEmptyValue(5, 0.0); 
//-------   
   SetIndexBuffer(6, Atr_Long );
   SetIndexStyle(6, DRAW_LINE, STYLE_SOLID, 2);   
   //SetIndexEmptyValue(4, 0.0); 
   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 i = -1; int debut =-1;  
  if( prev_calculated == 0  )  debut = rates_total-300; 
   if( prev_calculated > 300 ) debut = rates_total - prev_calculated;
   if( prev_calculated > 0 ) debut ++; 

 //  for(i = debut; i >=0; i-- )  std_brut[i] =   iStdDev(NULL,0,20,0,0, 0,i);   
  // for(i = debut; i >=0; i-- )  std[i] = iMAOnArray(std_brut,0,2,0,2,i);   

  for( i = debut; i >=0; i-- ) 
    {
     double atr = iATR(NULL,0,500,i);
     ma_250[i] = iMA(NULL,0,250,0,3,0,i);
    //  ma_courte[i]= iMA(NULL,0,5,0,1,0,i);
     ma_courte[i] = iCustom(NULL, 0, "NonLag//NonLagMA_v4",i250_Prix,i250_Lengh,0,i250_Filter,0,0,0,4,0,i);

    haut[i]=ma_250[i]+i250_x_atr*atr;
     bas[i]= ma_250[i]-i250_x_atr*atr; ;
   
    if( ma_courte[i] >  haut[i] && 
        ma_courte[i+2]<= ma_courte[i+1] && ma_courte[i+1] > ma_courte[i] )
                                                  arrow_down[i]=  ma_courte[i]+atr;
     else                                          arrow_down[i]= 0.0;  
        
    if( ma_courte[i] <  bas[i] && 
        ma_courte[i+2]>= ma_courte[i+1] && ma_courte[i+1] < ma_courte[i] )
                                                  arrow_up[i]=  ma_courte[i]-atr;
     else                                          arrow_up[i]= 0.0;     
    }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
