Skip to content

Commit

Permalink
add logging and READMe docs to mt5any package
Browse files Browse the repository at this point in the history
  • Loading branch information
seekersoftec committed Nov 28, 2024
1 parent 4e3f9f7 commit 91d8c54
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
66 changes: 66 additions & 0 deletions packages/mt5any/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# MetaTrader 5 for linux system

A simple package that uses [wine](https://www.winehq.org), [rpyc](https://github.com/tomerfiliba-org/rpyc) and a Python Windows version to allow using [MetaTrader5](https://pypi.org/project/MetaTrader5) on Linux.

## Install

1. Install [Wine](https://wiki.winehq.org/Download).

2. Install [Python for Windows](https://www.python.org/downloads/windows/) on Linux with the help of Wine.

3. Find the path to `python.exe`.

- Mine is installed on `/home/user/.wine/drive_c/users/user/Local Settings/Application Data/Programs/Python/Python39`.

4. Install [mt5](https://www.mql5.com/en/docs/integration/python_metatrader5) library on your **Windows** Python version.

```
pip install MetaTrader5
pip install --upgrade MetaTrader5
```

5. Install this package on both **Windows** and **Linux** Python versions:

```
pip install mt5linux
```

## How To Use

Follow the steps:

1. Open MetaTrader5.

2. On **Windows** side, start the server on a terminal:

```
python -m mt5linux <path/to/python.exe>
```

3. On **Linux** side, make your scripts/notebooks as you did with MetaTrader5:

```python
# import the package
from mt5linux import MetaTrader5
# connecto to the server
mt5 = MetaTrader5(
# host = 'localhost' (default)
# port = 18812 (default)
)
# use as you learned from: https://www.mql5.com/en/docs/integration/python_metatrader5/
mt5.initialize()
mt5.terminal_info()
mt5.copy_rates_from_pos('GOOG',mt5.TIMEFRAME_M1,0,1000)
# ...
# don't forget to shutdown
mt5.shutdown()
```

4. Be happy!

On step 2 you can provide the port, host, executable, etc... just type `python -m mt5linux --help`.

This core of this project is from:

- Lucas Prett Campagna https://github.com/lucas-campagna/mt5linux

82 changes: 82 additions & 0 deletions packages/mt5any/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import sys
import logging
from colorlog import ColoredFormatter


class Logger(logging.Logger):
"""
Custom logger for application logging with colored console output and optional file logging.
Args:
name (str, optional): Logger name. Defaults to __name__.
log_level (int, optional): Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL). Defaults to logging.DEBUG.
filename (str, optional): File to log messages to. Defaults to "" (no file logging).
Attributes:
stream_handler (logging.StreamHandler): Handler for console output with colored formatting.
file_handler (logging.FileHandler, optional): Handler for file output if filename is provided.
log_formatter (ColoredFormatter): Formatter for log messages with colored output.
Usage:
logger = AppLogger(name='my_logger', log_level=logging.INFO, filename='app.log')
logger.info('This is an info message.')
logger.error('This is an error message.')
"""

def __init__(
self,
name=__name__,
log_level=logging.DEBUG,
filename: str = "",
):
super().__init__(name, level=log_level)

# Colored formatter for console logging
self.log_formatter = ColoredFormatter(
"[TradeFlow] %(process)d - %(asctime)s %(log_color)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d, %I:%M:%S %p",
reset=True,
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red,bg_white",
},
secondary_log_colors={},
style="%",
)

# Stream handler for console logging with colored output
self.stream_handler = logging.StreamHandler(sys.stdout)
self.stream_handler.setFormatter(self.log_formatter)
self.addHandler(self.stream_handler)

# File handler for file logging if filename is supplied
if filename:
self.file_handler = logging.FileHandler(filename)
self.file_handler.setFormatter(
logging.Formatter("%(asctime)s [%(levelname)s] %(name)s: %(message)s")
)
self.addHandler(self.file_handler)

self.propagate = False # Prevent propagation to parent logger


# # Example usage:
# if __name__ == "__main__":
# # Example usage
# logger = Logger(name="my_app", log_level=logging.DEBUG, filename="app.log")
# logger.debug("Debug message")
# logger.info("Info message")
# logger.warning("Warning message")
# logger.error("Error message")
# logger.critical("Critical message")


# format = "%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s",
# format="%(asctime)s %(levelname)s %(message)s",
# datefmt = '%Y-%m-%d %H:%M:%S',
4 changes: 2 additions & 2 deletions packages/mt5any/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import ClassVar, Dict

from rpyc.utils.classic import DEFAULT_SERVER_PORT
from trade_flow.common.logging import Logger
from .logging import Logger


class ContainerStatus(IntEnum):
Expand Down Expand Up @@ -120,7 +120,7 @@ class DockerizedMT5Terminal:
"""

IMAGE: ClassVar[str] = "metatrader5-terminal:latest"
FALLBACK_IMAGE: ClassVar[str] = "ghcr.io/fortesenselabs/metatrader5-terminal:latest"
FALLBACK_IMAGE: ClassVar[str] = "ghcr.io/ramin951/metatrader5-terminal:latest"
CONTAINER_NAME: ClassVar[str] = "itbot-mt5"
PORTS: ClassVar[dict[str, int]] = {"web": 8000, "vnc": 5900, "rpyc": DEFAULT_SERVER_PORT}

Expand Down

0 comments on commit 91d8c54

Please sign in to comment.