Indicator will not draw horizontal or trend lines until timeframe change

 

 After applying a template of which the attached indicator is a part of, the indicator will not draw the horizontal or trend lines until I change timeframe.  After a timeframe change, it works fine. Could someone be so kind as to help me understand what is going on and how to fix the issue. Regards  ----Tom


//+------------------------------------------------------------------+
//|                                                ALERT - PRICE.mq4 |
//|                                     Copyright 2022, Tom Parimore |
//|                                              tparimore@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Tom Parimore"
#property link      "tparimore@yahoo.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

/* Last Updated 9/13/22   6:00 PM

   ----NEW-----


*/



  input  string             xxxxxxxxxxxx          = "*********** Line Properties ***********************************";
  input  color              UpperColor            = RosyBrown;
  input  color              LowerColor            = RosyBrown;
  input  int                LineWidth             = 1;
  input  string             xxxxxxxxxxx           = "*********** Object Locations ***********************************";
  input  int                PriceWindow           = 0;                   // WINDOW 0 IS PRICE WINDOW
  input  int                PriceCorner           = 3;                   // Corner: 0=UL 1=UR 2=LL 3=LR
  
  input  string             PriceText             = "PA";
  input  int                PriceText_XDISTANCE   = 160;
  input  int                PriceText_YDISTANCE   = 37;
  
  input  int                PriceHLS_YDISTANCE    = 55;
  input  int                PriceHLS_XDISTANCE    = 150;
  input  int                PriceTLS_XDISTANCE    = 100;
  input  int                PriceCLR_XDISTANCE    = 51;
  
  input  int                PriceAlert_YDISTANCE  = 32;
  input  int                PriceAlert_XDISTANCE  = 150;
  input  int                PricePush_XDISTANCE   = 75; 
  
  
  
  
  static double prevPrice;
  string upperName = "Upper Alert";
  string lowerName = "Lower Alert";
  double hiAlert, loAlert, topPrice, bottomPrice;
  bool DrawTrendline, upperAlertSent, lowerAlertSent, PricePushNotification, PriceAlert;
  string alertMsg;
  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {


   prevPrice = ((Ask + Bid) / 2.0);


   topPrice =     WindowPriceMax() - (WindowPriceMax() - WindowPriceMin())*0.4;
   bottomPrice =  WindowPriceMax() - (WindowPriceMax() - WindowPriceMin())*0.6;



   ObjectCreate ("PAText", OBJ_LABEL, 0, 0, 0);       
   ObjectSet    ("PAText", OBJPROP_CORNER, PriceCorner);       
   ObjectSet    ("PAText", OBJPROP_XDISTANCE, PriceText_XDISTANCE); 
   ObjectSet    ("PAText", OBJPROP_YDISTANCE, PriceText_YDISTANCE);
   ObjectSetText("PAText", PriceText, 10, "Arial", Yellow);
   
   
   Create_PAAlert();   
   Create_PAPush();
   
   Create_PAHLS();   
   Create_PATLS();
   Create_PACLR();   
   
   
   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[])
  {


   if( ObjectFind(upperName) != 0 && ObjectFind(lowerName) != 0 )                                 // Are both Objects missing from main chart window(0)?
    { ObjectSetInteger(0,"PAAlert",OBJPROP_STATE,false); PriceAlert = false; } 
      
   if(!PriceAlert) return(rates_total); 






   double midPrice = ((Ask + Bid) / 2.0);


   if(rates_total!=prev_calculated) return(rates_total);       //  True implies new period - one alart per bar
    {
      lowerAlertSent = false;
      upperAlertSent = false;
    }
   
   
   
   
   
   hiAlert = 0.0;
   if(ObjectFind(upperName) == 0)                                                                   // Is the Object in the main chart window(0)?
   {
      if(DrawTrendline) hiAlert = NormalizeDouble(ObjectGetValueByShift(upperName,0),Digits);
      else              hiAlert = NormalizeDouble(ObjectGet(upperName, OBJPROP_PRICE1),Digits);
   }
 
 
   loAlert = 0.0;
   if(ObjectFind(lowerName) == 0)                                                                    // Is the Object in the main chart window(0)?
   {
      if(DrawTrendline) loAlert = NormalizeDouble(ObjectGetValueByShift(lowerName,0),Digits);
      else              loAlert = NormalizeDouble(ObjectGet(lowerName, OBJPROP_PRICE1),Digits);
   }
 
    
    
    
    
    
   if(!upperAlertSent && hiAlert > 0.0 && prevPrice < hiAlert && midPrice > hiAlert)
    {
      alertMsg = "Upper Level " + Symbol() + " " + DoubleToStr(midPrice, Digits);
      Alert(alertMsg);
        if(PricePushNotification) SendNotification("Upper Alert on " + Symbol());
        upperAlertSent = true;
    }
      
   if(!lowerAlertSent && loAlert > 0.0 && prevPrice > loAlert && midPrice < loAlert)
   {
     alertMsg = "Lower Level " + Symbol() + " " + DoubleToStr(midPrice, Digits);
     Alert(alertMsg);
     if(PricePushNotification) SendNotification("Lower Alert on " + Symbol());
     lowerAlertSent = true;
   }
               
   
   
   
                        
   prevPrice = midPrice;



   return(rates_total);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| CREATE BUTTONS                                                   |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+


bool Create_PAHLS    (const long              chart_ID=0,               // chart's ID
                      const string            name="PAHLS",             // button name
                      const int               width=46,                 // button width
                      const int               height=20,                // button height
                      const string            text="HLS",               // text
                      const string            font="Courier New",       // font
                      const int               font_size=10,             // font size
                      const color             clr=clrBlack,             // text color
                      const color             back_clr=clrGray,         // background color
                      const bool              back=false                // in the background
                      )
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,PriceWindow,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,PriceCorner);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,PriceHLS_XDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,PriceHLS_YDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,false);
//--- successful execution
   return(true);
   }
   
   
   
   
   
bool Create_PATLS    (const long              chart_ID=0,               // chart's ID
                      const string            name="PATLS",             // button name
                      const int               width=46,                 // button width
                      const int               height=20,                // button height
                      const string            text="TLS",               // text
                      const string            font="Courier New",       // font
                      const int               font_size=10,             // font size
                      const color             clr=clrBlack,             // text color
                      const color             back_clr=clrGray,         // background color
                      const bool              back=false                // in the background
                      )
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,PriceWindow,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,PriceCorner);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,PriceTLS_XDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,PriceHLS_YDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,PricePushNotification);
//--- successful execution
   return(true);
   }
   
   
   
   
   
bool Create_PACLR    (const long              chart_ID=0,               // chart's ID
                      const string            name="PACLR",             // button name
                      const int               width=46,                 // button width
                      const int               height=20,                // button height
                      const string            text="CLR",               // text
                      const string            font="Courier New",       // font
                      const int               font_size=10,             // font size
                      const color             clr=clrBlack,             // text color
                      const color             back_clr=clrGray,         // background color
                      const bool              back=false                // in the background
                      )
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,PriceWindow,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,PriceCorner);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,PriceCLR_XDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,PriceHLS_YDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,PricePushNotification);
//--- successful execution
   return(true);
  }
  



bool Create_PAAlert  (const long              chart_ID=0,               // chart's ID
                      const string            name="PAAlert",           // button name
                      const int               width=70,                 // button width
                      const int               height=20,                // button height
                      const string            text="ALERT",             // text
                      const string            font="Courier New",       // font
                      const int               font_size=10,             // font size
                      const color             clr=clrBlack,             // text color
                      const color             back_clr=clrGray,         // background color
                      const bool              back=false                // in the background
                      )
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,PriceWindow,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,PriceCorner);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,PriceAlert_XDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,PriceAlert_YDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- successful execution
   return(true);
  }




bool Create_PAPush   (const long              chart_ID=0,               // chart's ID
                      const string            name="PAPush",            // button name
                      const int               width=70,                 // button width
                      const int               height=20,                // button height
                      const string            text="PUSH",              // text
                      const string            font="Courier New",       // font
                      const int               font_size=10,             // font size
                      const color             clr=clrBlack,             // text color
                      const color             back_clr=clrGray,         // background color
                      const bool              back=false                // in the background
                      )
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,PriceWindow,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,PriceCorner);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,PricePush_XDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,PriceAlert_YDISTANCE);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- successful execution
   return(true);
  }
  


  
  
  
//+------------------------------------------------------------------+  
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {

    if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="PAAlert")
        {
         if(ObjectGet("PAAlert",OBJPROP_STATE)==false) PriceAlert = false;
         else PriceAlert = true;
         
         lowerAlertSent = false;
         upperAlertSent = false;
        }
     }
 



    if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="PAPush")
        {
         if(ObjectGet("PAPush",OBJPROP_STATE)==false) PricePushNotification = false;
         else PricePushNotification = true;
        }
     }
       
       
       
       
       
    if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="PAHLS")
        {
          ObjectDelete(upperName);
          ObjectDelete(lowerName);
          
          ObjectCreate(upperName, OBJ_HLINE, 0, 0, topPrice);
          ObjectSet(upperName, OBJPROP_COLOR, UpperColor);
          ObjectSet(upperName, OBJPROP_BACK, false);
          ObjectSet(upperName, OBJPROP_WIDTH, LineWidth);
          
          ObjectCreate(lowerName, OBJ_HLINE, 0, 0, bottomPrice);
          ObjectSet(lowerName, OBJPROP_COLOR, LowerColor);
          ObjectSet(lowerName, OBJPROP_BACK, false);
          ObjectSet(lowerName, OBJPROP_WIDTH, LineWidth);
          
          ObjectSetInteger(0,"PAHLS",OBJPROP_STATE,false);
          
          ObjectSetInteger(0,"PAAlert",OBJPROP_STATE,true);
          PriceAlert = true;
          
          DrawTrendline = false;
          
        }
     }  
       
       
       
       
    if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="PATLS")
        {
          ObjectDelete(upperName);
          ObjectDelete(lowerName);
          
          ObjectCreate(upperName, OBJ_TREND, 0, Time[30], topPrice, Time[0], topPrice);
          ObjectSet(upperName, OBJPROP_COLOR, UpperColor);
          ObjectSet(upperName, OBJPROP_BACK, false);
          ObjectSet(upperName, OBJPROP_WIDTH, LineWidth);
          
          ObjectCreate(lowerName, OBJ_TREND, 0, Time[30], bottomPrice, Time[0], bottomPrice);
          ObjectSet(lowerName, OBJPROP_COLOR, LowerColor);
          ObjectSet(lowerName, OBJPROP_BACK, false);
          ObjectSet(lowerName, OBJPROP_WIDTH, LineWidth);
          
          ObjectSetInteger(0,"PATLS",OBJPROP_STATE,false);
          
          ObjectSetInteger(0,"PAAlert",OBJPROP_STATE,true);
          PriceAlert = true;
          
          DrawTrendline = true;
                   
        }
     }
     
     
     
     
     
     
     
    if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="PACLR")
        {
          ObjectDelete(upperName);
          ObjectDelete(lowerName);
          
          ObjectSetInteger(0,"PACLR",OBJPROP_STATE,false);
          
          DrawTrendline = false;
          
        }
     }
       
       
       
  
   }
 
tparimore: After applying a template of which the attached indicator is a part of, the indicator will not draw the horizontal or trend lines until I change timeframe. 
The objects are also part of the template. Edit it and remove them, and reapply.
<window>
height=282
fixed_height=0
<indicator>
name=main
<object>            
type=23             
object_name=BarTimer
⋮                    
</object>           
 
William Roeder #:
The objects are also part of the template. Edit it and remove them.

I always delete all objects before I save a template. As you can see there are no objects listed.  ---Tom

<chart>
id=133083287163227739
symbol=CHFJPY
period=5
leftpos=4354
digits=3
scale=8
graph=1
fore=0
grid=0
volume=0
scroll=1
shift=1
ohlc=1
one_click=0
one_click_btn=1
askline=1
days=0
descriptions=0
shift_size=13
fixed_pos=0
window_left=156
window_top=156
window_right=1816
window_bottom=727
window_type=3
background_color=0
foreground_color=16777215
barup_color=65280
bardown_color=255
bullcandle_color=25600
bearcandle_color=128
chartline_color=65280
volumes_color=3329330
grid_color=255
askline_color=3329330
stops_color=255

<window>
height=375
fixed_height=0
<indicator>
name=main
</indicator>
<indicator>
name=Custom Indicator
<expert>
name=ALERT - PRICE
flags=339
window_num=0
<inputs>
xxxxxxxxxxxx=*********** Line Properties ***********************************
UpperColor=9408444
LowerColor=9408444
LineWidth=1
xxxxxxxxxxx=*********** Object Locations ***********************************
PriceWindow=0
PriceCorner=3
PriceText=PA
PriceText_XDISTANCE=160
PriceText_YDISTANCE=37
PriceHLS_YDISTANCE=55
PriceHLS_XDISTANCE=150
PriceTLS_XDISTANCE=100
PriceCLR_XDISTANCE=51
PriceAlert_YDISTANCE=32
PriceAlert_XDISTANCE=150
PricePush_XDISTANCE=75
</inputs>
</expert>
shift_0=0
draw_0=0
color_0=0
style_0=0
weight_0=0
period_flags=0
show_data=1
</indicator>
</window>
</chart>
 
William Roeder #:
The objects are also part of the template. Edit it and remove them, and reapply.

I finally figured out how to fix issue. Although topPrice and bottomPrice are in the int OnInit(), they had a value of zero until I changed periods/timeframe (I don't understand why?). By moving the equations to the OnChartEvent, it now works properly. Thanks for your earlier suggestion. Kind Regards  ---Tom 

 
tparimore #: (I don't understand why?).

don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
William Roeder #:

don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

I will add this to my notes. Good to know. Thanks   ----Tom

Reason: