//+------------------------------------------------------------------+
//|                                         Freebie Signal Alert.mq4 |
//|                     Copyright © 2010, Gorbushin Grigory (Dzhini) |
//|                                              grin2000@rbcmail.ru |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2010, Gorbushin Grigory (Dzhini)"
#property link      "grin2000@rbcmail.ru"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 3
#property indicator_width2 3

//----
extern color color1 = Lime;
extern color color2 = Red;
extern bool SoundON = true;
extern bool Filter = true;
extern bool OnClose = true;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double alertTag;


//----
int ExtCountedBars=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators

   SetIndexStyle(0, DRAW_ARROW, EMPTY,3, color1);
   SetIndexArrow(0, 233);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,3, color2);
   SetIndexArrow(1, 234);

//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);

   
//---- initialization done



   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

   double HAOpen, HAClose, HAOpenPrev, HAClosePrev,StochasticMain,StochasticSignal;

   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-ExtCountedBars-1;
   while(pos>=0)
     {

     if(!OnClose)
     {
     HAOpen=iCustom(Symbol(),0,"Heiken Ashi",2,pos);
     HAOpenPrev=iCustom(Symbol(),0,"Heiken Ashi",2,pos+1);
     HAClose=iCustom(Symbol(),0,"Heiken Ashi",3,pos);
     HAClosePrev=iCustom(Symbol(),0,"Heiken Ashi",3,pos+1);
          
     StochasticMain=iStochastic(Symbol(),0,10,3,3,0,0,0,pos);
     StochasticSignal=iStochastic(Symbol(),0,10,3,3,0,0,1,pos); 	   

     if (!Filter&&StochasticMain>StochasticSignal&&HAOpenPrev>HAClosePrev&&HAOpen<HAClose) {ExtMapBuffer1[pos]=Low[pos]-5*Point;ExtMapBuffer2[pos]=0;}  
     if (!Filter&&StochasticMain<StochasticSignal&&HAOpenPrev<HAClosePrev&&HAOpen>HAClose) {ExtMapBuffer2[pos]=High[pos]+5*Point;ExtMapBuffer1[pos]=0;}

     if (Filter&&StochasticMain>StochasticSignal&&StochasticSignal<50&&HAOpenPrev>HAClosePrev&&HAOpen<HAClose) {ExtMapBuffer1[pos]=Low[pos]-5*Point;ExtMapBuffer2[pos]=0;}  
     if (Filter&&StochasticMain<StochasticSignal&&StochasticSignal>50&&HAOpenPrev<HAClosePrev&&HAOpen>HAClose) {ExtMapBuffer2[pos]=High[pos]+5*Point;ExtMapBuffer1[pos]=0;}
 	   
 	    	   
 	  pos--;
 	   
 	  if (SoundON==true && pos==1 && ExtMapBuffer1[pos] > ExtMapBuffer2[pos] && alertTag!=Time[0])
 	  {
         Alert("Freebie Alert (", Symbol(), ", ", Period(), ")--Possible BUY ");
         PlaySound("Alert2.wav");
         alertTag = Time[0];
     }
     if (SoundON==true && pos==1 && ExtMapBuffer2[pos]> ExtMapBuffer1[pos] && alertTag!=Time[0])
     {
         Alert("Freebie Alert (", Symbol(), ", ", Period(), ")--Possible SELL ");
         PlaySound("Alert2.wav");
         alertTag = Time[0];
     } 
 	 }
 	 
 	 if (OnClose)
 	 {
     HAOpen=iCustom(Symbol(),0,"Heiken Ashi",2,pos+1);
     HAOpenPrev=iCustom(Symbol(),0,"Heiken Ashi",2,pos+2);
     HAClose=iCustom(Symbol(),0,"Heiken Ashi",3,pos+1);
     HAClosePrev=iCustom(Symbol(),0,"Heiken Ashi",3,pos+2);
          
     StochasticMain=iStochastic(Symbol(),0,10,3,3,0,0,0,pos+1);
     StochasticSignal=iStochastic(Symbol(),0,10,3,3,0,0,1,pos+1); 	   

     if (!Filter&&StochasticMain>StochasticSignal&&HAOpenPrev>HAClosePrev&&HAOpen<HAClose) {ExtMapBuffer1[pos+1]=Low[pos+1]-5*Point;ExtMapBuffer2[pos+1]=0;}  
     if (!Filter&&StochasticMain<StochasticSignal&&HAOpenPrev<HAClosePrev&&HAOpen>HAClose) {ExtMapBuffer2[pos+1]=High[pos+1]+5*Point;ExtMapBuffer1[pos+1]=0;}

     if (Filter&&StochasticMain>StochasticSignal&&StochasticSignal<50&&HAOpenPrev>HAClosePrev&&HAOpen<HAClose) {ExtMapBuffer1[pos+1]=Low[pos+1]-5*Point;ExtMapBuffer2[pos+1]=0;}  
     if (Filter&&StochasticMain<StochasticSignal&&StochasticSignal>50&&HAOpenPrev<HAClosePrev&&HAOpen>HAClose) {ExtMapBuffer2[pos+1]=High[pos+1]+5*Point;ExtMapBuffer1[pos+1]=0;}
 	   
 	    	   
 	  pos--;
 	   
 	  if (SoundON==true && pos==1 && ExtMapBuffer1[pos] > ExtMapBuffer2[pos] && alertTag!=Time[0])
 	  {
         Alert("Freebie Alert Confirmed (", Symbol(), ", ", Period(), ")-- BUY ");
         PlaySound("Alert2.wav");
         alertTag = Time[0];
     }
     if (SoundON==true && pos==1 && ExtMapBuffer2[pos]> ExtMapBuffer1[pos] && alertTag!=Time[0])
     {
         Alert("Freebie Alert Confirmed (", Symbol(), ", ", Period(), ")-- SELL ");
         PlaySound("Alert2.wav");
         alertTag = Time[0];
     } 
 	 }
 	   
 	   

     }
     

//----
   return(0);
  }
//+------------------------------------------------------------------+

