Questions from Beginners MQL5 MT5 MetaTrader 5 - page 577

 
Artyom Trishkin:
Well, I would form it all into a function, and call it when I need it - even at every tick (if it is reasonable and necessary), or at opening of a new candle - for example, once an hour, if we work at H1.

With the function, of course, it's handy. I've got the code figured out. It's clear now. I will think, how to bring it to those purposes I want and understand, when and at what stage to take the necessary data.

 
Koldun Zloy:

If it was outright "the height of recklessness", it would be banned.

WinAPI greatly extends the capabilities of MQL. And you can do something stupid even without dll.

And here is an example of string passing to the clipboard:

Well done, and with comments too.

Thank you.

Here is another question.

Would it be possible to emulate a mouse click at given coordinates?

 
mila.com:

Great, and with a commentary too.

Thank you.

One more question.

Is it possible to make an emulation of a mouse click at the given coordinates?

If you don't have to do it from MQL, look at AutoIT, you can do anything there. The product is free, you only need one DLL to interact with MQL.
 
mila.com:

Great, and with a commentary too.

Thank you.

One more question.

Is it possible to make an emulation of a mouse click at the given coordinates?

Yes, we can.

#define  MK_LBUTTON     0x0001
#define  WM_LBUTTONDOWN 0x0201
#define  WM_LBUTTONUP   0x0202

struct POINT
{
   int x;
   int y;
};

#import "User32.dll"
   uint WindowFromPoint( int x, int y );
   uint PostMessageW( uint hWnd, uint Msg, uint wParam, uint lParam );
   int ScreenToClient( uint hWnd, POINT& lpPoint );
#import

int x = 1000;  // Экранные координаты
int y = 350;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   uint hwnd = WindowFromPoint( x, y ); // Получаем дескриптор окна в нужном месте
   
   if( hwnd ){
      POINT point;
      point.x = x;
      point.y = y;
      ScreenToClient( hwnd, point );  // Преобразуем экранные координаты в координаты рабочей области окна
      
      uint lParam = (point.y * 65536) + (point.x & 0xFFFF);  // Упаковываем координаты в 32-разрядное целое
      
      PostMessageW( hwnd, WM_LBUTTONDOWN, MK_LBUTTON, lParam ); // Посылаем сообщения от "мыши"
      PostMessageW( hwnd, WM_LBUTTONUP, MK_LBUTTON, lParam );
   }
}
 
Artyom Trishkin:
Well, I would form it all into a function and call it when I needed - at every tick (if needed and reasonable) or at opening of a new candle - for example, once an hour, if we work with H1.

Artem, I understand correctly, if we have a loop:for(int i=0; i<copy_bars; i++) and there is a loop for(int j=0; j<copy_bars; j++) where if(j==i) continue; ,

it will mean that the counting is parallel and if we have roughly 5 candlesticks, the comparison will continue:

1 with 1 - discard.

1 with 2, 1 with 3, 1 with 4, 1 with 5.

Then a cycle will start where it will be:

2 with 1, 2 with 3, 2 with 4, 2 with 5.

and so on.

Is this how it's going to be?

 
Andrey Koldorkin:

Artem, I understand correctly, if we have a loop:for(int i=0; i<copy_bars; i++) and there is a loop for(int j=0; j<copy_bars; j++) where if(j==i) continue; ,

it will mean that the counting is parallel and if we have roughly 5 candlesticks, the comparison will continue:

1 with 1 - discard.

1 with 2, 1 with 3, 1 with 4, 1 with 5.

Then a cycle will start where it will be:

2 with 1, 2 with 3, 2 with 4, 2 with 5.

and so on.

Will it be the same?

Yes.
 
Artyom Trishkin:
Yes.

Another question: If we don't need to compare the current candle, then don't we need to start the count from 1 and not zero?

Or should we understand that here 0 is candle 1, 1 is candle 2, etc.?

 
Andrey Koldorkin:

Another question: If we don't need to compare the current candle, then don't we need to start the count from 1 and not zero?

or should we understand that here 0 is candle 1, 1 is candle 2, etc.?

Here zero is the beginning of the array. And in the array we write the candlesticks from the first to ..., not "to", but in the number we need:

int copied=CopyRates(Symbol(),PERIOD_CURRENT,1,copy_bars,array);

1 is the number of candlestick to copy, and copy_bars is the number of candlesticks to copy.

Therefore, array[] contains the needed candlestick in the cell with index 0 - either 1 or 1+copy_bars-1. Depending on indexing direction of array[] (ArraySetAsSeries() sets the indexing direction, which can be checked using ArrayGetAsSeries(), ArrayIsSeries())

 
Artyom Trishkin:

Here zero is the beginning of the array. And in the array we write candlesticks from first to ..., not "to", but in number of candlesticks we need:

1 is the number of candlestick to copy, and copy_bars is the number of candlesticks to copy.

Therefore, array[] contains the required candle in the cell with index 0 - either 1 or 1+copy_bars-1. Depending on indexing direction of array[] (ArraySetAsSeries() sets indexing direction, which can be checked with ArrayGetAsSeries(), ArrayIsSeries())

This programming. The further into the woods....

I first just checked what it shows via Alert. It seems to be vice versa, i.e. the candle closest to the current one has maximal number.

Then I checked it via:

bool series=ArrayIsSeries(dataCandle);

Alert (series);

and the script shows "false".

By my logic, if false is on the wrong end, then true will be on the right end.

I prescribed:

ArraySetAsSeries (dataCandle, true); //change direction

bool series=ArrayIsSeries(dataCandle); //check again

Alert (series); //write the result on the screen.

But I still get "false" after that.

What's my problem?

 
Andrey Koldorkin:

Oh this programming. The further into the woods....

I first checked just through Alert what it was giving out. It turns out to be the opposite, i.e. the candle closest to the current candle has the maximum number.

Then I checked it via:

bool series=ArrayIsSeries(dataCandle);

Alert (series);

and the script shows "false".

By my logic, if false is on the wrong end, then true will be on the right end.

I prescribed:

ArraySetAsSeries (dataCandle, true); //change direction

bool series=ArrayIsSeries(dataCandle); //check again

Alert (series); //write the result on the screen.

But I still get "false" after that.

What's my problem?

The dataCandles is a structure. The array where we write the candles from the history - array[]. And we need to make it as a time series so that its indexing coincides with indexing of candlesticks on the chart. I.e. the zero cell of array[] will correspond to the closest candlesticks to the current date.

I.e. 1. copy candlesticks into array array[], 2. make it a time series, then read values from it into the structure.

You can do without array[] - just write data directly from the chart into the structure, but I suggested this for compatibility with Five - it allows to copy directly only in the indicator using high[i], low[i] and other data, but in the script or Expert Advisor, we have to first copy the necessary history interval into the array, which I did.