two event on chart

 
Hello geniuses
I have a button on my chart
The button is to get permission from the user, which is basically stop and start. I designed the button and also used the OnChartEvent() function.
So, at next step, if the user clicks anywhere on the chart, I want to give the information of that part
In this part, I'm a little confused because the OnChartEvent() function was used to click the button and I can't use it again
Can you guide me how can I do this?


Documentation on MQL5: Event Handling / OnChartEvent
Documentation on MQL5: Event Handling / OnChartEvent
  • www.mql5.com
OnChartEvent - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
soheyr6200:
Hello geniuses
I have a button on my chart
The button is to get permission from the user, which is basically stop and start. I designed the button and also used the OnChartEvent() function.
So, at next step, if the user clicks anywhere on the chart, I want to give the information of that part
In this part, I'm a little confused because the OnChartEvent() function was used to click the button and I can't use it again
Can you guide me how can I do this?


When the user event is fired it contain some infos inside OnChartEvent().

void OnChartEvent(const int id,         // Event ID 
                  const long& lparam,   // Parameter of type long event 
                  const double& dparam, // Parameter of type double event 
                  const string& sparam  // Parameter of type string events 
  );

you can tell the event source by these params,it is quite easy.

try to write a demo,just an Print inside OnChartEvent,which print id,lparm,dparam,sparam。

you will learn that click on the chart(not click on any object),and click on a object ,they are different.


more info please read manual

MQL4 Reference / Language Basics / Functions / Event Handling Functions

 
Guo Zheng Feng #:

When the user event is fired it contain some infos inside OnChartEvent().

you can tell the event source by these params,it is quite easy.

try to write a demo,just an Print inside OnChartEvent,which print id,lparm,dparam,sparam。

you will learn that click on the chart(not click on any object),and click on a object ,they are different.


more info please read manual

MQL4 Reference / Language Basics / Functions / Event Handling Functions

hi  Guo Zheng Feng,  thank you for your answer

i know I must used  OnChartEvent and have used it.

My problem is that my two events are not concurrent

I will click on the object first, if the object is clicked, then I will click if the chart is clicked.

I have written a test sample for clicking on the chart and clicking on the object and it works fine

But I can't write it back to back

If the object is clicked first and then the chart 



 
soheyr6200 #:

hi  Guo Zheng Feng,  thank you for your answer

i know I must used  OnChartEvent and have used it.

My problem is that my two events are not concurrent

I will click on the object first, if the object is clicked, then I will click if the chart is clicked.

I have written a test sample for clicking on the chart and clicking on the object and it works fine

But I can't write it back to back

If the object is clicked first and then the chart 



google for 'state machine' (algorithm),see if it can help you.
 
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  if(id==CHARTEVENT_OBJECT_CLICK  &&  sparam =="Button1" )
     {
      if(ObjectGetString(0,"Button1",OBJPROP_TEXT)=="Stop")
        {
         ObjectSetString(0,"Button1",OBJPROP_TEXT,"Start");
        }
     }
   if(id == CHARTEVENT_CLICK  && ObjectGetString(0,"Button1",OBJPROP_TEXT)=="Start")
     {
      int x = (int)lparam;
      int y = (int)dparam;
      ask=MarketInfo(Symbol(),MODE_ASK);
      bid=MarketInfo(Symbol(),MODE_BID);
         Print("Mouse clicked on: ",DoubleToString(clickprice,Digits),"   Ask: ",ask,"    Bid: ",bid, "   X: ",x,"    Y: ",y, );
         ObjectSetString(0,"Button1",OBJPROP_TEXT,"Stop");
        }
     }

when i click on button , it show me   ask,"    Bid: ",bid, "   X: ",x,"    Y: ",y,

but it must show them me when i click on chart

can say me where i was wrong?

 
soheyr6200 #:

when i click on button , it show me   ask,"    Bid: ",bid, "   X: ",x,"    Y: ",y,

but it must show them me when i click on chart

can say me where i was wrong?


double ask,bid,clickprice;
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  Print("Event Fired:id=",id," lparam=",lparam," dparam=",dparam," sparam=",sparam);
  if(id==CHARTEVENT_OBJECT_CLICK  &&  sparam =="Button1" )
     {
      if(ObjectGetString(0,"Button1",OBJPROP_TEXT)=="Stop")
        {
         ObjectSetString(0,"Button1",OBJPROP_TEXT,"Start");
        }
     }
   if(id == CHARTEVENT_CLICK  && ObjectGetString(0,"Button1",OBJPROP_TEXT)=="Start")
     {
      int x = (int)lparam;
      int y = (int)dparam;
      ask=MarketInfo(Symbol(),MODE_ASK);
      bid=MarketInfo(Symbol(),MODE_BID);
         Print("Mouse clicked on: ",DoubleToString(clickprice,Digits),"   Ask: ",ask,"    Bid: ",bid, "   X: ",x,"    Y: ",y);
         ObjectSetString(0,"Button1",OBJPROP_TEXT,"Stop");
        }
     }

