//+------------------------------------------------------------------+
//|                                                      PivotEa.mq4 |
//|                                                       Matheszabi |
//|                                        https://www.mathesoft.net |
//+------------------------------------------------------------------+
#property copyright "Matheszabi"
#property link      "https://www.mathesoft.net"
#property version   "1.00"
#property strict

extern bool Draw_Main_Lines = true;

extern color R3_color = OrangeRed;
extern color R2_color = OrangeRed;
extern color R1_color = OrangeRed;
extern color PP_color = DarkKhaki;
extern color S1_color = Red;
extern color S2_color = Red;
extern color S3_color = Red;

extern int MinRange  = 600;
// weakness: when the previous bar has a very few movement!

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   dailyPivotPoint = new PivotPoint(Symbol(), PERIOD_D1, 1 );
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   delete(dailyPivotPoint);
   
  Comment(""); 
  ObjectDelete("pp_Line");
  ObjectDelete("r1_Line");
  ObjectDelete("r2_Line");
  ObjectDelete("r3_Line");
  ObjectDelete("s1_Line");
  ObjectDelete("s2_Line");
  ObjectDelete("s3_Line");
  }
  




//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   doStuff();
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()//1Min
{   
   doStuff();
}
  
  
void doStuff()
{
   string comment = dailyPivotPoint.getComment();
   if(dailyPivotPoint.rangePoint < MinRange){      
      comment += "\nRange to low: possible fake signals: "+DoubleToStr(dailyPivotPoint.rangePoint,0)+" < "+MinRange;
   }
   Comment(comment);
   if(Draw_Main_Lines){
      drawMainLines();
   }   
}  
  
  
  
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+

  
class PivotPoint
{
   private:
      string mSymbol;
      int mTimeFrame;
      int mShift; //  1: last closed candle, 2 before last, 0: current candle
      
      datetime mCheckedCandle;
      double high, low, close;
    
   public: 
      // values
      double rangePoint, pp,r1,s1,r2,s2,r3,s3;
      
      void refresh();
      
      void PivotPoint(string symbol, int timeframe, int shift); 
      
      string getComment();
      
};

void PivotPoint::PivotPoint(string symbol, int timeframe, int shift)
{
   mSymbol = symbol;
   mTimeFrame = timeframe;
   mShift = shift;
   
   refresh();
}

void PivotPoint::refresh()
{
   high = iHigh(mSymbol, mTimeFrame, mShift);
   low = iLow(mSymbol, mTimeFrame, mShift);
   close = iClose(mSymbol, mTimeFrame, mShift);
   mCheckedCandle = iTime(mSymbol, mTimeFrame, mShift);
   double pointsMultiplier = MarketInfo(mSymbol,MODE_POINT);
   Print("pointsMultiplier:"+pointsMultiplier);;
   
   //calc:
   rangePoint = (high-low) / pointsMultiplier;
   pp = (high + low + close) / 3;      
   r1 = 2*pp  - low;
   s1 = 2*pp - high;   
   r2 = pp + (high - low);
   s2 = pp - (high - low);   
   r3 = 2*pp + (high - 2*low);
   s3 = 2*pp - (2*high - low);
}

string PivotPoint::getComment()
{
   int digits = (int)MarketInfo(Symbol(), MODE_DIGITS);
   string comment = "\n";
   
   comment += "r3 = "+DoubleToStr(r3, digits-1)+"\n";
   comment += "r2 = "+DoubleToStr(r2, digits-1)+"\n";
   comment += "r1 = "+DoubleToStr(r1, digits-1)+"\n";
   comment += "\n";
   comment += "pp = "+DoubleToStr(pp, digits-1)+"\n";
   comment += "\n";
   comment += "s1 = "+DoubleToStr(s1, digits-1)+"\n";
   comment += "s2 = "+DoubleToStr(s2, digits-1)+"\n";
   comment += "s3 = "+DoubleToStr(s3, digits-1)+"\n";
   return comment;
}

PivotPoint *dailyPivotPoint;


void drawMainLines()
{
   ObjectDelete("pp_Line");
   ObjectCreate("pp_Line", OBJ_HLINE,0, CurTime(),dailyPivotPoint.pp);
   ObjectSet("pp_Line",OBJPROP_COLOR,PP_color);
   ObjectSet("pp_Line",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("pp_Line",OBJPROP_WIDTH,1);

   ObjectDelete("r1_Line");
   ObjectCreate("r1_Line", OBJ_HLINE,0, CurTime(),dailyPivotPoint.r1);
   ObjectSet("r1_Line",OBJPROP_COLOR,R1_color);
   ObjectSet("r1_Line",OBJPROP_STYLE,STYLE_DOT);
   ObjectSet("r1_Line",OBJPROP_WIDTH,1);

   ObjectDelete("r2_Line");
   ObjectCreate("r2_Line", OBJ_HLINE,0, CurTime(),dailyPivotPoint.r2);
   ObjectSet("r2_Line",OBJPROP_COLOR,R2_color);
   ObjectSet("r2_Line",OBJPROP_STYLE,STYLE_DASH);
   ObjectSet("r2_Line",OBJPROP_WIDTH,1);

   ObjectDelete("r3_Line");
   ObjectCreate("r3_Line", OBJ_HLINE,0, CurTime(),dailyPivotPoint.r3);
   ObjectSet("r3_Line",OBJPROP_COLOR,R3_color);
   ObjectSet("r3_Line",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("r3_Line",OBJPROP_WIDTH,1);

   ObjectDelete("s1_Line");
   ObjectCreate("s1_Line", OBJ_HLINE,0, CurTime(),dailyPivotPoint.s1);
   ObjectSet("s1_Line",OBJPROP_COLOR,S1_color);
   ObjectSet("s1_Line",OBJPROP_STYLE,STYLE_DOT);
   ObjectSet("s1_Line",OBJPROP_WIDTH,1);

   ObjectDelete("s2_Line");
   ObjectCreate("s2_Line", OBJ_HLINE,0, CurTime(),dailyPivotPoint.s2);
   ObjectSet("s2_Line",OBJPROP_COLOR,S2_color);
   ObjectSet("s2_Line",OBJPROP_STYLE,STYLE_DASH);
   ObjectSet("s2_Line",OBJPROP_WIDTH,1);

   ObjectDelete("s3_Line");
   ObjectCreate("s3_Line", OBJ_HLINE,0, CurTime(),dailyPivotPoint.s3);
   ObjectSet("s3_Line",OBJPROP_COLOR,S3_color);
   ObjectSet("s3_Line",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("s3_Line",OBJPROP_WIDTH,1);
}