//+------------------------------------------------------------------+
//|                                                                  |
//|                                                                  |
//|                                                                  |
//|           FPI – Fractional Product Inefficiency                  |
//|                                                          FPI.mq4 |
//|                                                                  |
//|                                 http://www.marketprogramming.com |
//+------------------------------------------------------------------+

#property link      "http://www.marketprogramming.com"
#property copyright "Dan Ostapyuk" 


#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Yellow


#property indicator_level1 1.0
#property indicator_levelcolor DimGray
#property indicator_levelwidth 1
#property indicator_levelstyle 0


extern bool AlertWhenReachedLevel=true;
extern double AboveLevel,
              BelowLevel;
              
extern string iPair1 = "EURUSD" ;
extern string iPair2 = "USDCHF" ;
extern string iPair3 = "EURCHF" ;  


double Main[] , PAIR1[] ,PAIR2[] ,PAIR3[] ;
int alert_bar;

//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(4); 
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Main);
   
   SetIndexBuffer(1,PAIR1);
   SetIndexBuffer(2,PAIR2);
   SetIndexBuffer(3,PAIR3);

   ArrayInitialize(PAIR1,EMPTY_VALUE);
   ArrayInitialize(PAIR2,EMPTY_VALUE);
   ArrayInitialize(PAIR3,EMPTY_VALUE);
   ArrayInitialize(Main,EMPTY_VALUE);

   return(0);
  }

//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

//+------------------------------------------------------------------+
int start()
  {

   ArrayCopySeries(PAIR1,MODE_CLOSE,iPair1,Period());
   ArrayCopySeries(PAIR2,MODE_CLOSE,iPair2,Period());
   ArrayCopySeries(PAIR3,MODE_CLOSE,iPair3,Period());

   int counted_bars=IndicatorCounted();
   int i = 0;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   
   for(i=0; i<limit; i++)
   {
       Main[i] = PAIR1[i]* PAIR2[i]* (1/PAIR3[i]);
   }
   
  if(AlertWhenReachedLevel && alert_bar<Bars)
   {
    if(AboveLevel>0 && Main[0]>AboveLevel && Main[1]<=AboveLevel)
     {
      Alert("Above Level!");
      alert_bar=Bars;
     }
    if(BelowLevel>0 && Main[0]<BelowLevel && Main[1]>=BelowLevel)
     {
      Alert("Below Level!");
      alert_bar=Bars;
     }
   }

   return(0);
  }
//+------------------------------------------------------------------+