2023.11.13 00:37:33.775    test EURUSD,H1: Event Fired:id=4 lparam=230 dparam=57.0 sparam=
2023.11.13 00:37:33.775    test EURUSD,H1: Event Fired:id=1 lparam=230 dparam=57.0 sparam=Button1

it fired 2 event at the same time,both object and chart,even i just click 1 time on the button.

so, it needs to block another event in a short time.

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
   ObjectCreate(ChartID(),"Button1",OBJ_BUTTON,0,Time[0],Close[0]);
   ObjectSetString(0,"Button1",OBJPROP_TEXT,"Start");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
double ask,bid,clickprice;
#define SHAKE_TIME 50 //ms
long bt_tm;
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  Print("Event Fired:id=",id," lparam=",lparam," dparam=",dparam," sparam=",sparam);
  if(id==CHARTEVENT_OBJECT_CLICK  &&  sparam =="Button1" )
     {
      if(ObjectGetString(0,"Button1",OBJPROP_TEXT)=="Stop")
        {
         ObjectSetString(0,"Button1",OBJPROP_TEXT,"Start");
        }
        bt_tm=GetMicrosecondCount();
     }
   if(id == CHARTEVENT_CLICK  && ObjectGetString(0,"Button1",OBJPROP_TEXT)=="Start" && GetMicrosecondCount()-bt_tm>=SHAKE_TIME)
     {
      int x = (int)lparam;
      int y = (int)dparam;
      ask=MarketInfo(Symbol(),MODE_ASK);
      bid=MarketInfo(Symbol(),MODE_BID);
         Print("Mouse clicked on: ",DoubleToString(clickprice,Digits),"   Ask: ",ask,"    Bid: ",bid, "   X: ",x,"    Y: ",y);
         ObjectSetString(0,"Button1",OBJPROP_TEXT,"Stop");
        }
     }

2023.11.13 01:25:54.279    test EURUSD,H1: Mouse clicked on: 0.00000   Ask: 1.06857    Bid: 1.06828   X: 237    Y: 164
2023.11.13 01:25:54.279    test EURUSD,H1: Event Fired:id=4 lparam=237 dparam=164.0 sparam=
2023.11.13 01:25:52.437    test EURUSD,H1: Event Fired:id=4 lparam=237 dparam=52.0 sparam=
2023.11.13 01:25:52.437    test EURUSD,H1: Event Fired:id=1 lparam=237 dparam=52.0 sparam=Button1
2023.11.13 01:25:49.851    test EURUSD,H1: Mouse clicked on: 0.00000   Ask: 1.06857    Bid: 1.06828   X: 198    Y: 178
2023.11.13 01:25:49.851    test EURUSD,H1: Event Fired:id=4 lparam=198 dparam=178.0 sparam=
 it is working now.

the anime state when the button click need to be fixed.

 
Guo Zheng Feng #:


2023.11.13 00:37:33.775    test EURUSD,H1: Event Fired:id=4 lparam=230 dparam=57.0 sparam=
2023.11.13 00:37:33.775    test EURUSD,H1: Event Fired:id=1 lparam=230 dparam=57.0 sparam=Button1

it fired 2 event at the same time,both object and chart,even i just click 1 time on the button.

so, it needs to block another event in a short time.

2023.11.13 01:25:54.279    test EURUSD,H1: Mouse clicked on: 0.00000   Ask: 1.06857    Bid: 1.06828   X: 237    Y: 164
2023.11.13 01:25:54.279    test EURUSD,H1: Event Fired:id=4 lparam=237 dparam=164.0 sparam=
2023.11.13 01:25:52.437    test EURUSD,H1: Event Fired:id=4 lparam=237 dparam=52.0 sparam=
2023.11.13 01:25:52.437    test EURUSD,H1: Event Fired:id=1 lparam=237 dparam=52.0 sparam=Button1
2023.11.13 01:25:49.851    test EURUSD,H1: Mouse clicked on: 0.00000   Ask: 1.06857    Bid: 1.06828   X: 198    Y: 178
2023.11.13 01:25:49.851    test EURUSD,H1: Event Fired:id=4 lparam=198 dparam=178.0 sparam=
 it is working now.

the anime state when the button click need to be fixed.

thanks a lot sir.i dont know how say my my gratitude. thanks for your time and knowlege
 
soheyr6200 #:
thanks a lot sir.i dont know how say my my gratitude. thanks for your time and knowlege
you are welcome