212 lines
5.9 KiB
Python
212 lines
5.9 KiB
Python
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "53c28be9",
|
|
"metadata": {},
|
|
"source": [
|
|
"# 1) Data — Collect & Save OHLC (MT5)\n",
|
|
"\n",
|
|
"This notebook fetches OHLCV data from MT5 via a tiny adapter and saves it to CSV.\n",
|
|
"- Keep your credentials in `.env` and **do not commit** `.env`.\n",
|
|
"- If MT5 is not available, you can skip this and drop in a CSV manually."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "dcb5ac32",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Connecting to MT5...\n",
|
|
"MT5 connected: True\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import sys, os; sys.path.append(os.path.abspath('..')) # Path fix\n",
|
|
"\n",
|
|
"# Prereqs: `pip install -r requirements.txt` (locally)\n",
|
|
"import os\n",
|
|
"from pathlib import Path\n",
|
|
"import pandas as pd\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"\n",
|
|
"from adapters import broker\n",
|
|
"\n",
|
|
"# Load settings from env\n",
|
|
"load_dotenv()\n",
|
|
"SYMBOL = os.getenv(\"TRAINING_SYMBOL\", \"EURUSD\")\n",
|
|
"TIMEFRAME = os.getenv(\"TIMEFRAME\", \"M15\")\n",
|
|
"START = os.getenv(\"DATA_START\", \"2023-01-01\")\n",
|
|
"END = os.getenv(\"DATA_END\", \"2025-01-01\")\n",
|
|
"\n",
|
|
"print(\"Connecting to MT5...\")\n",
|
|
"ok = broker.open_session(\n",
|
|
" login=int(os.getenv(\"MT5_LOGIN\", \"0\")) or None,\n",
|
|
" password=os.getenv(\"MT5_PASSWORD\"),\n",
|
|
" server=os.getenv(\"MT5_SERVER\")\n",
|
|
")\n",
|
|
"print(\"MT5 connected:\", ok)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "3b690e97",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Saved: G:\\My Drive\\Bots DRL\\DRL\\DRL-MT5-Lab\\notebooks\\data\\ohlc_EURUSD_M15.csv\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>open</th>\n",
|
|
" <th>high</th>\n",
|
|
" <th>low</th>\n",
|
|
" <th>close</th>\n",
|
|
" <th>volume</th>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>time</th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>2024-12-31 20:45:00+00:00</th>\n",
|
|
" <td>1.03568</td>\n",
|
|
" <td>1.03568</td>\n",
|
|
" <td>1.03528</td>\n",
|
|
" <td>1.03531</td>\n",
|
|
" <td>529</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2024-12-31 21:00:00+00:00</th>\n",
|
|
" <td>1.03528</td>\n",
|
|
" <td>1.03625</td>\n",
|
|
" <td>1.03518</td>\n",
|
|
" <td>1.03619</td>\n",
|
|
" <td>425</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2024-12-31 21:15:00+00:00</th>\n",
|
|
" <td>1.03618</td>\n",
|
|
" <td>1.03647</td>\n",
|
|
" <td>1.03575</td>\n",
|
|
" <td>1.03590</td>\n",
|
|
" <td>465</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2024-12-31 21:30:00+00:00</th>\n",
|
|
" <td>1.03590</td>\n",
|
|
" <td>1.03590</td>\n",
|
|
" <td>1.03543</td>\n",
|
|
" <td>1.03560</td>\n",
|
|
" <td>545</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2024-12-31 21:45:00+00:00</th>\n",
|
|
" <td>1.03560</td>\n",
|
|
" <td>1.03571</td>\n",
|
|
" <td>1.03539</td>\n",
|
|
" <td>1.03542</td>\n",
|
|
" <td>306</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" open high low close volume\n",
|
|
"time \n",
|
|
"2024-12-31 20:45:00+00:00 1.03568 1.03568 1.03528 1.03531 529\n",
|
|
"2024-12-31 21:00:00+00:00 1.03528 1.03625 1.03518 1.03619 425\n",
|
|
"2024-12-31 21:15:00+00:00 1.03618 1.03647 1.03575 1.03590 465\n",
|
|
"2024-12-31 21:30:00+00:00 1.03590 1.03590 1.03543 1.03560 545\n",
|
|
"2024-12-31 21:45:00+00:00 1.03560 1.03571 1.03539 1.03542 306"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"\n",
|
|
"# Fetch OHLC and save\n",
|
|
"DATA_DIR = Path(\"data\"); DATA_DIR.mkdir(exist_ok=True)\n",
|
|
"df = broker.fetch_ohlc(SYMBOL, TIMEFRAME, START, END)\n",
|
|
"out_csv = DATA_DIR / f\"ohlc_{SYMBOL}_{TIMEFRAME}.csv\"\n",
|
|
"df.to_csv(out_csv)\n",
|
|
"print(\"Saved:\", out_csv.resolve())\n",
|
|
"df.tail()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5e151d9a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\n",
|
|
"# Clean shutdown\n",
|
|
"broker.close_session()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "drl",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.18"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|