// https://forex-station.com/viewtopic.php?p=1295414062#p1295414062
// modified by banzai; July/4/20
// thanks to mrtools codes/template
// not for sale, auction, rent, nor lease

#property copyright "www,forex-tsd.com"
#property link      "www.forex-station.com"
#property strict
#property indicator_chart_window

#property indicator_buffers 2

extern string              note1                = "------------------------------";
extern int                 MA_Period            = 10;
extern ENUM_APPLIED_PRICE  Applied_price        = PRICE_CLOSE;

//template code start1
extern ENUM_LINE_STYLE     FirstLineStyle       = STYLE_DOT;
extern ENUM_LINE_STYLE     SecondLineStyle      = STYLE_SOLID;
extern int                 FirstLineWidth       = 1;
extern int                 SecondLineWidth      = 1;
extern color               FirstLineColor       = clrRed;
extern color               SecondLineColor      = clrBlue;
extern string              button_note1          = "------------------------------";
extern ENUM_BASE_CORNER    btn_corner            = CORNER_LEFT_UPPER; // chart corner for anchoring
extern string              btn_text              = "Oracle";
extern color               btn_text_color        = clrWhite;
extern color               btn_background_color  = clrDimGray;
extern int                 btn_Offset_x          = 20;                                     //btn__x
extern int                 btn_Offset_y          = 13;                                     //btn__y
extern int                 btn_Width             = 60;                                 //btn__width
extern int                 btn_Height            = 20;                                //btn__height
extern int                 btn_FontSize          = 10;                             //btn__font size
extern string              UniqueButtonID        = "OracleMoveMod";
extern string              button_note2          = "------------------------------";
bool                       inpSelection          = false;                      // Highlight to move             
//template code end1

//---
double Buffer1[], Buffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//template code start2
   if (ObjectFind(UniqueButtonID)!=0)
   {
         ObjectCreate    (ChartID(),UniqueButtonID,OBJ_BUTTON,         0,0,0);
         ObjectSetString (ChartID(),UniqueButtonID,OBJPROP_TEXT,       btn_text);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_FONTSIZE,   btn_FontSize);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_CORNER,     btn_corner);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_COLOR,      btn_text_color);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_BGCOLOR,    btn_background_color);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_YDISTANCE,  btn_Offset_y);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_XDISTANCE,  btn_Offset_x);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_XSIZE,      btn_Width);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_YSIZE,      btn_Height);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_SELECTABLE, inpSelection);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_SELECTED,   inpSelection);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_HIDDEN,     true);
         ObjectSetInteger(ChartID(),UniqueButtonID,OBJPROP_STATE,      true);
   }

//--- indicator buffers mapping
   SetIndexBuffer(0, Buffer1);
   SetIndexBuffer(1, Buffer2);
   
   if (GetButtonState(UniqueButtonID)!="off")
      {
       SetIndexStyle(0, DRAW_LINE, FirstLineStyle, FirstLineWidth, FirstLineColor);
       SetIndexStyle(1, DRAW_LINE, SecondLineStyle, SecondLineWidth, SecondLineColor);
      }
   else for (int i=0; i<2; i++) SetIndexStyle(i,DRAW_NONE);
//template code end2

   IndicatorDigits(Digits + 1);
   IndicatorShortName("Oracle Move");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
         int limit = 1;
         if(prev_calculated==0) {
            limit = rates_total -1;
            for (int i = limit; i >1; i--) {
            Buffer1[i] = 0;
            Buffer2[i] = 0;
            }
         }
   
         for (int i = limit; i >=0; i--) {
            Buffer2[i] = 2.0 * iMA(NULL, 0, (int)MathFloor(MA_Period / 2), 0, MODE_LWMA, Applied_price, i) - iMA(NULL, 0, MA_Period, 0, MODE_LWMA, Applied_price, i);
            Buffer1[i] = iMAOnArray(Buffer2, 0, (int)MathFloor(MathSqrt(MA_Period)), 0, MODE_LWMA, i);
         }         
//--- return value of prev_calculated for next call
   return(rates_total);

  }
//-------------------------------------------------------- 
void OnDeinit(const int reason)
{
      switch(reason)
      {
         case REASON_PARAMETERS  :
         case REASON_CHARTCHANGE :
         case REASON_RECOMPILE   :
         case REASON_CLOSE       : break;
         default                 : ObjectDelete(UniqueButtonID);
      }
}
//--------------------------------------------------------
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
   static string prevState ="";
   if (id==CHARTEVENT_OBJECT_CLICK && sparam==UniqueButtonID)
   {
         string newState = GetButtonState(UniqueButtonID);
         if (newState!=prevState)
         if (newState=="off")
         { 
            for (int banzai=0; banzai<2; banzai++)
                SetIndexStyle(banzai,DRAW_NONE);
                prevState=newState; 
         }
         else  
         { 
         SetIndexStyle(0, DRAW_LINE, FirstLineStyle,  FirstLineWidth,  FirstLineColor);
         SetIndexStyle(1, DRAW_LINE, SecondLineStyle, SecondLineWidth, SecondLineColor);
          prevState=newState; 
         }
         ObjectSetString(ChartID(),UniqueButtonID,OBJPROP_TEXT,btn_text);
   }
}  
//+------------------------------------------------------------------+
string GetButtonState(string whichbutton)
{
      bool selected = ObjectGetInteger(ChartID(),whichbutton,OBJPROP_STATE);
      if (selected) { return ("on");  } 
      else          { return ("off"); }
}
//template code end3
//+------------------------------------------------------------------+


