//------------------------------------------------------------------
#property link      "www.forex-station.com"
#property copyright "www.forex-station.com maPriceCross by member csj179t"

//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers    3
#property indicator_color1     clrWhite
#property indicator_width1     2
#property indicator_color2     clrGreen
#property indicator_width2     2
#property indicator_color3    clrRed
#property indicator_width3     1



extern ENUM_TIMEFRAMES    TimeFrame       = PERIOD_CURRENT;
extern int              line_width = 2;
extern color             lineCol = clrWhite;
extern int                 ma_period = 13;
extern ENUM_APPLIED_PRICE   prices = PRICE_MEDIAN;
extern int                 ma_mode  = 0;
extern bool                 arrows = true;
extern double                  arrGap  = 1;
extern color                upCol = clrGreen;
extern color                dnCol = clrRed;
extern int                  arrSize = 2;
extern bool               Interpolate   = true;

extern string             name = "maPriceCross";


double buffer1[];
double buffer2[];
double buffer3[],dz[];
double macd[];


string indicatorFileName;
bool   returnBars;

string name2 = name + " " + TimeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {




   IndicatorBuffers(3);
   SetIndexBuffer(0,buffer1);
   SetIndexStyle(0,DRAW_LINE,0,line_width,lineCol);
   if(arrows)
           {
   SetIndexBuffer(1,buffer2);
   SetIndexStyle(1,DRAW_ARROW,0,arrSize,upCol);
   SetIndexArrow(1,233);
   SetIndexBuffer(2,buffer3);
   SetIndexStyle(2,DRAW_ARROW,0,arrSize,dnCol);
   SetIndexArrow(2,234);
}


   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame==-99;
   TimeFrame         = fmax(TimeFrame,_Period);

   IndicatorShortName(name2);

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {



   return(0);

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {




   int counted_bars=IndicatorCounted();
   int i,limit;


   if(counted_bars < 0)
      return(-1);
   if(counted_bars > 0)
      counted_bars--;
   limit = MathMin(Bars-counted_bars,Bars-1);
   if(returnBars)
     {
      buffer1[0] = limit+1;
      return(0);
     }


   if(TimeFrame == Period())
     {

      for(i=limit; i>=0; i--)
        {


         double gap  = iATR(NULL,0,20,i);

         buffer1[i] = iMA(NULL,0,ma_period,0,ma_mode,prices,i) ;

         
            buffer2[i] = (Close[i] > buffer1[i]) && (Close[i+1] < buffer1[i+1]) ? Low[i]- arrGap *gap: 0;
            buffer3[i] = (Close[i] < buffer1[i]) && (Close[i+1] > buffer1[i+1]) ? High[i]+ arrGap* gap :0;


           



        }






      return(0);
     }



   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   for(i=limit; i>=0; i--)
     {
      int y = iBarShift(NULL,TimeFrame,Time[i]);

      buffer1[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,line_width,lineCol,ma_period,prices,ma_mode,arrows,arrGap,0,y);
      buffer2[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,line_width,lineCol,ma_period,prices,ma_mode,arrows,arrGap,1,y);
      buffer3[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,line_width,lineCol,ma_period,prices,ma_mode,arrows,arrGap,2,y);

      //
      //
      //
      //
      //

      if(!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1]))
         continue;

      //
      //
      //
      //
      //

      datetime time = iTime(NULL,TimeFrame,y);
      for(int n = 1; i+n < Bars && Time[i+n] >= time; n++)
         continue;
      for(int x = 1; x < n; x++)
        {
        


         buffer1[i+x] = buffer1[i] + (buffer1[i+n] - buffer1[i]) * x/n;

         buffer2[i+x] = buffer2[i] + (buffer2[i+n] - buffer2[i]) * x/n;
 buffer3[i+x] = buffer3[i] + (buffer3[i+n] - buffer3[i]) * x/n;



        }
     }







   return(0);
  }



//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
