Question: how MQL4 works

 

hi guys,

I am new to MQL4 and Meta Trader4 as well as this forum.

just wondering how MQL4 works in terms of the Trading functions. Do all the functions required to call the backend server. Please let me explain a bit more my question below.

For example:

int total=OrdersTotal();
for(int pos=0;pos<total;pos++) {
if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
if(OrderType() == OP_BUYSTOP && OrderSymbol() == Symbol()) OrderDelete(OrderTicket());
}

for the above sample code, lets imagine the code has no error, my treal trading account has login and connected properly to my broker server. Everything is good and no problem to let the machine to trade.

when OrdersTotal() get called, does it require to call the backend server to get the total order number or the number has been kept in my Meta Trader 4 client, which is installed on my own local PC?
when OrderSelect(pos,SELECT_BY_POS) get called, does it require to call the backedn server to select/pick (this) particular order, or my local Meta Trader 4 client already has all the order information and all of my other orders?
so the same question to OrderType(), OrderSymbol(), OrderClosePrice(), OrderProfit() and ect..........

I am sure, functions such as OrderDelete(), OrderSend() and OrderModify(), they requies to call my broker server to delete/place/amend my orders. However, the others (above)?

Please help me on these questions.

thanks,

Richard

 
wenfeiyunyun:

hi guys,

I am new to MQL4 and Meta Trader4 as well as this forum.

just wondering how MQL4 works in terms of the Trading functions. Do all the functions required to call the backend server. Please let me explain a bit more my question below.

For example:

<CODE REMOVED>

for the above sample code, lets imagine the code has no error, my treal trading account has login and connected properly to my broker server. Everything is good and no problem to let the machine to trade.

when OrdersTotal() get called, does it require to call the backend server to get the total order number or the number has been kept in my Meta Trader 4 client, which is installed on my own local PC?
when OrderSelect(pos,SELECT_BY_POS) get called, does it require to call the backedn server to select/pick (this) particular order, or my local Meta Trader 4 client already has all the order information and all of my other orders?
so the same question to OrderType(), OrderSymbol(), OrderClosePrice(), OrderProfit() and ect..........

I am sure, functions such as OrderDelete(), OrderSend() and OrderModify(), they requies to call my broker server to delete/place/amend my orders. However, the others (above)?

Please read some other posts before posting . . .

Please edit your post . . . please use the SRC button to post code: How to use the SRC button.

Your code does have an error, always count down in loops where you are Closing or Deleting orders: Loops and Closing or Deleting Orders


I suspect you are correct that many of the trading functions get their information from a local copy of your trading history this would explain why users occasionally post that they are having intermittent issues with getting correct data, I suspect t is a synchronisation problem, perhaps a tick needs to pass to update the local copy of the trading history.

 

I've read elsewhere that only OrderSend() and OrderModify(),ORderClose() force communication with the broker, as some scalping EA's have another thread that repeatedly calls OrderModify on a 'dummy' pending order, too keep the connection alive, so that they do not waste time re-authenticating when they need to do something quickly..

Other alternatives failed to keep the connection open, and some brokers responding by charging a fraction of a cent for each OrderModify (or something like that)...

 
  1. OrderSend, modify, close, and delete all force a communication as they change something on the server.
  2. There is no need to keep communicating as the connection is ALWAYS open and the next price change is sent from the broker's server to the terminal. There is no need as there is no re-authenticating. If you do this the broker will return error 8
  3. Mt4 also recognizes when the server stops sending update OR keep alive packets (~1 minute timeout) and will attempt to reconnect.
 
WHRoeder:

  1. There is no need to keep communicating as the connection is ALWAYS open and the next price change is sent from the broker's server to the terminal. There is no need as there is no re-authenticating. If you do this the broker will return error 8
  2. Mt4 also recognizes when the server stops sending update OR keep alive packets (~1 minute timeout) and will attempt to reconnect.


First lets be clear that the context of my reply brought this up as an aside, it was not a recommended action to the OP, but just more info that reinforces the conclusion that only OrderSend/Modify/Close/Delete communite back to the broker, but not much else.

That being said, the aside was .. (to paraphrase/re-phrase)

"that it has been observed by others that execution speed of OrderSend, OrderModify etc increased by up to 500ms when there has been another recent one within 30 seconds."


Conversely, other MQ4 commands (OrdersTotal, OrderSelect etc) did not cause the improvement (and thus by implication do not require calls to the backend broker. This is discussed at length here http://www.forexfactory.com/showthread.php?t=337102 ( The conclusion on that thread was that some session had times out at some level and and to be re-created.)

Of course there is no need for majority of EA's to care about such things, it was, just an aside that lends weight to the observation of OrderSend/Modify/Delete/Close vs other commands.

So with that in mind lets diverge down this 'aside' path a little in response to :

"There is no need to keep communicating as the connection is ALWAYS open and the next price change is sent from the broker's server to the terminal. There is no need as there is no re-authenticating. If you do this the broker will return error 8"

Yes, some of the chaps on that thread, do get errors, and curt emails from their broker (again discussed at length how to avoid,

and yes, there is no NEED to keep communicating (for the majority of EAs), and certainly A connection is always open and active... but is it the SAME connection that places, updates and deletes market orders?

There is no need to do so but look :

C:\>tasklist | findstr terminal
terminal.exe                 820 RDP-Tcp#3               1  1,006,416 K

C::\>netstat -ano | findstr "820"
  TCP    192.168.88.128:1340    5.11.81.108:443        ESTABLISHED     820

  TCP    192.168.88.128:1342    5.11.81.108:443        ESTABLISHED     820


Terminal opens multiple connections to "somewhere".. Is it not outside the realms of possiblity suppose that quote data comes in on one 'always on' connection, but Market executions go out on another is it?

And if so, maybe, just maybe the latter connection does time out after 30 secons, and needs to be re-established?

Especially as quote data comes in pretty fast, on one connection, would not seem very sensible design to send market execution orders in the other direction on the same socket?

All supposition on my part of course, (I'm still playing with demo accounts, and not into scalping at the moment), but is the assumption that underpins your argument "there is only one connection to the brokers server" true ?


Of course this is all not really relevant to the OP question, and plenty more info in the linked thread.

I think I could have said this in a lot less words! But I just get the feeling I had to be verbose :)

 
ydrol: Terminal opens multiple connections to "somewhere"..
Not always
C:\Users\Bill>tl term
terminal.exe 3864 Console 1 397,692 K

C:\Users\Bill>netstat -ano|findstr 3864

TCP 192.168.0.2:55562 199.58.56.141:443 ESTABLISHED 3864

C:\Users\Bill>

 
maybe do the same immediately after a trade?
 

ydrol:

Is it not outside the realms of possiblity suppose that quote data comes in on one 'always on' connection, but Market executions go out on another is it?

And if so, maybe, just maybe the latter connection does time out after 30 secons, and needs to be re-established?


As I suspected ... MT4 recieves quotes on one 'ALWAYS Open' socket and uses a seperate connection to send order commands. The seperate connection is closed by the server after 30 seconds and eventually disappears...

Subsequent Order commands will establish a new session/socket. Not important for the majority of EA's I stress, but thought I'd post my findings up anyways...


$ while true ; do   echo ; date ; netstat -ano | grep 1484 ; sleep 15 ; done

[ Startup - My terminal opens two sockets ]

Thu, Aug 15, 2013 11:53:35 PM
  TCP    192.168.88.128:1524    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1526    5.11.81.108:443        ESTABLISHED     1484

Thu, Aug 15, 2013 11:53:58 PM
  TCP    192.168.88.128:1524    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1526    5.11.81.108:443        ESTABLISHED     1484

[ OrderSend - A new connection opened ]

Thu, Aug 15, 2013 11:54:14 PM
  TCP    192.168.88.128:1524    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1526    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1623    5.11.81.108:443        ESTABLISHED     1484

[ OrderSend again - same connection reused]

Thu, Aug 15, 2013 11:54:30 PM
  TCP    192.168.88.128:1524    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1526    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1623    5.11.81.108:443        ESTABLISHED     1484

[ Do nothing - connection still open ]

Thu, Aug 15, 2013 11:54:45 PM
  TCP    192.168.88.128:1524    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1526    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1623    5.11.81.108:443        ESTABLISHED     1484

[ Do nothing - connection closed - FIN_WAIT_2" means server closed the socket and didnt wait for client  - it will eventually disappear ]

Thu, Aug 15, 2013 11:55:01 PM
  TCP    192.168.88.128:1524    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1526    5.11.81.108:443        ESTABLISHED     1484
  TCP    192.168.88.128:1623    5.11.81.108:443        FIN_WAIT_2      1484

 
Just add to this, my description of FIN_WAIT_2 above is not accurate, the FIN_WAIT_2 state should be a transitory state and seeing it hanging around suggests a bug in either the MT4 client or server - FIN_WAIT_2
 

I would have expected authentication to take place each time a trade request is made. A permentantly open connection to the trading server would be a security risk.

 

Hi, it closes 30 seconds after a trade instruction is sent. (unless another one is sent etc).. So I think this is OK.

There is a permanent connection receiving quotes in the other direction. -

(caveat: All guesswork based on comments of others & observations.)

Reason: