diff --git a/README.ipynb b/README.ipynb index 5f592a1..15970c8 100644 --- a/README.ipynb +++ b/README.ipynb @@ -100,6 +100,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -139,7 +140,7 @@ "\n", "* Methods:\n", "\n", - " > `download_data`: Downloads required data from MetaTrader for a list of symbols in a time range. This method can be overridden in order to download data from servers other than MetaTrader.\n", + " > `download_data`: Downloads required data from MetaTrader for a list of symbols in a time range. This method can be overridden in order to download data from servers other than MetaTrader. *Note that this method only works on Windows, as the MetaTrader5 Python package is not available on other platforms.*\n", " >\n", " > `save_symbols`: Saves the downloaded symbols' data to a file.\n", " >\n", @@ -6918,12 +6919,13 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## References\n", "\n", - "* [https://www.mql5.com/en/docs/integration/python_metatrader5](https://www.mql5.com/en/docs/integration/python_metatrader5)\n", + "* [https://www.mql5.com/en/docs/python_metatrader5](https://www.mql5.com/en/docs/python_metatrader5)\n", "* [https://www.metatrader5.com/en/terminal/help/trading_advanced/margin_forex](https://www.metatrader5.com/en/terminal/help/trading_advanced/margin_forex)\n", "* [https://admiralmarkets.com/education/articles/forex-basics/margin-in-forex-trading-margin-level-vs-margin-call](https://admiralmarkets.com/education/articles/forex-basics/margin-in-forex-trading-margin-level-vs-margin-call)\n", "* [https://www.investopedia.com/articles/forex/12/calculating-profits-and-losses-of-forex-trades.asp](https://www.investopedia.com/articles/forex/12/calculating-profits-and-losses-of-forex-trades.asp)\n" diff --git a/README.md b/README.md index 5229647..78970fd 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ This is the core class that simulates the main parts of MetaTrader. Most of its * Methods: - > `download_data`: Downloads required data from MetaTrader for a list of symbols in a time range. This method can be overridden in order to download data from servers other than MetaTrader. + > `download_data`: Downloads required data from MetaTrader for a list of symbols in a time range. This method can be overridden in order to download data from servers other than MetaTrader. *Note that this method only works on Windows, as the MetaTrader5 Python package is not available on other platforms.* > > `save_symbols`: Saves the downloaded symbols' data to a file. > @@ -783,7 +783,7 @@ env.render('advanced_figure', time_format="%Y-%m-%d") ## References -* [https://www.mql5.com/en/docs/integration/python_metatrader5](https://www.mql5.com/en/docs/integration/python_metatrader5) +* [https://www.mql5.com/en/docs/python_metatrader5](https://www.mql5.com/en/docs/python_metatrader5) * [https://www.metatrader5.com/en/terminal/help/trading_advanced/margin_forex](https://www.metatrader5.com/en/terminal/help/trading_advanced/margin_forex) * [https://admiralmarkets.com/education/articles/forex-basics/margin-in-forex-trading-margin-level-vs-margin-call](https://admiralmarkets.com/education/articles/forex-basics/margin-in-forex-trading-margin-level-vs-margin-call) * [https://www.investopedia.com/articles/forex/12/calculating-profits-and-losses-of-forex-trades.asp](https://www.investopedia.com/articles/forex/12/calculating-profits-and-losses-of-forex-trades.asp) diff --git a/gym_mtsim/metatrader/api.py b/gym_mtsim/metatrader/api.py index a10cb0e..013fe7d 100644 --- a/gym_mtsim/metatrader/api.py +++ b/gym_mtsim/metatrader/api.py @@ -2,7 +2,7 @@ import pytz import calendar -from datetime import datetime, timedelta +from datetime import datetime import pandas as pd diff --git a/gym_mtsim/metatrader/interface.py b/gym_mtsim/metatrader/interface.py index 4ed1cb4..058e8c2 100644 --- a/gym_mtsim/metatrader/interface.py +++ b/gym_mtsim/metatrader/interface.py @@ -3,45 +3,59 @@ import numpy as np -import MetaTrader5 as mt5 -from MetaTrader5 import SymbolInfo as MtSymbolInfo +try: + import MetaTrader5 as mt5 + from MetaTrader5 import SymbolInfo as MtSymbolInfo + MT5_AVAILABLE = True +except ImportError: + MtSymbolInfo = object + MT5_AVAILABLE = False class Timeframe(Enum): - M1 = mt5.TIMEFRAME_M1 - M2 = mt5.TIMEFRAME_M2 - M3 = mt5.TIMEFRAME_M3 - M4 = mt5.TIMEFRAME_M4 - M5 = mt5.TIMEFRAME_M5 - M6 = mt5.TIMEFRAME_M6 - M10 = mt5.TIMEFRAME_M10 - M12 = mt5.TIMEFRAME_M12 - M15 = mt5.TIMEFRAME_M15 - M20 = mt5.TIMEFRAME_M20 - M30 = mt5.TIMEFRAME_M30 - H1 = mt5.TIMEFRAME_H1 - H2 = mt5.TIMEFRAME_H2 - H4 = mt5.TIMEFRAME_H4 - H3 = mt5.TIMEFRAME_H3 - H6 = mt5.TIMEFRAME_H6 - H8 = mt5.TIMEFRAME_H8 - H12 = mt5.TIMEFRAME_H12 - D1 = mt5.TIMEFRAME_D1 - W1 = mt5.TIMEFRAME_W1 - MN1 = mt5.TIMEFRAME_MN1 + M1 = 1 # mt5.TIMEFRAME_M1 + M2 = 2 # mt5.TIMEFRAME_M2 + M3 = 3 # mt5.TIMEFRAME_M3 + M4 = 4 # mt5.TIMEFRAME_M4 + M5 = 5 # mt5.TIMEFRAME_M5 + M6 = 6 # mt5.TIMEFRAME_M6 + M10 = 10 # mt5.TIMEFRAME_M10 + M12 = 12 # mt5.TIMEFRAME_M12 + M15 = 15 # mt5.TIMEFRAME_M15 + M20 = 20 # mt5.TIMEFRAME_M20 + M30 = 30 # mt5.TIMEFRAME_M30 + H1 = 1 | 0x4000 # mt5.TIMEFRAME_H1 + H2 = 2 | 0x4000 # mt5.TIMEFRAME_H2 + H4 = 4 | 0x4000 # mt5.TIMEFRAME_H4 + H3 = 3 | 0x4000 # mt5.TIMEFRAME_H3 + H6 = 6 | 0x4000 # mt5.TIMEFRAME_H6 + H8 = 8 | 0x4000 # mt5.TIMEFRAME_H8 + H12 = 12 | 0x4000 # mt5.TIMEFRAME_H12 + D1 = 24 | 0x4000 # mt5.TIMEFRAME_D1 + W1 = 1 | 0x8000 # mt5.TIMEFRAME_W1 + MN1 = 1 | 0xC000 # mt5.TIMEFRAME_MN1 def initialize() -> bool: + _check_mt5_available() return mt5.initialize() def shutdown() -> None: + _check_mt5_available() mt5.shutdown() def copy_rates_range(symbol: str, timeframe: Timeframe, date_from: datetime, date_to: datetime) -> np.ndarray: + _check_mt5_available() return mt5.copy_rates_range(symbol, timeframe.value, date_from, date_to) def symbol_info(symbol: str) -> MtSymbolInfo: + _check_mt5_available() return mt5.symbol_info(symbol) + + +def _check_mt5_available() -> None: + if not MT5_AVAILABLE: + raise OSError("MetaTrader5 is not available on your platform.") diff --git a/setup.py b/setup.py index 244ec96..2ae4f4a 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ 'plotly>=5.3.1', 'nbformat>=5.1.3', 'pathos>=0.2.8', - 'MetaTrader5>=5.0.35', + 'MetaTrader5>=5.0.35; platform_system == "Windows"', ], package_data={