Script that catchs two clicks on chart - page 2

 

I found that scripts are not aware of OnChartEvent(),

 

The only way I found was to create an expert advisor, as it's aware of OnChartEvent()

As it's the first time I wrote an expert advisor, I don't know what exactly it does, but it appears it waits for a new tick,
so when I give it click events, they will fire up when a new tick comes in,
apparently, but I maybe wrong about this, more testing needed (or reading in docs :) )

Anyway,

I would love to know if it's possible to get two, three or multiple click events from scripts some other way. 

 

Scripts can't catch events. Expert advisors and indicators can.

OnChartEvent() is independent from the OnTick() function.

Create a simple EA with OnChartEvent() and put a print inside - it will activate each time you move mouse or do some other thing on the chart.

 

Yeah, I tried exactly that,

even with mouse move event (saw an example in docs)

It triggers immediately actually, which is great.

(There was a sleep() or something else, but now it runs fast)

 

Thank you for responses,

 

It would be useful if we could use OnChartEvent() in scripts.

Right now I'm not aware of the issues that can appear, probably they had good reasons to not permit scripts to receive events 

 

One line in docs, made me raise an eyebrow though:

"It is not recommended to use infinite loops to handle events. The exception to this rule may be only scripts that process only a single Start event

( https://www.mql5.com/en/docs/runtime/running ) 

So that means it's possible to handle events from Start event inside scripts? I don't get it, but maybe they refer to something else... 

Documentation on MQL5: MQL5 programs / Program Running
Documentation on MQL5: MQL5 programs / Program Running
  • www.mql5.com
MQL5 programs / Program Running - Reference on algorithmic/automated trading language for MetaTrader 5
 

You can test it easy way -

  • create a script
  • make infinite loop with Sleep(50) inside 
  • implement OnChartEvent()

Other option would be to make expert advisor that will unload itself at some point with ExpertRemove() function. This way you can emulate script behaviour - do something and finish, and you will have OnChartEvent() without infinite loop.

 
Drazen Penic:

You can test it easy way -

  • create a script
  • make infinite loop with Sleep(50) inside 
  • implement OnChartEvent()

Other option would be to make expert advisor that will unload itself at some point with ExpertRemove() function. This way you can emulate script behaviour - do something and finish, and you will have OnChartEvent() without infinite loop.

 

I tested it.

OnChartEvent() doesn't work for script... at least up to this date :)

 

But many thanks for the idea with ExpertRemove(), I was not aware I could do that => great! 

Reason: