Discussion of article "Working with sockets in MQL, or How to become a signal provider" - page 7

[Deleted]  
Wemerson Guimaraes:

Hi, This code works on MQL4 ?

I'm trying to port it to mql4, and when the DLL bind() function is called at MQL StartServer function, gives me the message below:



 

Another super useful article!

This option is much more convenient than files and DB.

Thanks camarad ;)

 
Wemerson Guimaraes:

Hi, works fine on MT4.


Tx mate. Incredible code!

Hello, Trying to change this to MQL4 but running into issues due to Trade.mqh requirement on signalclient. How did you go about it?

If you could, please share your MT4 version as well, it will help newbies like me out.

Regards


[Deleted]  

I started copying trades, put an Expert Advisor (server/client) on the charts of each traded symbol for multicurrency Expert Advisor, the Expert Advisor generated a lot of trades during the evening-night-morning, but some trades were lost according to the copied volumes.

I.e. as a minimum it is necessary to add a periodic check for the need for intermediate synchronisation of server and client position volumes, similar to the way it works in the Signals service.

Well, and after emergency reloading of terminals the server sends messages to clients for some reason that it has zero positions (although it is not true), and clients obediently close their positions. These are such observations.

Thank you for the article with examples, I will make a service of signals on their basis.

S.Y.:

Очевидно, что данные примеры сервера, рассылающие любому клиенту информацию, не являются оптимальными. К примеру, вы наверняка захотите ограничить доступ к своей информации.  Значит, как минимум, к обязательным требованиям необходимо отнести:

  • client authorisation (login/password);
  • password protection (ban/block login or IP).

how can this be implemented? zero knowledge of this (even running a server/client on a local machine, some left client connected to the server).

 
Aleksandr Volotko:

How can this be implemented? zero knowledge of this (even running a server/client on the local machine, some left client connected to the server).

https://www.mql5.com/en/code/15534

Adding password protection to your Indicator / Expert Advisor
Adding password protection to your Indicator / Expert Advisor
  • votes: 19
  • 2016.05.25
  • Claude Beaudoin
  • www.mql5.com
Several methods of protecting your code have been proposed over the past, but were either too simple (less secure), required re-compilation of your code for each new customer (ok if you only plan on having a dozen or so customers) or far too complicated involving a remote host to validate the client terminal. Here I propose a simple password...
[Deleted]  
fxsaber:


Thanks, I'm gonna go look into it.

[Deleted]  
Aleksandr Volotko:

Well, after emergency reloading of terminals the server sends messages to clients for some reason that it has zero positions (although it is not true), and clients obediently close their positions. Such observations.

I understand that all sorts of misunderstandings with positions occur because of the moments when the terminal has started, but has not yet connected to the trading server, and the sockets are raised at once and either the server sends information about the lack of positions to clients and they cover the positions they have, or the client receives information from the server about positions and until he sees his own - hurries to open them, backing up the available volumes. I understand that a preliminary check of terminal connection with the trading server will solve these misunderstandings.

Otherwise, the stability of sockets is pleasing.

Added:

One damn takki slip from the server messages with zero volumes of positions, which is not true, I do not understand how this happens and where these zeros are taken.

