//+------------------------------------------------------------------+
//|                                                  Force Index.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
//#property copyright "Copyright © 2005, MetaQuotes Software Corp."
//#property link      "http://www.metaquotes.net"

#property copyright "Copyright © 30.08.2016, SwingMan"
#property strict

/*------------------------------------------
See: Xbeast - 15MIN Trading Pattern
http://www.forexfactory.com/showthread.php?t=604699
Autor of the indicator: Dr. Alexander Elder
------------------------------------------*/

#property indicator_separate_window
#property indicator_buffers 4

#property indicator_color1 clrDodgerBlue
#property indicator_color2 clrTomato
#property indicator_color3 clrDodgerBlue
#property indicator_color4 clrRed

#property indicator_width3 2
#property indicator_width4 2

#property indicator_level1 0
#property indicator_levelcolor clrSilver

//---- input parameters
extern int ExtForcePeriod=1;
extern ENUM_MA_METHOD ExtForceMAMethod=MODE_SMA;
extern ENUM_APPLIED_PRICE ExtForceAppliedPrice_1=PRICE_HIGH;
extern ENUM_APPLIED_PRICE ExtForceAppliedPrice_2=PRICE_LOW;
//---- buffers
double ExtForceBufferHighs[],ExtForceBufferLows[];
double SignalUP[],SignalDN[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string sShortName;
   SetIndexBuffer(0,ExtForceBufferHighs);
   SetIndexBuffer(1,ExtForceBufferLows);
   SetIndexBuffer(2,SignalUP);
   SetIndexBuffer(3,SignalDN);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,108);
   SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,108);
//---- name for DataWindow and indicator subwindow label
   sShortName=WindowExpertName()+" ("+(string)ExtForcePeriod+")";
   IndicatorShortName(sShortName);
   SetIndexLabel(0,"ForceIdx_Highs");
   SetIndexLabel(1,"ForceIdx_Lows");
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,NULL);

//---- first values aren't drawn
   SetIndexDrawBegin(0,ExtForcePeriod);
   SetIndexDrawBegin(1,ExtForcePeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Force Index indicator                                            |
//+------------------------------------------------------------------+
int start()
  {
   int nLimit;
   int nCountedBars=IndicatorCounted();
//---- insufficient data
   if(Bars<=ExtForcePeriod) return(0);
//---- last counted bar will be recounted
   if(nCountedBars>ExtForcePeriod) nCountedBars--;
   nLimit=Bars-nCountedBars;

//---- Force Index counted
   for(int i=0; i<nLimit; i++)
     {
      double avgHighs0=iMA(NULL,0,ExtForcePeriod,0,ExtForceMAMethod,ExtForceAppliedPrice_1,i);
      double avgHighs1=iMA(NULL,0,ExtForcePeriod,0,ExtForceMAMethod,ExtForceAppliedPrice_1,i+1);
      if(avgHighs1!=0)
         ExtForceBufferHighs[i]=100*Volume[i]*(avgHighs0-avgHighs1)/avgHighs1;

      double avgLows0=iMA(NULL,0,ExtForcePeriod,0,ExtForceMAMethod,ExtForceAppliedPrice_2,i);
      double avgLows1=iMA(NULL,0,ExtForcePeriod,0,ExtForceMAMethod,ExtForceAppliedPrice_2,i+1);
      if(avgLows1!=0)
         ExtForceBufferLows[i]=100*Volume[i]*(avgLows0-avgLows1)/avgLows1;
     }
//---- Signals
   for(int i=0; i<nLimit; i++)
     {
      //-- buy signal
      bool conditionBuy_Highs=(ExtForceBufferHighs[i]<0);
      bool conditionBuy_Lows=(ExtForceBufferLows[i]<0) && 
                             (ExtForceBufferLows[i]>ExtForceBufferHighs[i]) && 
                             (ExtForceBufferLows[i+1]<ExtForceBufferHighs[i+1]);
      if(conditionBuy_Highs && conditionBuy_Lows)
         SignalUP[i]=0; else SignalUP[i]=EMPTY_VALUE;
         
            //-- sell signal
      bool conditionSell_Lows=(ExtForceBufferLows[i]>0);
      bool conditionSell_Highs=(ExtForceBufferHighs[i]>0) && 
                             (ExtForceBufferHighs[i]<ExtForceBufferLows[i]) && 
                             (ExtForceBufferHighs[i+1]>ExtForceBufferLows[i+1]);
      if(conditionSell_Lows && conditionSell_Highs)
         SignalDN[i]=0; else SignalDN[i]=EMPTY_VALUE;
     }

//---- done
   return(0);
  }
//+------------------------------------------------------------------+
