Download MetaTrader 5

How do I let the Pivot Indicator Update after cycle?

To add comments, please log in or register
A31Trader
6
A31Trader  

Hi All,

I am fairly new to coding MQL4 and this might be something simple.

Basically this indicator should display the daily Pivots on the selected chart and timeframe.

If I attach my Indicator, it draws the pivots perfectly... but as soon as a new day begins, the old pivots still show and does not update.

For the life of me I cant seem to find the problem, can anyone please assist me?

Here is my code:

#property strict
#property indicator_chart_window

#property indicator_buffers 4;

double YesterdayHigh;
double YesterdayLow;
double YesterdayClose;
double Day_Price[][6];
double Pivot,S1,S2,S3,R1,R2,R3;

extern color Daily_Pivot      = Magenta;
extern color Daily_S_Levels   = Cyan;
extern color Daily_R_Levels   = Red;
extern color Daily_Mid_Level  = LightSalmon;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

   
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{
    if (true) ObjectDelete("Obj_Daily_PP");
    if (true) ObjectDelete("Obj_Daily_PP_Label");
    
    if (true) ObjectDelete("Obj_Daily_S1");
    if (true) ObjectDelete("Obj_Daily_S1_Label");
    
    if (true) ObjectDelete("Obj_Daily_S2");
    if (true) ObjectDelete("Obj_Daily_S2_Label");
    
    if (true) ObjectDelete("Obj_Daily_S3");
    if (true) ObjectDelete("Obj_Daily_S3_Label");
    
    if (true) ObjectDelete("Obj_Daily_R1");
    if (true) ObjectDelete("Obj_Daily_R1_Label");
    
    if (true) ObjectDelete("Obj_Daily_R2");
    if (true) ObjectDelete("Obj_Daily_R2_Label");
    
    if (true) ObjectDelete("Obj_Daily_R3");
    if (true) ObjectDelete("Obj_Daily_R3_Label");

}

int start()
{
   ArrayCopyRates(Day_Price,(Symbol()), 1440);
   
   YesterdayHigh  = Day_Price[1][3];
   YesterdayLow   = Day_Price[1][2];
   YesterdayClose = Day_Price[1][4];
   
   Pivot = (YesterdayClose + YesterdayHigh + YesterdayLow) / 3;
   S1 = (2*Pivot)-YesterdayHigh;
   R1 = (2*Pivot)-YesterdayLow;
   
   R2 = Pivot+(R1-S1);
   S2 = Pivot-(R1-S1);
   
   R3 = (YesterdayHigh + (2*(Pivot-YesterdayLow)));
   S3 = (YesterdayLow - (2*(YesterdayHigh-Pivot)));  
   
   Comment("Pivot: " + DoubleToString(Pivot));
   
   TimeToStr(CurTime());
   
   //***************************************************************************************************************************
   // CREATE DAILY PIVOT POINT
   //***************************************************************************************************************************
   if(ObjectFind("Obj_Daily_PP") != 0){
      ObjectCreate("Obj_Daily_PP", OBJ_HLINE,0, CurTime(),Pivot);
      ObjectSet("Obj_Daily_PP", OBJPROP_COLOR, Daily_Pivot);
      ObjectSet("Obj_Daily_PP", OBJPROP_STYLE, STYLE_DASH);
   }
   else {
      ObjectMove("Obj_Daily_PP", 0, Time[50], Pivot);   
   }

   if(ObjectFind("Obj_Daily_PP_Label") != 0)
   {
      ObjectCreate("Obj_Daily_PP_Label", OBJ_TEXT, 0, Time[50], Pivot);
      ObjectSetText("Obj_Daily_PP_Label", ("Daily Pivot: " + DoubleToString(Pivot)), 8, "Arial", Daily_Pivot);
   }
   else {
      ObjectMove("Obj_Daily_PP_Label", 0, Time[50], Pivot);
   }
   
   ObjectsRedraw();
   
   //***************************************************************************************************************************
   // CREATE DAILY S1
   //***************************************************************************************************************************
   if(ObjectFind("Obj_Daily_S1") != 0){
      ObjectCreate("Obj_Daily_S1", OBJ_HLINE,0, CurTime(),S1);
      ObjectSet("Obj_Daily_S1", OBJPROP_COLOR, Daily_S_Levels);
      ObjectSet("Obj_Daily_S1", OBJPROP_STYLE, STYLE_DASH);
   }
   else {
      ObjectMove("Obj_Daily_S1", 0, Time[50], S1);   
   }

   if(ObjectFind("Obj_Daily_S1_Label") != 0)
   {
      ObjectCreate("Obj_Daily_S1_Label", OBJ_TEXT, 0, Time[50], S1);
      ObjectSetText("Obj_Daily_S1_Label", ("Daily S1: " + DoubleToString(S1)), 8, "Arial", Daily_S_Levels);
   }
   else {
      ObjectMove("Obj_Daily_S1_Label", 0, Time[50], S1);
   }

   ObjectsRedraw();
   
   //***************************************************************************************************************************
   // CREATE DAILY S2
   //***************************************************************************************************************************
   if(ObjectFind("Obj_Daily_S2") != 0){
      ObjectCreate("Obj_Daily_S2", OBJ_HLINE,0, CurTime(),S2);
      ObjectSet("Obj_Daily_S2", OBJPROP_COLOR, Daily_S_Levels);
      ObjectSet("Obj_Daily_S2", OBJPROP_STYLE, STYLE_DASHDOTDOT);
   }
   else {
      ObjectMove("Obj_Daily_S2", 0, Time[50], S2);   
   }

   if(ObjectFind("Obj_Daily_S2_Label") != 0)
   {
      ObjectCreate("Obj_Daily_S2_Label", OBJ_TEXT, 0, Time[50], S2);
      ObjectSetText("Obj_Daily_S2_Label", ("Daily S2: " + DoubleToString(S2)), 8, "Arial", Daily_S_Levels);
   }
   else {
      ObjectMove("Obj_Daily_S2_Label", 0, Time[50], S2);
   }

   ObjectsRedraw();
   
   //***************************************************************************************************************************
   // CREATE DAILY S3
   //***************************************************************************************************************************
   if(ObjectFind("Obj_Daily_S3") != 0){
      ObjectCreate("Obj_Daily_S3", OBJ_HLINE,0, CurTime(),S3);
      ObjectSet("Obj_Daily_S3", OBJPROP_COLOR, Daily_S_Levels);
      ObjectSet("Obj_Daily_S3", OBJPROP_STYLE, STYLE_DOT);
   }
   else {
      ObjectMove("Obj_Daily_S3", 0, Time[50], S3);   
   }

   if(ObjectFind("Obj_Daily_S3_Label") != 0)
   {
      ObjectCreate("Obj_Daily_S3_Label", OBJ_TEXT, 0, Time[50], S3);
      ObjectSetText("Obj_Daily_S3_Label", ("Daily S3: " + DoubleToString(S3)), 8, "Arial", Daily_S_Levels);
   }
   else {
      ObjectMove("Obj_Daily_S3_Label", 0, Time[50], S3);
   }

   ObjectsRedraw();
   
   //***************************************************************************************************************************
   // CREATE DAILY R1
   //***************************************************************************************************************************
   if(ObjectFind("Obj_Daily_R1") != 0){
      ObjectCreate("Obj_Daily_R1", OBJ_HLINE,0, CurTime(),R1);
      ObjectSet("Obj_Daily_R1", OBJPROP_COLOR, Daily_R_Levels);
      ObjectSet("Obj_Daily_R1", OBJPROP_STYLE, STYLE_DASH);
   }
   else {
      ObjectMove("Obj_Daily_R1", 0, Time[50], R1);   
   }

   if(ObjectFind("Obj_Daily_R1_Label") != 0)
   {
      ObjectCreate("Obj_Daily_R1_Label", OBJ_TEXT, 0, Time[50], R1);
      ObjectSetText("Obj_Daily_R1_Label", ("Daily R1: " + DoubleToString(R1)), 8, "Arial", Daily_R_Levels);
   }
   else {
      ObjectMove("Obj_Daily_R1_Label", 0, Time[50], R1);
   }

   ObjectsRedraw();
   
   //***************************************************************************************************************************
   // CREATE DAILY R2
   //***************************************************************************************************************************
   if(ObjectFind("Obj_Daily_R2") != 0){
      ObjectCreate("Obj_Daily_R2", OBJ_HLINE,0, CurTime(),R2);
      ObjectSet("Obj_Daily_R2", OBJPROP_COLOR, Daily_R_Levels);
      ObjectSet("Obj_Daily_R2", OBJPROP_STYLE, STYLE_DASHDOTDOT);
   }
   else {
      ObjectMove("Obj_Daily_R2", 0, Time[50], R2);   
   }

   if(ObjectFind("Obj_Daily_R2_Label") != 0)
   {
      ObjectCreate("Obj_Daily_R2_Label", OBJ_TEXT, 0, Time[50], R2);
      ObjectSetText("Obj_Daily_R2_Label", ("Daily R2: " + DoubleToString(R2)), 8, "Arial", Daily_R_Levels);
   }
   else {
      ObjectMove("Obj_Daily_R2_Label", 0, Time[50], R2);
   }

   ObjectsRedraw();
   
   //***************************************************************************************************************************
   // CREATE DAILY R3
   //***************************************************************************************************************************
   if(ObjectFind("Obj_Daily_R3") != 0){
      ObjectCreate("Obj_Daily_R3", OBJ_HLINE,0, CurTime(),R3);
      ObjectSet("Obj_Daily_R3", OBJPROP_COLOR, Daily_R_Levels);
      ObjectSet("Obj_Daily_R3", OBJPROP_STYLE, STYLE_DOT);
   }
   else {
      ObjectMove("Obj_Daily_R3", 0, Time[50], R3);   
   }

   if(ObjectFind("Obj_Daily_R3_Label") != 0)
   {
      ObjectCreate("Obj_Daily_R3_Label", OBJ_TEXT, 0, Time[50], R3);
      ObjectSetText("Obj_Daily_R3_Label", ("Daily R3: " + DoubleToString(R3)), 8, "Arial", Daily_R_Levels);
   }
   else {
      ObjectMove("Obj_Daily_R3_Label", 0, Time[50], R3);
   }

   ObjectsRedraw();
      
   return(0);
}  
To add comments, please log in or register