//+------------------------------------------------------------------+
//|                                           FS-3Martingales.mq4   |
//|                                                Copyright 2017   |
//|           This is  version        1                             |
//+-----------------------------------------------------------------+

#property copyright "Copyright 2017,"
#property link      " "

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_width1 2
#property indicator_width2 2

// colors of indicators 

#property indicator_color1 LightSkyBlue
#property indicator_color2 Red

//#property indicator_color6 Red
//#property indicator_color7 Aqua 


//---- input parameters
//
extern double    TimeFrame  =  0;
extern int       Per1       =  4; // Periods of MA1 
extern int       Per2       =  21; // Periods of MA2 
extern int       Per3       =  50; // Periods of MA3 
extern int       ApPrice    =  0; // Price mode : 0-Close,1-Open,2-High,3-Low,4-Median,5-Typical,6-Weighted

extern int       KPeriods   =  8; // K periods
extern int       DPeriods   =  4; // D periods
extern int       Slowing    =  4; // Period of smoothing
extern int       StModeMA   =  0; // Mode of Moving Average
extern int       StPrice    =  0; // Stoch PriceField 0=HighLow  1=Close/Close

extern int       MAMe       =  2; // Type of Moving Average  for MA50     2=Smooth
//extern double    CandleSize =  0.08; // Size of CandleBody 0.08=8 pips in USDJPY  8.0=8 units in DAX 0.0008=8 pips in EURUSD
//extern int       Mult       =  1; // Multiplier 1,2 or 3 candles for allowable deviation of MA from Peak or Trough
   
extern double    AShift     =  0.060; // Arrow Shift
extern double    OverBought =  80; // OverBought Level
extern double    OverSold   =  20; // OverSold Level 

//---- buffers
double AUp[];
double ADn[];


double FUp[]; // 3 Advancing Soldiers
double FDn[]; // 3 Black Crows
double PrevMA=0;
double MA=0;
double Hi=0;
double Lo=0;
int Longs=0; //Number of Long Signals executed
int Shorts=0; //Number of Short Signals executed
int TotalSi=Longs+Shorts;
int Bar=0;
int NewTrendOB=1;
int NewTrendOS=1;
//double Mid=1 ; // 


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|

int init() {
   IndicatorBuffers(2);
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexArrow(0, 71);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, 72);
/*   SetIndexStyle(5, DRAW_ARROW);

*/   
   SetIndexBuffer(0, AUp);
   SetIndexBuffer(1, ADn);
   
//   
//   
//   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1, Lime);
//   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1, Pink);

   SetIndexLabel(0,"Buy");
   SetIndexLabel(1,"Sell");
   SetIndexLabel(2,"SmoothMA");




   return (0);
   
  }


//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   int i;
   
   for( i=0; i<Bars; i++ ) AUp[i]=EMPTY_VALUE;
   for( i=0; i<Bars; i++ ) ADn[i]=EMPTY_VALUE;
   for( i=0; i<Bars; i++ ) FUp[i]=EMPTY_VALUE;
   for( i=0; i<Bars; i++ ) FDn[i]=EMPTY_VALUE;
   Comment("                                                                ");     

   return (0);
   
  }


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+


int start()
  {
   int      i=Bars-10; // almost all bars  

   while(i>=0)
     {
     
     // check for new bar formation
bool Check_New_Bar = Function_New_Bar(); // Function call
/*
if (Check_New_Bar==false)
{
Comment("Waiting for new bar...  ");
return(0);
}
*/
      
//      FUp[i+1]=EMPTY_VALUE;      FDn[i+1]=EMPTY_VALUE;
      AUp[i+1]=EMPTY_VALUE;      ADn[i+1]=EMPTY_VALUE;
   
//Comment("Values read:  bars= ",Bars," i rec= ", i," -- Closes : ",Close[i+1]," " , Close[i+2], " ThreeAdvSol = ",ThreeAdvSolR,"  FUp=",FUp[i],"  FUp+1=",FUp[i+1])  ;     
      
  
       FDn[i+1]=Low[i+1]-AShift;
       PrevMA=MA;

   
//       MA=iMA(NULL, 0, MAPeriods,0,2,0, i+1);
 //      double MA=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//       WMA[i+1]=MA;
 //Comment("WMA i+1 = " , WMA[i+1],"  WMA i+2 = " , WMA[i+2],"  MA:= ",MA ," AUp i+1 = " , AUp[i+1] , " ThreeAdvSol = ",ThreeAdvSolR,"  FUp=",FUp[i],"  FUp+1=",FUp[i+1])  ;                 
 double      M1=iMA(NULL,0,Per1, 0, 0, 0,i+1);
 double      M2=iMA(NULL,0,Per2, 0, 0, 0,i+1);
 double      M3=iMA(NULL,0,Per3, 0, 2, 0,i+1);
 double      K1=iStochastic(NULL,0,KPeriods,DPeriods,Slowing,StModeMA,StPrice,0,i+1);
 double      K2=iStochastic(NULL,0,KPeriods,DPeriods,Slowing,StModeMA,StPrice,0,i+2);

 if (K2>=OverBought && K1<OverBought)
 {
 NewTrendOB=1;
 }
 
  if (K2<=OverSold && K1>OverSold)
 {
 NewTrendOS=1;
 }

 //+CandleSize
//Comment("AUp1= ", AUp[i+1], "AUp2= ", AUp[i+2],"Close = ", Close[1]," m1 = " , M1," m2 := ",M2," m3 = ",M3," Stoch = ", K1);// Long," SSignals := ", Short," TotalSig : ",Long+Short); //," AUp i+1 = " , AUp[i+1] , " ThreeAdvSol = ",ThreeAdvSolR,"  FUp+1=",FUp[i+1])  ;     
//&& AUp[i+2]!=EMPTY_VALUE
      if (Close[i+1]>Open[i+1] && AUp[i+2]==EMPTY_VALUE && Close[i+1]>M1 && Close[i+1]>M2 && Close[i+1]>M3 
           && K1>= OverBought && NewTrendOB==1  )
         {
         AUp[i+1]=Low[i+1]-2*AShift; Longs=Longs+1;
         NewTrendOB=0;

         }
      if (Close[i+1]<Open[i+1] && ADn[i+2]==EMPTY_VALUE && Close[i+1]<M1 && Close[i+1]<M2 && Close[i+1]<M3 
           && K1<= OverSold  && NewTrendOS==1 )
         {
         ADn[i+1]=High[i+1]+2*AShift; Shorts=Shorts+1;
         NewTrendOS=0;        }
    
  
  	   i--;
 	   
    }

   return(0);
  }
//+------------------------------------------------------------------+


//+----------------New bar----------------------+
int Function_New_Bar() // Funct. detecting ..
{ 
static datetime New_Time=0; // Time of the current bar
bool Check_New_Bar=false; // No new bar
if(New_Time!=Time[0]) // Compare time
{
New_Time=Time[0]; // Now time is so
Check_New_Bar=true; // A new bar detected
}
return(Check_New_Bar);
}