Create a Hline using a custom button (MT4)

To add comments, please log in or register
Simon Liu
663
Simon Liu  

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;
      }
Seng Joo Thio
1215
Seng Joo Thio  
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... 

Simon Liu
663
Simon Liu  
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.

To add comments, please log in or register