Unable to get popup in market off hours

 

I have some cool MQL5 code I found on this forum somewhere a long time ago.

I've modified it a little bit. It detects when you put a line on a chart, and pops up asking a question about the line.

It works perfectly, except that it doesn't work on the weekends when the market is closed. I don't know why. Can you see why that's happening or suggest anything I can try to get it to produce the popup on the weekends? Thanks!


#include <ChartObjects\ChartObjectsLines.mqh>
#include <Arrays\ArrayObj.mqh>
input int magic_number = 123456;

//////////////////////////////////////////////////////////////
// Popup for lines on chart functions
//////////////////////////////////////////////////////////////

class CSR : public CChartObjectTrend 
{
 public:
   bool Attach(string name) { return Attach(ChartID(), name, 0, 1); }
};
bool modify_line(string name) {
   string long_short = "";
   string open_close = "";
   string answer = MessageBox("Would you place Long/Short trade", NULL, MB_YESNOCANCEL);
   if (answer == IDYES) {
      long_short = "long";
   } else if (answer == IDNO) {
      long_short = "short";
   } else {
      return false;
   }   
   answer = MessageBox("Would you like to Open/Close?", NULL, MB_YESNOCANCEL);
   if (answer == IDYES){
      open_close = "open";
   } else if (answer == IDNO) {
      open_close = "close";
   } else {
      return false;
   }
   CSR * line = new CSR;
   
   if(!line.Attach(name)) { return false; }
   
   if (long_short == "long" && open_close == "open") {
      line.Color(clrBlue);
      line.Description("open long");
   } else if (long_short == "long" && open_close == "close") {
      line.Color(clrBlue);
      line.Style(STYLE_DOT);
      line.Description("close long");
   } else if (long_short == "short" && open_close == "open") {
      line.Color(clrRed);
      line.Description("open short");
   } else if (long_short == "short" && open_close == "close") {
      line.Color(clrRed);
      line.Style(STYLE_DOT);
      line.Description("close short");
   }
   return true;
}
...

//////////////////////////////////////////////////////////////
// MQL5 functions
//////////////////////////////////////////////////////////////

int OnInit()
{
   ChartSetInteger(0, CHART_EVENT_OBJECT_CREATE, true);
   ChartSetInteger(0, CHART_EVENT_OBJECT_DELETE, true);
   ChartSetInteger(0, CHART_SHOW_OBJECT_DESCR, true);
   return (INIT_SUCCEEDED);
}
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam) {
   if (id == CHARTEVENT_OBJECT_CREATE && ObjectGetInteger(0, sparam, OBJPROP_TYPE) == OBJ_TREND) {
      modify_line(sparam);
   }
}
...


PS. I guess there is another improvement to this script I don't know how to make: the popup comes up with 3 buttons Yes, No and Cancel I think. I'd rather the buttons say "open" "close" and "cancel" instead but that's just an aesthetics thing...

 
because the market closed
 
Ahmet Metin Yilmaz:
because the market closed

Yes, thank you Ahmet. I was able to put that together.

Nevertheless, I'd like to draw my charts for the week while the market is closed - on the weekends. I'm not interfacing with the market, I'm interfacing with the tool, MT5, so it should be programmatically possible to have the tool respond to behaviors I do without checking into the market.

You can see when I draw a line on the chart it's supposed to ask me (with a popup message box) what kind of tool this is and modify the line's description accordingly. None of that should require waiting for the next tick in the market or any information from the market. Thus I must be programming it incorrectly (or MQL5 has a huge design flaw).

I have, however, perhaps fixed the problem, after some troubleshooting. I discovered it wasn't a problem with the code, but rather a problem with my computer. After restarting and placing an Alert command in the OnChartEvent function it started behaving as expected. when I removed the Alert command, it continued to work as expected. I don't know if it was some problem in some cache or what it could have been, but it seems to be working correctly now.

I, unfortunately, still not know how to name the buttons correctly though.

Thanks for your consideration of this issue.

 

It looks like a forced update to the chart is missing to set the initial flags, but I'm just guessing.

int OnInit()
{
   ChartSetInteger(0, CHART_EVENT_OBJECT_CREATE, true);
   ChartSetInteger(0, CHART_EVENT_OBJECT_DELETE, true);
   ChartSetInteger(0, CHART_SHOW_OBJECT_DESCR, true);
//--- forced updating of chart properties ensures readiness for event processing 
   ChartRedraw(); 
   return (INIT_SUCCEEDED);
}
 
lippmaje:

It looks like a forced update to the chart is missing to set the initial flags, but I'm just guessing.

Thanks lippmaje! I'll play around with that and see if it improves.
 
shadrakkay:

PS. I guess there is another improvement to this script I don't know how to make: the popup comes up with 3 buttons Yes, No and Cancel I think. I'd rather the buttons say "open" "close" and "cancel" instead but that's just an aesthetics thing...

I'm afraid you are stuck with predefined values only.

 
Marcin Madrzak:

I'm afraid you are stuck with predefined values only.

ok thanks, good to know :)
lippmaje:

It looks like a forced update to the chart is missing to set the initial flags, but I'm just guessing.

Turns out that did improve the situation. I had another glitch where I'd have to apply the Expert twice before it would recognize drawn lines, and yes, your suggestion fixed that. Thanks!