HomeHome  Updated: November 15, 2020  [日本語 / English]

Linking MT4(MQL4) and Python - Passing Data through Named Pipe, Bidirectional

Introduction

Sometimes you may want to pass data from MT4 (MQL4) to an external program for processing. This is relatively easy to achieve with the Windows Named Pipes.

In "Linking MT4(MQL4) and Python - Passing Data Through Named Pipe", we passed data one-way from MT4(MQL4) to an external program written in Python, but this time we will try to do a bidirectional communication.

Named Pipe

Tested Environment

Program Flow

The program works as follows.

  1. Create a named pipe in the external program and wait for it to be connected.
  2. Connect to the named pipe from MT4 (MQL4).
  3. Write data from MT4 (MQL4) to the named pipe.
  4. The external program reads data from the named pipe.
  5. The external program processes the readings.
  6. Writes the results of the external program to the named pipe.
  7. Read data from the named pipe in MT4 (MQL4).

External Program

In this article, we'll use Python to write an external program. The program converts the string sent from the pipe to a number and doubles it to a string and writes it to the named pipe.

To work with Windows named pipes in Python, we can use the win32pipe module.

import win32pipe
import win32file

# Create a named pipe
    pipe = win32pipe.CreateNamedPipe(
    r'\\.\pipe\TestPipe', 
    win32pipe.PIPE_ACCESS_DUPLEX,
    win32pipe. PIPE_TYPE_BYTE | win32pipe.PIPE_READMODE_BYTE | win32pipe.PIPE_WAIT,
    1, 256, 256, 0, None)

# Waiting for the client to connect
win32pipe.ConnectNamedPipe(pipe, None)

# Prepare the buffer
s = b''

# Infinite Loop
while True:
    # Read one byte from the pipe.
    hr, c = win32file.ReadFile(pipe, 1)

    # Add to buffer
    s += c
    
    # When the line break is read
    if c == b'\n':
        # Convert the readings to a number and double it
        x = float(s) * 2.0
        
        # Convert the result to a string
        res = '{0:.3f}\r\n'.format(x)
        
        # Display on console
        print(res, end='')
        
        # Write to Pipe
        win32file.WriteFile(pipe, res.encode())
        
        # Prepare for the next one
        s = b''

MT4(MQL4) program

An MT4 (MQL4) program is written as an EA (Expert Advisor). This program converts the Bid price into a string for each tick, writes it to the pipe, reads the results of the external program from the pipe, and displays it on the terminal.

To connect to an existing pipe from MT4 (MQL4), we can use the FileOpen function.

#property strict

const string pipeName = "TestPipe";     // Pipe name. Matches the name created by an external program.
int pipe = INVALID_HANDLE;

int OnInit()
{
   // Open the named pipe (connect it).
   pipe = FileOpen("\\\\.\\pipe\\" + pipeName, FILE_READ | FILE_WRITE | FILE_BIN | FILE_ANSI);

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   // Close the pipe.
   FileClose(pipe);
}

// Read one line from the file.
inline string ReadLine(int file)
{
   string s;
   while (1)
   {
      string c = FileReadString(file, 1);
      s += c;
      if (c == "\n") break;
   } 
   
   return s;
}

void OnTick()
{
   // Convert the Bid price to a string
   string s = DoubleToString(Bid);

   // Write the string into the pipe.
   FileWriteString(pipe, s + "\r\n");
   
   // Read a line from the pipe and display it in the terminal
   Print(ReadLine(pipe));
}

Display Example

When you run the above program, start the external program and put it in the waiting state, and then apply the EA to the chart on the MT4 side. If it works, an external program will display the result of the process as shown below. Also, the same result will be displayed in the terminal of MT4.

209.386
209.388
209.388
209.392
209.388