Create a Hline using a custom button (MT4)

 

Hi,

I want my EA to have a custom button on screen. When I press this button it should allow me to place a horizontal line on the chart.

However, whenever I click on my button, the Hline(s) is/are also immediately placed.

It seems the CHAREVENT_CLICK gets triggered anyways.

Anyone can help me fix this? I am using MT4

See my code:

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  
  //Print("The coordinates of the mouse click on the chart are: x = ",lparam,"  y = ",dparam);
      datetime time;
      double price;
      int subwindow;
      ChartXYToTimePrice(0,lparam,dparam,subwindow,time,price);
   
      if(id==CHARTEVENT_OBJECT_CLICK && sparam=="DrawEP")
      {
         if(SetEntryPrice == false)
         {
            //activate DrawEP button
            DrawEP.Color(clrWhite);
            DrawEP.ColorBackground(clrBlack);
            SetEntryPrice = true;
         }
         else if(SetEntryPrice)
         {
            DrawEP.Color(clrBlack);
            DrawEP.ColorBackground(clrAquamarine);
            SetEntryPrice = false;
         }
      }
      
      if(id==CHARTEVENT_CLICK && SetEntryPrice)
      {
         //set stoploss
         StopLoss = 10*StrToDouble(ObjectDescription("Stoploss"));
         
         //place horizontal line at mousepointer
         ObjectCreate("EntrypriceLine","EntryPrice",OBJ_HLINE,0,0,price);
         ObjectSetInteger("Entryprice","EntryPrice",OBJPROP_COLOR,clrBlack);
         
         //place stoploss line
         ObjectCreate("StopLossLine","StopLossLine",OBJ_HLINE,0,0,(price-StopLoss*Point));
         ObjectSetInteger("StopLossLine","StopLossLine",OBJPROP_COLOR,clrRed); 
         
         //deactivate drawEP button
         DrawEP.Color(clrBlack);
         DrawEP.ColorBackground(clrAquamarine);
         SetEntryPrice = false;
      }
 
Simon Liu:

I want my EA to have a custom button on screen. When I press this button it should allow me to place a horizontal line on the chart.

However, whenever I click on my button, the Hline(s) is/are also immediately placed.

It seems the CHAREVENT_CLICK gets triggered anyways.

Anyone can help me fix this? I am using MT4

Check out this test code:

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   static int iCnt = 0;
   Print ("iCnt = ", iCnt++, ", id = ", id);
  }

You'll see that one click on the chart object can trigger-off multiple calls to OnChartEvent() one right after the other. So what you observed is not unexpected.

To separate them, you can perhaps check time... 

 
Seng Joo Thio:

Check out this test code:

You'll see that one click on the chart object can trigger-off multiple calls to OnChartEvent() one right after the other. So what you observed is not unexpected.

To separate them, you can perhaps check time... 

Thank you for your reply. This will indeed help me further.

 
Simon Liu:

Thank you for your reply. This will indeed help me further.

OH man how to fix this ? I also want to make a custom button on screen,then when to click the button can mark the text like "wave 1" on the chart with the ChartXYToTimePrice(0,lparam,dparam,subwindow,time,price)by the coordinate.
Reason: