Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1461

 

I can't figure out how to do it.

When the mouse wheel scroll event occurs, the code is executed.

The mouse wheel can be scrolled for several clicks at a time, and we will get not one event with dparam = number of clicks, but several events with dparam = +/-120.

The code is executed for some time, which is much longer than the time between clicks when scrolling the mouse wheel.

As a result, it turns out that nobody is scrolling the mouse wheel anymore, and the programme continues to recalculate for some more time.

//+------------------------------------------------------------------+
void OnInit()
  {
   ChartSetInteger(0, CHART_EVENT_MOUSE_WHEEL, 0, true);
  }
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long & lparam, const double & dparam, const string & sparam)
  {
   static double delta = 0;

   if(id == CHARTEVENT_MOUSE_WHEEL)
     {
      //--- какой то код
      delta += dparam / 120;
      Sleep(1000);
      //---
      Print(delta);
     }
  }
//+------------------------------------------------------------------+
15:12:22.820    test (EURUSD,H1)        0.0
15:12:23.821    test (EURUSD,H1)        1.0
15:12:24.811    test (EURUSD,H1)        2.0
15:12:25.823    test (EURUSD,H1)        3.0
15:12:26.817    test (EURUSD,H1)        4.0
15:12:27.814    test (EURUSD,H1)        5.0
The mouse wheel scrolled at 15:12:22.820 and then the programme was executed 5 more times, for 5 whole seconds.


Question: how to execute the programme code once instead of several times.

That is, the wheel scrolled for 5 clicks, delta = dparam / 120 * 5.

 
Aleksandr Slavskii #:

I can't figure out how to do it.

When the mouse wheel scroll event occurs, the code is executed.

The mouse wheel can be scrolled for several clicks at a time, and we will get not one event with dparam = number of clicks, but several events with dparam = +/-120.

The code is executed for some time, which is much longer than the time between clicks when scrolling the mouse wheel.

As a result, it turns out that nobody is scrolling the mouse wheel anymore, and the programme keeps recalculating for some more time.

The mouse wheel scrolled at 15:12:22.820 and then the programme was executed 5 more times, for 5 whole seconds.


Question: how to execute the programme code once instead of several times.

That is, the wheel scrolled for 5 clicks, delta = dparam / 120 * 5.

Try this

//+------------------------------------------------------------------+
void OnInit()
  {
   ChartSetInteger(0, CHART_EVENT_MOUSE_WHEEL, 0, true);
  }
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long & lparam, const double & dparam, const string & sparam)
  {
   static double delta = 0;
   bool flag = true;
   if(id == CHARTEVENT_MOUSE_WHEEL)
     {
      //--- какой то код
      delta += dparam / 120;
      Sleep(1000);
      //---
      if(flag)
       {
        Print(delta);
        flag = false;
       }
     }
  }
//+------------------------------------------------------------------+

I didn't check it, if anything, it's not my fault...)))))) I'll have to do something with the flag... and maybe I'll have to put the code into a custom function and mess with the flag there.

 
Thank you very much!
 

On 03 June I published my script for MT5(https://www.mql5.com/ru/code/44732).... It is still in the status of "being checked by moderator". Who knows how long to wait for checking?

Скачать бесплатно скрипт 'Quick Change MA (method, period)' от 'NotBuffett' для MetaTrader 5 в MQL5 Code Base
  • www.mql5.com
Скачать бесплатно скрипт 'Quick Change MA (method, period)' от 'NotBuffett' для MetaTrader 5 в MQL5 Code Base
 
Alexey Viktorov #:

Try this

I didn't check it, if it's not my fault...)))))) I'll have to mess with the flag somehow... and maybe I'll have to put the code into a custom function and mess with the flag there.

The problem is that custom functions are run only inside event functions, and I have only two such functions OnInit and OnChartEvent in my EA.

In general, I managed to do it, though not quite ace, but still better than it was.

//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long & lparam, const double & dparam, const string & sparam)
  {
   static double delta = 0;
   static ulong timeWheel = 0;
   static bool mouse_wheel = false;

   if(id == CHARTEVENT_MOUSE_WHEEL)
     {
      delta += dparam / 120;
      mouse_wheel = true;
      timeWheel = GetMicrosecondCount();
     }

   if(mouse_wheel && GetMicrosecondCount() - timeWheel > 0)
     {
      Print(delta);
      //--- какой то код
      Sleep(1000);
      //---
      delta = 0;
      mouse_wheel = false;
     }
  }
//+------------------------------------------------------------------+
23:48:28.853    test (EURUSD,H1)        1.0
23:48:29.857    test (EURUSD,H1)        1.0
23:48:30.856    test (EURUSD,H1)        4.0
23:48:31.881    test (EURUSD,H1)        2.0
23:48:32.880    test (EURUSD,H1)        4.0
23:48:33.887    test (EURUSD,H1)        11.0
23:48:34.914    test (EURUSD,H1)        15.0
23:48:35.926    test (EURUSD,H1)        7.0
23:48:36.936    test (EURUSD,H1)        9.0
23:48:46.477    test (EURUSD,H1)        6.0
23:48:47.479    test (EURUSD,H1)        -1.0
 
Aleksandr Slavskii #:

The problem is that custom functions are run only inside event functions, and I have only two such functions OnInit and OnChartEvent in my EA.

In general, I managed to do it, although not quite a good idea, but still better than it was.

You can also call custom functions from OnChartEvent without any problems.

The flag is declared at the global level. Inserting OnTick solely to reset the flag is not a problem and will not delay code execution.

If the flag is open, a custom function is called. Internally, the flag was closed. In OnTick, the flag is opened again and waits for the next execution of the user function.

I think it's easier than getting stuck on execution time.

 
Alexey Viktorov #:

You can call custom functions from OnChartEvent too, no problem.

The flag is declared at the global level. Inserting OnTick solely to reset the flag is not a problem and will not delay code execution.

If the flag is open, a custom function is called. Internally, the flag was closed. In OnTick the flag is opened again and waits for the next execution of the user function.

I think it's easier than getting tied to the execution time.

I don't quite get it, or rather I don't get it at all :)

And if there are no ticks, OnTick doesn't work without ticks, what should I do?


Actually I call custom functions from OnChartEvent anyway. It was just a problem to track the last mouse click when scrolling the wheel.

The task was to delay the execution of the custom function until the moment of stopping the wheel scrolling, while counting the number of clicks during scrolling.

In principle, it worked.

 
NotBuffett #:

On 03 June I published my script for MT5(https://www.mql5.com/ru/code/44732).... It is still in the status of "being checked by moderator". Who knows how long to wait for checking?

Just published the code. Verification took less than a minute.

Most likely you made a mistake while publishing.

 
Aleksandr Slavskii #:

Just published the code. Checking it took less than a minute.

Most likely you made a mistake when publishing it.

Tried publishing again... and it's the same thing:

under "5. Test" it says"Thetest was completed without errors" and offers to publish it, but when you click on the "Publish" button you get an error

404. This page does not exist

In the subsection "My Codes" there are now two codes and on both of them "checked by moderator".

In general, I'll ask a question in the general thread, maybe the moderators will still notice.

 
NotBuffett #:

Tried publishing again... and it's the same thing:

under "5. Test" it says"Thetest was completed without errors" and offers to publish it, but when I click on the "Publish" button I get an error

404. This page does not exist

There are now two codes in the "My Codes" subsection and both of them have "being checked by moderator".

In general, I will ask a question in the general thread, maybe moderators will still notice.

So they see everything here. You can rest assured.

Reason: