Conditionally adding/deleting a horizontal line

 

Hi,

I try to draw a horizontal line, when a button is pressed.

In addition, when the same button is pressed for a second time, I like to remove the horizontal line. So on so forth.

The problem is that if I insert code related to drawing objects inside the inner if conditional block, the behavior of the OnChartEvent() becomes totally unstable.

I added Print() lines in order to debug it where Print() functions executed several times and sometimes the line is not deleted.

I could not figured out, what is going on.

Please while testing also try to comment out the ObjectDelete() function, and vice versa.

Click at least 10 times to the button, then check the Experts tab for printed lines.

Thank you.


//+------------------------------------------------------------------+
//|                                                          Tmp.mq4 |
//|                                                    fxbender 2021 |
//|                                           https://www.google.com |
//+------------------------------------------------------------------+
#property copyright "Fxbender 2021"
#property link      "https://www.google.com"
#property version   "1.00"
#property strict


#include <Controls/Dialog.mqh>

#include <Controls/Button.mqh>
#include <Controls/SpinEdit.mqh>
#include <ChartObjects/ChartObjectsLines.mqh>

#include <stdlib.mqh>
#include <WinUser32.mqh> 

CAppDialog cpInterface;

CButton btn_test1;
const string btnID_test1="ButtonTest1";

CSpinEdit input_test2;
const string inputID_test2="InputTest2";

CChartObjectHLine hLine_test3;
const string hLineID_test3="HLineTest3";

static int a_x = int(ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0)-10);  

int OnInit()
{
   const int   sub_window  = 0;           // subwindow index
   int         x           = 10;          // X coordinate
   int         y           = 10;          // Y coordinate
   int         width       = 50;          // object width
   int         height      = 20;          // object height
   const int   space       = 3;           // space between objects
   int         left=0,top=0,right=0,bottom=0;

   cpInterface.Create(0, "Tmp", 0, 200, 0, 200 + width*10, 0 + height*6);
   
   left = x + (width + space) * 2.5; top = y; right = left + width * 3; bottom = top + height;
   btn_test1.Create(0, btnID_test1, sub_window, left, top, right, bottom);
   btn_test1.Text("Test");
   btn_test1.FontSize(9);
   btn_test1.Color(clrWhite);
   btn_test1.ColorBackground(clrRed);
   btn_test1.ColorBorder(clrBlack);
   cpInterface.Add(btn_test1);
   
   left = x + width * 0.5; top = y * 4; right = left + width; bottom = top + height;
   input_test2.Create(0, inputID_test2, sub_window, left, top, right, bottom);
   input_test2.MinValue(-100);
   input_test2.MaxValue(100);
   input_test2.Value(1);
   cpInterface.Add(input_test2);
   
   cpInterface.Run();
   
   return(INIT_SUCCEEDED);
   
}

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

   if (btn_test1.Pressed())
   {
      if (btn_test1.ColorBackground()==clrRed)
      {
         btn_test1.ColorBackground(clrGreen);
         Print ("Line Activated");
         
         hLine_test3.Create(0, hLineID_test3, 0, MarketInfo(0, MODE_ASK));
         hLine_test3.Color(clrBlue);
         hLine_test3.Style(STYLE_DOT);
         hLine_test3.Width(1);
                        
      }
      else if (btn_test1.ColorBackground()==clrGreen)
      {
         ObjectDelete(0, hLineID_test3);
         btn_test1.ColorBackground(clrRed);
         Print ("Line Deactivated ", input_test2.Value());
      }
   }
      
   ChartRedraw();
}

void OnDeinit(const int reason)
{
   cpInterface.Destroy(reason);
}
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 

Libraries can make programming easier, but it seems to make it more complicated.

I don't know what's going on.

Instead, I extracted the functions of the panel display from the ranking board which I use and created a new program as follows.

This is a "indicator", not "EA".


Files:
Test.mq4  10 kb
 
fxforces:

I try to draw a horizontal line, when a button is pressed.

In addition, when the same button is pressed for a second time, I like to remove the horizontal line. So on so forth.

The problem is that if I insert code related to drawing objects inside the inner if conditional block, the behavior of the OnChartEvent() becomes totally unstable.

Just add the highlighted part to further filter the chart events:

   if (btn_test1.Pressed() && id==CHARTEVENT_MOUSE_MOVE)
 
Naguisa Unada:

Libraries can make programming easier, but it seems to make it more complicated.

I don't know what's going on.

Instead, I extracted the functions of the panel display from the ranking board which I use and created a new program as follows.

This is a "indicator", not "EA".



I plan to make trade orders in this code, so I suppose I need an EA.

But I  would like to thank for your attention.


Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Seng Joo Thio:

Just add the highlighted part to further filter the chart events:

Thank you very much.

It works.

Reason: