//+------------------------------------------------------------------+
//|                                           OnChart Stochastic.mq4 |
//|                                                           mladen |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "https://www.forexfactory.com/thread/post/14050778#post14050778"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 DarkSlateGray
#property indicator_color2 DarkSlateGray
#property indicator_color3 DarkSlateGray
#property indicator_color4 LimeGreen
#property indicator_color5 PaleVioletRed
#property indicator_color6 PaleVioletRed
#property indicator_color7 Red
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 2
#property indicator_style1 STYLE_DOT
#property indicator_style7 STYLE_DOT

//---- parameters
//
//
//
//

extern string timeFrame = "Current time frame";
extern int KPeriod    = 14;
extern int Slowing    =  3;
extern int DPeriod    =  3;
extern int maPeriod   = 20;
extern int maMethod   =  1;
extern int maPrice    =  0;
extern int overBought = 80;
extern int overSold   = 20;
extern bool   ColorOnSlope  = false;
enum showPR { lOFF,/*Labels OFF*/ lPrice,/*Price*/ lStoch/*Stochastic*/ };
extern showPR  ShowLabels  = lStoch;
extern color   PriceColor  = clrLime;  //PaleVioletRed
extern int     PriceSize   = 15;
extern string  UniqueID    = "OnChartStoch";

//---- buffers
//
//
//
//

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double stoDa[];
double stoDb[];
double state[];
int    TimeFrame;
int    atrTimeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexBuffer(4,stoDa);
   SetIndexBuffer(5,stoDb);
   SetIndexBuffer(6,ExtMapBuffer5);
   SetIndexBuffer(7,state);

   //
   //
   //
   //
   //
   
   TimeFrame = stringToTimeFrame(timeFrame);
      switch (TimeFrame)
         {
            case PERIOD_M1:  atrTimeFrame = PERIOD_M30; break;
            case PERIOD_M5:  atrTimeFrame = PERIOD_H1;  break;
            case PERIOD_M15: atrTimeFrame = PERIOD_H4;  break;
            case PERIOD_M30: atrTimeFrame = PERIOD_D1;  break;
            case PERIOD_H1:  atrTimeFrame = PERIOD_D1;  break;
            case PERIOD_H4:  atrTimeFrame = PERIOD_W1;  break;
            case PERIOD_D1:  atrTimeFrame = PERIOD_W1;  break;
            default:         atrTimeFrame = PERIOD_MN1;
         }
   return(0);
}
int deinit()
{
   string lookFor       = UniqueID+":";
   int    lookForLength = StringLen(lookFor);
   for (int i = ObjectsTotal(); i>= 0; i--)
   {
      string name = ObjectName(i); if (StringSubstr(name,0,lookForLength)==lookFor) ObjectDelete(name);
   }
   return (0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit=MathMax(Bars-counted_bars,MathMin(5*atrTimeFrame/Period(),Bars-1));   
      

   //----
   //
   //
   //
   //
   
      if (state[limit] ==-1) CleanPoint(limit,stoDa,stoDb);
      for (int i=limit; i>=0; i--)
         {
            int y = iBarShift(NULL,TimeFrame   ,Time[i]);
            int r = iBarShift(NULL,atrTimeFrame,Time[i]);
               double avgRange     = iATR(NULL,atrTimeFrame,maPeriod,r);
               double maValue      = iMA(NULL,TimeFrame,maPeriod,0,maMethod,maPrice,y);
               double stochValue   = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,maMethod,0,MODE_MAIN,  y);
               double stochValue2  = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,maMethod,0,MODE_MAIN,  y+1);
               double signalValue  = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,maMethod,0,MODE_SIGNAL,y);
               state[i]  = state[i+1];
               if (ColorOnSlope)
               {
                  if (stochValue > stochValue2) state[i] =  1;
                  if (stochValue < stochValue2) state[i] = -1;
               }
               else
               {
                  if (stochValue > signalValue) state[i] =  1;
                  if (stochValue < signalValue) state[i] = -1;
               }            
               
               //
               //
               //
               //
               //
               
               stoDa[i]         = EMPTY_VALUE;
               stoDb[i]         = EMPTY_VALUE;
               ExtMapBuffer1[i] = maValue;
               ExtMapBuffer2[i] = maValue+(avgRange*(overBought-50)/100);
               ExtMapBuffer3[i] = maValue-(avgRange*(50-  overSold)/100);
               ExtMapBuffer4[i] = maValue+(stochValue -50)/100*avgRange;
               ExtMapBuffer5[i] = maValue+(signalValue-50)/100*avgRange;
               if (state[i] ==-1) PlotPoint(i,stoDa,stoDb,ExtMapBuffer4);
   }
   if (ShowLabels>0)   //enum showPR { lOFF, lPrice, lStoch };
   {
         //string  name = UniqueID+":"+"ocs1";
         // ObjectCreate(name,OBJ_ARROW,0,0,0);
            // ObjectSet(name,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
            // ObjectSet(name,OBJPROP_COLOR,PriceColor);
            // ObjectSet(name,OBJPROP_WIDTH,PriceSize);
            // ObjectSet(name,OBJPROP_PRICE1,ExtMapBuffer1[0]);
            // ObjectSet(name,OBJPROP_TIME1,Time[0]);
          
          //name = UniqueID+":"+"ocs2";
         // ObjectCreate(name,OBJ_ARROW,0,0,0);
            // ObjectSet(name,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
            // ObjectSet(name,OBJPROP_COLOR,PriceColor);
            // ObjectSet(name,OBJPROP_WIDTH,PriceSize);
            // ObjectSet(name,OBJPROP_PRICE1,ExtMapBuffer2[0]);
            // ObjectSet(name,OBJPROP_TIME1,Time[0]);
             
          //name = UniqueID+":"+"ocs3";
         // ObjectCreate(name,OBJ_ARROW,0,0,0);
            // ObjectSet(name,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
            // ObjectSet(name,OBJPROP_COLOR,PriceColor);
            // ObjectSet(name,OBJPROP_WIDTH,PriceSize);
            // ObjectSet(name,OBJPROP_PRICE1,ExtMapBuffer3[0]);
            // ObjectSet(name,OBJPROP_TIME1,Time[0]);
             
          ENUM_OBJECT oType = ShowLabels==1 ? OBJ_ARROW : OBJ_TEXT;  
          datetime T1 = ShowLabels==1 ? Time[0] : Time[0]+60*_Period*12/fmax(0.5,ChartGetInteger(0,CHART_SCALE,0));
             
          string name = UniqueID+":"+"ocs4";
          ObjectDelete(name);
          ObjectCreate(name,oType,0,T1,ExtMapBuffer4[0]);
             ObjectSetText(name,DoubleToStr(stochValue,1),PriceSize,"Verdana",PriceColor);  
             ObjectSet(name,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
             ObjectSet(name,OBJPROP_COLOR,PriceColor);
             ObjectSet(name,OBJPROP_WIDTH,PriceSize);
             ObjectSet(name,OBJPROP_SELECTABLE,false);
             ObjectSet(name,OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER); 
             
            // name = UniqueID+":"+"ocs5";
          //ObjectCreate(name,OBJ_ARROW,0,0,0);
             //ObjectSet(name,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
            // ObjectSet(name,OBJPROP_COLOR,PriceColor);
            // ObjectSet(name,OBJPROP_WIDTH,PriceSize);
            // ObjectSet(name,OBJPROP_PRICE1,ExtMapBuffer5[0]);
            // ObjectSet(name,OBJPROP_TIME1,Time[0]);
             
       }
   
   //
   //
   //
   //
   //
         
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   StringToUpper(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}