2018.02.10 17:42:49.549 signalclient (USDCHF,M15)       connect OK
2018.02.10 17:42:49.555 signalclient (USDCAD,M15)       connect OK
2018.02.10 17:42:49.555 signalclient (NZDUSD,M15)       connect OK
2018.02.10 17:42:49.555 signalclient (USDJPY,M15)       connect OK
2018.02.10 17:42:49.581 signalclient (AUDUSD,M15)       connect OK
2018.02.10 17:42:49.582 signalclient (GBPUSD,M15)       connect OK
2018.02.10 17:42:49.596 signalclient (EURUSD,M15)       connect OK
2018.02.10 17:42:50.562 signalclient (USDCHF,M15)       received msg from server: <<USDCHF|0.00>>
2018.02.10 17:42:50.562 signalclient (NZDUSD,M15)       received msg from server: <<NZDUSD|0.00>>
2018.02.10 17:42:50.562 signalclient (NZDUSD,M15)       server position: NZDUSD|0.00
2018.02.10 17:42:50.562 signalclient (USDJPY,M15)       received msg from server: <<USDJPY|0.00>>
2018.02.10 17:42:50.562 signalclient (USDCHF,M15)       server position: USDCHF|0.00
2018.02.10 17:42:50.562 signalclient (USDCAD,M15)       received msg from server: <<USDCAD|0.00>>
2018.02.10 17:42:50.562 signalclient (USDJPY,M15)       server position: USDJPY|0.00
2018.02.10 17:42:50.562 signalclient (USDCAD,M15)       server position: USDCAD|0.00
2018.02.10 17:42:50.574 signalclient (USDCAD,M15)       full close position
2018.02.10 17:42:50.575 signalclient (USDJPY,M15)       nothing change
2018.02.10 17:42:50.575 signalclient (NZDUSD,M15)       full close position
2018.02.10 17:42:50.577 signalclient (USDCHF,M15)       nothing change
2018.02.10 17:42:50.593 signalclient (AUDUSD,M15)       received msg from server: <<AUDUSD|0.00>>
2018.02.10 17:42:50.593 signalclient (AUDUSD,M15)       server position: AUDUSD|0.00
2018.02.10 17:42:50.594 signalclient (GBPUSD,M15)       received msg from server: <<GBPUSD|0.00>>
2018.02.10 17:42:50.594 signalclient (GBPUSD,M15)       server position: GBPUSD|0.00
2018.02.10 17:42:50.600 signalclient (GBPUSD,M15)       full close position
2018.02.10 17:42:50.601 signalclient (AUDUSD,M15)       full close position
2018.02.10 17:42:50.609 signalclient (EURUSD,M15)       received msg from server: <<EURUSD|0.00>>
2018.02.10 17:42:50.609 signalclient (EURUSD,M15)       server position: EURUSD|0.00
2018.02.10 17:42:50.613 signalclient (EURUSD,M15)       nothing change
2018.02.10 17:42:50.701 signalclient (USDCAD,M15)       CTrade::OrderSend: market sell 3.54 USDCAD [market closed]
2018.02.10 17:42:50.701 signalclient (NZDUSD,M15)       CTrade::OrderSend: market sell 2.50 NZDUSD [market closed]
2018.02.10 17:42:50.705 signalclient (GBPUSD,M15)       CTrade::OrderSend: market sell 0.10 GBPUSD [market closed]
2018.02.10 17:42:50.705 signalclient (AUDUSD,M15)       CTrade::OrderSend: market buy 1.27 AUDUSD [market closed]

If not closed market positions would have collapsed for some reason....

H.Y.: and it happens once in a while, once you reload terminals - everything is normal, as it should be, and the other time it closes everything to zero....

Alexey ( @o_o ), if you know where to dig, please.

I rebooted the terminals afterwards, and no problems, everything worked as it should....

2018.02.10 17:51:17.896 signalclient (EURUSD,M15)       close socket
2018.02.10 17:51:17.896 signalclient (AUDUSD,M15)       close socket
2018.02.10 17:51:17.896 signalclient (USDJPY,M15)       close socket
2018.02.10 17:51:17.898 signalclient (USDCAD,M15)       close socket
2018.02.10 17:51:17.899 signalclient (NZDUSD,M15)       close socket
2018.02.10 17:51:17.900 signalclient (GBPUSD,M15)       close socket
2018.02.10 17:51:17.901 signalclient (USDCHF,M15)       close socket
2018.02.10 17:52:02.337 signalclient (AUDUSD,M15)       connect OK
2018.02.10 17:52:02.340 signalclient (USDCHF,M15)       connect OK
2018.02.10 17:52:02.340 signalclient (USDCAD,M15)       connect OK
2018.02.10 17:52:02.432 signalclient (EURUSD,M15)       connect OK
2018.02.10 17:52:02.481 signalclient (GBPUSD,M15)       connect OK
2018.02.10 17:52:02.481 signalclient (NZDUSD,M15)       connect OK
2018.02.10 17:52:02.665 signalclient (USDJPY,M15)       connect OK
2018.02.10 17:52:03.350 signalclient (AUDUSD,M15)       received msg from server: <<AUDUSD|-1.27>>
2018.02.10 17:52:03.350 signalclient (USDCHF,M15)       received msg from server: <<USDCHF|0.00>>
2018.02.10 17:52:03.350 signalclient (USDCHF,M15)       server position: USDCHF|0.00
2018.02.10 17:52:03.350 signalclient (USDCAD,M15)       received msg from server: <<USDCAD|3.54>>
2018.02.10 17:52:03.350 signalclient (AUDUSD,M15)       server position: AUDUSD|-1.27
2018.02.10 17:52:03.350 signalclient (USDCAD,M15)       server position: USDCAD|3.54
2018.02.10 17:52:03.360 signalclient (AUDUSD,M15)       nothing change
2018.02.10 17:52:03.360 signalclient (USDCAD,M15)       nothing change
2018.02.10 17:52:03.360 signalclient (USDCHF,M15)       nothing change
2018.02.10 17:52:03.444 signalclient (EURUSD,M15)       received msg from server: <<EURUSD|0.00>>
2018.02.10 17:52:03.444 signalclient (EURUSD,M15)       server position: EURUSD|0.00
2018.02.10 17:52:03.449 signalclient (EURUSD,M15)       nothing change
2018.02.10 17:52:03.492 signalclient (GBPUSD,M15)       received msg from server: <<GBPUSD|0.10>>
2018.02.10 17:52:03.492 signalclient (NZDUSD,M15)       received msg from server: <<NZDUSD|2.50>>
2018.02.10 17:52:03.492 signalclient (NZDUSD,M15)       server position: NZDUSD|2.50
2018.02.10 17:52:03.492 signalclient (GBPUSD,M15)       server position: GBPUSD|0.10
2018.02.10 17:52:03.498 signalclient (NZDUSD,M15)       nothing change
2018.02.10 17:52:03.498 signalclient (GBPUSD,M15)       nothing change
2018.02.10 17:52:03.679 signalclient (USDJPY,M15)       received msg from server: <<USDJPY|0.00>>
2018.02.10 17:52:03.679 signalclient (USDJPY,M15)       server position: USDJPY|0.00
2018.02.10 17:52:03.683 signalclient (USDJPY,M15)       nothing change
 

dig in the server. where it determines how many lots it has open by symbol

Add error checks for MQL position request functions, etc.

Add a printout there to see that it was MQL that returned zeros and not the socket that was stupid.

[Deleted]  

I removed OnTrade() from OnInit()-and OnTrade() to begin with, maybe this will be the end of the mess.

H.I.: It didn't end. But a couple of checks for terminal connection with the trade server seem to have solved the problem.

 

Hi all,


I'm compiling on MQL5 and I keep getting the error:

cannot cast 'sockaddr_in' to 'ref_sockaddr'

at the line:

ref_sockaddr ref=(ref_sockaddr)addrin;

EDIT:

For those interested, I solved by manually copying the fields from one struct to another.

Here's the code:

...
  // NOT WORKING
  // ref_sockaddr ref = (ref_sockaddr) addrin;
  // my WA - WORKING
  ref_sockaddr ref = {0};
  sockaddrIn2RefSockaddr( addrin, ref );
...

// ------------------------ 
// Cast (copies) a struct sockaddr_in to a struct ref_sockaddr
// ------------------------
void sockaddrIn2RefSockaddr( sockaddr_in& sai, ref_sockaddr& rsa ) {
  // family
  rsa.ref[ 0] = (char) (( sai.sin_family      ) & 0xff );
  rsa.ref[ 1] = (char) (( sai.sin_family >> 8 )) ;
  // port
  rsa.ref[ 2] = (char) (( sai.sin_port        ) & 0xff) ;
  rsa.ref[ 3] = (char) (( sai.sin_port >>  8  ) );
  // address
  rsa.ref[ 4] = (char) (( sai.sin_addr        ) & 0xff );
  rsa.ref[ 5] = (char) (( sai.sin_addr >>  8  ) & 0xff );
  rsa.ref[ 6] = (char) (( sai.sin_addr >> 16  ) & 0xff );
  rsa.ref[ 7] = (char) (( sai.sin_addr >> 24  ) & 0xff );
  rsa.ref[ 8] = (char) (( sai.sin_addr >> 32  ) & 0xff );
  rsa.ref[ 9] = (char) (( sai.sin_addr >> 40  ) & 0xff );
  rsa.ref[10] = (char) (( sai.sin_addr >> 48  ) & 0xff );
  rsa.ref[11] = (char) (( sai.sin_addr >> 56  ) & 0xff );
  // zero
  rsa.ref[12] = 0;
  rsa.ref[13] = 0;
  rsa.ref[14] = 0;
  rsa.ref[15] = 0;
}
I might add that I had many compilation errors on some WSA error codes, which I had to import manually from winsock.h, and on the function:
WSAErrorDescript