mirror of
https://github.com/Qoyyuum/Forex-MT5-Bot.git
synced 2026-02-22 23:53:15 +00:00
871 lines
34 KiB
Text
871 lines
34 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Find out what model works best with the dataset from XM"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[(1622160000, 1.21932, 1.2205 , 1.21327, 1.21896, 60110, 0, 0)\n",
|
|
" (1622419200, 1.21993, 1.22314, 1.21833, 1.2225 , 36694, 0, 0)\n",
|
|
" (1622505600, 1.22254, 1.22543, 1.22118, 1.22126, 52448, 0, 0)\n",
|
|
" (1622592000, 1.22125, 1.22265, 1.21643, 1.221 , 53793, 0, 0)\n",
|
|
" (1622678400, 1.22097, 1.22143, 1.21183, 1.21265, 59299, 0, 0)\n",
|
|
" (1622764800, 1.21261, 1.21856, 1.21041, 1.21662, 56073, 0, 0)\n",
|
|
" (1623024000, 1.21595, 1.22018, 1.21448, 1.21893, 40542, 0, 0)\n",
|
|
" (1623110400, 1.21891, 1.21942, 1.21643, 1.21726, 48682, 0, 0)\n",
|
|
" (1623196800, 1.21722, 1.22179, 1.21709, 1.21787, 42992, 0, 0)\n",
|
|
" (1623283200, 1.21787, 1.2195 , 1.21432, 1.21751, 66547, 0, 0)\n",
|
|
" (1623369600, 1.21751, 1.21931, 1.20927, 1.21063, 54807, 0, 0)\n",
|
|
" (1623628800, 1.21025, 1.21303, 1.20942, 1.212 , 46944, 0, 0)\n",
|
|
" (1623715200, 1.2119 , 1.21473, 1.21014, 1.21264, 50679, 0, 0)\n",
|
|
" (1623801600, 1.21256, 1.21348, 1.1994 , 1.19943, 66416, 0, 0)\n",
|
|
" (1623888000, 1.19939, 1.20064, 1.18918, 1.19069, 90570, 0, 0)\n",
|
|
" (1623974400, 1.19056, 1.19251, 1.18537, 1.18597, 81407, 0, 0)\n",
|
|
" (1624233600, 1.1855 , 1.19212, 1.18477, 1.19185, 87967, 0, 0)\n",
|
|
" (1624320000, 1.19166, 1.19525, 1.18809, 1.19413, 68415, 0, 0)\n",
|
|
" (1624406400, 1.19376, 1.19703, 1.19116, 1.19258, 66823, 0, 0)\n",
|
|
" (1624492800, 1.19254, 1.19563, 1.19175, 1.19315, 65027, 0, 0)\n",
|
|
" (1624579200, 1.19315, 1.19752, 1.19265, 1.1937 , 59055, 0, 0)\n",
|
|
" (1624838400, 1.19316, 1.19445, 1.19025, 1.19242, 60674, 0, 0)\n",
|
|
" (1624924800, 1.1923 , 1.19298, 1.18777, 1.1896 , 61593, 0, 0)\n",
|
|
" (1625011200, 1.18954, 1.1909 , 1.18452, 1.1856 , 70561, 0, 0)\n",
|
|
" (1625097600, 1.18559, 1.18842, 1.18374, 1.18497, 70316, 0, 0)\n",
|
|
" (1625184000, 1.18491, 1.18743, 1.18071, 1.18646, 71827, 0, 0)\n",
|
|
" (1625443200, 1.18576, 1.18805, 1.18515, 1.18634, 47260, 0, 0)\n",
|
|
" (1625529600, 1.18619, 1.1895 , 1.18067, 1.18231, 79642, 0, 0)\n",
|
|
" (1625616000, 1.18235, 1.18362, 1.17817, 1.17903, 75932, 0, 0)\n",
|
|
" (1625702400, 1.17899, 1.18679, 1.17836, 1.18449, 90999, 0, 0)\n",
|
|
" (1625788800, 1.1844 , 1.18811, 1.18251, 1.18774, 73949, 0, 0)\n",
|
|
" (1626048000, 1.18715, 1.18802, 1.18361, 1.18606, 56598, 0, 0)\n",
|
|
" (1626134400, 1.18593, 1.18752, 1.17724, 1.17765, 69168, 0, 0)\n",
|
|
" (1626220800, 1.17761, 1.18388, 1.17719, 1.18358, 70801, 0, 0)\n",
|
|
" (1626307200, 1.18349, 1.18506, 1.17962, 1.18118, 74081, 0, 0)\n",
|
|
" (1626393600, 1.18118, 1.18224, 1.17921, 1.18055, 70772, 0, 0)\n",
|
|
" (1626652800, 1.18054, 1.18243, 1.1764 , 1.17987, 80957, 0, 0)\n",
|
|
" (1626739200, 1.1798 , 1.18025, 1.17558, 1.17805, 70566, 0, 0)\n",
|
|
" (1626825600, 1.17796, 1.18047, 1.17519, 1.17946, 56519, 0, 0)\n",
|
|
" (1626912000, 1.17907, 1.18303, 1.17575, 1.17707, 64812, 0, 0)\n",
|
|
" (1626998400, 1.17705, 1.17865, 1.17544, 1.17689, 49090, 0, 0)\n",
|
|
" (1627257600, 1.17677, 1.18173, 1.17635, 1.18029, 53860, 0, 0)\n",
|
|
" (1627344000, 1.1803 , 1.18411, 1.17701, 1.18165, 64421, 0, 0)\n",
|
|
" (1627430400, 1.18159, 1.18494, 1.17721, 1.18441, 64207, 0, 0)\n",
|
|
" (1627516800, 1.18431, 1.18927, 1.18402, 1.18865, 49044, 0, 0)\n",
|
|
" (1627603200, 1.18858, 1.19087, 1.18517, 1.1873 , 54242, 0, 0)\n",
|
|
" (1627862400, 1.18641, 1.18969, 1.1859 , 1.18688, 47011, 0, 0)\n",
|
|
" (1627948800, 1.1867 , 1.18933, 1.18537, 1.18637, 49688, 0, 0)\n",
|
|
" (1628035200, 1.18631, 1.18997, 1.18328, 1.18367, 48391, 0, 0)\n",
|
|
" (1628121600, 1.1836 , 1.18572, 1.18282, 1.18327, 47001, 0, 0)\n",
|
|
" (1628208000, 1.18324, 1.18357, 1.17544, 1.17611, 47467, 0, 0)\n",
|
|
" (1628467200, 1.17568, 1.1769 , 1.17349, 1.17362, 47081, 0, 0)\n",
|
|
" (1628553600, 1.17358, 1.17429, 1.17098, 1.17203, 41851, 0, 0)\n",
|
|
" (1628640000, 1.17184, 1.1754 , 1.17061, 1.17388, 45441, 0, 0)\n",
|
|
" (1628726400, 1.1738 , 1.17482, 1.17237, 1.17296, 36888, 0, 0)\n",
|
|
" (1628812800, 1.1729 , 1.18048, 1.17281, 1.17962, 35375, 0, 0)\n",
|
|
" (1629072000, 1.17941, 1.18007, 1.17675, 1.17768, 37488, 0, 0)\n",
|
|
" (1629158400, 1.17762, 1.17845, 1.17081, 1.17081, 50070, 0, 0)\n",
|
|
" (1629244800, 1.17088, 1.17425, 1.16937, 1.17108, 48811, 0, 0)\n",
|
|
" (1629331200, 1.17087, 1.17176, 1.16657, 1.16756, 59625, 0, 0)\n",
|
|
" (1629417600, 1.16752, 1.17045, 1.1664 , 1.16996, 42854, 0, 0)\n",
|
|
" (1629676800, 1.16897, 1.17503, 1.16897, 1.17457, 42138, 0, 0)\n",
|
|
" (1629763200, 1.17449, 1.1765 , 1.17273, 1.17555, 40186, 0, 0)\n",
|
|
" (1629849600, 1.17537, 1.17743, 1.1726 , 1.17716, 45250, 0, 0)\n",
|
|
" (1629936000, 1.17706, 1.1779 , 1.17459, 1.17515, 49613, 0, 0)\n",
|
|
" (1630022400, 1.17514, 1.18026, 1.17349, 1.17955, 54550, 0, 0)\n",
|
|
" (1630281600, 1.17926, 1.18099, 1.17829, 1.17962, 36224, 0, 0)\n",
|
|
" (1630368000, 1.17961, 1.1845 , 1.17939, 1.18083, 56492, 0, 0)\n",
|
|
" (1630454400, 1.18078, 1.18572, 1.17936, 1.18384, 50142, 0, 0)\n",
|
|
" (1630540800, 1.18374, 1.18759, 1.18344, 1.18744, 42004, 0, 0)\n",
|
|
" (1630627200, 1.18739, 1.19091, 1.18657, 1.18772, 53226, 0, 0)\n",
|
|
" (1630886400, 1.18768, 1.18862, 1.18557, 1.18701, 29883, 0, 0)\n",
|
|
" (1630972800, 1.18675, 1.18852, 1.18373, 1.18402, 54549, 0, 0)\n",
|
|
" (1631059200, 1.18356, 1.18512, 1.18022, 1.1815 , 64756, 0, 0)\n",
|
|
" (1631145600, 1.18125, 1.18413, 1.18053, 1.18244, 68564, 0, 0)\n",
|
|
" (1631232000, 1.18229, 1.1851 , 1.18091, 1.18107, 52650, 0, 0)\n",
|
|
" (1631491200, 1.18143, 1.18171, 1.17702, 1.18103, 51815, 0, 0)\n",
|
|
" (1631577600, 1.18092, 1.1846 , 1.18001, 1.18025, 58490, 0, 0)\n",
|
|
" (1631664000, 1.18013, 1.18319, 1.17991, 1.18165, 58073, 0, 0)\n",
|
|
" (1631750400, 1.18153, 1.18206, 1.17506, 1.17652, 58283, 0, 0)\n",
|
|
" (1631836800, 1.17675, 1.17888, 1.17242, 1.1725 , 56213, 0, 0)\n",
|
|
" (1632096000, 1.17272, 1.17363, 1.17004, 1.17257, 58444, 0, 0)\n",
|
|
" (1632182400, 1.17248, 1.17486, 1.17149, 1.17252, 58514, 0, 0)\n",
|
|
" (1632268800, 1.1724 , 1.17555, 1.16843, 1.16866, 72571, 0, 0)\n",
|
|
" (1632355200, 1.1686 , 1.17501, 1.16835, 1.17381, 68536, 0, 0)\n",
|
|
" (1632441600, 1.17379, 1.17476, 1.17007, 1.17206, 51401, 0, 0)\n",
|
|
" (1632700800, 1.17255, 1.17255, 1.16848, 1.16952, 57807, 0, 0)\n",
|
|
" (1632787200, 1.1694 , 1.17032, 1.16682, 1.16826, 64793, 0, 0)\n",
|
|
" (1632873600, 1.16825, 1.16901, 1.15894, 1.15969, 68964, 0, 0)\n",
|
|
" (1632960000, 1.15963, 1.16097, 1.15626, 1.15769, 68517, 0, 0)\n",
|
|
" (1633046400, 1.1574 , 1.16075, 1.1563 , 1.15927, 66777, 0, 0)\n",
|
|
" (1633305600, 1.15922, 1.164 , 1.15876, 1.16193, 54990, 0, 0)\n",
|
|
" (1633392000, 1.16184, 1.16216, 1.15806, 1.15972, 50533, 0, 0)\n",
|
|
" (1633478400, 1.15959, 1.16019, 1.15292, 1.15556, 63394, 0, 0)\n",
|
|
" (1633564800, 1.15538, 1.15723, 1.15474, 1.15527, 50611, 0, 0)\n",
|
|
" (1633651200, 1.15526, 1.15861, 1.15414, 1.15736, 59970, 0, 0)\n",
|
|
" (1633910400, 1.15659, 1.15867, 1.15487, 1.15517, 46485, 0, 0)\n",
|
|
" (1633996800, 1.15522, 1.15702, 1.15242, 1.15295, 57798, 0, 0)\n",
|
|
" (1634083200, 1.15294, 1.15973, 1.1525 , 1.1594 , 67602, 0, 0)\n",
|
|
" (1634169600, 1.15903, 1.16245, 1.15876, 1.15992, 33794, 0, 0)]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"from datetime import datetime\n",
|
|
"import MetaTrader5 as mt5\n",
|
|
"\n",
|
|
"if not mt5.initialize():\n",
|
|
"\tprint(f\"MT5 Init failed, error code {mt5.last_error()}\")\n",
|
|
"\tquit()\n",
|
|
"\n",
|
|
"pair = \"EURUSD\"\n",
|
|
"rates = mt5.copy_rates_from_pos(pair, mt5.TIMEFRAME_D1, 0, 100)\n",
|
|
"print(rates)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Convert the `rates` into a Dataframe"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<class 'pandas.core.frame.DataFrame'>\n",
|
|
" time open high low close tick_volume spread \\\n",
|
|
"0 2021-05-28 1.21932 1.22050 1.21327 1.21896 60110 0 \n",
|
|
"1 2021-05-31 1.21993 1.22314 1.21833 1.22250 36694 0 \n",
|
|
"2 2021-06-01 1.22254 1.22543 1.22118 1.22126 52448 0 \n",
|
|
"3 2021-06-02 1.22125 1.22265 1.21643 1.22100 53793 0 \n",
|
|
"4 2021-06-03 1.22097 1.22143 1.21183 1.21265 59299 0 \n",
|
|
"\n",
|
|
" real_volume \n",
|
|
"0 0 \n",
|
|
"1 0 \n",
|
|
"2 0 \n",
|
|
"3 0 \n",
|
|
"4 0 \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>time</th>\n",
|
|
" <th>open</th>\n",
|
|
" <th>high</th>\n",
|
|
" <th>low</th>\n",
|
|
" <th>close</th>\n",
|
|
" <th>tick_volume</th>\n",
|
|
" <th>spread</th>\n",
|
|
" <th>real_volume</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>2021-05-28</td>\n",
|
|
" <td>1.21932</td>\n",
|
|
" <td>1.22050</td>\n",
|
|
" <td>1.21327</td>\n",
|
|
" <td>1.21896</td>\n",
|
|
" <td>60110</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>2021-05-31</td>\n",
|
|
" <td>1.21993</td>\n",
|
|
" <td>1.22314</td>\n",
|
|
" <td>1.21833</td>\n",
|
|
" <td>1.22250</td>\n",
|
|
" <td>36694</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>2021-06-01</td>\n",
|
|
" <td>1.22254</td>\n",
|
|
" <td>1.22543</td>\n",
|
|
" <td>1.22118</td>\n",
|
|
" <td>1.22126</td>\n",
|
|
" <td>52448</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>2021-06-02</td>\n",
|
|
" <td>1.22125</td>\n",
|
|
" <td>1.22265</td>\n",
|
|
" <td>1.21643</td>\n",
|
|
" <td>1.22100</td>\n",
|
|
" <td>53793</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>2021-06-03</td>\n",
|
|
" <td>1.22097</td>\n",
|
|
" <td>1.22143</td>\n",
|
|
" <td>1.21183</td>\n",
|
|
" <td>1.21265</td>\n",
|
|
" <td>59299</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>...</th>\n",
|
|
" <td>...</td>\n",
|
|
" <td>...</td>\n",
|
|
" <td>...</td>\n",
|
|
" <td>...</td>\n",
|
|
" <td>...</td>\n",
|
|
" <td>...</td>\n",
|
|
" <td>...</td>\n",
|
|
" <td>...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>95</th>\n",
|
|
" <td>2021-10-08</td>\n",
|
|
" <td>1.15526</td>\n",
|
|
" <td>1.15861</td>\n",
|
|
" <td>1.15414</td>\n",
|
|
" <td>1.15736</td>\n",
|
|
" <td>59970</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>96</th>\n",
|
|
" <td>2021-10-11</td>\n",
|
|
" <td>1.15659</td>\n",
|
|
" <td>1.15867</td>\n",
|
|
" <td>1.15487</td>\n",
|
|
" <td>1.15517</td>\n",
|
|
" <td>46485</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>97</th>\n",
|
|
" <td>2021-10-12</td>\n",
|
|
" <td>1.15522</td>\n",
|
|
" <td>1.15702</td>\n",
|
|
" <td>1.15242</td>\n",
|
|
" <td>1.15295</td>\n",
|
|
" <td>57798</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>98</th>\n",
|
|
" <td>2021-10-13</td>\n",
|
|
" <td>1.15294</td>\n",
|
|
" <td>1.15973</td>\n",
|
|
" <td>1.15250</td>\n",
|
|
" <td>1.15940</td>\n",
|
|
" <td>67602</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>99</th>\n",
|
|
" <td>2021-10-14</td>\n",
|
|
" <td>1.15903</td>\n",
|
|
" <td>1.16245</td>\n",
|
|
" <td>1.15876</td>\n",
|
|
" <td>1.15992</td>\n",
|
|
" <td>33794</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"<p>100 rows × 8 columns</p>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" time open high low close tick_volume spread \\\n",
|
|
"0 2021-05-28 1.21932 1.22050 1.21327 1.21896 60110 0 \n",
|
|
"1 2021-05-31 1.21993 1.22314 1.21833 1.22250 36694 0 \n",
|
|
"2 2021-06-01 1.22254 1.22543 1.22118 1.22126 52448 0 \n",
|
|
"3 2021-06-02 1.22125 1.22265 1.21643 1.22100 53793 0 \n",
|
|
"4 2021-06-03 1.22097 1.22143 1.21183 1.21265 59299 0 \n",
|
|
".. ... ... ... ... ... ... ... \n",
|
|
"95 2021-10-08 1.15526 1.15861 1.15414 1.15736 59970 0 \n",
|
|
"96 2021-10-11 1.15659 1.15867 1.15487 1.15517 46485 0 \n",
|
|
"97 2021-10-12 1.15522 1.15702 1.15242 1.15295 57798 0 \n",
|
|
"98 2021-10-13 1.15294 1.15973 1.15250 1.15940 67602 0 \n",
|
|
"99 2021-10-14 1.15903 1.16245 1.15876 1.15992 33794 0 \n",
|
|
"\n",
|
|
" real_volume \n",
|
|
"0 0 \n",
|
|
"1 0 \n",
|
|
"2 0 \n",
|
|
"3 0 \n",
|
|
"4 0 \n",
|
|
".. ... \n",
|
|
"95 0 \n",
|
|
"96 0 \n",
|
|
"97 0 \n",
|
|
"98 0 \n",
|
|
"99 0 \n",
|
|
"\n",
|
|
"[100 rows x 8 columns]"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"data = pd.DataFrame(rates)\n",
|
|
"data['time'] = pd.to_datetime(data['time'], unit='s')\n",
|
|
"print(type(data))\n",
|
|
"print(data.head())\n",
|
|
"data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<bound method NDFrame.describe of time open high low close tick_volume spread \\\n",
|
|
"0 2021-05-28 1.21932 1.22050 1.21327 1.21896 60110 0 \n",
|
|
"1 2021-05-31 1.21993 1.22314 1.21833 1.22250 36694 0 \n",
|
|
"2 2021-06-01 1.22254 1.22543 1.22118 1.22126 52448 0 \n",
|
|
"3 2021-06-02 1.22125 1.22265 1.21643 1.22100 53793 0 \n",
|
|
"4 2021-06-03 1.22097 1.22143 1.21183 1.21265 59299 0 \n",
|
|
".. ... ... ... ... ... ... ... \n",
|
|
"95 2021-10-08 1.15526 1.15861 1.15414 1.15736 59970 0 \n",
|
|
"96 2021-10-11 1.15659 1.15867 1.15487 1.15517 46485 0 \n",
|
|
"97 2021-10-12 1.15522 1.15702 1.15242 1.15295 57798 0 \n",
|
|
"98 2021-10-13 1.15294 1.15973 1.15250 1.15940 67602 0 \n",
|
|
"99 2021-10-14 1.15903 1.16245 1.15876 1.15992 33794 0 \n",
|
|
"\n",
|
|
" real_volume \n",
|
|
"0 0 \n",
|
|
"1 0 \n",
|
|
"2 0 \n",
|
|
"3 0 \n",
|
|
"4 0 \n",
|
|
".. ... \n",
|
|
"95 0 \n",
|
|
"96 0 \n",
|
|
"97 0 \n",
|
|
"98 0 \n",
|
|
"99 0 \n",
|
|
"\n",
|
|
"[100 rows x 8 columns]>\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Understand the dataset\n",
|
|
"print(data.describe)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We'll be using scikit learn's to train_test_split the dataset but we have to define a few things"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Need to define the X set and the y set from the data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 28,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" time open high low tick_volume spread real_volume\n",
|
|
"0 2021-05-28 1.21932 1.22050 1.21327 60110 0 0\n",
|
|
"1 2021-05-31 1.21993 1.22314 1.21833 36694 0 0\n",
|
|
"2 2021-06-01 1.22254 1.22543 1.22118 52448 0 0\n",
|
|
"3 2021-06-02 1.22125 1.22265 1.21643 53793 0 0\n",
|
|
"4 2021-06-03 1.22097 1.22143 1.21183 59299 0 0\n",
|
|
"0 1.22100\n",
|
|
"1 1.21265\n",
|
|
"2 1.21662\n",
|
|
"3 1.21893\n",
|
|
"4 1.21726\n",
|
|
"Name: close, dtype: float64\n",
|
|
"95 1.15940\n",
|
|
"96 1.15992\n",
|
|
"97 0.00000\n",
|
|
"98 0.00000\n",
|
|
"99 0.00000\n",
|
|
"Name: close, dtype: float64\n",
|
|
"(100,)\n",
|
|
"(100,)\n",
|
|
"1 1.21265\n",
|
|
"2 1.21662\n",
|
|
"3 1.21893\n",
|
|
"4 1.21726\n",
|
|
"5 1.21787\n",
|
|
"Name: close, dtype: float64\n",
|
|
"96 1.15992\n",
|
|
"97 0.00000\n",
|
|
"98 0.00000\n",
|
|
"99 0.00000\n",
|
|
"100 0.00000\n",
|
|
"Name: close, dtype: float64\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"X = data.drop(columns='close')\n",
|
|
"print(X.head())\n",
|
|
"y = data['close']\n",
|
|
"print(y.head())\n",
|
|
"print(y.tail())\n",
|
|
"print(y.shape)\n",
|
|
"# Shift the close prices by 1\n",
|
|
"y.drop(0,inplace=True)\n",
|
|
"y.loc[100] = 0\n",
|
|
"print(y.shape)\n",
|
|
"print(y.head())\n",
|
|
"print(y.tail())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"X_train: time open high low tick_volume spread real_volume\n",
|
|
"65 2021-08-27 1.17514 1.18026 1.17349 54550 0 0\n",
|
|
"62 2021-08-24 1.17449 1.17650 1.17273 40186 0 0\n",
|
|
"27 2021-07-06 1.18619 1.18950 1.18067 79642 0 0\n",
|
|
"75 2021-09-10 1.18229 1.18510 1.18091 52650 0 0\n",
|
|
"61 2021-08-23 1.16897 1.17503 1.16897 42138 0 0\n",
|
|
".. ... ... ... ... ... ... ...\n",
|
|
"87 2021-09-28 1.16940 1.17032 1.16682 64793 0 0\n",
|
|
"1 2021-05-31 1.21993 1.22314 1.21833 36694 0 0\n",
|
|
"69 2021-09-02 1.18374 1.18759 1.18344 42004 0 0\n",
|
|
"55 2021-08-13 1.17290 1.18048 1.17281 35375 0 0\n",
|
|
"46 2021-08-02 1.18641 1.18969 1.18590 47011 0 0\n",
|
|
"\n",
|
|
"[70 rows x 7 columns]\n",
|
|
"X_test: time open high low tick_volume spread real_volume\n",
|
|
"20 2021-06-25 1.19315 1.19752 1.19265 59055 0 0\n",
|
|
"10 2021-06-11 1.21751 1.21931 1.20927 54807 0 0\n",
|
|
"96 2021-10-11 1.15659 1.15867 1.15487 46485 0 0\n",
|
|
"16 2021-06-21 1.18550 1.19212 1.18477 87967 0 0\n",
|
|
"63 2021-08-25 1.17537 1.17743 1.17260 45250 0 0\n",
|
|
"24 2021-07-01 1.18559 1.18842 1.18374 70316 0 0\n",
|
|
"53 2021-08-11 1.17184 1.17540 1.17061 45441 0 0\n",
|
|
"97 2021-10-12 1.15522 1.15702 1.15242 57798 0 0\n",
|
|
"41 2021-07-26 1.17677 1.18173 1.17635 53860 0 0\n",
|
|
"47 2021-08-03 1.18670 1.18933 1.18537 49688 0 0\n",
|
|
"43 2021-07-28 1.18159 1.18494 1.17721 64207 0 0\n",
|
|
"2 2021-06-01 1.22254 1.22543 1.22118 52448 0 0\n",
|
|
"95 2021-10-08 1.15526 1.15861 1.15414 59970 0 0\n",
|
|
"26 2021-07-05 1.18576 1.18805 1.18515 47260 0 0\n",
|
|
"13 2021-06-16 1.21256 1.21348 1.19940 66416 0 0\n",
|
|
"37 2021-07-20 1.17980 1.18025 1.17558 70566 0 0\n",
|
|
"14 2021-06-17 1.19939 1.20064 1.18918 90570 0 0\n",
|
|
"29 2021-07-08 1.17899 1.18679 1.17836 90999 0 0\n",
|
|
"35 2021-07-16 1.18118 1.18224 1.17921 70772 0 0\n",
|
|
"54 2021-08-12 1.17380 1.17482 1.17237 36888 0 0\n",
|
|
"80 2021-09-17 1.17675 1.17888 1.17242 56213 0 0\n",
|
|
"4 2021-06-03 1.22097 1.22143 1.21183 59299 0 0\n",
|
|
"81 2021-09-20 1.17272 1.17363 1.17004 58444 0 0\n",
|
|
"76 2021-09-13 1.18143 1.18171 1.17702 51815 0 0\n",
|
|
"85 2021-09-24 1.17379 1.17476 1.17007 51401 0 0\n",
|
|
"60 2021-08-20 1.16752 1.17045 1.16640 42854 0 0\n",
|
|
"5 2021-06-04 1.21261 1.21856 1.21041 56073 0 0\n",
|
|
"70 2021-09-03 1.18739 1.19091 1.18657 53226 0 0\n",
|
|
"71 2021-09-06 1.18768 1.18862 1.18557 29883 0 0\n",
|
|
"19 2021-06-24 1.19254 1.19563 1.19175 65027 0 0\n",
|
|
"y_train: 65 1.17955\n",
|
|
"62 1.17555\n",
|
|
"27 1.18231\n",
|
|
"75 1.18107\n",
|
|
"61 1.17457\n",
|
|
" ... \n",
|
|
"87 1.16826\n",
|
|
"1 1.22250\n",
|
|
"69 1.18744\n",
|
|
"55 1.17962\n",
|
|
"46 1.18688\n",
|
|
"Name: close, Length: 70, dtype: float64\n",
|
|
"y_test: 20 1.19370\n",
|
|
"10 1.21063\n",
|
|
"96 1.15517\n",
|
|
"16 1.19185\n",
|
|
"63 1.17716\n",
|
|
"24 1.18497\n",
|
|
"53 1.17388\n",
|
|
"97 1.15295\n",
|
|
"41 1.18029\n",
|
|
"47 1.18637\n",
|
|
"43 1.18441\n",
|
|
"2 1.22126\n",
|
|
"95 1.15736\n",
|
|
"26 1.18634\n",
|
|
"13 1.19943\n",
|
|
"37 1.17805\n",
|
|
"14 1.19069\n",
|
|
"29 1.18449\n",
|
|
"35 1.18055\n",
|
|
"54 1.17296\n",
|
|
"80 1.17250\n",
|
|
"4 1.21265\n",
|
|
"81 1.17257\n",
|
|
"76 1.18103\n",
|
|
"85 1.17206\n",
|
|
"60 1.16996\n",
|
|
"5 1.21662\n",
|
|
"70 1.18772\n",
|
|
"71 1.18701\n",
|
|
"19 1.19315\n",
|
|
"Name: close, dtype: float64\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Split the data set into training and test set\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=4)\n",
|
|
"print(f\"X_train: {X_train}\")\n",
|
|
"print(f\"X_test: {X_test}\")\n",
|
|
"print(f\"y_train: {y_train}\")\n",
|
|
"print(f\"y_test: {y_test}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Using LazyRegressor to find out the models"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# from lazypredict.Supervised import LazyRegressor\n",
|
|
"# reg = LazyRegressor(verbose=0, ignore_warnings=False, custom_metric=None)\n",
|
|
"# models, predictions = reg.fit(X_train, X_test, y_train, y_test)\n",
|
|
"\n",
|
|
"# print(models)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Pick a Model to study\n",
|
|
"\n",
|
|
"So according to the `LazyRegressor`, there are a bunch of models are unknown to me at this time of writing this. The only model I am familiar with is the LinearRegression. I may start with that and if it doesn't perform as much, I will continue up the models. RidgeCV being the top."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# import torch\n",
|
|
"# import math\n",
|
|
"\n",
|
|
"# class LinearRegression:\n",
|
|
"# def fit(self, X, y, method, learning_rate=0.01, iterations=500, batch_size=32):\n",
|
|
"# X, y = torch.from_numpy(X), torch.from_numpy(y)\n",
|
|
"# X = torch.cat([(X), torch.ones_like(y)], dim=1)\n",
|
|
"# rows, cols = X.size()\n",
|
|
"# if method == 'solve':\n",
|
|
"# if rows >= cols == torch.matrix_rank(X):\n",
|
|
"# self.weights = torch.matmul(\n",
|
|
"# torch.matmul(\n",
|
|
"# torch.inverse(\n",
|
|
"# torch.matmul(\n",
|
|
"# torch.transpose(X, 0, 1),\n",
|
|
"# X)),\n",
|
|
"# torch.transpose(X, 0, 1)),\n",
|
|
"# y)\n",
|
|
"# else:\n",
|
|
"# print('X has not full column rank. method=\\'solve\\' cannot be used.')\n",
|
|
"# elif method == 'sgd':\n",
|
|
"# self.weights = torch.normal(mean=0, std=1/cols, size=(cols, 1), dtype=torch.float64)\n",
|
|
"# for i in range(iterations):\n",
|
|
"# Xy = torch.cat([X, y], dim=1)\n",
|
|
"# Xy = Xy[torch.randperm(Xy.size()[0])]\n",
|
|
"# X, y = torch.split(Xy, [Xy.size()[1]-1, 1], dim=1)\n",
|
|
"# for j in range(int(math.ceil(rows/batch_size))):\n",
|
|
"# start, end = batch_size*j, min(batch_size*(j+1), rows)\n",
|
|
"# Xb = torch.index_select(X, 0, torch.arange(start, end))\n",
|
|
"# yb = torch.index_select(y, 0, torch.arange(start, end))\n",
|
|
" \n",
|
|
"# self.weights.requires_grad_(True)\n",
|
|
"# diff = torch.matmul(Xb, self.weights) - yb\n",
|
|
"# loss = torch.matmul(torch.transpose(diff, 0, 1), diff)\n",
|
|
"# loss.backward()\n",
|
|
" \n",
|
|
"# self.weights = (self.weights - learning_rate*self.weights.grad).detach()\n",
|
|
"# else:\n",
|
|
"# print(f'Unknown method: \\'{method}\\'')\n",
|
|
" \n",
|
|
"# return self\n",
|
|
" \n",
|
|
"# def predict(self, X):\n",
|
|
"# X = torch.from_numpy(X)\n",
|
|
"# if not hasattr(self, 'weights'):\n",
|
|
"# print('Cannot predict. You should call the .fit() method first.')\n",
|
|
"# return\n",
|
|
" \n",
|
|
"# X = torch.cat([X, torch.ones((X.size()[0], 1))], dim=1)\n",
|
|
" \n",
|
|
"# if X.size()[1] != self.weights.size()[0]:\n",
|
|
"# print(f'Shapes do not match. {X.size()[1]} != {self.weights.size()[0]}')\n",
|
|
"# return\n",
|
|
" \n",
|
|
"# return torch.matmul(X, self.weights)\n",
|
|
" \n",
|
|
"# def rmse(self, X, y):\n",
|
|
"# y = torch.from_numpy(y)\n",
|
|
"# y_hat = self.predict(X)\n",
|
|
" \n",
|
|
"# if y_hat is None:\n",
|
|
"# return\n",
|
|
" \n",
|
|
"# return torch.sqrt(torch.mean(torch.square(y_hat - y)))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"With the Linear Regression model defined with fitting and predicting, let's map and plot the current dataset"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# import mplfinance as mpf\n",
|
|
"\n",
|
|
"# data = data.set_index('time')\n",
|
|
"# mpf.plot(data, type='line', title=pair)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The Metatrader 5 seems to not be able to handle order management very well. This includes current order sent.\n",
|
|
"However, the broker that I'm using (XM) is giving me a high spread that the predicted price just happens to be in the middle of."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"------------USDCAD--------------\n",
|
|
"Points : 1e-05\n",
|
|
"Ask Price : 1.23816\n",
|
|
"Bid Price : 1.23816\n",
|
|
"Current Open Price : 1.2382900000000001\n",
|
|
"Predicted Price : 1.237242894242747\n",
|
|
"Stop Loss : 1.2381600104710575\n",
|
|
"Signal : Sell\n",
|
|
"Distance : 0.0010471057572531972\n",
|
|
"Can we place a trade? : True\n",
|
|
"Calculated Profit : 7.41\n",
|
|
"Current DataFrame before splitting close price\n",
|
|
" time open high low close tick_volume spread \\\n",
|
|
"0 1634223600 1.23829 1.23831 1.23725 1.23816 1520 0 \n",
|
|
"\n",
|
|
" real_volume \n",
|
|
"0 0 \n",
|
|
"---------------------------------------\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Login\n",
|
|
"import config\n",
|
|
"client = mt5.login(config.ACCOUNT, password=config.PASSWORD, server=config.SERVER)\n",
|
|
"for p in config.PAIRS:\n",
|
|
"\tsymbol_info = mt5.symbol_info(p)\n",
|
|
"\tsymbol_tick = mt5.symbol_info_tick(p)\n",
|
|
"\tpoints = symbol_info.point\n",
|
|
"\task = symbol_tick.ask\n",
|
|
"\tbid = symbol_tick.bid\n",
|
|
"\tcurrent_rate = mt5.copy_rates_from_pos(p, config.TIMEFRAME, 0, 1)\n",
|
|
"\tcurrent_df = pd.DataFrame(current_rate)\n",
|
|
"\tcurrent_open_price = current_df.iloc[0][\"open\"]\n",
|
|
"\tto_predict = current_df.drop(columns=\"close\")\n",
|
|
"\tsymbol_predict = model.predict(to_predict)\n",
|
|
"\tif current_open_price > symbol_predict[0]:\n",
|
|
"\t\tsignal = \"Sell\"\n",
|
|
"\t\ttrade_feasible = symbol_predict[0] < bid\n",
|
|
"\t\tprofit=mt5.order_calc_profit(mt5.ORDER_TYPE_SELL, p, config.LOT_SIZE, bid, symbol_predict[0])\n",
|
|
"\t\tdistance = current_open_price - symbol_predict[0]\n",
|
|
"\t\tstop_loss = bid+distance*points\n",
|
|
"\telse:\n",
|
|
"\t\tsignal = \"Buy\"\n",
|
|
"\t\ttrade_feasible = symbol_predict[0] > ask\n",
|
|
"\t\tprofit=mt5.order_calc_profit(mt5.ORDER_TYPE_BUY, p, config.LOT_SIZE, ask, symbol_predict[0])\n",
|
|
"\t\tdistance = symbol_predict[0] - current_open_price\n",
|
|
"\t\tstop_loss = ask-distance*points\n",
|
|
"\tprint(f\"------------{p}--------------\")\n",
|
|
"\tprint(f\"Points : {points}\")\n",
|
|
"\tprint(f\"Ask Price : {ask}\")\n",
|
|
"\tprint(f\"Bid Price : {bid}\")\n",
|
|
"\tprint(f\"Current Open Price : {current_open_price}\")\n",
|
|
"\tprint(f\"Predicted Price : {symbol_predict[0]}\")\n",
|
|
"\tprint(f\"Stop Loss : {stop_loss}\")\n",
|
|
"\tprint(f\"Signal : {signal}\")\n",
|
|
"\tprint(f\"Distance : {distance}\")\n",
|
|
"\tprint(f\"Can we place a trade? : {trade_feasible}\")\n",
|
|
"\tprint(f\"Calculated Profit : {profit}\")\n",
|
|
"\tprint(\"Current DataFrame before splitting close price\")\n",
|
|
"\tprint(current_df)\n",
|
|
"\tprint(\"---------------------------------------\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Try forecasting 2 ticks ahead. This should solve the \"invalid stops\" issue."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[(1634225580, 1.23825, 1.23863, 1.23818, 1.23863, 86, 0, 0)]\n",
|
|
" time open high low close tick_volume spread \\\n",
|
|
"0 1634225580 1.23825 1.23863 1.23818 1.23863 86 0 \n",
|
|
"\n",
|
|
" real_volume \n",
|
|
"0 0 \n",
|
|
"Index(['time', 'open', 'high', 'low', 'close', 'tick_volume', 'spread',\n",
|
|
" 'real_volume'],\n",
|
|
" dtype='object')\n",
|
|
"RangeIndex(start=0, stop=1, step=1)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import config\n",
|
|
"import MetaTrader5 as mt5\n",
|
|
"import pandas as pd\n",
|
|
"if not mt5.initialize():\n",
|
|
"\tprint(\"Something went wrong\")\n",
|
|
"elif mt5.login(login=config.ACCOUNT, password=config.PASSWORD, server=config.SERVER):\n",
|
|
"\tcurrent_rate = mt5.copy_rates_from_pos(\"USDCAD\", mt5.TIMEFRAME_M1, 0, 1)\n",
|
|
"\tprint(current_rate)\n",
|
|
"\tcurrent_rate_df = pd.DataFrame(current_rate)\n",
|
|
"\tprint(current_rate_df)\n",
|
|
"\tfeatures = current_rate_df.columns\n",
|
|
"\tprint(features)\n",
|
|
"\tindex = current_rate_df.index\n",
|
|
"\tprint(index)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"interpreter": {
|
|
"hash": "398df2b33328d7497935b300d4507ade3456a01361fefa2e3b5959f5ae897d3f"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3.9.4 64-bit ('forex-zmq-GFmDeAiW': virtualenv)",
|
|
"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.9.4"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|