rezakarbasi / RL-agent-trader

In this repository I'm going to train an RL agent using metatrader in python!
https://medium.com/@a.rz.karbasi/enhancing-trading-performance-through-deep-q-network-hyper-parameter-tuning-7475e2f11a06
6 stars 3 forks source link

MQL - Python Connection Test issue #1

Open jim-rosenvelt opened 1 week ago

jim-rosenvelt commented 1 week ago

Hello,

Running Metatrader 5 in a Wine/Linux Debian environment, I encountered issues during the MQL - Python Connection Test.

Here are the steps and results:

Step #1, Starting server.py ("python3 server.py") -> OK.

Step #2, Starting client.ex5 -> 2024.09.13 17:18:43.621 client (EURUSD,H1) what's up in mql ? 2024.09.13 17:18:43.751 client (EURUSD,H1)

Comment: So, only during the first tick a connection was established.

Step #3, removing the client EA. Metatrader -> 2024.09.13 17:18:59.338 client (EURUSD,H1) 28 undeleted objects left 2024.09.13 17:18:59.338 client (EURUSD,H1) 28 objects of type ClientSocket left 2024.09.13 17:18:59.338 client (EURUSD,H1) 3584 bytes of leaked memory

Python -> Connected by ('127.0.0.1', 35368) b'1' Traceback (most recent call last): File "/home/administrator/Downloads/RL-agent-trader/connection-test/python-files/server.py", line 8, in s.bind((HOST, PORT)) OSError: [Errno 98] Address already in use

Comment: So it seems that the python server.py switches to another port, because client.ex5 keeps the default port (23456) occupied. I've tried other port settings in both scripts, but that won't solve this issue)

If you need any more information, please let me know.

rezakarbasi commented 5 days ago

Hi,

Thank you for bringing this issue to my attention.

I've experienced similar challenges when attempting to run the connection using Wine on macOS—it can indeed be quite buggy. To ensure the process works smoothly, I recommend first testing it on a native Windows environment before attempting to establish the connection with Wine.

Aside from that, the specific problem you're encountering, it seems the Python server isn't properly handling socket reuse, which leads to the OSError: [Errno 98] Address already in use error. This happens because the socket remains in a TIME_WAIT state after the connection is closed.

Here's a modified version of server.py that should resolve the issue:

import socket

HOST = '127.0.0.1'
PORT = 23456

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # Allow immediate reuse of the socket
s.bind((HOST, PORT))
s.listen()

print(f"Server listening on {HOST}:{PORT}")

while True:
    conn, addr = s.accept()
    print('Connected by', addr)
    data = conn.recv(1024)
    print(f"Received data: {data}")
    conn.close()

Explanation:

Please try this updated script and see if it resolves the issue. Let me know if you have any further questions or run into any other problems.