Button not working issue

 
int OnInit()
{
        ObjectCreate(0, "BreakEven", OBJ_BUTTON, 0, 0, 0);
        ObjectSetInteger(0, "BreakEven", OBJPROP_CORNER, CORNER_LEFT_LOWER);
        ObjectSetInteger(0, "BreakEven", OBJPROP_XSIZE, 240);
        ObjectSetInteger(0, "BreakEven", OBJPROP_YSIZE, 30);
        ObjectSetInteger(0, "BreakEven", OBJPROP_XDISTANCE, 10);
        ObjectSetInteger(0, "BreakEven", OBJPROP_YDISTANCE, 40);

        return (INIT_SUCCEEDED);
}

//----------------
void OnDeinit(const int reason)
{
        ObjectDelete(0, "BreakEven");
}

//----------------
void OnChartEvent(const int id, const long &Lparam, const double &Dparam, const string &Sparam)
{
    if (id == CHARTEVENT_OBJECT_CLICK)
        if (Sparam == "BreakEven")
                if (PositionsTotal() > 0)
                        BreakEven();
}

Hello everyone,

I have a little problem with the buttons of a code on MetaTrader 5 in MQL5.

I created a small program to set a Break Even with an OBJ_BUTTON that is on my chart. The issue is that very often when I click on the button, it doesn't work and the mouse briefly displays the cursor to horizontally scroll the chart. I have to press the button multiple times for it to randomly activate at some point.

I have tried with other buttons such as a "Buy" button that triggers a CTrade function like "trade.buy();" and the problem is also present...

Do you have a solution?

This is my code :


Thank you very much.


Ps : 
MetaTrader 5 version 5.00 build 3555 on Windows 10

Graphical Interfaces V: The Vertical and Horizontal Scrollbar (Chapter 1)
Graphical Interfaces V: The Vertical and Horizontal Scrollbar (Chapter 1)
  • www.mql5.com
We are still discussing the development of the library for creating graphical interfaces in the MetaTrader environment. In the first article of the fifth part of the series, we will write classes for creating vertical and horizontal scrollbars.
 
void OnChartEvent(const int id, const long &Lparam, const double &Dparam, const string &Sparam)
{
        if (Sparam == "BreakEven")
                if (PositionsTotal() > 0)
                        BreakEven();
}

I suggest that you read the documentation for OnChartEvent() and ObjectGetInteger(0,xxxx,OBJPROP_STATE).

 
Escuze i forget to rewrite this on my example code ... I Edit my first post

if (id == CHARTEVENT_OBJECT_CLICK)

the problem is the same ...
 
For me the buttons on the chart, but also buttons in the terminal trade tab work dodgy. Therefor i dropped buttons all together in favor of hot-keys.
 
Charles, Marie Tilly #:
Escuze i forget to rewrite this on my example code ... I Edit my first post


the problem is the same ...

Hi

What Keith implied is are you testing if the button works with a print or an alert or solely based on how it looks on the chart ?

 
Charles, Marie Tilly #:
Escuze i forget to rewrite this on my example code ... I Edit my first post


the problem is the same ...

Did you read the documentation regarding  ObjectGetInteger(0,xxxx,OBJPROP_STATE)?

Keith Watford #:

I suggest that you read the documentation for OnChartEvent() and ObjectGetInteger(0,xxxx,OBJPROP_STATE).

 

Thanks for your answers.
No better works with  print and alert ...


After doing some additional testing, I think I have found the source of the problem.
My program, in addition to the buttons, draws lines (OBJ_HLINE) on the High and Low of the X last candles and moves them via an ObjectMove (the creation of objects is done on OnInit, the movement on OnTimer).

If I disable the creation and updating of these hlines, the buttons work fine...


The full code with the hline :

int OnInit()
{
        //Timer init
        EventSetTimer(1);

        //Chart setting
        ChartSetInteger(0, CHART_SHOW_ONE_CLICK, false);
        ChartSetInteger(0, CHART_SHIFT, true);
        ChartSetInteger(0, CHART_SHOW_ASK_LINE, 0, 1);

        //BreakEven Button
        ObjectCreate(0, "BreakEven", OBJ_BUTTON, 0, 0, 0);
        ObjectSetInteger(0, "BreakEven", OBJPROP_CORNER, CORNER_LEFT_LOWER);
        ObjectSetInteger(0, "BreakEven", OBJPROP_XSIZE, 240);
        ObjectSetInteger(0, "BreakEven", OBJPROP_YSIZE, 30);
        ObjectSetInteger(0, "BreakEven", OBJPROP_XDISTANCE, 10);
        ObjectSetInteger(0, "BreakEven", OBJPROP_YDISTANCE, 40);
        ObjectSetInteger(0, "BreakEven", OBJPROP_STATE, false);

        //High and Low Line
        ObjectCreate(0, "High", OBJ_HLINE, 0, 0, 0);
        ObjectSetInteger(0, "High", OBJPROP_COLOR, clrBlue);
        ObjectSetInteger(0, "High", OBJPROP_BACK, true);
        ObjectCreate(0, "Low", OBJ_HLINE, 0, 0, 0);
        ObjectSetInteger(0, "Low", OBJPROP_COLOR, clrOrange);
        ObjectSetInteger(0, "Low", OBJPROP_BACK, true);

        return (INIT_SUCCEEDED);
}

//----------------
void OnDeinit(const int reason)
{
        EventKillTimer();
        ObjectDelete(0, "BreakEven");
        ObjectDelete(0, "High");
        ObjectDelete(0, "Low");
}

//----------------
void OnTimer()
{
        CopyHigh(_Symbol, _Period, 0, 150, xHigh);
        CopyLow(_Symbol, _Period, 0, 150, xLow);
        CopyRates(_Symbol, _Period, 0, 150, xPriceInformation);
        HighCandle = ArrayMaximum(xHigh, 0, 150);
        LowCandle = ArrayMinimum(xLow, 0, 150);
        ObjectMove(0, "High", 0, 0, xPriceInformation[HighCandle].high);
        ObjectMove(0, "Low", 0, 0, xPriceInformation[LowCandle].low);
}

//----------------
void OnChartEvent(const int id, const long &Lparam, const double &Dparam, const string &Sparam)
{
        if (id == CHARTEVENT_OBJECT_CLICK)
                if (Sparam == "BreakEven")
                {
                        ObjectSetInteger(0, "BreakEven", OBJPROP_STATE, true);
                        if (PositionsTotal() > 0)
                                BreakEven();
                        ObjectSetInteger(0, "BreakEven", OBJPROP_STATE, false);
                 }
}
 
Hi !

Someone have solutions ?

Is there something that I did not understand about the use of OBJPROP_STAT, OBJPROP_BACK or other ?
 
Charles, Marie Tilly #:
Hi !

Someone have solutions ?

Is there something that I did not understand about the use of OBJPROP_STAT, OBJPROP_BACK or other ?

It's a button (on/off), when you click on it the state is automatically changed. You have to read the state of the button, not set it.

   if(id == CHARTEVENT_OBJECT_CLICK)
      if(Sparam == "BreakEven")
        {
         bool state = (bool)ObjectGetInteger(0, "BreakEven", OBJPROP_STATE);

         if(state && PositionsTotal() > 0)
           {
            Alert("Set breakeven");
            //--- Possibly reset button state
            ObjectSetInteger(0, "BreakEven", OBJPROP_STATE, false);
            ChartRedraw();
           }
        }
Reason: