//+------------------------------------------------------------------+
//|                                                    PA_Spread.mq4 |
//|                                             Copyright © 2010, PA |
//|                                             http://www.blai5.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, PA # 0.2"
#property link      "http://www.blai5.net"

#property indicator_separate_window

#property indicator_buffers 3
#property indicator_color1 Orange
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_levelcolor Gray            // Color de linea de ref

extern string Notas = "Spread entre el simbolo del gráfico y PAR";
extern string Par = "CORN";
extern double LineaRef = 130;
extern bool   MostrarRatio = true;
extern double FactorAjusteRatio = 100;

extern string AlertWav = "alert2.wav";

string Par1;
string s;

int TimeFrame = 0;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];

int Periodo = 20;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

   Par1 = Symbol();

//---- indicators
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexLabel(0, "Ratio");
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexLabel(1, "Dif");
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexLabel(2, "Media Dif");

   s = "PA_Spread: "+Par1+" / "+Par;
   IndicatorShortName(s);

   SetLevelValue(0, LineaRef);     // Pinto nivel de ref

//----
   TimeFrame = MathMax(TimeFrame, Period());

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return(-1);
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;

   int i;
   int Error;
   //////////////////////////////////////////////////////////////////////////////// Calculo de la Media /////////
   for (i = 0; i < limit; i++) {
      if (MostrarRatio == true) { 
            if (iClose(Par, TimeFrame, i) != 0.0)
               ExtMapBuffer1[i] = iClose(Par1, TimeFrame, i) / iClose(Par, TimeFrame, i) * FactorAjusteRatio;
            else
               ExtMapBuffer1[i] = 0.0;
      }         
      ExtMapBuffer2[i] = (iClose(Par1, TimeFrame, i) - iClose(Par, TimeFrame, i));
   }

   for (i = 0; i < limit; i++) { 
      ExtMapBuffer3[i] = iMAOnArray(ExtMapBuffer2, 0, Periodo, 0, MODE_EMA, i);
   }

   // Chequeo Lineas Superior e Inferior de Alarma
   Comment("");
   double ALSup = ObjectGet("ALSup", OBJPROP_PRICE1);
   if (GetLastError() != 4202)   // Si no existe la linea, nada
      if (ExtMapBuffer1[0] >= ALSup) {
         PlaySound(AlertWav);
         Comment("ALARMA: Limite Superior !");
      }

   double ALInf = ObjectGet("ALInf", OBJPROP_PRICE1);
   if (GetLastError() != 4202)   // Si no existe la linea, nada
      if (ExtMapBuffer1[0] <= ALInf) {
         PlaySound(AlertWav);
         Comment("ALARMA: Limite Inferior !");
      }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+