Objects Don't Appear Until Indicator is Edited

 

Hey there!


I'm new to coding with MQL4. I'm simply trying to have my indicator plot vertical lines at specified times in history. It actually does this correctly! The only problem is when I initially drag the indicator onto a blank chart, I click "OK", but the vertical lines don't appear. However, if I think right click on the chart, click on "Indicators", select my indicator and click on "Edit", then immediately click "OK", they plot perfectly. I was wondering if anyone has any idea why this could be happening.


Thanks for any help!


Jimmy

 
JimmyWalters:

Hey there!


I'm new to coding with MQL4. I'm simply trying to have my indicator plot vertical lines at specified times in history. It actually does this correctly! The only problem is when I initially drag the indicator onto a blank chart, I click "OK", but the vertical lines don't appear. However, if I think right click on the chart, click on "Indicators", select my indicator and click on "Edit", then immediately click "OK", they plot perfectly. I was wondering if anyone has any idea why this could be happening.


Thanks for any help!


Jimmy

Check the journals and experts tab for errors

Or post your code that needs to be checked

 
Mladen Rakic:

Check the journals and experts tab for errors

Or post your code that needs to be checked


Update #2: I had to set my initial values using "DayOfWeek()" in "OnCalculate()" rather that in "OnInit()" because to get the proper values, the platform has to receive a data tick. It seems like the problem is solved! Thank you for offering to help!

Update: I noticed that "DayOfWeek()" is returning 0 instead of 4. At the time of writing this, it's Thursday for this particular server which would be represented by 4 rather than 0 which is Sunday. However, whenever I recompile the code in MetaEditor, "DayOfWeek()" then returns 4 as it should. I'm not quite sure why it's doing this, but I'm looking into it.


I checked both of those tabs and there's no errors being thrown unfortunately. So what I'm trying to do is draw a vertical line for the New York session Monday - Friday. When I first drag and drop the indicator onto the chart, it does this:


As you can see, it only places lines (for now, focus on the black lines. The red lines are for London Session, and the white dotted is the MT4 session dividers) on Monday, Tuesday, and Friday. But when I edit the indicator while on the chart and click "OK", or click compile in the MetaEditor while the indicator is on the chart, it realigns everything correctly as so:



The I'll post the code right after this short explanation. So my thinking is that I have the current day and I want to walk back one day at a time (in my for loop) for a specified amount of days, called KillZoneHistoryLimit. So I walk back one day at a time and if it's a Saturday or Sunday (specified by MqlDateTime's day_of_week variable which is an int), I don't want to print those lines. Here's the code:

datetime serverTime = TimeCurrent();
   datetime gmtTime = TimeGMT();
   
   MqlDateTime NYKillZone = currentDateTime;
   MqlDateTime LondonKillZone = currentDateTime;
   
   //1) get difference between server and gmt
   //2) add difference to const gmt time and put onto chart
   int GmtServerDifferenceSeconds = serverTime - gmtTime;
   NYKillZone.hour = GMTNewYorkKillZone + ((GmtServerDifferenceSeconds/60)/60);
   LondonKillZone.hour = GMTLondonKillZone + ((GmtServerDifferenceSeconds/60)/60); 
   
   if(ShowKillZoneHistory){
      //this will paint lines for KillZoneHistoryLimit amount of days
      for(int i = 1; i < KillZoneHistoryLimit; i++, NYKillZone.day--, LondonKillZone.day--, NYKillZone.day_of_week--, LondonKillZone.day_of_week--){
         //--- Adjust day ---
         if(ShowNYKillZone){
            if(NYKillZone.day == 0){
               NYKillZone.mon--;
               if(NYKillZone.mon == 0){
                  NYKillZone.year--;
                  NYKillZone.mon = 12;         
               } else {
               
                  int mon = NYKillZone.mon;
                  
                  if(mon == 1 || mon == 3 || mon == 5 || mon == 7
                     || mon == 8 || mon == 10 || mon == 12){
                        NYKillZone.day = 31;
                  } else if(mon == 4 || mon == 6 || mon == 9 || mon == 11){
                        NYKillZone.day = 30;
                  } else if(mon == 2){
                        NYKillZone.day = 28;
                  }
               }
            }
            
            if(NYKillZone.day_of_week == -1){
               NYKillZone.day_of_week = 6;
            }
            
            if(NYKillZone.day_of_week != 6 && NYKillZone.day_of_week != 0){
               string name = "NY Killzone " + NYLinesIndex + " " + LondonKillZone.day_of_week;
            
               ObjectCreate(name, OBJ_VLINE, 0, StructToTime(NYKillZone), 0); 
               ObjectSet(name,OBJPROP_COLOR,NYVerticalLineColor);
               ObjectSet(name,OBJPROP_WIDTH,NYVerticalLineWidth);
               ObjectSet(name,OBJPROP_STYLE,NYVerticalLineStyle);
               NYVerticalLines[NYLinesIndex] = name;
               //Print("NY DOW: " + NYKillZone.day_of_week);
               NYLinesIndex++;
               if(NYLinesIndex == ArraySize(NYVerticalLines)){
                  NYLinesIndex = ArraySize(NYVerticalLines) - 1;
               }
            }
         }



Hopefully I've given a good enough explanation! Again, any help is greatly appreciated!

 
JimmyWalters: Update: I noticed that "DayOfWeek()" is returning 0 instead of 4. At the time of writing this, it's Thursday for this particular server which would be represented by 4 rather than 0 which is Sunday. However, whenever I recompile the code in MetaEditor, "DayOfWeek()" then returns 4 as it should. I'm not quite sure why it's doing this, but I'm looking into it.
Don't try to use any price or server related functions in OnInit as there may be no connection/chart yet:
  1. Terminal starts.
  2. indicators/EAs are loaded.
  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. New tick is received, OnCalculate/OnTick is called; Now TickValue, TimeCurrent and prices are now valid.
Reason: