Experts: MQL5 Programming for Traders – Source Codes from the Book. Part 7 - page 2

 

Here is another bunch of minor bugfixes and improvements of wss source codes in a zip-archive.

Among other things you can now analyze unsuccessful upgrade (for example, if authorization is required and status code other than 101 is returned) and take measures:

if(wss.open(initial_headers))
{
   // success, normal flow
}
else
{
  // something went wrong - not upgraded or not connected
  if(wss.isConnected())
  {
     Print("Can't upgrade to websockets");
     bool resolved = false;
     string headers[][2]; // response
     if(wss.getHeaders(headers))
     {
       // TODO: analyze status and solve it
       resolved = ... // true/false
     }
     if(!resolved)
     {
       wss.close(); // close to prevent further attempts to read/write from non-upgraded socket
     }
     else
     {
       wss.open(updated_headers); // try again
     }
  }
}

Also StringUtils.mqh and URL.mqh are also received important fixes.

Files:
wss.zip  16 kb
URL.mqh  5 kb
 

Hi @Stanislav Korotky, i'm new to the MQL5. Find that you post a wss.zip file for websocket use. How to use it, is there a demo or something i can learn. Thank you sincerely!

Stanislav Korotky
Stanislav Korotky
  • 2025.04.07
  • www.mql5.com
Trader's profile
 
wf2000cn #:

Hi @Stanislav Korotky, i'm new to the MQL5. Find that you post a wss.zip file for websocket use. How to use it, is there a demo or something i can learn. Thank you sincerely!

You can find several examples in the algotrading book, in the section describing (step by step) implementation of a web service project for copying trades and signals.

MQL5 Book: Advanced language tools / Projects / Project plan of a web service for copying trades and signals
MQL5 Book: Advanced language tools / Projects / Project plan of a web service for copying trades and signals
  • www.mql5.com
As an end-to-end demonstration project, which we will develop throughout this chapter, we will take a simple, but at the same time quite...
 
Stanislav Korotky #:

You can find several examples in the algotrading book, in the section describing (step by step) implementation of a web service project for copying trades and signals.

Ok,i'll learn it first. Thank you sincerely!
 

Hello @Stanislav Korotky 

I am trying to implement a partial position close with a pending order in the opposite direction.

eg:

  • if I have an open Buy position on XAUUSD with 2 lots size 
  • open a Sell pending order on XAUUSD with 1 lot size
  • within the OnTradeTransaction -> TRADE_TRANSACTION_DEAL_ADD (transaction.type) event, I have

MqlTradeRequestSync reqs(transaction.symbol);

reqs.closeby(target_position_ticket_id, transaction.order);

I believe the above line should be able to get the job done but my challenge is how to get the `target_position_ticket_id` within the OnTradeTransaction -> TRADE_TRANSACTION_DEAL_ADD (transaction.type) event handler or somehow associate  the pending order with the target_position_ticket_id it is expected to close whenever the pending order is filled.

I tried adding the `target_position_ticket_id` to the pending order comment when I am creating the pending order but the issue is that within the OnTradeTransaction -> TRADE_TRANSACTION_DEAL_ADD (transaction.type) I cannot access the comment since 
MqlTradeTransaction attributes does not have 'comment'

Please I will appreciate your help 

 
pauldic #:

I am trying to implement a partial position close with a pending order in the opposite direction.

eg:

  • if I have an open Buy position on XAUUSD with 2 lots size 
  • open a Sell pending order on XAUUSD with 1 lot size
  • within the OnTradeTransaction -> TRADE_TRANSACTION_DEAL_ADD (transaction.type) event, I have

MqlTradeRequestSync reqs(transaction.symbol);

reqs.closeby(target_position_ticket_id, transaction.order);

I believe the above line should be able to get the job done but my challenge is how to get the `target_position_ticket_id` within the OnTradeTransaction -> TRADE_TRANSACTION_DEAL_ADD (transaction.type) event handler or somehow associate  the pending order with the target_position_ticket_id it is expected to close whenever the pending order is filled.

I tried adding the `target_position_ticket_id` to the pending order comment when I am creating the pending order but the issue is that within the OnTradeTransaction -> TRADE_TRANSACTION_DEAL_ADD (transaction.type) I cannot access the comment since 
MqlTradeTransaction attributes does not have 'comment'

The closeby(ticket1, ticket2) method from the book assumes that you use a hedging account and already have 2 open positions (buy and sell) with tickets ticket1 and ticket2. I suppose, it's impossible (in general) to perform TRADE_ACTION_CLOSE_BY action against pending order. But if your account is in netting mode, I think you can simply place a pending order in opposite direction and when it triggers, it will modify existing position accordingly - that's close it, or does partial close, or even invert (if new volume is larger than old).

 
Small patches in websockets (a la carte & en masse).
Files:
wsprotocol.mqh  17 kb
wss.zip  16 kb
 
Stanislav Korotky #:
Small patches in websockets (a la carte & en masse).
@Stanislav Korotky Thanks for the continuous update, please there is something I have been struggling to figure out. On the MT5 "Market Watch" I can right click on any "Symbol" and view all it's associated properties just as I would programmatically by clicking on "Specification". 

One of these properties is the Symbol's "Commissions" which obviously differs from brokers as can be seen in the attached picture. However, I have not been be able to access this property programmatically. Please can you help me with a clue on how to get the symbol's  commission?


On the side this message often shows on the log whenever am working with PositionFilter especially when the EA is loaded the first time or recompiled "Unresolved int value as enum: 8 for MonitorInterface<ENUM_POSITION_PROPERTY_INTEGER,ENUM_POSITION_PROPERTY_DOUBLE,ENUM_POSITION_PROPERTY_STRING>". It still works fine and does not stop anything
 
pauldic #:
@Stanislav Korotky Thanks for the continuous update, please there is something I have been struggling to figure out. On the MT5 "Market Watch" I can right click on any "Symbol" and view all it's associated properties just as I would programmatically by clicking on "Specification". 

One of these properties is the Symbol's "Commissions" which obviously differs from brokers as can be seen in the attached picture. However, I have not been be able to access this property programmatically. Please can you help me with a clue on how to get the symbol's  commission?

Unfortunately, not all properties of the terminal's entities are accessible via MQL5.

pauldic #:
On the side this message often shows on the log whenever am working with PositionFilter especially when the EA is loaded the first time or recompiled "Unresolved int value as enum: 8 for MonitorInterface<ENUM_POSITION_PROPERTY_INTEGER,ENUM_POSITION_PROPERTY_DOUBLE,ENUM_POSITION_PROPERTY_STRING>". It still works fine and does not stop anything

This is just a warning caused by the fact that the corresponding built-in enumeration of MQL5 has a gap in the constants which are normally assigned consecutively. This gap is occured because MQL5 is constantly changing, and some constants can become obsolete and then be eliminated. You can edit the source code to prevent such warnings.

 

I'm attaching an updated version of JSON library - toyjson3.mqh - a replacement for toyjson2.mqh and toyjson.mqh from the book.

This version is optimized for processing huge json files. For the sake of simplicity, the previous versions use StringSplit function for text tokenization during parsing, and this call can internally request gigabytes of spare memory due to fragmentation and anticipation of further data flow. It's released after the parsing, but MQL5 memory manager can keep it reserved for a while, because it frees garbage in postponed manner.

Now it takes times less memory during parsing.

Files:
toyjson3.mqh  39 kb