Giter VIP home page Giter VIP logo

lws2mql's Introduction

lws2mql

Websocket client library for MetaTrader Trading Platform. Supports MQL4 and MQL5. Lets the MetaTrader Scripts, Expert Advisors, or Indicators connect to a Websocket server. This is based on the libwebsocket library.

How To

Since we are generating a Windows DLL for MetaTrader, it is easier to build on a Windows environment or at least using Wine.

Build

Change to src folder

C:\> cd ${git-repo-dir}\src

Compile with GCC

C:\> gcc -c -DLWS_EXPORTS lws2mql.c

Generate the .dll file also linking in lib\libwebsockets.dll

C:\> gcc -shared -o lws2mql.dll lws2mql.o -L..\lib\ -lwebsockets

Deploy

Copy the files under MQL5\Include and MQL5\Libraries folders to the relevant places under your MetaTrader 5 installation folder. Libraries are the pre-built DLLs or you can use the ones you've built yourself. Includes are the MQL header files that will provide easy interfacing with the DLLs.

Example

In your .mq5 source file, include the Websocket.mqh file and make connection, send/receive data as the example below.

#include "Websocket.mqh"

CWebsocket ws;

int OnInit()
  {
    ws.Init();
    ConnectWebsocket();

    //--- succeed
    return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
    if(ws.ClientDisconnect()<0) PrintFormat("[%s] Websocket %i disconnect error: %s",__FUNCTION__,ws.GetHandle(),ws.GetError());
    ws.Deinit();
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
    if (Broadcast(1, "Hello lws2mql")>-1) PrintFormat("Signal %i broadcast OK via socket %i",i,ws.GetHandle());
    else PrintFormat("Signal %i broadcast FAILED on socket %i, Error: %s",i,ws.GetHandle(),ws.GetError());
  }

bool ConnectWebsocket()
  {
    int ret = ws.ClientConnect("127.0.0.1",8080);
    if (ret<0)
      {
        PrintFormat("Websocket %i connection error: %s",ws.GetHandle(),ws.GetError());
        return false;
      }
    else
      {
        string msg;
        msg = "{\"type\":\"init\", \"message\":\"Hola lws2mql\"}";
        ret = ws.Send(msg);
        return true;
      }
  }

int Broadcast(int _channel_id, string _message)
  {
    payload = ""
     "{"
       "\"type\":\"signal\","
       "\"payload\": {"
         "\"channel_id\":"+(string)_channel_id+","
         "\"message\":\""+_message+"\""
       "}"
     "}";
    return (ws.Send(payload));
  }

lws2mql's People

Contributors

krisn avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.