//+------------------------------------------------------------------+
// ALF-slope.mq4 by Squalou
//+------------------------------------------------------------------+

// ALF is a good price exhaustion indicator, 
// and can be used as an exit signal when it is getting FLAT;
// i.e when its SLOPE is nearing 0.

// "ALF-slope" draws the slope of an ALF line.
// Exit your trade when it "returns" to a near-0 level.
// For a stronger exit signal, set "timeframe" 1 step higher than the chart's timeframe.
// (e.g. 15 on an M5 chart)

#property copyright "by Squalou"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_maximum 0.0005
#property indicator_minimum -0.0005
#property indicator_level1 0.0


extern int LookBack = 20;
extern int Median = 5;
extern int PriceType = PRICE_MEDIAN;
extern color Color1 = Blue;
extern int TimeFrame=0;

//---- buffers

double ALFslope[];

//+------------------------------------------------------------------+
int init()
  {

//---- indicators
   IndicatorDigits(Digits+1);
   SetIndexStyle(0,DRAW_LINE,0,0,Color1);
   SetIndexBuffer(0,ALFslope);
  
//---- name for DataWindow and indicator subwindow label   
   switch(TimeFrame)
   {
      case 1 : string TimeFrameStr="M1"; break;
      case 5 : TimeFrameStr="M5"; break;
      case 15 : TimeFrameStr="M15"; break;
      case 30 : TimeFrameStr="M30"; break;
      case 60 : TimeFrameStr="H1"; break;
      case 240 : TimeFrameStr="H4"; break;
      case 1440 : TimeFrameStr="D1"; break;
      case 10080 : TimeFrameStr="W1"; break;
      case 43200 : TimeFrameStr="MN1"; break;
      default : TimeFrameStr="Current Timeframe";
   }
   IndicatorShortName("ALF-MTF slope("+TimeFrameStr+")");   
//----
   return(0);
  }
 
//+------------------------------------------------------------------+
int start()
  {
   datetime TimeArray[];
   int    i,limit,y=0,counted_bars=IndicatorCounted();

// Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
   limit=Bars-counted_bars;
   limit=Bars-counted_bars;
   for(i=0,y=0;i<limit;i++)
   {
      if (Time[i]<TimeArray[y]) y++;

      ALFslope[i]=
        iCustom(NULL,TimeFrame,"alf",LookBack,Median,PriceType,Color1,0,y)
      - iCustom(NULL,TimeFrame,"alf",LookBack,Median,PriceType,Color1,0,y+1);
   }  
   return(0);
  }
//+------------------------------------------------------------------+