Forex-MT5-Bot/fxstudy.ipynb

798 lines
35 KiB
Text
Raw Permalink Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"source": [
"# Find out what model works best with the dataset from XM"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"source": [
"import pandas as pd\r\n",
"\r\n",
"from datetime import datetime\r\n",
"import MetaTrader5 as mt5\r\n",
"\r\n",
"if not mt5.initialize():\r\n",
"\tprint(f\"MT5 Init failed, error code {mt5.last_error()}\")\r\n",
"\tquit()\r\n",
"\r\n",
"pair = \"GBPUSD\"\r\n",
"rates = mt5.copy_rates_from_pos(pair, mt5.TIMEFRAME_D1, 0, 100)\r\n",
"print(rates)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[(1620604800, 1.39994, 1.41574, 1.39929, 1.41148, 28358, 19, 0)\n",
" (1620691200, 1.41148, 1.41653, 1.41033, 1.41359, 32194, 19, 0)\n",
" (1620777600, 1.41359, 1.41518, 1.40488, 1.40495, 40832, 19, 0)\n",
" (1620864000, 1.40495, 1.40771, 1.40047, 1.40465, 37476, 19, 0)\n",
" (1620950400, 1.40465, 1.41098, 1.40351, 1.40944, 28589, 19, 0)\n",
" (1621209600, 1.41011, 1.4146 , 1.40762, 1.41258, 28368, 19, 0)\n",
" (1621296000, 1.41258, 1.42193, 1.41258, 1.41827, 31182, 19, 0)\n",
" (1621382400, 1.41829, 1.41998, 1.40993, 1.41097, 44625, 19, 0)\n",
" (1621468800, 1.41097, 1.41915, 1.41002, 1.41825, 33713, 19, 0)\n",
" (1621555200, 1.41825, 1.4233 , 1.41395, 1.4146 , 33552, 19, 0)\n",
" (1621814400, 1.41531, 1.41708, 1.41102, 1.41481, 29336, 19, 0)\n",
" (1621900800, 1.41481, 1.42101, 1.41148, 1.41451, 33269, 19, 0)\n",
" (1621987200, 1.41451, 1.4175 , 1.41104, 1.41124, 31269, 19, 0)\n",
" (1622073600, 1.41124, 1.42183, 1.40905, 1.41988, 33789, 19, 0)\n",
" (1622160000, 1.41988, 1.42073, 1.41352, 1.41871, 44691, 19, 0)\n",
" (1622419200, 1.41771, 1.42169, 1.41629, 1.42042, 26769, 19, 0)\n",
" (1622505600, 1.42042, 1.42483, 1.41448, 1.41448, 42181, 19, 0)\n",
" (1622592000, 1.41448, 1.41824, 1.41112, 1.4163 , 36998, 19, 0)\n",
" (1622678400, 1.4163 , 1.4202 , 1.40855, 1.40987, 43213, 19, 0)\n",
" (1622764800, 1.40987, 1.41993, 1.40823, 1.41551, 41735, 19, 0)\n",
" (1623024000, 1.41454, 1.41891, 1.41105, 1.41731, 36046, 19, 0)\n",
" (1623110400, 1.41747, 1.41834, 1.412 , 1.41505, 30964, 19, 0)\n",
" (1623196800, 1.41505, 1.41879, 1.41092, 1.41125, 29958, 19, 0)\n",
" (1623283200, 1.41125, 1.41772, 1.40725, 1.41721, 41583, 19, 0)\n",
" (1623369600, 1.41721, 1.41845, 1.40946, 1.41071, 31313, 19, 0)\n",
" (1623628800, 1.41041, 1.41227, 1.40692, 1.41039, 22491, 19, 0)\n",
" (1623715200, 1.41032, 1.41276, 1.40332, 1.40762, 32252, 19, 0)\n",
" (1623801600, 1.40752, 1.41316, 1.39808, 1.39817, 39772, 19, 0)\n",
" (1623888000, 1.39817, 1.40075, 1.38946, 1.39138, 45190, 19, 0)\n",
" (1623974400, 1.39138, 1.39433, 1.37908, 1.37964, 47299, 19, 0)\n",
" (1624233600, 1.38064, 1.39358, 1.37854, 1.3929 , 46126, 19, 0)\n",
" (1624320000, 1.3929 , 1.39625, 1.38593, 1.39414, 34394, 19, 0)\n",
" (1624406400, 1.39414, 1.40002, 1.3923 , 1.39565, 35253, 19, 0)\n",
" (1624492800, 1.39565, 1.39855, 1.38886, 1.39127, 38538, 19, 0)\n",
" (1624579200, 1.39127, 1.39346, 1.38702, 1.38762, 32352, 19, 0)\n",
" (1624838400, 1.38915, 1.39386, 1.38699, 1.38772, 30848, 19, 0)\n",
" (1624924800, 1.38772, 1.38818, 1.38133, 1.38303, 30670, 19, 0)\n",
" (1625011200, 1.38303, 1.38718, 1.37974, 1.38226, 34827, 19, 0)\n",
" (1625097600, 1.38226, 1.38331, 1.3752 , 1.37597, 35844, 19, 0)\n",
" (1625184000, 1.37597, 1.38435, 1.37302, 1.38297, 38104, 19, 0)\n",
" (1625443200, 1.38214, 1.38607, 1.38163, 1.38362, 24428, 19, 0)\n",
" (1625529600, 1.38362, 1.38966, 1.37719, 1.37936, 41510, 19, 0)\n",
" (1625616000, 1.37936, 1.38406, 1.37527, 1.37938, 46461, 19, 0)\n",
" (1625702400, 1.37938, 1.38038, 1.3741 , 1.37795, 52161, 19, 0)\n",
" (1625788800, 1.37788, 1.39 , 1.37548, 1.38964, 46135, 19, 0)\n",
" (1626048000, 1.38917, 1.39091, 1.38381, 1.38747, 37683, 19, 0)\n",
" (1626134400, 1.38747, 1.39043, 1.37986, 1.38066, 37188, 19, 0)\n",
" (1626220800, 1.38066, 1.3891 , 1.38005, 1.38557, 39483, 19, 0)\n",
" (1626307200, 1.38557, 1.38977, 1.3804 , 1.38242, 43484, 19, 0)\n",
" (1626393600, 1.38242, 1.38611, 1.37595, 1.37615, 35530, 19, 0)\n",
" (1626652800, 1.37715, 1.37735, 1.36541, 1.36724, 54735, 19, 0)\n",
" (1626739200, 1.36724, 1.36883, 1.35708, 1.36211, 47256, 19, 0)\n",
" (1626825600, 1.36211, 1.37219, 1.35904, 1.37119, 37403, 19, 0)\n",
" (1626912000, 1.37119, 1.3786 , 1.36895, 1.37602, 37734, 19, 0)\n",
" (1626998400, 1.37602, 1.37789, 1.3719 , 1.37466, 30737, 19, 0)\n",
" (1627257600, 1.37504, 1.38321, 1.3736 , 1.38098, 35242, 19, 0)\n",
" (1627344000, 1.38096, 1.3893 , 1.37656, 1.38725, 35966, 19, 0)\n",
" (1627430400, 1.38721, 1.391 , 1.38422, 1.3893 , 33732, 19, 0)\n",
" (1627516800, 1.38951, 1.39806, 1.38913, 1.39519, 26251, 19, 0)\n",
" (1627603200, 1.39506, 1.39822, 1.38871, 1.39006, 25422, 19, 0)\n",
" (1627862400, 1.38996, 1.39316, 1.38748, 1.38777, 23673, 19, 0)\n",
" (1627948800, 1.38777, 1.39374, 1.38736, 1.39106, 25014, 19, 0)\n",
" (1628035200, 1.39096, 1.39566, 1.38826, 1.38862, 23921, 19, 0)\n",
" (1628121600, 1.38859, 1.39478, 1.38713, 1.39272, 27727, 19, 0)\n",
" (1628208000, 1.39272, 1.39317, 1.38602, 1.38723, 22461, 19, 0)\n",
" (1628467200, 1.38677, 1.38931, 1.38385, 1.38385, 24500, 19, 0)\n",
" (1628553600, 1.3844 , 1.38714, 1.38264, 1.38359, 20029, 19, 0)\n",
" (1628640000, 1.38364, 1.38869, 1.38017, 1.38611, 21696, 19, 0)\n",
" (1628726400, 1.38611, 1.38773, 1.37936, 1.37977, 20612, 19, 0)\n",
" (1628812800, 1.37977, 1.38741, 1.37894, 1.38658, 17201, 19, 0)\n",
" (1629072000, 1.38641, 1.38769, 1.38268, 1.38421, 19562, 19, 0)\n",
" (1629158400, 1.38421, 1.38426, 1.37249, 1.37321, 23801, 19, 0)\n",
" (1629244800, 1.37321, 1.3785 , 1.3719 , 1.375 , 21758, 19, 0)\n",
" (1629331200, 1.375 , 1.37586, 1.36304, 1.36304, 27007, 19, 0)\n",
" (1629417600, 1.36306, 1.36398, 1.36011, 1.36241, 22204, 19, 0)\n",
" (1629676800, 1.36226, 1.37308, 1.36051, 1.37111, 21328, 19, 0)\n",
" (1629763200, 1.37111, 1.37466, 1.36927, 1.37252, 19377, 19, 0)\n",
" (1629849600, 1.3726 , 1.37659, 1.36955, 1.37559, 20814, 19, 0)\n",
" (1629936000, 1.3754 , 1.37668, 1.36886, 1.3695 , 23813, 19, 0)\n",
" (1630022400, 1.36945, 1.378 , 1.3679 , 1.37549, 25338, 19, 0)\n",
" (1630281600, 1.37522, 1.37743, 1.37327, 1.37559, 18588, 19, 0)\n",
" (1630368000, 1.37559, 1.38068, 1.3742 , 1.37533, 31027, 19, 0)\n",
" (1630454400, 1.37519, 1.37972, 1.37302, 1.37663, 27741, 19, 0)\n",
" (1630540800, 1.3766 , 1.3839 , 1.37654, 1.383 , 20250, 19, 0)\n",
" (1630627200, 1.38323, 1.3891 , 1.38169, 1.38543, 27989, 19, 0)\n",
" (1630886400, 1.38527, 1.38674, 1.38178, 1.3831 , 20769, 19, 0)\n",
" (1630972800, 1.38323, 1.38554, 1.37666, 1.37813, 34685, 19, 0)\n",
" (1631059200, 1.37812, 1.37894, 1.37254, 1.37684, 32355, 19, 0)\n",
" (1631145600, 1.37688, 1.38617, 1.37526, 1.3834 , 35609, 19, 0)\n",
" (1631232000, 1.38329, 1.38873, 1.38255, 1.3829 , 27899, 19, 0)\n",
" (1631491200, 1.38375, 1.38503, 1.3796 , 1.38311, 30923, 19, 0)\n",
" (1631577600, 1.38335, 1.3912 , 1.38022, 1.38038, 33876, 19, 0)\n",
" (1631664000, 1.38058, 1.38527, 1.37917, 1.38344, 33271, 19, 0)\n",
" (1631750400, 1.38358, 1.3852 , 1.37633, 1.37894, 30453, 19, 0)\n",
" (1631836800, 1.37918, 1.38118, 1.37259, 1.37261, 35121, 19, 0)\n",
" (1632096000, 1.37444, 1.37486, 1.364 , 1.3652 , 36677, 19, 0)\n",
" (1632182400, 1.36497, 1.36921, 1.36402, 1.36552, 41087, 19, 0)\n",
" (1632268800, 1.36553, 1.36886, 1.36083, 1.36136, 41217, 19, 0)\n",
" (1632355200, 1.36126, 1.37499, 1.36107, 1.37149, 43973, 19, 0)\n",
" (1632441600, 1.37183, 1.37352, 1.37091, 1.37181, 3993, 19, 0)]\n"
]
}
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"# Convert the `rates` into a Dataframe"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"source": [
"data = pd.DataFrame(rates)\r\n",
"data['time'] = pd.to_datetime(data['time'], unit='s')\r\n",
"print(type(data))\r\n",
"print(data.head())\r\n",
"data"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
" time open high low close tick_volume spread \\\n",
"0 2021-05-10 1.39994 1.41574 1.39929 1.41148 28358 19 \n",
"1 2021-05-11 1.41148 1.41653 1.41033 1.41359 32194 19 \n",
"2 2021-05-12 1.41359 1.41518 1.40488 1.40495 40832 19 \n",
"3 2021-05-13 1.40495 1.40771 1.40047 1.40465 37476 19 \n",
"4 2021-05-14 1.40465 1.41098 1.40351 1.40944 28589 19 \n",
"\n",
" real_volume \n",
"0 0 \n",
"1 0 \n",
"2 0 \n",
"3 0 \n",
"4 0 \n"
]
},
{
"output_type": "execute_result",
"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-10</td>\n",
" <td>1.39994</td>\n",
" <td>1.41574</td>\n",
" <td>1.39929</td>\n",
" <td>1.41148</td>\n",
" <td>28358</td>\n",
" <td>19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2021-05-11</td>\n",
" <td>1.41148</td>\n",
" <td>1.41653</td>\n",
" <td>1.41033</td>\n",
" <td>1.41359</td>\n",
" <td>32194</td>\n",
" <td>19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2021-05-12</td>\n",
" <td>1.41359</td>\n",
" <td>1.41518</td>\n",
" <td>1.40488</td>\n",
" <td>1.40495</td>\n",
" <td>40832</td>\n",
" <td>19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2021-05-13</td>\n",
" <td>1.40495</td>\n",
" <td>1.40771</td>\n",
" <td>1.40047</td>\n",
" <td>1.40465</td>\n",
" <td>37476</td>\n",
" <td>19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2021-05-14</td>\n",
" <td>1.40465</td>\n",
" <td>1.41098</td>\n",
" <td>1.40351</td>\n",
" <td>1.40944</td>\n",
" <td>28589</td>\n",
" <td>19</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-09-20</td>\n",
" <td>1.37444</td>\n",
" <td>1.37486</td>\n",
" <td>1.36400</td>\n",
" <td>1.36520</td>\n",
" <td>36677</td>\n",
" <td>19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>96</th>\n",
" <td>2021-09-21</td>\n",
" <td>1.36497</td>\n",
" <td>1.36921</td>\n",
" <td>1.36402</td>\n",
" <td>1.36552</td>\n",
" <td>41087</td>\n",
" <td>19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>97</th>\n",
" <td>2021-09-22</td>\n",
" <td>1.36553</td>\n",
" <td>1.36886</td>\n",
" <td>1.36083</td>\n",
" <td>1.36136</td>\n",
" <td>41217</td>\n",
" <td>19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>98</th>\n",
" <td>2021-09-23</td>\n",
" <td>1.36126</td>\n",
" <td>1.37499</td>\n",
" <td>1.36107</td>\n",
" <td>1.37149</td>\n",
" <td>43973</td>\n",
" <td>19</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>99</th>\n",
" <td>2021-09-24</td>\n",
" <td>1.37183</td>\n",
" <td>1.37352</td>\n",
" <td>1.37091</td>\n",
" <td>1.37181</td>\n",
" <td>3993</td>\n",
" <td>19</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-10 1.39994 1.41574 1.39929 1.41148 28358 19 \n",
"1 2021-05-11 1.41148 1.41653 1.41033 1.41359 32194 19 \n",
"2 2021-05-12 1.41359 1.41518 1.40488 1.40495 40832 19 \n",
"3 2021-05-13 1.40495 1.40771 1.40047 1.40465 37476 19 \n",
"4 2021-05-14 1.40465 1.41098 1.40351 1.40944 28589 19 \n",
".. ... ... ... ... ... ... ... \n",
"95 2021-09-20 1.37444 1.37486 1.36400 1.36520 36677 19 \n",
"96 2021-09-21 1.36497 1.36921 1.36402 1.36552 41087 19 \n",
"97 2021-09-22 1.36553 1.36886 1.36083 1.36136 41217 19 \n",
"98 2021-09-23 1.36126 1.37499 1.36107 1.37149 43973 19 \n",
"99 2021-09-24 1.37183 1.37352 1.37091 1.37181 3993 19 \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]"
]
},
"metadata": {},
"execution_count": 3
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"source": [
"# Understand the dataset\r\n",
"print(data.describe)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<bound method NDFrame.describe of time open high low close tick_volume spread \\\n",
"0 2021-05-10 1.39994 1.41574 1.39929 1.41148 28358 19 \n",
"1 2021-05-11 1.41148 1.41653 1.41033 1.41359 32194 19 \n",
"2 2021-05-12 1.41359 1.41518 1.40488 1.40495 40832 19 \n",
"3 2021-05-13 1.40495 1.40771 1.40047 1.40465 37476 19 \n",
"4 2021-05-14 1.40465 1.41098 1.40351 1.40944 28589 19 \n",
".. ... ... ... ... ... ... ... \n",
"95 2021-09-20 1.37444 1.37486 1.36400 1.36520 36677 19 \n",
"96 2021-09-21 1.36497 1.36921 1.36402 1.36552 41087 19 \n",
"97 2021-09-22 1.36553 1.36886 1.36083 1.36136 41217 19 \n",
"98 2021-09-23 1.36126 1.37499 1.36107 1.37149 43973 19 \n",
"99 2021-09-24 1.37183 1.37352 1.37091 1.37181 3993 19 \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"
]
}
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"We'll be using scikit learn's to train_test_split the dataset but we have to define a few things"
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"Need to define the X set and the y set from the data"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 5,
"source": [
"X = data.drop(columns='close')\r\n",
"print(X.head())\r\n",
"y = data['close']\r\n",
"print(y.head())"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" time open high low tick_volume spread real_volume\n",
"0 2021-05-10 1.39994 1.41574 1.39929 28358 19 0\n",
"1 2021-05-11 1.41148 1.41653 1.41033 32194 19 0\n",
"2 2021-05-12 1.41359 1.41518 1.40488 40832 19 0\n",
"3 2021-05-13 1.40495 1.40771 1.40047 37476 19 0\n",
"4 2021-05-14 1.40465 1.41098 1.40351 28589 19 0\n",
"0 1.41148\n",
"1 1.41359\n",
"2 1.40495\n",
"3 1.40465\n",
"4 1.40944\n",
"Name: close, dtype: float64\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 6,
"source": [
"# Split the data set into training and test set\r\n",
"from sklearn.model_selection import train_test_split\r\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=4)\r\n",
"print(f\"X_train: {X_train}\")\r\n",
"print(f\"X_test: {X_test}\")\r\n",
"print(f\"y_train: {y_train}\")\r\n",
"print(f\"y_test: {y_test}\")"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"X_train: time open high low tick_volume spread real_volume\n",
"65 2021-08-09 1.38677 1.38931 1.38385 24500 19 0\n",
"62 2021-08-04 1.39096 1.39566 1.38826 23921 19 0\n",
"27 2021-06-16 1.40752 1.41316 1.39808 39772 19 0\n",
"75 2021-08-23 1.36226 1.37308 1.36051 21328 19 0\n",
"61 2021-08-03 1.38777 1.39374 1.38736 25014 19 0\n",
".. ... ... ... ... ... ... ...\n",
"87 2021-09-08 1.37812 1.37894 1.37254 32355 19 0\n",
"1 2021-05-11 1.41148 1.41653 1.41033 32194 19 0\n",
"69 2021-08-13 1.37977 1.38741 1.37894 17201 19 0\n",
"55 2021-07-26 1.37504 1.38321 1.37360 35242 19 0\n",
"46 2021-07-13 1.38747 1.39043 1.37986 37188 19 0\n",
"\n",
"[70 rows x 7 columns]\n",
"X_test: time open high low tick_volume spread real_volume\n",
"20 2021-06-07 1.41454 1.41891 1.41105 36046 19 0\n",
"10 2021-05-24 1.41531 1.41708 1.41102 29336 19 0\n",
"96 2021-09-21 1.36497 1.36921 1.36402 41087 19 0\n",
"16 2021-06-01 1.42042 1.42483 1.41448 42181 19 0\n",
"63 2021-08-05 1.38859 1.39478 1.38713 27727 19 0\n",
"24 2021-06-11 1.41721 1.41845 1.40946 31313 19 0\n",
"53 2021-07-22 1.37119 1.37860 1.36895 37734 19 0\n",
"97 2021-09-22 1.36553 1.36886 1.36083 41217 19 0\n",
"41 2021-07-06 1.38362 1.38966 1.37719 41510 19 0\n",
"47 2021-07-14 1.38066 1.38910 1.38005 39483 19 0\n",
"43 2021-07-08 1.37938 1.38038 1.37410 52161 19 0\n",
"2 2021-05-12 1.41359 1.41518 1.40488 40832 19 0\n",
"95 2021-09-20 1.37444 1.37486 1.36400 36677 19 0\n",
"26 2021-06-15 1.41032 1.41276 1.40332 32252 19 0\n",
"13 2021-05-27 1.41124 1.42183 1.40905 33789 19 0\n",
"37 2021-06-30 1.38303 1.38718 1.37974 34827 19 0\n",
"14 2021-05-28 1.41988 1.42073 1.41352 44691 19 0\n",
"29 2021-06-18 1.39138 1.39433 1.37908 47299 19 0\n",
"35 2021-06-28 1.38915 1.39386 1.38699 30848 19 0\n",
"54 2021-07-23 1.37602 1.37789 1.37190 30737 19 0\n",
"80 2021-08-30 1.37522 1.37743 1.37327 18588 19 0\n",
"4 2021-05-14 1.40465 1.41098 1.40351 28589 19 0\n",
"81 2021-08-31 1.37559 1.38068 1.37420 31027 19 0\n",
"76 2021-08-24 1.37111 1.37466 1.36927 19377 19 0\n",
"85 2021-09-06 1.38527 1.38674 1.38178 20769 19 0\n",
"60 2021-08-02 1.38996 1.39316 1.38748 23673 19 0\n",
"5 2021-05-17 1.41011 1.41460 1.40762 28368 19 0\n",
"70 2021-08-16 1.38641 1.38769 1.38268 19562 19 0\n",
"71 2021-08-17 1.38421 1.38426 1.37249 23801 19 0\n",
"19 2021-06-04 1.40987 1.41993 1.40823 41735 19 0\n",
"y_train: 65 1.38385\n",
"62 1.38862\n",
"27 1.39817\n",
"75 1.37111\n",
"61 1.39106\n",
" ... \n",
"87 1.37684\n",
"1 1.41359\n",
"69 1.38658\n",
"55 1.38098\n",
"46 1.38066\n",
"Name: close, Length: 70, dtype: float64\n",
"y_test: 20 1.41731\n",
"10 1.41481\n",
"96 1.36552\n",
"16 1.41448\n",
"63 1.39272\n",
"24 1.41071\n",
"53 1.37602\n",
"97 1.36136\n",
"41 1.37936\n",
"47 1.38557\n",
"43 1.37795\n",
"2 1.40495\n",
"95 1.36520\n",
"26 1.40762\n",
"13 1.41988\n",
"37 1.38226\n",
"14 1.41871\n",
"29 1.37964\n",
"35 1.38772\n",
"54 1.37466\n",
"80 1.37559\n",
"4 1.40944\n",
"81 1.37533\n",
"76 1.37252\n",
"85 1.38310\n",
"60 1.38777\n",
"5 1.41258\n",
"70 1.38421\n",
"71 1.37321\n",
"19 1.41551\n",
"Name: close, dtype: float64\n"
]
}
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"Using LazyRegressor to find out the models"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 7,
"source": [
"from lazypredict.Supervised import LazyRegressor\r\n",
"reg = LazyRegressor(verbose=0, ignore_warnings=False, custom_metric=None)\r\n",
"models, predictions = reg.fit(X_train, X_test, y_train, y_test)\r\n",
"\r\n",
"print(models)"
],
"outputs": [
{
"output_type": "error",
"ename": "ModuleNotFoundError",
"evalue": "No module named 'lazypredict'",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-7-0bb6600d1485>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mlazypredict\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSupervised\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mLazyRegressor\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mreg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mLazyRegressor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mverbose\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mignore_warnings\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcustom_metric\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mmodels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpredictions\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mreg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mX_test\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmodels\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'lazypredict'"
]
}
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"# Pick a Model to study\r\n",
"\r\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."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"import torch\r\n",
"import math\r\n",
"\r\n",
"class LinearRegression:\r\n",
" def fit(self, X, y, method, learning_rate=0.01, iterations=500, batch_size=32):\r\n",
" X, y = torch.from_numpy(X), torch.from_numpy(y)\r\n",
" X = torch.cat([(X), torch.ones_like(y)], dim=1)\r\n",
" rows, cols = X.size()\r\n",
" if method == 'solve':\r\n",
" if rows >= cols == torch.matrix_rank(X):\r\n",
" self.weights = torch.matmul(\r\n",
" torch.matmul(\r\n",
" torch.inverse(\r\n",
" torch.matmul(\r\n",
" torch.transpose(X, 0, 1),\r\n",
" X)),\r\n",
" torch.transpose(X, 0, 1)),\r\n",
" y)\r\n",
" else:\r\n",
" print('X has not full column rank. method=\\'solve\\' cannot be used.')\r\n",
" elif method == 'sgd':\r\n",
" self.weights = torch.normal(mean=0, std=1/cols, size=(cols, 1), dtype=torch.float64)\r\n",
" for i in range(iterations):\r\n",
" Xy = torch.cat([X, y], dim=1)\r\n",
" Xy = Xy[torch.randperm(Xy.size()[0])]\r\n",
" X, y = torch.split(Xy, [Xy.size()[1]-1, 1], dim=1)\r\n",
" for j in range(int(math.ceil(rows/batch_size))):\r\n",
" start, end = batch_size*j, min(batch_size*(j+1), rows)\r\n",
" Xb = torch.index_select(X, 0, torch.arange(start, end))\r\n",
" yb = torch.index_select(y, 0, torch.arange(start, end))\r\n",
" \r\n",
" self.weights.requires_grad_(True)\r\n",
" diff = torch.matmul(Xb, self.weights) - yb\r\n",
" loss = torch.matmul(torch.transpose(diff, 0, 1), diff)\r\n",
" loss.backward()\r\n",
" \r\n",
" self.weights = (self.weights - learning_rate*self.weights.grad).detach()\r\n",
" else:\r\n",
" print(f'Unknown method: \\'{method}\\'')\r\n",
" \r\n",
" return self\r\n",
" \r\n",
" def predict(self, X):\r\n",
" X = torch.from_numpy(X)\r\n",
" if not hasattr(self, 'weights'):\r\n",
" print('Cannot predict. You should call the .fit() method first.')\r\n",
" return\r\n",
" \r\n",
" X = torch.cat([X, torch.ones((X.size()[0], 1))], dim=1)\r\n",
" \r\n",
" if X.size()[1] != self.weights.size()[0]:\r\n",
" print(f'Shapes do not match. {X.size()[1]} != {self.weights.size()[0]}')\r\n",
" return\r\n",
" \r\n",
" return torch.matmul(X, self.weights)\r\n",
" \r\n",
" def rmse(self, X, y):\r\n",
" y = torch.from_numpy(y)\r\n",
" y_hat = self.predict(X)\r\n",
" \r\n",
" if y_hat is None:\r\n",
" return\r\n",
" \r\n",
" return torch.sqrt(torch.mean(torch.square(y_hat - y)))"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"With the Linear Regression model defined with fitting and predicting, let's map and plot the current dataset"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 8,
"source": [
"import mplfinance as mpf\r\n",
"\r\n",
"data = data.set_index('time')\r\n",
"mpf.plot(data, type='line', title=pair)"
],
"outputs": [
{
"output_type": "error",
"ename": "ModuleNotFoundError",
"evalue": "No module named 'mplfinance'",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-8-fdf674f39b0e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mmplfinance\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mmpf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'time'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mmpf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'line'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtitle\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpair\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'mplfinance'"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"from sklearn.linear_model import LinearRegression\r\n",
"\r\n",
"df_data = pd.DataFrame(rates)\r\n",
"df_x = df_data.drop(columns='close')\r\n",
"df_y = df_data['close']\r\n",
"x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.2, random_state=0)\r\n",
"model = LinearRegression()\r\n",
"model.fit(x_train, y_train)\r\n",
"y_pre = model.predict(x_test)\r\n",
"\r\n",
"import matplotlib.pyplot as plt\r\n",
"\r\n",
"\r\n",
"plt.figure(num = 3, figsize=(10, 5))\r\n",
"y_test_array = np.array(y_test)\r\n",
"plt.plot(y_test_array)\r\n",
"plt.plot(y_pre)\r\n",
"plt.show()"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"The Metatrader 5 seems to not be able to handle order management very well. This includes current order sent."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 12,
"source": [
"# Login\r\n",
"import config\r\n",
"client = mt5.login(config.ACCOUNT, password=config.PASSWORD, server=config.SERVER)\r\n",
"positions = mt5.positions_get(symbol=\"GBPUSD\")\r\n",
"positions"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(TradePosition(ticket=226046089, time=1632416475, time_msc=1632416475155, time_update=1632416475, time_update_msc=1632416475155, type=0, magic=20210922, identifier=226046089, reason=3, volume=0.01, price_open=1.37374, sl=0.0, tp=1.37533, price_current=1.3719000000000001, swap=-0.04, profit=-1.8399999999999999, symbol='GBPUSD', comment='my last shot', external_id=''),\n",
" TradePosition(ticket=226046148, time=1632416502, time_msc=1632416502449, time_update=1632416502, time_update_msc=1632416502449, type=0, magic=20210922, identifier=226046148, reason=3, volume=0.01, price_open=1.3738299999999999, sl=0.0, tp=1.37541, price_current=1.3719000000000001, swap=-0.04, profit=-1.9300000000000002, symbol='GBPUSD', comment='my last shot', external_id=''))"
]
},
"metadata": {},
"execution_count": 12
}
],
"metadata": {}
}
],
"metadata": {
"orig_nbformat": 4,
"language_info": {
"name": "python",
"version": "3.9.4",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.9.4 64-bit"
},
"interpreter": {
"hash": "63fd5069d213b44bf678585dea6b12cceca9941eaf7f819626cde1f2670de90d"
}
},
"nbformat": 4,
"nbformat_minor": 2
}