量化交易系统

MT5-Native Websocket

  • 型号:
Main Image

An easy to use, fast, asynchronous WebSocket library for MQL5.

It supports:

  • ws:// and wss:// (Secure "TLS" WebSocket)
  • text and binary data

It handles:

  • fragmented message automatically (large data transfer)
  • ping-pong frames automatically (keep-alive handshake)

Benefits:

  • No DLL required.
  • No OpenSSL installation required.
  • Up to 128 WebSocket Connections from a single program.
  • Various Log Levels for error tracing
  • Can be synchronized to MQL5 Virtual Hosting .
  • Completely native to MQL5.

Sample code below:

//include WSMQL.mqh - a file that has all the declarations required to interact with the library
#include <WSMQL.mqh>
// Or Import WebsocketClient.ex5 declarations
//#import "WebSocketClient.ex5"
//   WSHANDLE Initialize(void);
//   ENUM_WEBSOCKET_STATE ClientState(WSHANDLE handle);
//   void SetMaxSendSize(WSHANDLE handle, int max_send_size);
//   bool Connect(const WSHANDLE handle, const string url, const uint port = 443, const uint timeout = 5000, bool use_tls = true, ENUM_LOG_LEVEL log_level = LOG_LEVEL_ERROR);
//   bool Disconnect(WSHANDLE handle, ENUM_CLOSE_CODE close_code = NORMAL_CLOSE, const string msg = "");
//   int  SendString(WSHANDLE handle, const string message);
//   int  SendData(WSHANDLE handle, uchar& message_buffer[]);
//   int  SendPong(WSHANDLE handle, const string msg = "");
//   int  SendPing(WSHANDLE handle, const string msg);
//   uint ReadString(WSHANDLE handle, string& out);
//   uint ReadBinary(WSHANDLE handle, uchar& out[]);
//   uint ReadStrings(WSHANDLE handle, string& out[]);
//   uint OnReceiveString(WSHANDLE handle, OnMessage callback);
//   uint OnReceiveBinary(WSHANDLE handle, OnBinaryMessage callback);
//#import
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() {
   // library handle can be retrieved through initialization method before being used across the library methods
   WSHANDLE handle = WebSocketClient::Initialize();
   if(handle == 0)
      return;
   string url = "stream.binance.com/ws";//Or wss://stream.binance.com/ws
   string msg = "{\"params\":[\"btcusdt@bookTicker\"],\"method\":\"SUBSCRIBE\",\"id\":27175}";
   //ALERT: Make sure stream.binance.com has been added to WebRequest list in Options -> Expert Advisors tab
   if(!WebSocketClient::Connect(handle, url))
      return;
   WebSocketClient::SendString(handle, msg);
   while(true) {
      if(IsStopped())
         break;
      uint frames = WebSocketClient::OnReceiveString(handle, OnMessage);
      Print("Frames Processed : ", frames);
   }
   Print("Disconnecting...");
   if(WebSocketClient::Disconnect(handle))
      Print("Disconnected!");
}
//+------------------------------------------------------------------+
void OnMessage(string message) {
   Print(message);
}
//+------------------------------------------------------------------+
//Sample Outputs:
//{"result":null,"id":27175}
//Frames Processed : 1
//---
//{"u":35893555769,"s":"BTCUSDT","b":"27812.78000000","B":"7.14299000","a":"27812.79000000","A":"0.81665000"}
//{"u":35893555770,"s":"BTCUSDT","b":"27812.78000000","B":"7.14299000","a":"27812.79000000","A":"0.82309000"}
//{"u":35893555771,"s":"BTCUSDT","b":"27812.78000000","B":"7.14964000","a":"27812.79000000","A":"0.82309000"}
//Frames Processed : 3

Feel free to contact me for support and questions before/after purchase.

https://www.mql5.com/en/users/nikkirachael