timurila/mt4rest

By timurila

Updated 4 days ago

MT4 RESTFul API that allows to work directly with any MT4 Server.

Image
0

224

Client RESTful API for MT4 Servers

Full functional trial version for 14 days. More details and full version available at:

If you need to run trial version at your own server you can use docker:

docker run  -d --restart always --name mt4rest -p 5000:80 mtapiio/mt4rest 

After that open in browser: http://localhost:5000

Connect to MT4 Server

To establish connection with MT4 Server use Connect endpoint. It requires broker host, port, mt4 account number and password.

file

https://mt4.mtapi.io/Connect?user=500476959&password=ehj4bod&host=mt4-demo.roboforex.com&port=443

It returns token that you need to use as 'id' parameter in further requests to server.

Account details

If you need Balance, Equity, Currency, FreeMargin etc. please use AccountSummary endpoint.

file

https://mt4.mtapi.io/AccountSummary?id=demo-token-mt4

Trading

To send different types of orders use OrderSend endpoint.

Order history

Use OrderHistory endpoint to get list of closed orders.

https://mt4.mtapi.io/OrderHistory?id=demo-token-mt4&from=1970-01-01T00%3A00%3A00&to=2022-09-01T00%3A00%3A00

Opened orders

To get list of active market and pending orders use OpenedOrders endpoint.

https://mt4.mtapi.io/OpenedOrders?id=demo-token-mt4

It returns array of orders.

Quote history

To get OHLC price history please use QuoteHistory endpoint.

https://mt4.mtapi.io/QuoteHistory?id=demo-token-mt4&symbol=EURUSD&timeframe=D1&from=2022-06-01T00%3A00%3A00&count=30

It returns array of bars.

Trading instruments

To get list of trading instruments please use Symbols endpoint.

https://mt4.mtapi.io/Symbols?id=demo-token-mt4

To get details of certain symbol use SymbolParams endpoint.

https://mt4.mtapi.io/SymbolParams?id=demo-token-mt4&symbol=EURUSD

Realtime quotes with websockets

Simple Web Socket test client for chrome browser: https://chrome.google.com/webstore/detail/websocket-test-client/fgponpodhbmadfljofbimhhlengambbn

  • Call /Subscribe method
  • Quotes should start to flow in websocket test client

Installation with Docker

  1. Install docker. https://docs.docker.com/engine/install/debian/

  2. Pull and run

docker pull mtapiio/mt4rest
docker run --rm -p 5000:80 mtapiio/mt4rest

After that you can access API at http://localhost:5000.

Installation with microk8s

We recommend to install on Debian 11 as below:

sudo apt-get update
sudo apt install snapd
sudo snap install microk8s –classic
cd /snap/bin
./microk8s enable dns dashboard storage
nano ~/mt4dep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mt4rest
labels:
   app: mt4rest
spec:
replicas: 1
selector:
   matchLabels:
     app: mt4rest
template:
   metadata:
     labels:
       app: mt4rest
   spec:
     containers:
     - name: mt4rest
       image: mtapiio/mt4rest
       ports:
        containerPort: 80
       livenessProbe:
         httpGet:
           path: /healthz
           port: 80
         initialDelaySeconds: 10
         timeoutSeconds: 2
         periodSeconds: 3
./microk8s.kubectl apply -f ~/mt4dep.yaml
nano ~/mt4srv.yaml
kind: Service
apiVersion: v1
metadata:
name: mt4rest
spec:
selector:
   app: mt4rest
type: NodePort
ports:
 name: http
   protocol: TCP
   port: 80
   nodePort: 30400
sessionAffinity: ClientIP
./microk8s.kubectl apply -f ~/mt4srv.yaml
./microk8s.kubectl –namespace kube-system patch svc kubernetes-dashboard -p ‘{“spec”: {“type”: “NodePort”}}’
./microk8s.kubectl –namespace kube-system patch svc kubernetes-dashboard -p ‘{“spec”: {“ports”:[{“nodePort”: 30100,”port”: 443,”protocol”: “TCP”,”targetPort”: 8443}]}}’

Check how is whole system running:

./microk8s kubectl get all –all-namespaces

Get control panel token:

token=$(./microk8s kubectl -n kube-system get secret | grep default-token | cut -d ” ” -f1)
./microk8s kubectl -n kube-system describe secret $token

After that control panel should be available at https://localhost:30100 and restful service at http://localhost:30400.

k8s: how to get latest version from our container registry

  1. Install docker. https://docs.docker.com/engine/install/debian/

  2. Login to our container registry with your username and password.

docker login reg.mtapi.io:5050
  1. Create k8s credentials.
microk8s kubectl create secret generic regcred2 --from-file=.dockerconfigjson=/root/.docker/config.json --type=kubernetes.io/dockerconfigjson
  1. Use created credentails in deployment like below.
apiVersion: apps/v1
kind: Deployment
metadata:
name: mt4rest
labels:
   app: mt4rest
spec:
replicas: 1
selector:
   matchLabels:
     app: mt4rest
template:
   metadata:
     labels:
       app: mt4rest
   spec:
     containers:
     - name: mt4rest
       image: reg.mtapi.io:5050/root/mt4rest-full/mt4rest
       ports:
        containerPort: 80
       livenessProbe:
         httpGet:
           path: /healthz
           port: 80
         initialDelaySeconds: 10
         timeoutSeconds: 2
         periodSeconds: 3
     imagePullSecrets:
     - name: regcred2

How to add node to Microk8s cluster

On existing node run:

microk8s add-node

It will return the command that need to run on new node.

Setup ingress

Make yaml file like this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mt4
  annotations:
    nginx.ingress.kubernetes.io/upstream-hash-by: $remote_addr
spec:
  ingressClassName: "nginx"
  rules:
  - host: "mt4.mtapi.io"
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service: 
            name: mt4
            port: 
              number: 80

If you going to work with restful service from lot of client applications you can set annoations like this:

  annotations:
    nginx.ingress.kubernetes.io/upstream-hash-by: $remote_addr

In this case k8s will route request to required pod by client IP address.

But if you going to work with with resful service from one or several server applications need to make sticky sessions like this:

  annotations:
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "StickySession"
    nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"

In this case /Connect reply returning StickySession coockie that need to copy in further requests, this helps k8s route your requests correctly. More details with some java examples availiable here:

https://git.mtapi.io/root/sticky-sessions-java

Docker Pull Command

docker pull timurila/mt4rest