-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add logging and READMe docs to mt5any package
- Loading branch information
1 parent
4e3f9f7
commit 91d8c54
Showing
3 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters