├── .gitignore ├── .ipynb_checkpoints └── ES_TA_Capítulo_09_MT5_Trading_en_Vivo_SMA-checkpoint.ipynb ├── CRYPTO H1 ├── BTCUSD_H1.csv └── ETHUSD_H1.csv ├── Capitulo_09_MT5.py ├── ES_TA_Capítulo_01_Los_fundamentos_de_Python.ipynb ├── ES_TA_Capítulo_02_Python_para_Data_Science.ipynb ├── ES_TA_Capítulo_03_Pre_Procesado_de_Datos.ipynb ├── ES_TA_Capítulo_04_Crea_tu_primera_estrategia_de_trading.ipynb ├── ES_TA_Capítulo_05_Backtesting_Vectorizado.ipynb ├── ES_TA_Capítulo_06_Scalping_usando_la_estrategia_de_price_action.ipynb ├── ES_TA_Capítulo_07_Ingeniería_de_Características.ipynb ├── ES_TA_Capítulo_08_Regresión_Lineal_para_predecir_los_precios_de_las_acciones.ipynb ├── ES_TA_Capítulo_08_bis_Aplicación.ipynb ├── ES_TA_Capítulo_09_MT5_Trading_en_Vivo_Random.ipynb ├── ES_TA_Capítulo_09_MT5_Trading_en_Vivo_SMA.ipynb ├── FOREX D1 ├── AUDCAD_D1.csv ├── AUDUSD_D1.csv ├── EURGBP_D1.csv ├── EURUSD_D1.csv ├── GBPUSD_D1.csv ├── USDCAD_D1.csv ├── USDCHF_D1.csv ├── USDCNH_D1.csv └── USDJPY_D1.csv ├── FOREX M1 ├── AUDCAD_M1.csv ├── EURUSD_M1.csv ├── GBPUSD_M1.csv ├── USDCAD_M1.csv ├── USDCHF_M1.csv └── USDJPY_M1.csv ├── README.md └── assets.csv /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | .ipynb_checkpoints/ES_TA_Capítulo_09_MT5_Trading_en_Vivo_SMA -checkpoint.ipynb 4 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/ES_TA_Capítulo_09_MT5_Trading_en_Vivo_SMA-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "id": "dbea0502", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Collecting numpy==1.19.2\n", 14 | " Downloading numpy-1.19.2-cp38-cp38-macosx_10_9_x86_64.whl (15.3 MB)\n", 15 | "\u001b[K |████████████████████████████████| 15.3 MB 23.6 MB/s eta 0:00:01\n", 16 | "\u001b[?25hInstalling collected packages: numpy\n", 17 | "\u001b[33m WARNING: The scripts f2py, f2py3 and f2py3.8 are installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 18 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 19 | "Successfully installed numpy-1.19.2\n", 20 | "Requirement already satisfied: scipy==1.6.2 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (1.6.2)\n", 21 | "Requirement already satisfied: numpy<1.23.0,>=1.16.5 in /Users/juangabriel/.local/lib/python3.8/site-packages (from scipy==1.6.2) (1.19.2)\n", 22 | "Collecting ta\n", 23 | " Downloading ta-0.10.1.tar.gz (24 kB)\n", 24 | "Requirement already satisfied: numpy in /Users/juangabriel/.local/lib/python3.8/site-packages (from ta) (1.19.2)\n", 25 | "Requirement already satisfied: pandas in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from ta) (1.2.4)\n", 26 | "Requirement already satisfied: python-dateutil>=2.7.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas->ta) (2.8.1)\n", 27 | "Requirement already satisfied: pytz>=2017.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas->ta) (2021.1)\n", 28 | "Requirement already satisfied: six>=1.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas->ta) (1.15.0)\n", 29 | "Building wheels for collected packages: ta\n", 30 | " Building wheel for ta (setup.py) ... \u001b[?25ldone\n", 31 | "\u001b[?25h Created wheel for ta: filename=ta-0.10.1-py3-none-any.whl size=28986 sha256=19fa3fed51f7d63585b496cb389f80668b180259336e597d8daea515771e391c\n", 32 | " Stored in directory: /Users/juangabriel/Library/Caches/pip/wheels/18/9a/81/694fa8602da445fa009fd13c8da25001be19efdfb67a9cc348\n", 33 | "Successfully built ta\n", 34 | "Installing collected packages: ta\n", 35 | "Successfully installed ta-0.10.1\n", 36 | "Collecting datetime\n", 37 | " Downloading DateTime-4.4-py2.py3-none-any.whl (51 kB)\n", 38 | "\u001b[K |████████████████████████████████| 51 kB 1.1 MB/s eta 0:00:01\n", 39 | "\u001b[?25hRequirement already satisfied: pytz in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from datetime) (2021.1)\n", 40 | "Requirement already satisfied: zope.interface in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from datetime) (5.3.0)\n", 41 | "Requirement already satisfied: setuptools in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from zope.interface->datetime) (52.0.0.post20210125)\n", 42 | "Installing collected packages: datetime\n", 43 | "Successfully installed datetime-4.4\n", 44 | "Collecting yfinance\n", 45 | " Downloading yfinance-0.1.72-py2.py3-none-any.whl (27 kB)\n", 46 | "Collecting requests>=2.26\n", 47 | " Downloading requests-2.28.0-py3-none-any.whl (62 kB)\n", 48 | "\u001b[K |████████████████████████████████| 62 kB 2.0 MB/s eta 0:00:011\n", 49 | "\u001b[?25hRequirement already satisfied: lxml>=4.5.1 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from yfinance) (4.6.3)\n", 50 | "Requirement already satisfied: numpy>=1.15 in /Users/juangabriel/.local/lib/python3.8/site-packages (from yfinance) (1.19.2)\n", 51 | "Requirement already satisfied: pandas>=0.24.0 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from yfinance) (1.2.4)\n", 52 | "Collecting multitasking>=0.0.7\n", 53 | " Downloading multitasking-0.0.10.tar.gz (8.2 kB)\n", 54 | "Requirement already satisfied: python-dateutil>=2.7.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas>=0.24.0->yfinance) (2.8.1)\n", 55 | "Requirement already satisfied: pytz>=2017.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas>=0.24.0->yfinance) (2021.1)\n", 56 | "Requirement already satisfied: six>=1.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.15.0)\n", 57 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (1.22)\n", 58 | "Requirement already satisfied: idna<4,>=2.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2.6)\n", 59 | "Requirement already satisfied: certifi>=2017.4.17 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2020.12.5)\n", 60 | "Collecting charset-normalizer~=2.0.0\n", 61 | " Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB)\n", 62 | "Building wheels for collected packages: multitasking\n", 63 | " Building wheel for multitasking (setup.py) ... \u001b[?25ldone\n", 64 | "\u001b[?25h Created wheel for multitasking: filename=multitasking-0.0.10-py3-none-any.whl size=8488 sha256=464b015e696019c4bbeb2915e0281594b248900ca08545b6be77a7fe15d58b1d\n", 65 | " Stored in directory: /Users/juangabriel/Library/Caches/pip/wheels/21/c9/66/b41c847de65c7985db52ec21d59996841598b8b0e93f2b9500\n", 66 | "Successfully built multitasking\n", 67 | "Installing collected packages: charset-normalizer, requests, multitasking, yfinance\n", 68 | "\u001b[33m WARNING: The script normalizer is installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 69 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 70 | "\u001b[33m WARNING: The script sample is installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 71 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 72 | "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", 73 | "conda-repo-cli 1.0.4 requires pathlib, which is not installed.\n", 74 | "anaconda-project 0.9.1 requires ruamel-yaml, which is not installed.\n", 75 | "sphinx 4.0.1 requires Jinja2<3.0,>=2.3, but you have jinja2 3.0.1 which is incompatible.\n", 76 | "sphinx 4.0.1 requires MarkupSafe<2.0, but you have markupsafe 2.0.1 which is incompatible.\u001b[0m\n", 77 | "Successfully installed charset-normalizer-2.0.12 multitasking-0.0.10 requests-2.28.0 yfinance-0.1.72\n", 78 | "\u001b[31mERROR: Could not find a version that satisfies the requirement MetaTrader5--user\u001b[0m\n", 79 | "\u001b[31mERROR: No matching distribution found for MetaTrader5--user\u001b[0m\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "import sys\n", 85 | "\n", 86 | "!{sys.executable} -m pip install numpy==1.19.2 --user\n", 87 | "!{sys.executable} -m pip install scipy==1.6.2 --user\n", 88 | "!{sys.executable} -m pip install ta --user\n", 89 | "!{sys.executable} -m pip install datetime --user\n", 90 | "!{sys.executable} -m pip install yfinance --user\n", 91 | "!{sys.executable} -m pip install MetaTrader5 --user" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 12, 97 | "id": "021d25d0", 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "------------------------------------------------------------------\n", 105 | "Date: 2022-01-14 08:33:32\n", 106 | "Balance: 10000.0 USD, \tEquity: 9991.28 USD, \tProfit: -8.72 USD\n", 107 | "------------------------------------------------------------------\n", 108 | "------------------------------------------------------------------\n", 109 | "Date: 2022-01-14 08:33:32\n", 110 | "SYMBOL: BTCUSD\n", 111 | "BUY: False \t SHORT: False\n", 112 | "POSITION: None \t ID: None\n", 113 | "------------------------------------------------------------------\n", 114 | "------------------------------------------------------------------\n", 115 | "Date: 2022-01-14 08:33:32\n", 116 | "SYMBOL: XMRUSD\n", 117 | "BUY: False \t SHORT: False\n", 118 | "POSITION: None \t ID: None\n", 119 | "------------------------------------------------------------------\n", 120 | "------------------------------------------------------------------\n", 121 | "Date: 2022-01-14 08:33:32\n", 122 | "SYMBOL: ETHUSD\n", 123 | "BUY: False \t SHORT: False\n", 124 | "POSITION: None \t ID: None\n", 125 | "------------------------------------------------------------------\n", 126 | "------------------------------------------------------------------\n", 127 | "Date: 2022-01-14 08:33:32\n", 128 | "SYMBOL: ETCUSD\n", 129 | "BUY: False \t SHORT: False\n", 130 | "POSITION: None \t ID: None\n", 131 | "------------------------------------------------------------------\n", 132 | "------------------------------------------------------------------\n", 133 | "Date: 2022-01-14 08:33:32\n", 134 | "SYMBOL: ZECUSD\n", 135 | "BUY: False \t SHORT: False\n", 136 | "POSITION: None \t ID: None\n", 137 | "------------------------------------------------------------------\n", 138 | "------------------------------------------------------------------\n", 139 | "Date: 2022-01-14 08:33:32\n", 140 | "SYMBOL: BTCUSD\n", 141 | "BUY: False \t SHORT: False\n", 142 | "POSITION: None \t ID: None\n", 143 | "------------------------------------------------------------------\n", 144 | "------------------------------------------------------------------\n", 145 | "Date: 2022-01-14 08:33:32\n", 146 | "SYMBOL: XMRUSD\n", 147 | "BUY: False \t SHORT: False\n", 148 | "POSITION: None \t ID: None\n", 149 | "------------------------------------------------------------------\n", 150 | "------------------------------------------------------------------\n", 151 | "Date: 2022-01-14 08:33:32\n", 152 | "SYMBOL: ETHUSD\n", 153 | "BUY: False \t SHORT: False\n", 154 | "POSITION: None \t ID: None\n", 155 | "------------------------------------------------------------------\n", 156 | "------------------------------------------------------------------\n", 157 | "Date: 2022-01-14 08:33:32\n", 158 | "SYMBOL: ETCUSD\n", 159 | "BUY: False \t SHORT: False\n", 160 | "POSITION: None \t ID: None\n", 161 | "------------------------------------------------------------------\n", 162 | "------------------------------------------------------------------\n", 163 | "Date: 2022-01-14 08:33:32\n", 164 | "SYMBOL: ZECUSD\n", 165 | "BUY: False \t SHORT: False\n", 166 | "POSITION: None \t ID: None\n", 167 | "------------------------------------------------------------------\n", 168 | "------------------------------------------------------------------\n", 169 | "Date: 2022-01-14 08:33:32\n", 170 | "SYMBOL: BTCUSD\n", 171 | "BUY: False \t SHORT: False\n", 172 | "POSITION: None \t ID: None\n", 173 | "------------------------------------------------------------------\n", 174 | "------------------------------------------------------------------\n", 175 | "Date: 2022-01-14 08:33:32\n", 176 | "SYMBOL: XMRUSD\n", 177 | "BUY: False \t SHORT: False\n", 178 | "POSITION: None \t ID: None\n", 179 | "------------------------------------------------------------------\n", 180 | "------------------------------------------------------------------\n", 181 | "Date: 2022-01-14 08:33:32\n", 182 | "SYMBOL: ETHUSD\n", 183 | "BUY: False \t SHORT: False\n", 184 | "POSITION: None \t ID: None\n", 185 | "------------------------------------------------------------------\n", 186 | "------------------------------------------------------------------\n", 187 | "Date: 2022-01-14 08:33:32\n", 188 | "SYMBOL: ETCUSD\n", 189 | "BUY: False \t SHORT: False\n", 190 | "POSITION: None \t ID: None\n", 191 | "------------------------------------------------------------------\n", 192 | "------------------------------------------------------------------\n", 193 | "Date: 2022-01-14 08:33:32\n", 194 | "SYMBOL: ZECUSD\n", 195 | "BUY: False \t SHORT: False\n", 196 | "POSITION: None \t ID: None\n", 197 | "------------------------------------------------------------------\n", 198 | "------------------------------------------------------------------\n", 199 | "Date: 2022-01-14 08:33:32\n", 200 | "SYMBOL: BTCUSD\n", 201 | "BUY: False \t SHORT: False\n", 202 | "POSITION: None \t ID: None\n", 203 | "------------------------------------------------------------------\n", 204 | "------------------------------------------------------------------\n", 205 | "Date: 2022-01-14 08:33:32\n", 206 | "SYMBOL: XMRUSD\n", 207 | "BUY: False \t SHORT: False\n", 208 | "POSITION: None \t ID: None\n", 209 | "------------------------------------------------------------------\n", 210 | "------------------------------------------------------------------\n", 211 | "Date: 2022-01-14 08:33:32\n", 212 | "SYMBOL: ETHUSD\n", 213 | "BUY: False \t SHORT: False\n", 214 | "POSITION: None \t ID: None\n", 215 | "------------------------------------------------------------------\n", 216 | "------------------------------------------------------------------\n", 217 | "Date: 2022-01-14 08:33:33\n", 218 | "SYMBOL: ETCUSD\n", 219 | "BUY: False \t SHORT: False\n", 220 | "POSITION: None \t ID: None\n", 221 | "------------------------------------------------------------------\n", 222 | "------------------------------------------------------------------\n", 223 | "Date: 2022-01-14 08:33:33\n", 224 | "SYMBOL: ZECUSD\n", 225 | "BUY: False \t SHORT: False\n", 226 | "POSITION: None \t ID: None\n", 227 | "------------------------------------------------------------------\n" 228 | ] 229 | }, 230 | { 231 | "ename": "KeyboardInterrupt", 232 | "evalue": "", 233 | "output_type": "error", 234 | "traceback": [ 235 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 236 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 237 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_5916/4152256203.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[1;31m# Verfication for launch\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 73\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mweekday\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# If you want to trade only from Monday to Friday\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 74\u001b[1;33m \u001b[0mis_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrftime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%H:%M:%S\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mstart\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 75\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[0mis_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 238 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: " 239 | ] 240 | } 241 | ], 242 | "source": [ 243 | "import ta\n", 244 | "import numpy as np\n", 245 | "import warnings\n", 246 | "from datetime import datetime\n", 247 | "import pandas as pd\n", 248 | "import MetaTrader5 as mt5\n", 249 | "warnings.filterwarnings(\"ignore\")\n", 250 | "mt5.initialize()\n", 251 | "from Chapter_09_MT5 import *\n", 252 | "import time\n", 253 | "\n", 254 | "\n", 255 | "\n", 256 | "\n", 257 | "\n", 258 | "def SMA_strategy(symbol):\n", 259 | " \n", 260 | " # Import / Features engineering\n", 261 | " df = MT5.get_data(symbol, 70)\n", 262 | " \n", 263 | " # Creamos la Resistencia haciendo un rolling máximo\n", 264 | " df[\"SMA fast\"] = df[\"close\"].rolling(30).mean()\n", 265 | "\n", 266 | " # Creamos el Soporte haciendo un rolling mínimo\n", 267 | " df[\"SMA slow\"] = df[\"close\"].rolling(60).mean()\n", 268 | "\n", 269 | "\n", 270 | " # Señales\n", 271 | " condition_1_buy = df[\"SMA fast\"].iloc[-1] > df[\"SMA slow\"].iloc[-1]\n", 272 | " \n", 273 | " condition_1_sell = df[\"SMA fast\"].iloc[-1] < df[\"SMA slow\"].iloc[-1]\n", 274 | " \n", 275 | " buy = condition_1_buy\n", 276 | " sell = condition_1_sell\n", 277 | " \n", 278 | "\n", 279 | " return buy, sell\n", 280 | "\n", 281 | "\n", 282 | "mt5.initialize()\n", 283 | "# True = Live Trading and False = Screener\n", 284 | "live = True\n", 285 | "\n", 286 | "if live:\n", 287 | " current_account_info = mt5.account_info()\n", 288 | " print(\"------------------------------------------------------------------\")\n", 289 | " print(\"Date: \", datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))\n", 290 | " print(f\"Balance: {current_account_info.balance} USD, \\t\"\n", 291 | " f\"Equity: {current_account_info.equity} USD, \\t\"\n", 292 | " f\"Profit: {current_account_info.profit} USD\")\n", 293 | " print(\"------------------------------------------------------------------\")\n", 294 | "\n", 295 | "\n", 296 | "info_order = {\n", 297 | " \"Bitcoin\": [\"EURUSD\", 1.]\n", 298 | " \n", 299 | "}\n", 300 | "\n", 301 | "\n", 302 | "start = datetime.now().strftime(\"%H:%M:%S\") #\"23:59:59\"\n", 303 | "while True:\n", 304 | "\n", 305 | " # Verificación para lanzar el algoritmo\n", 306 | " if datetime.now().weekday() not in (5,6): # Para hacer trading solamente de Lunes a Viernes\n", 307 | " is_time = datetime.now().strftime(\"%H:%M:%S\") == start \n", 308 | " else:\n", 309 | " is_time = False\n", 310 | "\n", 311 | " \n", 312 | " # Lanzamos el algoritmo\n", 313 | " if is_time:\n", 314 | "\n", 315 | " # Abrimos el trading\n", 316 | " for asset in info_order.keys():\n", 317 | "\n", 318 | " # Inicializamos la entrada de datos\n", 319 | " symbol = info_order[asset][0]\n", 320 | " lot = info_order[asset][1]\n", 321 | "\n", 322 | " # Creamos la señal\n", 323 | " buy, sell = SMA_strategy(symbol)\n", 324 | "\n", 325 | " # Ejecutamos el algoritmo\n", 326 | " if live:\n", 327 | " MT5.run(symbol, buy, sell,lot)\n", 328 | "\n", 329 | " else:\n", 330 | " print(f\"Symbol: {symbol}\\t\"\n", 331 | " f\"Buy: {buy}\\t\"\n", 332 | " f\"Sell: {sell}\")\n" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "id": "36b23b8b", 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [] 342 | } 343 | ], 344 | "metadata": { 345 | "kernelspec": { 346 | "display_name": "Python 3", 347 | "language": "python", 348 | "name": "python3" 349 | }, 350 | "language_info": { 351 | "codemirror_mode": { 352 | "name": "ipython", 353 | "version": 3 354 | }, 355 | "file_extension": ".py", 356 | "mimetype": "text/x-python", 357 | "name": "python", 358 | "nbconvert_exporter": "python", 359 | "pygments_lexer": "ipython3", 360 | "version": "3.8.8" 361 | } 362 | }, 363 | "nbformat": 4, 364 | "nbformat_minor": 5 365 | } 366 | -------------------------------------------------------------------------------- /Capitulo_09_MT5.py: -------------------------------------------------------------------------------- 1 | import warnings 2 | from datetime import datetime 3 | import pandas as pd 4 | import MetaTrader5 as mt5 5 | warnings.filterwarnings("ignore") 6 | mt5.initialize() 7 | 8 | 9 | class MT5: 10 | 11 | def get_data(symbol, n, timeframe=mt5.TIMEFRAME_D1): 12 | """ Función para importar los datos del símbolo elegido""" 13 | 14 | # Inicializamos la conexión si no estaba activa 15 | mt5.initialize() 16 | 17 | # Current date extract 18 | utc_from = datetime.now() 19 | 20 | # Import the data into a tuple 21 | rates = mt5.copy_rates_from(symbol, timeframe, utc_from, n) 22 | 23 | # Tuple to dataframe 24 | rates_frame = pd.DataFrame(rates) 25 | 26 | # Convert time in seconds into the datetime format 27 | rates_frame['time'] = pd.to_datetime(rates_frame['time'], unit='s') 28 | 29 | # Convert the column "time" in the right format 30 | rates_frame['time'] = pd.to_datetime(rates_frame['time'], format='%Y-%m-%d') 31 | 32 | # Set column time as the index of the dataframe 33 | rates_frame = rates_frame.set_index('time') 34 | return rates_frame 35 | 36 | def orders(symbol, lot, buy=True, id_position=None): 37 | """ Enviamos las órdenes """ 38 | 39 | # Inicializamos la conexión si no estaba activa 40 | if mt5.initialize() == False: 41 | mt5.initialize() 42 | 43 | # Get filling mode 44 | filling_mode = mt5.symbol_info(symbol).filling_mode - 1 45 | 46 | # Take ask price 47 | ask_price = mt5.symbol_info_tick(symbol).ask 48 | 49 | # Take bid price 50 | bid_price = mt5.symbol_info_tick(symbol).bid 51 | 52 | # Take the point of the asset 53 | point = mt5.symbol_info(symbol).point 54 | 55 | deviation = 20 # mt5.getSlippage(symbol) 56 | # **************************** Open a trade ***************************** 57 | if id_position == None: 58 | 59 | # Buy order Parameters 60 | if buy: 61 | type_trade = mt5.ORDER_TYPE_BUY 62 | sl = ask_price*(1-0.01) 63 | tp = ask_price*(1+0.01) 64 | price = ask_price 65 | 66 | # Sell order Parameters 67 | else: 68 | type_trade = mt5.ORDER_TYPE_SELL 69 | sl = bid_price*(1+0.01) 70 | tp = bid_price*(1-0.01) 71 | price = bid_price 72 | 73 | # Open the trade 74 | request = { 75 | "action": mt5.TRADE_ACTION_DEAL, 76 | "symbol": symbol, 77 | "volume": lot, 78 | "type": type_trade, 79 | "price": price, 80 | "deviation": deviation, 81 | "sl": sl, 82 | "tp": tp, 83 | "magic": 234000, 84 | "comment": "python script order", 85 | "type_time": mt5.ORDER_TIME_GTC, 86 | "type_filling": filling_mode, 87 | } 88 | # send a trading request 89 | result = mt5.order_send(request) 90 | result_comment = result.comment 91 | 92 | # **************************** Close a trade ***************************** 93 | else: 94 | # Buy order Parameters 95 | if buy: 96 | type_trade = mt5.ORDER_TYPE_SELL 97 | price = bid_price 98 | 99 | # Sell order Parameters 100 | else: 101 | type_trade = mt5.ORDER_TYPE_BUY 102 | price = ask_price 103 | 104 | # Close the trade 105 | request = { 106 | "action": mt5.TRADE_ACTION_DEAL, 107 | "symbol": symbol, 108 | "volume": lot, 109 | "type": type_trade, 110 | "position": id_position, 111 | "price": price, 112 | "deviation": deviation, 113 | "magic": 234000, 114 | "comment": "python script order", 115 | "type_time": mt5.ORDER_TIME_GTC, 116 | "type_filling": filling_mode, 117 | } 118 | 119 | # send a trading request 120 | result = mt5.order_send(request) 121 | result_comment = result.comment 122 | return result.comment 123 | 124 | def resume(): 125 | """ Return the current positions. Position=0 --> Buy """ 126 | # Initialize the connection if there is not 127 | mt5.initialize() 128 | 129 | # Define the name of the columns that we will create 130 | columns = ["ticket", "position", "symbol", "volume"] 131 | 132 | # Go take the current open trades 133 | current = mt5.positions_get() 134 | 135 | # Create a empty dataframe 136 | summary = pd.DataFrame() 137 | 138 | # Loop to add each row in dataframe 139 | # (Can be ameliorate using of list of list) 140 | for element in current: 141 | element_pandas = pd.DataFrame([element.ticket, 142 | element.type, 143 | element.symbol, 144 | element.volume], 145 | index=columns).transpose() 146 | summary = pd.concat((summary, element_pandas), axis=0) 147 | 148 | return summary 149 | 150 | 151 | def run(symbol, long, short, lot): 152 | 153 | # Inicializamos la conexión si no estaba activa 154 | if mt5.initialize() == False: 155 | mt5.initialize() 156 | 157 | # Choose your symbol 158 | print("------------------------------------------------------------------") 159 | print("Date: ", datetime.now().strftime("%Y-%m-%d %H:%M:%S")) 160 | print("SYMBOL:", symbol) 161 | 162 | # Initialize the device 163 | current_open_positions = MT5.resume() 164 | # Buy or sell 165 | print(f"BUY: {long} \t SHORT: {short}") 166 | 167 | """ Close trade eventually """ 168 | # Extraction type trade 169 | try: 170 | position = current_open_positions.loc[current_open_positions["symbol"]==symbol].values[0][1] 171 | 172 | identifier = current_open_positions.loc[current_open_positions["symbol"]==symbol].values[0][0] 173 | except: 174 | position= None 175 | identifier = None 176 | 177 | print(f"POSITION: {position} \t ID: {identifier}") 178 | 179 | # Close trades 180 | if long==True and position==0: 181 | long=False 182 | 183 | elif long==False and position==0: 184 | res = MT5.orders(symbol, lot, buy=True, id_position=identifier) 185 | print(f"CLOSE LONG TRADE: {res}") 186 | 187 | elif short==True and position ==1: 188 | short=False 189 | 190 | elif short == False and position == 1: 191 | res = MT5.orders(symbol, lot, buy=False, id_position=identifier) 192 | print(f"CLOSE SHORT TRADE: {res}") 193 | 194 | else: 195 | pass 196 | 197 | 198 | """ Buy or short """ 199 | if long==True: 200 | 201 | res = MT5.orders(symbol, lot, buy=True, id_position=None) 202 | print(f"OPEN LONG TRADE: {res}") 203 | 204 | if short==True: 205 | res = MT5.orders(symbol, lot, buy=False, id_position=None) 206 | print(f"OPEN SHORT TRADE: {res}") 207 | 208 | print("------------------------------------------------------------------") 209 | 210 | def close_all_night(): 211 | result = MT5.resume() 212 | for i in range(len(result)): 213 | before = mt5.account_info().balance 214 | row = result.iloc[0+i:1+i,:] 215 | if row["position"][0]==0: 216 | res = MT5.orders(row["symbol"][0], row["volume"][0], buy=True, id_position=row["ticket"][0]) 217 | 218 | else: 219 | res = MT5.orders(row["symbol"][0], row["volume"][0], buy=False, id_position=row["ticket"][0]) -------------------------------------------------------------------------------- /ES_TA_Capítulo_01_Los_fundamentos_de_Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "ES-TA Capítulo 01: Los fundamentos de Python.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [ 9 | "Kd-SYHI-ZF85", 10 | "2UHCKDk8bJEf", 11 | "W9Vc62veaNKg", 12 | "7xKs11J6akHQ", 13 | "wwObVh1w9Wl9", 14 | "-9ElsbKP-xvK", 15 | "emrr2xJjhmKd", 16 | "S0TnQtBMfRXB", 17 | "F8rax_dDgnFI", 18 | "4NWfx4mRhpgX" 19 | ], 20 | "toc_visible": true, 21 | "include_colab_link": true 22 | }, 23 | "kernelspec": { 24 | "name": "python3", 25 | "display_name": "Python 3" 26 | } 27 | }, 28 | "cells": [ 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "id": "view-in-github", 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "\"Open" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "id": "HwIk5lNgltIS" 43 | }, 44 | "source": [ 45 | "

\"Colaboratory\n", 46 | "\"Colaboratory

\n", 47 | "\n", 48 | "# PRERREQUISITOS - **Las Bases de Python**\n", 49 | "\n", 50 | "Este capítulo te dará los fundamentos de python que necesitas para seguir el resto del curso. Estos son sólo recordatorios de lo que será útil. Por eso te aconsejo que sigas una formación rápida de Python para tus proyectos, porque si eres totalmente ajeno a él, puede ser difícil hacer tu proyecto. Nosotros te recomendamos el curso de más de 50 horas de programación, [Python de la A a la Z que puedes obtener desde nuestra web de frogames](https://cursos.frogamesformacion.com/courses/python-az/).\n", 51 | "\n", 52 | "\n", 53 | "### Parte 1: Los fundamentos de Python\n", 54 | "* Tipos de objetos:\n", 55 | "> * Números, Strings y Booleanos\n", 56 | "> * Tuplas y Listas\n", 57 | "> * Asignación de Variables\n", 58 | "* Estructuras de Python:\n", 59 | "> * If/Elif/Else\n", 60 | "> * For\n", 61 | "> * While\n", 62 | "* Las funciones\n", 63 | "> * Base de las funciones\n", 64 | "> * Variable local\n", 65 | "> * Variable global\n", 66 | "> * Función lambda\n", 67 | "\n", 68 | "\n", 69 | "\n", 70 | "💰 Únete a la comunidad de [Discord](https://discord.gg/z3dx5XpkX4)\n", 71 | "\n", 72 | "📚 Puedes leer nuestro libro en [Amazon](https://www.amazon.es/Python-para-finanzas-trading-algor%C3%ADtmico-ebook/dp/B0BT4ZS9Q3/)\n", 73 | "\n", 74 | "🖥️ El canal de [YouTube de Quantreo's](https://www.youtube.com/channel/UCp7jckfiEglNf_Gj62VR0pw) (en inglés) y el de [Frogames](https://www.youtube.com/channel/UCMUxXNYrVCv6-bQakhomvBg) en español\n", 75 | "\n", 76 | "\n", 77 | "\n", 78 | "\n" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": { 84 | "id": "nJYyIbZ_WbFT" 85 | }, 86 | "source": [ 87 | "### Tipos de Objetos" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": { 93 | "id": "DuY3oXKNWd1h" 94 | }, 95 | "source": [ 96 | "##### Números" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "metadata": { 102 | "id": "yoYnGbme8kcq", 103 | "colab": { 104 | "base_uri": "https://localhost:8080/" 105 | }, 106 | "outputId": "437cc0f9-8fe7-48d2-eed3-2f291f12b80d" 107 | }, 108 | "source": [ 109 | "# Suma\n", 110 | "6 + 3" 111 | ], 112 | "execution_count": null, 113 | "outputs": [ 114 | { 115 | "output_type": "execute_result", 116 | "data": { 117 | "text/plain": [ 118 | "9" 119 | ] 120 | }, 121 | "metadata": {}, 122 | "execution_count": 2 123 | } 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "metadata": { 129 | "id": "lPQ-ZedRCtL0", 130 | "colab": { 131 | "base_uri": "https://localhost:8080/" 132 | }, 133 | "outputId": "c048434e-1297-4fc4-af46-2e3c6c60068f" 134 | }, 135 | "source": [ 136 | "# Resta\n", 137 | "6 - 3" 138 | ], 139 | "execution_count": null, 140 | "outputs": [ 141 | { 142 | "output_type": "execute_result", 143 | "data": { 144 | "text/plain": [ 145 | "3" 146 | ] 147 | }, 148 | "metadata": {}, 149 | "execution_count": 3 150 | } 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "metadata": { 156 | "id": "FSWWYIqBCwlg", 157 | "colab": { 158 | "base_uri": "https://localhost:8080/" 159 | }, 160 | "outputId": "7a563f0c-e5ff-4e7c-ab34-43e71e9f4868" 161 | }, 162 | "source": [ 163 | "# Multiplicación\n", 164 | "6 * 3" 165 | ], 166 | "execution_count": null, 167 | "outputs": [ 168 | { 169 | "output_type": "execute_result", 170 | "data": { 171 | "text/plain": [ 172 | "18" 173 | ] 174 | }, 175 | "metadata": {}, 176 | "execution_count": 4 177 | } 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "metadata": { 183 | "id": "fMQNWXVvC0Sx", 184 | "colab": { 185 | "base_uri": "https://localhost:8080/" 186 | }, 187 | "outputId": "49d726ae-5b3c-4325-a41f-776ac22050e0" 188 | }, 189 | "source": [ 190 | "# División \n", 191 | "16 / 3" 192 | ], 193 | "execution_count": null, 194 | "outputs": [ 195 | { 196 | "output_type": "execute_result", 197 | "data": { 198 | "text/plain": [ 199 | "5.333333333333333" 200 | ] 201 | }, 202 | "metadata": {}, 203 | "execution_count": 5 204 | } 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "metadata": { 210 | "id": "DH1jRr5_Xxo2", 211 | "colab": { 212 | "base_uri": "https://localhost:8080/" 213 | }, 214 | "outputId": "73f6324b-0da9-4aac-ef28-e3c137926ef3" 215 | }, 216 | "source": [ 217 | "# Potencias 2*2*2\n", 218 | "2**3" 219 | ], 220 | "execution_count": null, 221 | "outputs": [ 222 | { 223 | "output_type": "execute_result", 224 | "data": { 225 | "text/plain": [ 226 | "8" 227 | ] 228 | }, 229 | "metadata": {}, 230 | "execution_count": 8 231 | } 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "metadata": { 237 | "id": "abqfE8I2C2cr", 238 | "colab": { 239 | "base_uri": "https://localhost:8080/" 240 | }, 241 | "outputId": "72e08e7e-5327-428a-baef-4f543e1d02be" 242 | }, 243 | "source": [ 244 | "# División entera (euclídea) | 5.33 --> 5\n", 245 | "16 // 3" 246 | ], 247 | "execution_count": null, 248 | "outputs": [ 249 | { 250 | "output_type": "execute_result", 251 | "data": { 252 | "text/plain": [ 253 | "5" 254 | ] 255 | }, 256 | "metadata": {}, 257 | "execution_count": 9 258 | } 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "metadata": { 264 | "id": "SnktoxGAC_ew", 265 | "colab": { 266 | "base_uri": "https://localhost:8080/" 267 | }, 268 | "outputId": "69f73e5f-62a7-4ee5-d5fb-aedcf4e86af6" 269 | }, 270 | "source": [ 271 | "# Módulo | 16 = 5*3 + 1 \n", 272 | "16 % 3" 273 | ], 274 | "execution_count": null, 275 | "outputs": [ 276 | { 277 | "output_type": "execute_result", 278 | "data": { 279 | "text/plain": [ 280 | "1" 281 | ] 282 | }, 283 | "metadata": {}, 284 | "execution_count": 10 285 | } 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": { 291 | "id": "iEQQbsVFWkKi" 292 | }, 293 | "source": [ 294 | "##### String" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "metadata": { 300 | "id": "8jOPFgexDF2H", 301 | "colab": { 302 | "base_uri": "https://localhost:8080/", 303 | "height": 35 304 | }, 305 | "outputId": "b85f9965-8c17-410b-cf3a-31d544dbe048" 306 | }, 307 | "source": [ 308 | "# Simple String\n", 309 | "print(\"Soy una información\")" 310 | ], 311 | "execution_count": null, 312 | "outputs": [ 313 | { 314 | "output_type": "execute_result", 315 | "data": { 316 | "text/plain": [ 317 | "'Soy una información'" 318 | ], 319 | "application/vnd.google.colaboratory.intrinsic+json": { 320 | "type": "string" 321 | } 322 | }, 323 | "metadata": {}, 324 | "execution_count": 2 325 | } 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "metadata": { 331 | "colab": { 332 | "base_uri": "https://localhost:8080/" 333 | }, 334 | "id": "ibeFvaYOnCDd", 335 | "outputId": "b7fc4fde-fcc3-4c07-c2a4-f7b423ca28ba" 336 | }, 337 | "source": [ 338 | "# Añadir una tabulación\n", 339 | "print(\"Media:10% \\t Volatilidad:10%\")" 340 | ], 341 | "execution_count": null, 342 | "outputs": [ 343 | { 344 | "output_type": "stream", 345 | "name": "stdout", 346 | "text": [ 347 | "Media:10% \t Volatilidad:10%\n" 348 | ] 349 | } 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "metadata": { 355 | "colab": { 356 | "base_uri": "https://localhost:8080/" 357 | }, 358 | "id": "rGWkCu3vpDoW", 359 | "outputId": "156abeae-6884-45a8-deee-3a2c317f50a5" 360 | }, 361 | "source": [ 362 | "# Salto de línea\n", 363 | "print(\"Media:10% \\nVolatilidad:10%\")" 364 | ], 365 | "execution_count": null, 366 | "outputs": [ 367 | { 368 | "output_type": "stream", 369 | "name": "stdout", 370 | "text": [ 371 | "Media:10% \n", 372 | "Volatilidad:10%\n" 373 | ] 374 | } 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "metadata": { 380 | "id": "zLAVLu5rWv2a", 381 | "colab": { 382 | "base_uri": "https://localhost:8080/" 383 | }, 384 | "outputId": "1ba52c4b-7fa0-44b6-b559-7428edbeb48c" 385 | }, 386 | "source": [ 387 | "# String dinámico (primera opción: simple string using the format function)\n", 388 | "print(\"Precio de las acciones de Tesla: {}$ en la fecha {}\".format(110,\"01-01-2020\"))" 389 | ], 390 | "execution_count": null, 391 | "outputs": [ 392 | { 393 | "output_type": "stream", 394 | "name": "stdout", 395 | "text": [ 396 | "Precio de las acciones de Tesla: 110$ en la fecha 01-01-2020\n" 397 | ] 398 | } 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "metadata": { 404 | "id": "shGzNr67Wvx0", 405 | "colab": { 406 | "base_uri": "https://localhost:8080/" 407 | }, 408 | "outputId": "f8c2e1f3-877e-4cda-92a4-34290d4c1489" 409 | }, 410 | "source": [ 411 | "# String dinámico (segunda opción: f-string )\n", 412 | "print(f\"Precio de las acciones de Tesla: {110}$ en la fecha {'01-01-2010'}\")" 413 | ], 414 | "execution_count": null, 415 | "outputs": [ 416 | { 417 | "output_type": "stream", 418 | "name": "stdout", 419 | "text": [ 420 | "Precio de las acciones de Tesla: 110$ en la fecha 01-01-2010\n" 421 | ] 422 | } 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "metadata": { 428 | "colab": { 429 | "base_uri": "https://localhost:8080/" 430 | }, 431 | "id": "Uaz_tqJgpMae", 432 | "outputId": "d7c120c8-94e4-4b86-9a3e-75e62093c010" 433 | }, 434 | "source": [ 435 | "# Redondear un número con f-string\n", 436 | "print(f\"Precio de las acciones de Tesla: {'%.2f' % 110.15645147854}$\")" 437 | ], 438 | "execution_count": null, 439 | "outputs": [ 440 | { 441 | "output_type": "stream", 442 | "name": "stdout", 443 | "text": [ 444 | "Precio de las acciones de Tesla: 110.16$\n" 445 | ] 446 | } 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "metadata": { 452 | "id": "ud27MNZYdwSG", 453 | "colab": { 454 | "base_uri": "https://localhost:8080/", 455 | "height": 35 456 | }, 457 | "outputId": "6a81412a-0bca-47d0-943f-19f71cdaecad" 458 | }, 459 | "source": [ 460 | "# Slicing en strings\n", 461 | "\"Soy una información\"[0:6]" 462 | ], 463 | "execution_count": null, 464 | "outputs": [ 465 | { 466 | "output_type": "execute_result", 467 | "data": { 468 | "text/plain": [ 469 | "'Soy un'" 470 | ], 471 | "application/vnd.google.colaboratory.intrinsic+json": { 472 | "type": "string" 473 | } 474 | }, 475 | "metadata": {}, 476 | "execution_count": 20 477 | } 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": { 483 | "id": "JUULnsP_Xemv" 484 | }, 485 | "source": [ 486 | "##### Operadores booleanos / lógicos" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "metadata": { 492 | "id": "vM_KlXcMWvtr", 493 | "colab": { 494 | "base_uri": "https://localhost:8080/" 495 | }, 496 | "outputId": "0a313eb1-7dd2-4d3d-e7d0-d6fdbfa87f77" 497 | }, 498 | "source": [ 499 | "# Tipo Booleano (True)\n", 500 | "True" 501 | ], 502 | "execution_count": null, 503 | "outputs": [ 504 | { 505 | "output_type": "execute_result", 506 | "data": { 507 | "text/plain": [ 508 | "True" 509 | ] 510 | }, 511 | "metadata": {}, 512 | "execution_count": 21 513 | } 514 | ] 515 | }, 516 | { 517 | "cell_type": "code", 518 | "metadata": { 519 | "id": "53gTC6s5WvsF", 520 | "colab": { 521 | "base_uri": "https://localhost:8080/" 522 | }, 523 | "outputId": "ead7f7ef-4360-4066-b2ac-678935d6f3a9" 524 | }, 525 | "source": [ 526 | "# Tipo Booleano (False)\n", 527 | "False" 528 | ], 529 | "execution_count": null, 530 | "outputs": [ 531 | { 532 | "output_type": "execute_result", 533 | "data": { 534 | "text/plain": [ 535 | "False" 536 | ] 537 | }, 538 | "metadata": {}, 539 | "execution_count": 22 540 | } 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "metadata": { 546 | "id": "IoozzT2bdK_u", 547 | "colab": { 548 | "base_uri": "https://localhost:8080/" 549 | }, 550 | "outputId": "3bf66adb-979c-47bd-9788-7881cd5ad3d8" 551 | }, 552 | "source": [ 553 | "# Igualdad\n", 554 | "5 == 6" 555 | ], 556 | "execution_count": null, 557 | "outputs": [ 558 | { 559 | "output_type": "execute_result", 560 | "data": { 561 | "text/plain": [ 562 | "False" 563 | ] 564 | }, 565 | "metadata": {}, 566 | "execution_count": 23 567 | } 568 | ] 569 | }, 570 | { 571 | "cell_type": "code", 572 | "metadata": { 573 | "id": "_f1XD2w3dgY5", 574 | "colab": { 575 | "base_uri": "https://localhost:8080/" 576 | }, 577 | "outputId": "cc706dde-a4e8-4138-a41b-fe0d2d0a8ebe" 578 | }, 579 | "source": [ 580 | "# Sup\n", 581 | "5 > 6" 582 | ], 583 | "execution_count": null, 584 | "outputs": [ 585 | { 586 | "output_type": "execute_result", 587 | "data": { 588 | "text/plain": [ 589 | "False" 590 | ] 591 | }, 592 | "metadata": {}, 593 | "execution_count": 24 594 | } 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "metadata": { 600 | "id": "yZj4kBbLdkUo", 601 | "colab": { 602 | "base_uri": "https://localhost:8080/" 603 | }, 604 | "outputId": "b3d124fd-25f1-4749-9255-1071af1b3e1c" 605 | }, 606 | "source": [ 607 | "# Inf\n", 608 | "5 < 6" 609 | ], 610 | "execution_count": null, 611 | "outputs": [ 612 | { 613 | "output_type": "execute_result", 614 | "data": { 615 | "text/plain": [ 616 | "True" 617 | ] 618 | }, 619 | "metadata": {}, 620 | "execution_count": 25 621 | } 622 | ] 623 | }, 624 | { 625 | "cell_type": "code", 626 | "metadata": { 627 | "id": "BbEIqsW4ducB", 628 | "colab": { 629 | "base_uri": "https://localhost:8080/" 630 | }, 631 | "outputId": "b11aa784-f150-47bf-8008-e9603f3514d9" 632 | }, 633 | "source": [ 634 | "# No Igual\n", 635 | "5 != 6" 636 | ], 637 | "execution_count": null, 638 | "outputs": [ 639 | { 640 | "output_type": "execute_result", 641 | "data": { 642 | "text/plain": [ 643 | "True" 644 | ] 645 | }, 646 | "metadata": {}, 647 | "execution_count": 26 648 | } 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "metadata": { 654 | "id": "15J-4du3d0o-", 655 | "colab": { 656 | "base_uri": "https://localhost:8080/" 657 | }, 658 | "outputId": "cc67cd4e-2e58-499b-8013-51c3b6565bb3" 659 | }, 660 | "source": [ 661 | "# Y \n", 662 | "(1<2) and (5>6)" 663 | ], 664 | "execution_count": null, 665 | "outputs": [ 666 | { 667 | "output_type": "execute_result", 668 | "data": { 669 | "text/plain": [ 670 | "False" 671 | ] 672 | }, 673 | "metadata": {}, 674 | "execution_count": 29 675 | } 676 | ] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "metadata": { 681 | "id": "XtzLTurHd47e", 682 | "colab": { 683 | "base_uri": "https://localhost:8080/" 684 | }, 685 | "outputId": "e57589ab-2768-49a0-d50e-2db3522fd49e" 686 | }, 687 | "source": [ 688 | "# O\n", 689 | "(1<2) or (5>6)" 690 | ], 691 | "execution_count": null, 692 | "outputs": [ 693 | { 694 | "output_type": "execute_result", 695 | "data": { 696 | "text/plain": [ 697 | "True" 698 | ] 699 | }, 700 | "metadata": {}, 701 | "execution_count": 32 702 | } 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "metadata": { 708 | "id": "pTRdjRHyZKZ7", 709 | "colab": { 710 | "base_uri": "https://localhost:8080/" 711 | }, 712 | "outputId": "d194dabe-5ad9-4d9c-d851-a35bd1a4093b" 713 | }, 714 | "source": [ 715 | "# No \n", 716 | "not (5>6)" 717 | ], 718 | "execution_count": null, 719 | "outputs": [ 720 | { 721 | "output_type": "execute_result", 722 | "data": { 723 | "text/plain": [ 724 | "True" 725 | ] 726 | }, 727 | "metadata": {}, 728 | "execution_count": 34 729 | } 730 | ] 731 | }, 732 | { 733 | "cell_type": "markdown", 734 | "metadata": { 735 | "id": "cGqtdDyYX1Dk" 736 | }, 737 | "source": [ 738 | "##### Asignación de variables" 739 | ] 740 | }, 741 | { 742 | "cell_type": "code", 743 | "metadata": { 744 | "id": "q3PsKlhZWvlz", 745 | "colab": { 746 | "base_uri": "https://localhost:8080/" 747 | }, 748 | "outputId": "adde20ff-1905-4e46-a915-6fe23c153e5f" 749 | }, 750 | "source": [ 751 | "# Variables Numéricas\n", 752 | "x = 6\n", 753 | "y = 5\n", 754 | "print(x + y)" 755 | ], 756 | "execution_count": null, 757 | "outputs": [ 758 | { 759 | "output_type": "stream", 760 | "name": "stdout", 761 | "text": [ 762 | "11\n" 763 | ] 764 | } 765 | ] 766 | }, 767 | { 768 | "cell_type": "code", 769 | "metadata": { 770 | "id": "NrwwCpCCWvkU", 771 | "colab": { 772 | "base_uri": "https://localhost:8080/" 773 | }, 774 | "outputId": "665c7bc5-3a0c-4120-e892-a9e818329a3b" 775 | }, 776 | "source": [ 777 | "# Variables String\n", 778 | "date = \"03-13-2020\"\n", 779 | "price = 180\n", 780 | "info = f\"Tesla: {price}$, Fecha: {date}\"\n", 781 | "\n", 782 | "print(info)" 783 | ], 784 | "execution_count": null, 785 | "outputs": [ 786 | { 787 | "output_type": "stream", 788 | "name": "stdout", 789 | "text": [ 790 | "Tesla: 180$, Fecha: 03-13-2020\n" 791 | ] 792 | } 793 | ] 794 | }, 795 | { 796 | "cell_type": "code", 797 | "metadata": { 798 | "id": "ufENsej-WveT", 799 | "colab": { 800 | "base_uri": "https://localhost:8080/" 801 | }, 802 | "outputId": "229f98c1-c9e7-40b0-c6a0-08a064510907" 803 | }, 804 | "source": [ 805 | "# Variables Booleanas\n", 806 | "time = \"20:00\"\n", 807 | "market_open = (\"08:00\" < time) and (time < \"19:00\")\n", 808 | "print(market_open)" 809 | ], 810 | "execution_count": null, 811 | "outputs": [ 812 | { 813 | "output_type": "stream", 814 | "name": "stdout", 815 | "text": [ 816 | "False\n" 817 | ] 818 | } 819 | ] 820 | }, 821 | { 822 | "cell_type": "markdown", 823 | "metadata": { 824 | "id": "EMAdDk_6YXgw" 825 | }, 826 | "source": [ 827 | "##### Tuplas y Listas" 828 | ] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "metadata": { 833 | "id": "WY3Eyz5PWvcm", 834 | "colab": { 835 | "base_uri": "https://localhost:8080/" 836 | }, 837 | "outputId": "57db579f-888d-4b6e-fe88-109437893669" 838 | }, 839 | "source": [ 840 | "# Tupla\n", 841 | "my_tuple = (1,2,3)\n", 842 | "my_tuple" 843 | ], 844 | "execution_count": null, 845 | "outputs": [ 846 | { 847 | "output_type": "execute_result", 848 | "data": { 849 | "text/plain": [ 850 | "(1, 2, 3)" 851 | ] 852 | }, 853 | "metadata": {}, 854 | "execution_count": 42 855 | } 856 | ] 857 | }, 858 | { 859 | "cell_type": "code", 860 | "metadata": { 861 | "id": "L7RGe8QgWvYt", 862 | "colab": { 863 | "base_uri": "https://localhost:8080/" 864 | }, 865 | "outputId": "635fdc99-0bfa-4080-bd42-5abb8f157776" 866 | }, 867 | "source": [ 868 | "# Crear una lista\n", 869 | "my_list = [1,2,3,4,5,6,7,8,9,10]\n", 870 | "my_list" 871 | ], 872 | "execution_count": null, 873 | "outputs": [ 874 | { 875 | "output_type": "execute_result", 876 | "data": { 877 | "text/plain": [ 878 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" 879 | ] 880 | }, 881 | "metadata": {}, 882 | "execution_count": 43 883 | } 884 | ] 885 | }, 886 | { 887 | "cell_type": "code", 888 | "metadata": { 889 | "id": "OSQw1R5yWvPy", 890 | "colab": { 891 | "base_uri": "https://localhost:8080/" 892 | }, 893 | "outputId": "aed8de95-11ec-4344-9814-4e51adaf1cd4" 894 | }, 895 | "source": [ 896 | "# Añadir valores\n", 897 | "my_list.append(11)\n", 898 | "my_list" 899 | ], 900 | "execution_count": null, 901 | "outputs": [ 902 | { 903 | "output_type": "execute_result", 904 | "data": { 905 | "text/plain": [ 906 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" 907 | ] 908 | }, 909 | "metadata": {}, 910 | "execution_count": 44 911 | } 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "metadata": { 917 | "colab": { 918 | "base_uri": "https://localhost:8080/" 919 | }, 920 | "id": "8p9Tpd42zOq_", 921 | "outputId": "5be98bbb-0c0b-4feb-e923-d4daefd60213" 922 | }, 923 | "source": [ 924 | "# Añadir valores\n", 925 | "my_list.extend([12,13,14,15,16,17])\n", 926 | "my_list" 927 | ], 928 | "execution_count": null, 929 | "outputs": [ 930 | { 931 | "output_type": "execute_result", 932 | "data": { 933 | "text/plain": [ 934 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]" 935 | ] 936 | }, 937 | "metadata": {}, 938 | "execution_count": 45 939 | } 940 | ] 941 | }, 942 | { 943 | "cell_type": "code", 944 | "metadata": { 945 | "id": "i391z7t4ZwnM", 946 | "colab": { 947 | "base_uri": "https://localhost:8080/" 948 | }, 949 | "outputId": "236b147c-9142-4a2b-8318-6378ab511054" 950 | }, 951 | "source": [ 952 | "# Índices\n", 953 | "my_list[0]" 954 | ], 955 | "execution_count": null, 956 | "outputs": [ 957 | { 958 | "output_type": "execute_result", 959 | "data": { 960 | "text/plain": [ 961 | "1" 962 | ] 963 | }, 964 | "metadata": {}, 965 | "execution_count": 51 966 | } 967 | ] 968 | }, 969 | { 970 | "cell_type": "code", 971 | "metadata": { 972 | "id": "owXGp0IsZ3Lg", 973 | "colab": { 974 | "base_uri": "https://localhost:8080/" 975 | }, 976 | "outputId": "ad56180f-9c36-4553-cf6b-a4b669a266f9" 977 | }, 978 | "source": [ 979 | "# Selección de un rango de valores\n", 980 | "my_list[0:9]" 981 | ], 982 | "execution_count": null, 983 | "outputs": [ 984 | { 985 | "output_type": "execute_result", 986 | "data": { 987 | "text/plain": [ 988 | "[1, 2, 3, 4, 5, 6, 7, 8, 9]" 989 | ] 990 | }, 991 | "metadata": {}, 992 | "execution_count": 56 993 | } 994 | ] 995 | }, 996 | { 997 | "cell_type": "code", 998 | "metadata": { 999 | "id": "g5SsaYNjZ6Jo", 1000 | "colab": { 1001 | "base_uri": "https://localhost:8080/" 1002 | }, 1003 | "outputId": "6dedb085-aef8-4525-e9d1-265284929463" 1004 | }, 1005 | "source": [ 1006 | "# Eliminar un valor\n", 1007 | "del my_list[0]\n", 1008 | "my_list" 1009 | ], 1010 | "execution_count": null, 1011 | "outputs": [ 1012 | { 1013 | "output_type": "execute_result", 1014 | "data": { 1015 | "text/plain": [ 1016 | "[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]" 1017 | ] 1018 | }, 1019 | "metadata": {}, 1020 | "execution_count": 57 1021 | } 1022 | ] 1023 | }, 1024 | { 1025 | "cell_type": "code", 1026 | "metadata": { 1027 | "id": "JH1xzsjTZOhr", 1028 | "colab": { 1029 | "base_uri": "https://localhost:8080/" 1030 | }, 1031 | "outputId": "6b5e73a4-377f-4e75-f451-f9792121517c" 1032 | }, 1033 | "source": [ 1034 | "# Bonus: Listas anidadas\n", 1035 | "my_list = [[1,15], 3, \"Finance\"]\n", 1036 | "\n", 1037 | "# Imprimir la sublista\n", 1038 | "print(my_list[0])\n", 1039 | "\n", 1040 | "# Imprimir el valor 15\n", 1041 | "print(my_list[0][1])" 1042 | ], 1043 | "execution_count": null, 1044 | "outputs": [ 1045 | { 1046 | "output_type": "stream", 1047 | "name": "stdout", 1048 | "text": [ 1049 | "[1, 15]\n", 1050 | "15\n" 1051 | ] 1052 | } 1053 | ] 1054 | }, 1055 | { 1056 | "cell_type": "markdown", 1057 | "metadata": { 1058 | "id": "Kd-SYHI-ZF85" 1059 | }, 1060 | "source": [ 1061 | "##### Diccionarios" 1062 | ] 1063 | }, 1064 | { 1065 | "cell_type": "code", 1066 | "metadata": { 1067 | "id": "7PasvUGAZw7L" 1068 | }, 1069 | "source": [ 1070 | "# Inicializar un diccionario\n", 1071 | "dic = {}" 1072 | ], 1073 | "execution_count": null, 1074 | "outputs": [] 1075 | }, 1076 | { 1077 | "cell_type": "code", 1078 | "metadata": { 1079 | "id": "FngIfxOfZ-o5", 1080 | "colab": { 1081 | "base_uri": "https://localhost:8080/" 1082 | }, 1083 | "outputId": "1875dd1e-d27b-4830-f73e-a76f29929a63" 1084 | }, 1085 | "source": [ 1086 | "# Inicializar con valores\n", 1087 | "dic = {\"Tesla price\" : 1500,\n", 1088 | " \"Google price\": 3000}\n", 1089 | "dic" 1090 | ], 1091 | "execution_count": null, 1092 | "outputs": [ 1093 | { 1094 | "output_type": "execute_result", 1095 | "data": { 1096 | "text/plain": [ 1097 | "{'Google price': 3000, 'Tesla price': 1500}" 1098 | ] 1099 | }, 1100 | "metadata": {}, 1101 | "execution_count": 65 1102 | } 1103 | ] 1104 | }, 1105 | { 1106 | "cell_type": "code", 1107 | "metadata": { 1108 | "id": "XT9t_7nraYJL", 1109 | "colab": { 1110 | "base_uri": "https://localhost:8080/" 1111 | }, 1112 | "outputId": "4680300b-414e-4a25-c364-71ec3422aad7" 1113 | }, 1114 | "source": [ 1115 | "# Añadir un valor\n", 1116 | "dic[\"Microsoft price\"] = 1500\n", 1117 | "dic" 1118 | ], 1119 | "execution_count": null, 1120 | "outputs": [ 1121 | { 1122 | "output_type": "execute_result", 1123 | "data": { 1124 | "text/plain": [ 1125 | "{'Google price': 3000, 'Microsoft price': 1500, 'Tesla price': 1500}" 1126 | ] 1127 | }, 1128 | "metadata": {}, 1129 | "execution_count": 66 1130 | } 1131 | ] 1132 | }, 1133 | { 1134 | "cell_type": "code", 1135 | "metadata": { 1136 | "colab": { 1137 | "base_uri": "https://localhost:8080/" 1138 | }, 1139 | "id": "1fnoJHGmaRtG", 1140 | "outputId": "91dcd881-dac2-4841-cb36-af28484bcebf" 1141 | }, 1142 | "source": [ 1143 | "# Cambiar un valor\n", 1144 | "dic[\"Microsoft price\"] = 3000\n", 1145 | "dic" 1146 | ], 1147 | "execution_count": null, 1148 | "outputs": [ 1149 | { 1150 | "output_type": "execute_result", 1151 | "data": { 1152 | "text/plain": [ 1153 | "{'Google price': 3000, 'Microsoft price': 3000, 'Tesla price': 1500}" 1154 | ] 1155 | }, 1156 | "metadata": {}, 1157 | "execution_count": 67 1158 | } 1159 | ] 1160 | }, 1161 | { 1162 | "cell_type": "code", 1163 | "metadata": { 1164 | "id": "t4GhmGtdapWP", 1165 | "colab": { 1166 | "base_uri": "https://localhost:8080/" 1167 | }, 1168 | "outputId": "17603bb6-99dd-461d-8921-1e4bf3e98b9a" 1169 | }, 1170 | "source": [ 1171 | "# Extraer un valor\n", 1172 | "dic[\"Microsoft price\"]" 1173 | ], 1174 | "execution_count": null, 1175 | "outputs": [ 1176 | { 1177 | "output_type": "execute_result", 1178 | "data": { 1179 | "text/plain": [ 1180 | "3000" 1181 | ] 1182 | }, 1183 | "metadata": {}, 1184 | "execution_count": 68 1185 | } 1186 | ] 1187 | }, 1188 | { 1189 | "cell_type": "code", 1190 | "metadata": { 1191 | "colab": { 1192 | "base_uri": "https://localhost:8080/" 1193 | }, 1194 | "id": "rOBalLlC3Et8", 1195 | "outputId": "5b5a1592-8b2b-482c-c502-be8a5df99fcb" 1196 | }, 1197 | "source": [ 1198 | "# Eliminar un valor\n", 1199 | "del dic[\"Microsoft price\"]\n", 1200 | "dic" 1201 | ], 1202 | "execution_count": null, 1203 | "outputs": [ 1204 | { 1205 | "output_type": "execute_result", 1206 | "data": { 1207 | "text/plain": [ 1208 | "{'Google price': 3000, 'Tesla price': 1500}" 1209 | ] 1210 | }, 1211 | "metadata": {}, 1212 | "execution_count": 69 1213 | } 1214 | ] 1215 | }, 1216 | { 1217 | "cell_type": "code", 1218 | "metadata": { 1219 | "colab": { 1220 | "base_uri": "https://localhost:8080/" 1221 | }, 1222 | "id": "otvn059la3Ct", 1223 | "outputId": "fe4b018b-7f13-4182-eec6-70ad0cd473d0" 1224 | }, 1225 | "source": [ 1226 | "# Extraer el nombre de las claves\n", 1227 | "dic.keys()" 1228 | ], 1229 | "execution_count": null, 1230 | "outputs": [ 1231 | { 1232 | "output_type": "execute_result", 1233 | "data": { 1234 | "text/plain": [ 1235 | "dict_keys(['Tesla price', 'Google price'])" 1236 | ] 1237 | }, 1238 | "metadata": {}, 1239 | "execution_count": 70 1240 | } 1241 | ] 1242 | }, 1243 | { 1244 | "cell_type": "code", 1245 | "metadata": { 1246 | "colab": { 1247 | "base_uri": "https://localhost:8080/" 1248 | }, 1249 | "id": "ndKUymfzbF4t", 1250 | "outputId": "df471e24-bb8d-4f43-9dd9-344ca72f0a12" 1251 | }, 1252 | "source": [ 1253 | "# Extraer los valores del diccionario\n", 1254 | "dic.values()" 1255 | ], 1256 | "execution_count": null, 1257 | "outputs": [ 1258 | { 1259 | "output_type": "execute_result", 1260 | "data": { 1261 | "text/plain": [ 1262 | "dict_values([1500, 3000])" 1263 | ] 1264 | }, 1265 | "metadata": {}, 1266 | "execution_count": 71 1267 | } 1268 | ] 1269 | }, 1270 | { 1271 | "cell_type": "markdown", 1272 | "metadata": { 1273 | "id": "2UHCKDk8bJEf" 1274 | }, 1275 | "source": [ 1276 | "##### Conjuntos" 1277 | ] 1278 | }, 1279 | { 1280 | "cell_type": "code", 1281 | "metadata": { 1282 | "id": "viHNp-EHbQ9H", 1283 | "colab": { 1284 | "base_uri": "https://localhost:8080/" 1285 | }, 1286 | "outputId": "130d4fa3-df3c-46a8-fbd9-06cb707f69c5" 1287 | }, 1288 | "source": [ 1289 | "# Crear un conjunto\n", 1290 | "{1,3,9}" 1291 | ], 1292 | "execution_count": 1, 1293 | "outputs": [ 1294 | { 1295 | "output_type": "execute_result", 1296 | "data": { 1297 | "text/plain": [ 1298 | "{1, 3, 9}" 1299 | ] 1300 | }, 1301 | "metadata": {}, 1302 | "execution_count": 1 1303 | } 1304 | ] 1305 | }, 1306 | { 1307 | "cell_type": "code", 1308 | "metadata": { 1309 | "id": "_E1aFnTcbYpJ", 1310 | "colab": { 1311 | "base_uri": "https://localhost:8080/" 1312 | }, 1313 | "outputId": "c7d78f55-cce1-4fb7-fec7-ff97ff2a23f3" 1314 | }, 1315 | "source": [ 1316 | "# Propiedad de los Conjuntos\n", 1317 | "{1,1,1, 3,3,3, 9,9,9}" 1318 | ], 1319 | "execution_count": 2, 1320 | "outputs": [ 1321 | { 1322 | "output_type": "execute_result", 1323 | "data": { 1324 | "text/plain": [ 1325 | "{1, 3, 9}" 1326 | ] 1327 | }, 1328 | "metadata": {}, 1329 | "execution_count": 2 1330 | } 1331 | ] 1332 | }, 1333 | { 1334 | "cell_type": "code", 1335 | "metadata": { 1336 | "id": "2kc9rLF6be3l", 1337 | "colab": { 1338 | "base_uri": "https://localhost:8080/" 1339 | }, 1340 | "outputId": "e7b7e73e-623d-4f6d-868e-4d73ccad2e92" 1341 | }, 1342 | "source": [ 1343 | "# Crear una lista\n", 1344 | "my_list = [\"Finanzas\", \"Estadística\", \"Matemáticas\",\n", 1345 | " \"Finanzas\", \"Estadística\", \"Finanzas\",\n", 1346 | " \"Matemáticas\", \"Finanzas\", \"Estadística\"]\n", 1347 | "\n", 1348 | "# Transformar una lista en un conjunto\n", 1349 | "set(my_list)" 1350 | ], 1351 | "execution_count": 3, 1352 | "outputs": [ 1353 | { 1354 | "output_type": "execute_result", 1355 | "data": { 1356 | "text/plain": [ 1357 | "{'Estadística', 'Finanzas', 'Matemáticas'}" 1358 | ] 1359 | }, 1360 | "metadata": {}, 1361 | "execution_count": 3 1362 | } 1363 | ] 1364 | }, 1365 | { 1366 | "cell_type": "code", 1367 | "metadata": { 1368 | "id": "s2b-8LgkcLs1", 1369 | "colab": { 1370 | "base_uri": "https://localhost:8080/" 1371 | }, 1372 | "outputId": "1b06f669-e2c3-4343-836d-8367caecfc02" 1373 | }, 1374 | "source": [ 1375 | "# Añadir un valor\n", 1376 | "s = {1,3,5}\n", 1377 | "print(s)\n", 1378 | "\n", 1379 | "s.add(56)\n", 1380 | "s.add(68)\n", 1381 | "print(s)" 1382 | ], 1383 | "execution_count": 4, 1384 | "outputs": [ 1385 | { 1386 | "output_type": "stream", 1387 | "name": "stdout", 1388 | "text": [ 1389 | "{1, 3, 5}\n", 1390 | "{1, 3, 68, 5, 56}\n" 1391 | ] 1392 | } 1393 | ] 1394 | }, 1395 | { 1396 | "cell_type": "markdown", 1397 | "metadata": { 1398 | "id": "W9Vc62veaNKg" 1399 | }, 1400 | "source": [ 1401 | "### Estructuras de código en Python " 1402 | ] 1403 | }, 1404 | { 1405 | "cell_type": "markdown", 1406 | "metadata": { 1407 | "id": "7xKs11J6akHQ" 1408 | }, 1409 | "source": [ 1410 | "##### If/ Elif/ Else" 1411 | ] 1412 | }, 1413 | { 1414 | "cell_type": "code", 1415 | "metadata": { 1416 | "id": "CqZigieEaOkf", 1417 | "colab": { 1418 | "base_uri": "https://localhost:8080/" 1419 | }, 1420 | "outputId": "41b03c0f-1f20-454f-b488-0e6811604694" 1421 | }, 1422 | "source": [ 1423 | "# Estructuras condicionales IF\n", 1424 | "if 5<6:\n", 1425 | " print(\"Sí\")" 1426 | ], 1427 | "execution_count": 6, 1428 | "outputs": [ 1429 | { 1430 | "output_type": "stream", 1431 | "name": "stdout", 1432 | "text": [ 1433 | "Sí\n" 1434 | ] 1435 | } 1436 | ] 1437 | }, 1438 | { 1439 | "cell_type": "code", 1440 | "metadata": { 1441 | "id": "neKqpCKn8y9N", 1442 | "colab": { 1443 | "base_uri": "https://localhost:8080/" 1444 | }, 1445 | "outputId": "89d60d03-66f9-427f-8502-acd44a74bf96" 1446 | }, 1447 | "source": [ 1448 | "# Estructuras condicionales IF/ ELSE\n", 1449 | "if 5>6:\n", 1450 | " print(\"Sí\")\n", 1451 | "else:\n", 1452 | " print(\"No\")" 1453 | ], 1454 | "execution_count": 9, 1455 | "outputs": [ 1456 | { 1457 | "output_type": "stream", 1458 | "name": "stdout", 1459 | "text": [ 1460 | "No\n" 1461 | ] 1462 | } 1463 | ] 1464 | }, 1465 | { 1466 | "cell_type": "code", 1467 | "metadata": { 1468 | "id": "qEKi-yu_873c", 1469 | "colab": { 1470 | "base_uri": "https://localhost:8080/" 1471 | }, 1472 | "outputId": "1a9da90c-cbee-4abb-a2b7-f0827b793238" 1473 | }, 1474 | "source": [ 1475 | "# Estructuras condicionales IF/ ELIF/ ELSE\n", 1476 | "x = 11\n", 1477 | "\n", 1478 | "if x>=15:\n", 1479 | " print(\"X>=15\")\n", 1480 | "elif x>10:\n", 1481 | " print(\"10\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmy_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1858 | "\u001b[0;31mNameError\u001b[0m: name 'loc' is not defined" 1859 | ] 1860 | } 1861 | ] 1862 | }, 1863 | { 1864 | "cell_type": "markdown", 1865 | "metadata": { 1866 | "id": "F8rax_dDgnFI" 1867 | }, 1868 | "source": [ 1869 | "##### Variable Global" 1870 | ] 1871 | }, 1872 | { 1873 | "cell_type": "code", 1874 | "metadata": { 1875 | "id": "7330M1h4gpnQ" 1876 | }, 1877 | "source": [ 1878 | "# Variable Global\n", 1879 | "def my_function():\n", 1880 | " global variable\n", 1881 | " variable=15" 1882 | ], 1883 | "execution_count": 18, 1884 | "outputs": [] 1885 | }, 1886 | { 1887 | "cell_type": "code", 1888 | "metadata": { 1889 | "id": "LgzzqLCDgp-M" 1890 | }, 1891 | "source": [ 1892 | "# Ejecutar la función e imprimir el valor de la variable global\n", 1893 | "my_function()" 1894 | ], 1895 | "execution_count": 19, 1896 | "outputs": [] 1897 | }, 1898 | { 1899 | "cell_type": "code", 1900 | "metadata": { 1901 | "colab": { 1902 | "base_uri": "https://localhost:8080/" 1903 | }, 1904 | "id": "KFEQ7BB-2m5g", 1905 | "outputId": "20f93ce8-b84d-4272-fff6-c66eb322f61c" 1906 | }, 1907 | "source": [ 1908 | "print(variable)" 1909 | ], 1910 | "execution_count": 20, 1911 | "outputs": [ 1912 | { 1913 | "output_type": "stream", 1914 | "name": "stdout", 1915 | "text": [ 1916 | "15\n" 1917 | ] 1918 | } 1919 | ] 1920 | }, 1921 | { 1922 | "cell_type": "markdown", 1923 | "metadata": { 1924 | "id": "4NWfx4mRhpgX" 1925 | }, 1926 | "source": [ 1927 | "##### Lambda" 1928 | ] 1929 | }, 1930 | { 1931 | "cell_type": "code", 1932 | "metadata": { 1933 | "id": "mhxlEyFthuQa" 1934 | }, 1935 | "source": [ 1936 | "# Crear un objeto lambda\n", 1937 | "f = lambda x: x**2" 1938 | ], 1939 | "execution_count": 21, 1940 | "outputs": [] 1941 | }, 1942 | { 1943 | "cell_type": "code", 1944 | "metadata": { 1945 | "id": "NFQxwMIWh-B_", 1946 | "colab": { 1947 | "base_uri": "https://localhost:8080/" 1948 | }, 1949 | "outputId": "d420dfad-4246-4264-990b-bf0e93ed30da" 1950 | }, 1951 | "source": [ 1952 | "# Crear una lista para aplicar una función lambda\n", 1953 | "lis = [1,2,3,4,5,6,7,8]\n", 1954 | "\n", 1955 | "# Usar la función map para aplicar la función lambda a una lista\n", 1956 | "generator = map(f,lis)\n", 1957 | "generator" 1958 | ], 1959 | "execution_count": 23, 1960 | "outputs": [ 1961 | { 1962 | "output_type": "execute_result", 1963 | "data": { 1964 | "text/plain": [ 1965 | "" 1966 | ] 1967 | }, 1968 | "metadata": {}, 1969 | "execution_count": 23 1970 | } 1971 | ] 1972 | }, 1973 | { 1974 | "cell_type": "code", 1975 | "metadata": { 1976 | "id": "_AX5enMai7O-", 1977 | "colab": { 1978 | "base_uri": "https://localhost:8080/" 1979 | }, 1980 | "outputId": "47555d4d-c771-4652-c5a2-3f07db4e3ff8" 1981 | }, 1982 | "source": [ 1983 | "# La transformamos en una lista\n", 1984 | "list(generator)" 1985 | ], 1986 | "execution_count": 24, 1987 | "outputs": [ 1988 | { 1989 | "output_type": "execute_result", 1990 | "data": { 1991 | "text/plain": [ 1992 | "[1, 4, 9, 16, 25, 36, 49, 64]" 1993 | ] 1994 | }, 1995 | "metadata": {}, 1996 | "execution_count": 24 1997 | } 1998 | ] 1999 | }, 2000 | { 2001 | "cell_type": "code", 2002 | "metadata": { 2003 | "id": "bWRIV_RZ4cBs" 2004 | }, 2005 | "source": [ 2006 | "" 2007 | ], 2008 | "execution_count": null, 2009 | "outputs": [] 2010 | } 2011 | ] 2012 | } -------------------------------------------------------------------------------- /ES_TA_Capítulo_03_Pre_Procesado_de_Datos.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "ES - TA Capítulo 03: Pre Procesado de Datos.ipynb", 7 | "provenance": [], 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "

\"Colaboratory\n", 33 | "\"Colaboratory

\n", 34 | "\n", 35 | "# Preprocesado de Datos\n", 36 | "\n", 37 | "\n", 38 | "
\n", 39 | "\n", 40 | "\n", 41 | "💰 Únete a la comunidad de [Discord](https://discord.gg/z3dx5XpkX4)\n", 42 | "\n", 43 | "📚 Puedes leer nuestro libro en [Amazon](https://www.amazon.es/Python-para-finanzas-trading-algor%C3%ADtmico-ebook/dp/B0BT4ZS9Q3/)\n", 44 | "\n", 45 | "🖥️ El canal de [YouTube de Quantreo's](https://www.youtube.com/channel/UCp7jckfiEglNf_Gj62VR0pw) (en inglés) y el de [Frogames](https://www.youtube.com/channel/UCMUxXNYrVCv6-bQakhomvBg) en español\n" 46 | ], 47 | "metadata": { 48 | "id": "1ZsY5xy5sSf2" 49 | } 50 | }, 51 | { 52 | "cell_type": "code", 53 | "source": [ 54 | "!pip install yfinance" 55 | ], 56 | "metadata": { 57 | "colab": { 58 | "base_uri": "https://localhost:8080/" 59 | }, 60 | "id": "SELynRZH3hfA", 61 | "outputId": "5dcd53df-2d2a-45d4-f23e-c20994552d8c" 62 | }, 63 | "execution_count": 1, 64 | "outputs": [ 65 | { 66 | "output_type": "stream", 67 | "name": "stdout", 68 | "text": [ 69 | "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", 70 | "Collecting yfinance\n", 71 | " Downloading yfinance-0.1.72-py2.py3-none-any.whl (27 kB)\n", 72 | "Collecting requests>=2.26\n", 73 | " Downloading requests-2.28.0-py3-none-any.whl (62 kB)\n", 74 | "\u001b[K |████████████████████████████████| 62 kB 1.2 MB/s \n", 75 | "\u001b[?25hRequirement already satisfied: pandas>=0.24.0 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.3.5)\n", 76 | "Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.7/dist-packages (from yfinance) (0.0.10)\n", 77 | "Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.21.6)\n", 78 | "Collecting lxml>=4.5.1\n", 79 | " Downloading lxml-4.9.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (6.4 MB)\n", 80 | "\u001b[K |████████████████████████████████| 6.4 MB 9.7 MB/s \n", 81 | "\u001b[?25hRequirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->yfinance) (2.8.2)\n", 82 | "Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->yfinance) (2022.1)\n", 83 | "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.15.0)\n", 84 | "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2.0.12)\n", 85 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (1.24.3)\n", 86 | "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2022.5.18.1)\n", 87 | "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2.10)\n", 88 | "Installing collected packages: requests, lxml, yfinance\n", 89 | " Attempting uninstall: requests\n", 90 | " Found existing installation: requests 2.23.0\n", 91 | " Uninstalling requests-2.23.0:\n", 92 | " Successfully uninstalled requests-2.23.0\n", 93 | " Attempting uninstall: lxml\n", 94 | " Found existing installation: lxml 4.2.6\n", 95 | " Uninstalling lxml-4.2.6:\n", 96 | " Successfully uninstalled lxml-4.2.6\n", 97 | "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", 98 | "google-colab 1.0.0 requires requests~=2.23.0, but you have requests 2.28.0 which is incompatible.\n", 99 | "datascience 0.10.6 requires folium==0.2.1, but you have folium 0.8.3 which is incompatible.\u001b[0m\n", 100 | "Successfully installed lxml-4.9.0 requests-2.28.0 yfinance-0.1.72\n" 101 | ] 102 | } 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 2, 108 | "metadata": { 109 | "id": "vjIltp4XqmxX" 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "import numpy as np\n", 114 | "import pandas as pd\n", 115 | "import yfinance as yf\n", 116 | "import warnings\n", 117 | "warnings.filterwarnings(\"ignore\")" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "source": [ 123 | "# Manejar datos que proceden de MT5 (manualmente)\n" 124 | ], 125 | "metadata": { 126 | "id": "ojd6NbD3Us06" 127 | } 128 | }, 129 | { 130 | "cell_type": "code", 131 | "source": [ 132 | "# Importar los datos en bruto\n", 133 | "df = pd.read_csv(\"/content/EURUSD_D1.csv\")\n", 134 | "df" 135 | ], 136 | "metadata": { 137 | "colab": { 138 | "base_uri": "https://localhost:8080/", 139 | "height": 424 140 | }, 141 | "id": "I0G5yH4B4kQM", 142 | "outputId": "b0d1c048-a699-45a0-b7f5-7e1ab17f22ba" 143 | }, 144 | "execution_count": 3, 145 | "outputs": [ 146 | { 147 | "output_type": "execute_result", 148 | "data": { 149 | "text/plain": [ 150 | " \\t\\t\\t\\t\\t\\t\\t\n", 151 | "0 2011.01.03\\t1.33443\\t1.33945\\t1.32501\\t1.33499... \n", 152 | "1 2011.01.04\\t1.33507\\t1.34330\\t1.32919\\t1.33104... \n", 153 | "2 2011.01.05\\t1.33099\\t1.33247\\t1.31255\\t1.31485... \n", 154 | "3 2011.01.06\\t1.31495\\t1.31699\\t1.29834\\t1.30022... \n", 155 | "4 2011.01.07\\t1.30018\\t1.30206\\t1.29044\\t1.29055... \n", 156 | "... ... \n", 157 | "2861 2022.01.10\\t1.13550\\t1.13599\\t1.12853\\t1.13252... \n", 158 | "2862 2022.01.11\\t1.13230\\t1.13753\\t1.13131\\t1.13666... \n", 159 | "2863 2022.01.12\\t1.13655\\t1.14528\\t1.13545\\t1.14411... \n", 160 | "2864 2022.01.13\\t1.14402\\t1.14817\\t1.14357\\t1.14539... \n", 161 | "2865 2022.01.14\\t1.14531\\t1.14829\\t1.13987\\t1.14151... \n", 162 | "\n", 163 | "[2866 rows x 1 columns]" 164 | ], 165 | "text/html": [ 166 | "\n", 167 | "
\n", 168 | "
\n", 169 | "
\n", 170 | "\n", 183 | "\n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | "
<DATE>\\t<OPEN>\\t<HIGH>\\t<LOW>\\t<CLOSE>\\t<TICKVOL>\\t<VOL>\\t<SPREAD>
02011.01.03\\t1.33443\\t1.33945\\t1.32501\\t1.33499...
12011.01.04\\t1.33507\\t1.34330\\t1.32919\\t1.33104...
22011.01.05\\t1.33099\\t1.33247\\t1.31255\\t1.31485...
32011.01.06\\t1.31495\\t1.31699\\t1.29834\\t1.30022...
42011.01.07\\t1.30018\\t1.30206\\t1.29044\\t1.29055...
......
28612022.01.10\\t1.13550\\t1.13599\\t1.12853\\t1.13252...
28622022.01.11\\t1.13230\\t1.13753\\t1.13131\\t1.13666...
28632022.01.12\\t1.13655\\t1.14528\\t1.13545\\t1.14411...
28642022.01.13\\t1.14402\\t1.14817\\t1.14357\\t1.14539...
28652022.01.14\\t1.14531\\t1.14829\\t1.13987\\t1.14151...
\n", 237 | "

2866 rows × 1 columns

\n", 238 | "
\n", 239 | " \n", 249 | " \n", 250 | " \n", 287 | "\n", 288 | " \n", 312 | "
\n", 313 | "
\n", 314 | " " 315 | ] 316 | }, 317 | "metadata": {}, 318 | "execution_count": 3 319 | } 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "source": [ 325 | "# Añadir delimitador\n", 326 | "df = pd.read_csv(\"/content/EURUSD_D1.csv\", delimiter=\"\\t\")\n", 327 | "df" 328 | ], 329 | "metadata": { 330 | "colab": { 331 | "base_uri": "https://localhost:8080/", 332 | "height": 424 333 | }, 334 | "id": "l5Yw3s0KVNBJ", 335 | "outputId": "ace3341e-d1f4-4755-cfee-146bd4fb5ed2" 336 | }, 337 | "execution_count": 4, 338 | "outputs": [ 339 | { 340 | "output_type": "execute_result", 341 | "data": { 342 | "text/plain": [ 343 | " \\\n", 344 | "0 2011.01.03 1.33443 1.33945 1.32501 1.33499 63214 0 \n", 345 | "1 2011.01.04 1.33507 1.34330 1.32919 1.33104 80673 0 \n", 346 | "2 2011.01.05 1.33099 1.33247 1.31255 1.31485 83725 0 \n", 347 | "3 2011.01.06 1.31495 1.31699 1.29834 1.30022 73723 0 \n", 348 | "4 2011.01.07 1.30018 1.30206 1.29044 1.29055 82379 0 \n", 349 | "... ... ... ... ... ... ... ... \n", 350 | "2861 2022.01.10 1.13550 1.13599 1.12853 1.13252 53457 0 \n", 351 | "2862 2022.01.11 1.13230 1.13753 1.13131 1.13666 49386 0 \n", 352 | "2863 2022.01.12 1.13655 1.14528 1.13545 1.14411 51872 0 \n", 353 | "2864 2022.01.13 1.14402 1.14817 1.14357 1.14539 53078 0 \n", 354 | "2865 2022.01.14 1.14531 1.14829 1.13987 1.14151 59882 0 \n", 355 | "\n", 356 | " \n", 357 | "0 9 \n", 358 | "1 8 \n", 359 | "2 8 \n", 360 | "3 8 \n", 361 | "4 8 \n", 362 | "... ... \n", 363 | "2861 0 \n", 364 | "2862 0 \n", 365 | "2863 0 \n", 366 | "2864 0 \n", 367 | "2865 0 \n", 368 | "\n", 369 | "[2866 rows x 8 columns]" 370 | ], 371 | "text/html": [ 372 | "\n", 373 | "
\n", 374 | "
\n", 375 | "
\n", 376 | "\n", 389 | "\n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | " \n", 474 | " \n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | " \n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | " \n", 516 | " \n", 517 | " \n", 518 | " \n", 519 | " \n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | "
<DATE><OPEN><HIGH><LOW><CLOSE><TICKVOL><VOL><SPREAD>
02011.01.031.334431.339451.325011.334996321409
12011.01.041.335071.343301.329191.331048067308
22011.01.051.330991.332471.312551.314858372508
32011.01.061.314951.316991.298341.300227372308
42011.01.071.300181.302061.290441.290558237908
...........................
28612022.01.101.135501.135991.128531.132525345700
28622022.01.111.132301.137531.131311.136664938600
28632022.01.121.136551.145281.135451.144115187200
28642022.01.131.144021.148171.143571.145395307800
28652022.01.141.145311.148291.139871.141515988200
\n", 527 | "

2866 rows × 8 columns

\n", 528 | "
\n", 529 | " \n", 539 | " \n", 540 | " \n", 577 | "\n", 578 | " \n", 602 | "
\n", 603 | "
\n", 604 | " " 605 | ] 606 | }, 607 | "metadata": {}, 608 | "execution_count": 4 609 | } 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "source": [ 615 | "# Función de preprocesado\n", 616 | "\n", 617 | "def preprocessing(name):\n", 618 | " \n", 619 | " # Importar los datos\n", 620 | " df = pd.read_csv(name, delimiter=\"\\t\", index_col=\"\", parse_dates=True).dropna()\n", 621 | "\n", 622 | " # Eliminar las últimas dos columnas\n", 623 | " df = df.iloc[:,:-2]\n", 624 | "\n", 625 | " # Renombrar \n", 626 | " df.columns = [\"open\", \"high\", \"low\", \"close\", \"volume\"]\n", 627 | " df.index.name = \"time\"\n", 628 | "\n", 629 | " return df" 630 | ], 631 | "metadata": { 632 | "id": "3nkP0bhO4pMq" 633 | }, 634 | "execution_count": 5, 635 | "outputs": [] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "source": [ 640 | "df = preprocessing(\"/content/EURUSD_D1.csv\")\n", 641 | "df" 642 | ], 643 | "metadata": { 644 | "id": "vIxzE2pJ-l0a", 645 | "colab": { 646 | "base_uri": "https://localhost:8080/", 647 | "height": 455 648 | }, 649 | "outputId": "1f7ebea8-8544-400b-8c6c-10140570feb2" 650 | }, 651 | "execution_count": 6, 652 | "outputs": [ 653 | { 654 | "output_type": "execute_result", 655 | "data": { 656 | "text/plain": [ 657 | " open high low close volume\n", 658 | "time \n", 659 | "2011-01-03 1.33443 1.33945 1.32501 1.33499 63214\n", 660 | "2011-01-04 1.33507 1.34330 1.32919 1.33104 80673\n", 661 | "2011-01-05 1.33099 1.33247 1.31255 1.31485 83725\n", 662 | "2011-01-06 1.31495 1.31699 1.29834 1.30022 73723\n", 663 | "2011-01-07 1.30018 1.30206 1.29044 1.29055 82379\n", 664 | "... ... ... ... ... ...\n", 665 | "2022-01-10 1.13550 1.13599 1.12853 1.13252 53457\n", 666 | "2022-01-11 1.13230 1.13753 1.13131 1.13666 49386\n", 667 | "2022-01-12 1.13655 1.14528 1.13545 1.14411 51872\n", 668 | "2022-01-13 1.14402 1.14817 1.14357 1.14539 53078\n", 669 | "2022-01-14 1.14531 1.14829 1.13987 1.14151 59882\n", 670 | "\n", 671 | "[2866 rows x 5 columns]" 672 | ], 673 | "text/html": [ 674 | "\n", 675 | "
\n", 676 | "
\n", 677 | "
\n", 678 | "\n", 691 | "\n", 692 | " \n", 693 | " \n", 694 | " \n", 695 | " \n", 696 | " \n", 697 | " \n", 698 | " \n", 699 | " \n", 700 | " \n", 701 | " \n", 702 | " \n", 703 | " \n", 704 | " \n", 705 | " \n", 706 | " \n", 707 | " \n", 708 | " \n", 709 | " \n", 710 | " \n", 711 | " \n", 712 | " \n", 713 | " \n", 714 | " \n", 715 | " \n", 716 | " \n", 717 | " \n", 718 | " \n", 719 | " \n", 720 | " \n", 721 | " \n", 722 | " \n", 723 | " \n", 724 | " \n", 725 | " \n", 726 | " \n", 727 | " \n", 728 | " \n", 729 | " \n", 730 | " \n", 731 | " \n", 732 | " \n", 733 | " \n", 734 | " \n", 735 | " \n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | " \n", 748 | " \n", 749 | " \n", 750 | " \n", 751 | " \n", 752 | " \n", 753 | " \n", 754 | " \n", 755 | " \n", 756 | " \n", 757 | " \n", 758 | " \n", 759 | " \n", 760 | " \n", 761 | " \n", 762 | " \n", 763 | " \n", 764 | " \n", 765 | " \n", 766 | " \n", 767 | " \n", 768 | " \n", 769 | " \n", 770 | " \n", 771 | " \n", 772 | " \n", 773 | " \n", 774 | " \n", 775 | " \n", 776 | " \n", 777 | " \n", 778 | " \n", 779 | " \n", 780 | " \n", 781 | " \n", 782 | " \n", 783 | " \n", 784 | " \n", 785 | " \n", 786 | " \n", 787 | " \n", 788 | " \n", 789 | " \n", 790 | " \n", 791 | " \n", 792 | " \n", 793 | " \n", 794 | " \n", 795 | " \n", 796 | " \n", 797 | " \n", 798 | " \n", 799 | " \n", 800 | "
openhighlowclosevolume
time
2011-01-031.334431.339451.325011.3349963214
2011-01-041.335071.343301.329191.3310480673
2011-01-051.330991.332471.312551.3148583725
2011-01-061.314951.316991.298341.3002273723
2011-01-071.300181.302061.290441.2905582379
..................
2022-01-101.135501.135991.128531.1325253457
2022-01-111.132301.137531.131311.1366649386
2022-01-121.136551.145281.135451.1441151872
2022-01-131.144021.148171.143571.1453953078
2022-01-141.145311.148291.139871.1415159882
\n", 801 | "

2866 rows × 5 columns

\n", 802 | "
\n", 803 | " \n", 813 | " \n", 814 | " \n", 851 | "\n", 852 | " \n", 876 | "
\n", 877 | "
\n", 878 | " " 879 | ] 880 | }, 881 | "metadata": {}, 882 | "execution_count": 6 883 | } 884 | ] 885 | }, 886 | { 887 | "cell_type": "markdown", 888 | "source": [ 889 | "# Manejar los datos que proceden de Yfinance\n" 890 | ], 891 | "metadata": { 892 | "id": "d5kArxfa4And" 893 | } 894 | }, 895 | { 896 | "cell_type": "code", 897 | "source": [ 898 | "yf.download(\"EURUSD=X\")" 899 | ], 900 | "metadata": { 901 | "colab": { 902 | "base_uri": "https://localhost:8080/", 903 | "height": 473 904 | }, 905 | "id": "3zel86pF1CIu", 906 | "outputId": "738636f0-298e-43d3-af9a-08349e325c1b" 907 | }, 908 | "execution_count": 11, 909 | "outputs": [ 910 | { 911 | "output_type": "stream", 912 | "name": "stdout", 913 | "text": [ 914 | "\r[*********************100%***********************] 1 of 1 completed\n" 915 | ] 916 | }, 917 | { 918 | "output_type": "execute_result", 919 | "data": { 920 | "text/plain": [ 921 | " Open High Low Close Adj Close Volume\n", 922 | "Date \n", 923 | "2003-12-01 1.203398 1.204007 1.194401 1.196501 1.196501 0\n", 924 | "2003-12-02 1.196101 1.210903 1.194600 1.208897 1.208897 0\n", 925 | "2003-12-03 1.209000 1.213003 1.207700 1.212298 1.212298 0\n", 926 | "2003-12-04 1.212004 1.214403 1.204398 1.208094 1.208094 0\n", 927 | "2003-12-05 1.207802 1.219096 1.206593 1.218695 1.218695 0\n", 928 | "... ... ... ... ... ... ...\n", 929 | "2022-06-10 1.061921 1.064215 1.050773 1.061921 1.061921 0\n", 930 | "2022-06-13 1.048999 1.049759 1.041905 1.048999 1.048999 0\n", 931 | "2022-06-14 1.041515 1.048240 1.039825 1.041515 1.041515 0\n", 932 | "2022-06-15 1.044059 1.050718 1.038627 1.044059 1.044059 0\n", 933 | "2022-06-16 1.044714 1.060220 1.038314 1.060108 1.060108 0\n", 934 | "\n", 935 | "[4810 rows x 6 columns]" 936 | ], 937 | "text/html": [ 938 | "\n", 939 | "
\n", 940 | "
\n", 941 | "
\n", 942 | "\n", 955 | "\n", 956 | " \n", 957 | " \n", 958 | " \n", 959 | " \n", 960 | " \n", 961 | " \n", 962 | " \n", 963 | " \n", 964 | " \n", 965 | " \n", 966 | " \n", 967 | " \n", 968 | " \n", 969 | " \n", 970 | " \n", 971 | " \n", 972 | " \n", 973 | " \n", 974 | " \n", 975 | " \n", 976 | " \n", 977 | " \n", 978 | " \n", 979 | " \n", 980 | " \n", 981 | " \n", 982 | " \n", 983 | " \n", 984 | " \n", 985 | " \n", 986 | " \n", 987 | " \n", 988 | " \n", 989 | " \n", 990 | " \n", 991 | " \n", 992 | " \n", 993 | " \n", 994 | " \n", 995 | " \n", 996 | " \n", 997 | " \n", 998 | " \n", 999 | " \n", 1000 | " \n", 1001 | " \n", 1002 | " \n", 1003 | " \n", 1004 | " \n", 1005 | " \n", 1006 | " \n", 1007 | " \n", 1008 | " \n", 1009 | " \n", 1010 | " \n", 1011 | " \n", 1012 | " \n", 1013 | " \n", 1014 | " \n", 1015 | " \n", 1016 | " \n", 1017 | " \n", 1018 | " \n", 1019 | " \n", 1020 | " \n", 1021 | " \n", 1022 | " \n", 1023 | " \n", 1024 | " \n", 1025 | " \n", 1026 | " \n", 1027 | " \n", 1028 | " \n", 1029 | " \n", 1030 | " \n", 1031 | " \n", 1032 | " \n", 1033 | " \n", 1034 | " \n", 1035 | " \n", 1036 | " \n", 1037 | " \n", 1038 | " \n", 1039 | " \n", 1040 | " \n", 1041 | " \n", 1042 | " \n", 1043 | " \n", 1044 | " \n", 1045 | " \n", 1046 | " \n", 1047 | " \n", 1048 | " \n", 1049 | " \n", 1050 | " \n", 1051 | " \n", 1052 | " \n", 1053 | " \n", 1054 | " \n", 1055 | " \n", 1056 | " \n", 1057 | " \n", 1058 | " \n", 1059 | " \n", 1060 | " \n", 1061 | " \n", 1062 | " \n", 1063 | " \n", 1064 | " \n", 1065 | " \n", 1066 | " \n", 1067 | " \n", 1068 | " \n", 1069 | " \n", 1070 | " \n", 1071 | " \n", 1072 | " \n", 1073 | " \n", 1074 | " \n", 1075 | " \n", 1076 | " \n", 1077 | "
OpenHighLowCloseAdj CloseVolume
Date
2003-12-011.2033981.2040071.1944011.1965011.1965010
2003-12-021.1961011.2109031.1946001.2088971.2088970
2003-12-031.2090001.2130031.2077001.2122981.2122980
2003-12-041.2120041.2144031.2043981.2080941.2080940
2003-12-051.2078021.2190961.2065931.2186951.2186950
.....................
2022-06-101.0619211.0642151.0507731.0619211.0619210
2022-06-131.0489991.0497591.0419051.0489991.0489990
2022-06-141.0415151.0482401.0398251.0415151.0415150
2022-06-151.0440591.0507181.0386271.0440591.0440590
2022-06-161.0447141.0602201.0383141.0601081.0601080
\n", 1078 | "

4810 rows × 6 columns

\n", 1079 | "
\n", 1080 | " \n", 1090 | " \n", 1091 | " \n", 1128 | "\n", 1129 | " \n", 1153 | "
\n", 1154 | "
\n", 1155 | " " 1156 | ] 1157 | }, 1158 | "metadata": {}, 1159 | "execution_count": 11 1160 | } 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "code", 1165 | "source": [ 1166 | "def preprocessing_yf(symbol):\n", 1167 | " \n", 1168 | " #Importar los datos\n", 1169 | " df = yf.download(symbol).dropna()\n", 1170 | "\n", 1171 | " #Renombrar\n", 1172 | " df.columns = [\"open\", \"high\", \"low\", \"close\", \"adj close\", \"volume\"]\n", 1173 | " df.index.name = \"time\"\n", 1174 | "\n", 1175 | " # Eliminar la columna adj close\n", 1176 | " del df[\"adj close\"]\n", 1177 | "\n", 1178 | " return df" 1179 | ], 1180 | "metadata": { 1181 | "id": "CAfQp8Jc4D5M" 1182 | }, 1183 | "execution_count": 12, 1184 | "outputs": [] 1185 | }, 1186 | { 1187 | "cell_type": "code", 1188 | "source": [ 1189 | "df = preprocessing_yf(\"EURUSD=X\")\n", 1190 | "df" 1191 | ], 1192 | "metadata": { 1193 | "id": "vnBDvdnP1EfF", 1194 | "colab": { 1195 | "base_uri": "https://localhost:8080/", 1196 | "height": 473 1197 | }, 1198 | "outputId": "fdf1e36f-66a0-47e0-ee01-a2a85d71ecec" 1199 | }, 1200 | "execution_count": 13, 1201 | "outputs": [ 1202 | { 1203 | "output_type": "stream", 1204 | "name": "stdout", 1205 | "text": [ 1206 | "\r[*********************100%***********************] 1 of 1 completed\n" 1207 | ] 1208 | }, 1209 | { 1210 | "output_type": "execute_result", 1211 | "data": { 1212 | "text/plain": [ 1213 | " open high low close volume\n", 1214 | "time \n", 1215 | "2003-12-01 1.203398 1.204007 1.194401 1.196501 0\n", 1216 | "2003-12-02 1.196101 1.210903 1.194600 1.208897 0\n", 1217 | "2003-12-03 1.209000 1.213003 1.207700 1.212298 0\n", 1218 | "2003-12-04 1.212004 1.214403 1.204398 1.208094 0\n", 1219 | "2003-12-05 1.207802 1.219096 1.206593 1.218695 0\n", 1220 | "... ... ... ... ... ...\n", 1221 | "2022-06-10 1.061921 1.064215 1.050773 1.061921 0\n", 1222 | "2022-06-13 1.048999 1.049759 1.041905 1.048999 0\n", 1223 | "2022-06-14 1.041515 1.048240 1.039825 1.041515 0\n", 1224 | "2022-06-15 1.044059 1.050718 1.038627 1.044059 0\n", 1225 | "2022-06-16 1.044714 1.060220 1.038314 1.059434 0\n", 1226 | "\n", 1227 | "[4810 rows x 5 columns]" 1228 | ], 1229 | "text/html": [ 1230 | "\n", 1231 | "
\n", 1232 | "
\n", 1233 | "
\n", 1234 | "\n", 1247 | "\n", 1248 | " \n", 1249 | " \n", 1250 | " \n", 1251 | " \n", 1252 | " \n", 1253 | " \n", 1254 | " \n", 1255 | " \n", 1256 | " \n", 1257 | " \n", 1258 | " \n", 1259 | " \n", 1260 | " \n", 1261 | " \n", 1262 | " \n", 1263 | " \n", 1264 | " \n", 1265 | " \n", 1266 | " \n", 1267 | " \n", 1268 | " \n", 1269 | " \n", 1270 | " \n", 1271 | " \n", 1272 | " \n", 1273 | " \n", 1274 | " \n", 1275 | " \n", 1276 | " \n", 1277 | " \n", 1278 | " \n", 1279 | " \n", 1280 | " \n", 1281 | " \n", 1282 | " \n", 1283 | " \n", 1284 | " \n", 1285 | " \n", 1286 | " \n", 1287 | " \n", 1288 | " \n", 1289 | " \n", 1290 | " \n", 1291 | " \n", 1292 | " \n", 1293 | " \n", 1294 | " \n", 1295 | " \n", 1296 | " \n", 1297 | " \n", 1298 | " \n", 1299 | " \n", 1300 | " \n", 1301 | " \n", 1302 | " \n", 1303 | " \n", 1304 | " \n", 1305 | " \n", 1306 | " \n", 1307 | " \n", 1308 | " \n", 1309 | " \n", 1310 | " \n", 1311 | " \n", 1312 | " \n", 1313 | " \n", 1314 | " \n", 1315 | " \n", 1316 | " \n", 1317 | " \n", 1318 | " \n", 1319 | " \n", 1320 | " \n", 1321 | " \n", 1322 | " \n", 1323 | " \n", 1324 | " \n", 1325 | " \n", 1326 | " \n", 1327 | " \n", 1328 | " \n", 1329 | " \n", 1330 | " \n", 1331 | " \n", 1332 | " \n", 1333 | " \n", 1334 | " \n", 1335 | " \n", 1336 | " \n", 1337 | " \n", 1338 | " \n", 1339 | " \n", 1340 | " \n", 1341 | " \n", 1342 | " \n", 1343 | " \n", 1344 | " \n", 1345 | " \n", 1346 | " \n", 1347 | " \n", 1348 | " \n", 1349 | " \n", 1350 | " \n", 1351 | " \n", 1352 | " \n", 1353 | " \n", 1354 | " \n", 1355 | " \n", 1356 | "
openhighlowclosevolume
time
2003-12-011.2033981.2040071.1944011.1965010
2003-12-021.1961011.2109031.1946001.2088970
2003-12-031.2090001.2130031.2077001.2122980
2003-12-041.2120041.2144031.2043981.2080940
2003-12-051.2078021.2190961.2065931.2186950
..................
2022-06-101.0619211.0642151.0507731.0619210
2022-06-131.0489991.0497591.0419051.0489990
2022-06-141.0415151.0482401.0398251.0415150
2022-06-151.0440591.0507181.0386271.0440590
2022-06-161.0447141.0602201.0383141.0594340
\n", 1357 | "

4810 rows × 5 columns

\n", 1358 | "
\n", 1359 | " \n", 1369 | " \n", 1370 | " \n", 1407 | "\n", 1408 | " \n", 1432 | "
\n", 1433 | "
\n", 1434 | " " 1435 | ] 1436 | }, 1437 | "metadata": {}, 1438 | "execution_count": 13 1439 | } 1440 | ] 1441 | } 1442 | ] 1443 | } -------------------------------------------------------------------------------- /ES_TA_Capítulo_09_MT5_Trading_en_Vivo_Random.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "id": "dacf6cd9", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Collecting numpy==1.19.2\n", 14 | " Downloading numpy-1.19.2-cp38-cp38-macosx_10_9_x86_64.whl (15.3 MB)\n", 15 | "\u001b[K |████████████████████████████████| 15.3 MB 23.6 MB/s eta 0:00:01\n", 16 | "\u001b[?25hInstalling collected packages: numpy\n", 17 | "\u001b[33m WARNING: The scripts f2py, f2py3 and f2py3.8 are installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 18 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 19 | "Successfully installed numpy-1.19.2\n", 20 | "Requirement already satisfied: scipy==1.6.2 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (1.6.2)\n", 21 | "Requirement already satisfied: numpy<1.23.0,>=1.16.5 in /Users/juangabriel/.local/lib/python3.8/site-packages (from scipy==1.6.2) (1.19.2)\n", 22 | "Collecting ta\n", 23 | " Downloading ta-0.10.1.tar.gz (24 kB)\n", 24 | "Requirement already satisfied: numpy in /Users/juangabriel/.local/lib/python3.8/site-packages (from ta) (1.19.2)\n", 25 | "Requirement already satisfied: pandas in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from ta) (1.2.4)\n", 26 | "Requirement already satisfied: python-dateutil>=2.7.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas->ta) (2.8.1)\n", 27 | "Requirement already satisfied: pytz>=2017.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas->ta) (2021.1)\n", 28 | "Requirement already satisfied: six>=1.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas->ta) (1.15.0)\n", 29 | "Building wheels for collected packages: ta\n", 30 | " Building wheel for ta (setup.py) ... \u001b[?25ldone\n", 31 | "\u001b[?25h Created wheel for ta: filename=ta-0.10.1-py3-none-any.whl size=28986 sha256=19fa3fed51f7d63585b496cb389f80668b180259336e597d8daea515771e391c\n", 32 | " Stored in directory: /Users/juangabriel/Library/Caches/pip/wheels/18/9a/81/694fa8602da445fa009fd13c8da25001be19efdfb67a9cc348\n", 33 | "Successfully built ta\n", 34 | "Installing collected packages: ta\n", 35 | "Successfully installed ta-0.10.1\n", 36 | "Collecting datetime\n", 37 | " Downloading DateTime-4.4-py2.py3-none-any.whl (51 kB)\n", 38 | "\u001b[K |████████████████████████████████| 51 kB 1.1 MB/s eta 0:00:01\n", 39 | "\u001b[?25hRequirement already satisfied: pytz in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from datetime) (2021.1)\n", 40 | "Requirement already satisfied: zope.interface in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from datetime) (5.3.0)\n", 41 | "Requirement already satisfied: setuptools in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from zope.interface->datetime) (52.0.0.post20210125)\n", 42 | "Installing collected packages: datetime\n", 43 | "Successfully installed datetime-4.4\n", 44 | "Collecting yfinance\n", 45 | " Downloading yfinance-0.1.72-py2.py3-none-any.whl (27 kB)\n", 46 | "Collecting requests>=2.26\n", 47 | " Downloading requests-2.28.0-py3-none-any.whl (62 kB)\n", 48 | "\u001b[K |████████████████████████████████| 62 kB 2.0 MB/s eta 0:00:011\n", 49 | "\u001b[?25hRequirement already satisfied: lxml>=4.5.1 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from yfinance) (4.6.3)\n", 50 | "Requirement already satisfied: numpy>=1.15 in /Users/juangabriel/.local/lib/python3.8/site-packages (from yfinance) (1.19.2)\n", 51 | "Requirement already satisfied: pandas>=0.24.0 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from yfinance) (1.2.4)\n", 52 | "Collecting multitasking>=0.0.7\n", 53 | " Downloading multitasking-0.0.10.tar.gz (8.2 kB)\n", 54 | "Requirement already satisfied: python-dateutil>=2.7.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas>=0.24.0->yfinance) (2.8.1)\n", 55 | "Requirement already satisfied: pytz>=2017.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas>=0.24.0->yfinance) (2021.1)\n", 56 | "Requirement already satisfied: six>=1.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.15.0)\n", 57 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (1.22)\n", 58 | "Requirement already satisfied: idna<4,>=2.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2.6)\n", 59 | "Requirement already satisfied: certifi>=2017.4.17 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2020.12.5)\n", 60 | "Collecting charset-normalizer~=2.0.0\n", 61 | " Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB)\n", 62 | "Building wheels for collected packages: multitasking\n", 63 | " Building wheel for multitasking (setup.py) ... \u001b[?25ldone\n", 64 | "\u001b[?25h Created wheel for multitasking: filename=multitasking-0.0.10-py3-none-any.whl size=8488 sha256=464b015e696019c4bbeb2915e0281594b248900ca08545b6be77a7fe15d58b1d\n", 65 | " Stored in directory: /Users/juangabriel/Library/Caches/pip/wheels/21/c9/66/b41c847de65c7985db52ec21d59996841598b8b0e93f2b9500\n", 66 | "Successfully built multitasking\n", 67 | "Installing collected packages: charset-normalizer, requests, multitasking, yfinance\n", 68 | "\u001b[33m WARNING: The script normalizer is installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 69 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 70 | "\u001b[33m WARNING: The script sample is installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 71 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 72 | "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", 73 | "conda-repo-cli 1.0.4 requires pathlib, which is not installed.\n", 74 | "anaconda-project 0.9.1 requires ruamel-yaml, which is not installed.\n", 75 | "sphinx 4.0.1 requires Jinja2<3.0,>=2.3, but you have jinja2 3.0.1 which is incompatible.\n", 76 | "sphinx 4.0.1 requires MarkupSafe<2.0, but you have markupsafe 2.0.1 which is incompatible.\u001b[0m\n", 77 | "Successfully installed charset-normalizer-2.0.12 multitasking-0.0.10 requests-2.28.0 yfinance-0.1.72\n", 78 | "\u001b[31mERROR: Could not find a version that satisfies the requirement MetaTrader5--user\u001b[0m\n", 79 | "\u001b[31mERROR: No matching distribution found for MetaTrader5--user\u001b[0m\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "import sys\n", 85 | "\n", 86 | "!{sys.executable} -m pip install numpy==1.19.2 --user\n", 87 | "!{sys.executable} -m pip install scipy==1.6.2 --user\n", 88 | "!{sys.executable} -m pip install ta --user\n", 89 | "!{sys.executable} -m pip install datetime --user\n", 90 | "!{sys.executable} -m pip install yfinance --user\n", 91 | "!{sys.executable} -m pip install MetaTrader5 --user" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 12, 97 | "id": "021d25d0", 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "------------------------------------------------------------------\n", 105 | "Date: 2022-01-14 08:33:32\n", 106 | "Balance: 10000.0 USD, \tEquity: 9991.28 USD, \tProfit: -8.72 USD\n", 107 | "------------------------------------------------------------------\n", 108 | "------------------------------------------------------------------\n", 109 | "Date: 2022-01-14 08:33:32\n", 110 | "SYMBOL: BTCUSD\n", 111 | "BUY: False \t SHORT: False\n", 112 | "POSITION: None \t ID: None\n", 113 | "------------------------------------------------------------------\n", 114 | "------------------------------------------------------------------\n", 115 | "Date: 2022-01-14 08:33:32\n", 116 | "SYMBOL: XMRUSD\n", 117 | "BUY: False \t SHORT: False\n", 118 | "POSITION: None \t ID: None\n", 119 | "------------------------------------------------------------------\n", 120 | "------------------------------------------------------------------\n", 121 | "Date: 2022-01-14 08:33:32\n", 122 | "SYMBOL: ETHUSD\n", 123 | "BUY: False \t SHORT: False\n", 124 | "POSITION: None \t ID: None\n", 125 | "------------------------------------------------------------------\n", 126 | "------------------------------------------------------------------\n", 127 | "Date: 2022-01-14 08:33:32\n", 128 | "SYMBOL: ETCUSD\n", 129 | "BUY: False \t SHORT: False\n", 130 | "POSITION: None \t ID: None\n", 131 | "------------------------------------------------------------------\n", 132 | "------------------------------------------------------------------\n", 133 | "Date: 2022-01-14 08:33:32\n", 134 | "SYMBOL: ZECUSD\n", 135 | "BUY: False \t SHORT: False\n", 136 | "POSITION: None \t ID: None\n", 137 | "------------------------------------------------------------------\n", 138 | "------------------------------------------------------------------\n", 139 | "Date: 2022-01-14 08:33:32\n", 140 | "SYMBOL: BTCUSD\n", 141 | "BUY: False \t SHORT: False\n", 142 | "POSITION: None \t ID: None\n", 143 | "------------------------------------------------------------------\n", 144 | "------------------------------------------------------------------\n", 145 | "Date: 2022-01-14 08:33:32\n", 146 | "SYMBOL: XMRUSD\n", 147 | "BUY: False \t SHORT: False\n", 148 | "POSITION: None \t ID: None\n", 149 | "------------------------------------------------------------------\n", 150 | "------------------------------------------------------------------\n", 151 | "Date: 2022-01-14 08:33:32\n", 152 | "SYMBOL: ETHUSD\n", 153 | "BUY: False \t SHORT: False\n", 154 | "POSITION: None \t ID: None\n", 155 | "------------------------------------------------------------------\n", 156 | "------------------------------------------------------------------\n", 157 | "Date: 2022-01-14 08:33:32\n", 158 | "SYMBOL: ETCUSD\n", 159 | "BUY: False \t SHORT: False\n", 160 | "POSITION: None \t ID: None\n", 161 | "------------------------------------------------------------------\n", 162 | "------------------------------------------------------------------\n", 163 | "Date: 2022-01-14 08:33:32\n", 164 | "SYMBOL: ZECUSD\n", 165 | "BUY: False \t SHORT: False\n", 166 | "POSITION: None \t ID: None\n", 167 | "------------------------------------------------------------------\n", 168 | "------------------------------------------------------------------\n", 169 | "Date: 2022-01-14 08:33:32\n", 170 | "SYMBOL: BTCUSD\n", 171 | "BUY: False \t SHORT: False\n", 172 | "POSITION: None \t ID: None\n", 173 | "------------------------------------------------------------------\n", 174 | "------------------------------------------------------------------\n", 175 | "Date: 2022-01-14 08:33:32\n", 176 | "SYMBOL: XMRUSD\n", 177 | "BUY: False \t SHORT: False\n", 178 | "POSITION: None \t ID: None\n", 179 | "------------------------------------------------------------------\n", 180 | "------------------------------------------------------------------\n", 181 | "Date: 2022-01-14 08:33:32\n", 182 | "SYMBOL: ETHUSD\n", 183 | "BUY: False \t SHORT: False\n", 184 | "POSITION: None \t ID: None\n", 185 | "------------------------------------------------------------------\n", 186 | "------------------------------------------------------------------\n", 187 | "Date: 2022-01-14 08:33:32\n", 188 | "SYMBOL: ETCUSD\n", 189 | "BUY: False \t SHORT: False\n", 190 | "POSITION: None \t ID: None\n", 191 | "------------------------------------------------------------------\n", 192 | "------------------------------------------------------------------\n", 193 | "Date: 2022-01-14 08:33:32\n", 194 | "SYMBOL: ZECUSD\n", 195 | "BUY: False \t SHORT: False\n", 196 | "POSITION: None \t ID: None\n", 197 | "------------------------------------------------------------------\n", 198 | "------------------------------------------------------------------\n", 199 | "Date: 2022-01-14 08:33:32\n", 200 | "SYMBOL: BTCUSD\n", 201 | "BUY: False \t SHORT: False\n", 202 | "POSITION: None \t ID: None\n", 203 | "------------------------------------------------------------------\n", 204 | "------------------------------------------------------------------\n", 205 | "Date: 2022-01-14 08:33:32\n", 206 | "SYMBOL: XMRUSD\n", 207 | "BUY: False \t SHORT: False\n", 208 | "POSITION: None \t ID: None\n", 209 | "------------------------------------------------------------------\n", 210 | "------------------------------------------------------------------\n", 211 | "Date: 2022-01-14 08:33:32\n", 212 | "SYMBOL: ETHUSD\n", 213 | "BUY: False \t SHORT: False\n", 214 | "POSITION: None \t ID: None\n", 215 | "------------------------------------------------------------------\n", 216 | "------------------------------------------------------------------\n", 217 | "Date: 2022-01-14 08:33:33\n", 218 | "SYMBOL: ETCUSD\n", 219 | "BUY: False \t SHORT: False\n", 220 | "POSITION: None \t ID: None\n", 221 | "------------------------------------------------------------------\n", 222 | "------------------------------------------------------------------\n", 223 | "Date: 2022-01-14 08:33:33\n", 224 | "SYMBOL: ZECUSD\n", 225 | "BUY: False \t SHORT: False\n", 226 | "POSITION: None \t ID: None\n", 227 | "------------------------------------------------------------------\n" 228 | ] 229 | }, 230 | { 231 | "ename": "KeyboardInterrupt", 232 | "evalue": "", 233 | "output_type": "error", 234 | "traceback": [ 235 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 236 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 237 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_5916/4152256203.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[1;31m# Verfication for launch\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 73\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mweekday\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# If you want to trade only from Monday to Friday\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 74\u001b[1;33m \u001b[0mis_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrftime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%H:%M:%S\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mstart\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 75\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[0mis_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 238 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: " 239 | ] 240 | } 241 | ], 242 | "source": [ 243 | "from Chapter_09_MT5 import *\n", 244 | "import warnings\n", 245 | "warnings.filterwarnings(\"ignore\")\n", 246 | "import time\n", 247 | "import numpy as np\n", 248 | "\n", 249 | "import MetaTrader5 as mt5\n", 250 | "mt5.initialize()\n", 251 | "\n", 252 | "\n", 253 | "def random():\n", 254 | " \n", 255 | " values = [True, False]\n", 256 | " \n", 257 | " buy = np.random.choice(values)\n", 258 | " sell = not buy\n", 259 | " \n", 260 | "\n", 261 | " return buy, sell\n", 262 | "\n", 263 | "\n", 264 | "symbols_list = {\n", 265 | " \"Nasdaq 100\": [\"NAS100\", 0.1],\n", 266 | " \"Russel 2000\": [\"US2000\", 0.1],\n", 267 | " \"Gold USD\": [\"XAUUSD\", 0.01],\n", 268 | " \"S&P 500\": [\"US500\", 0.1],\n", 269 | " \"US dollar vs Canadian dollar\": [\"USDCAD\", 0.01],\n", 270 | " \"Euro vs US dollar\": [\"EURUSD\", 0.01]\n", 271 | "}\n", 272 | "\n", 273 | "current_account_info = mt5.account_info()\n", 274 | "print(\"------------------------------------------------------------------\")\n", 275 | "print(\"Date: \", datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))\n", 276 | "print(f\"Balance: {current_account_info.balance} USD, \\t\"\n", 277 | " f\"Equity: {current_account_info.equity} USD, \\t\"\n", 278 | " f\"Profit: {current_account_info.profit} USD\")\n", 279 | "print(\"------------------------------------------------------------------\")\n", 280 | "\n", 281 | "\n", 282 | "start = datetime.now().strftime(\"%H:%M:%S\") #\"23:59:59\"\n", 283 | "\n", 284 | "while True:\n", 285 | "\n", 286 | " # Verificación para lanzar el algoritmo\n", 287 | " if datetime.now().weekday() not in (5,6): # Para hacer trading solamente de Lunes a Viernes\n", 288 | " is_time = datetime.now().strftime(\"%H:%M:%S\") == start \n", 289 | " else:\n", 290 | " is_time = False\n", 291 | "\n", 292 | " \n", 293 | " # Lanzamos el algoritmo\n", 294 | " if is_time:\n", 295 | "\n", 296 | " # Podemos cerrar todas las posiciones abiertas\n", 297 | " if mt5.positions_total() > 0:\n", 298 | " MT5.close_all_night()\n", 299 | " \n", 300 | " # Abrimos el trading\n", 301 | " for asset in symbols_list.keys():\n", 302 | "\n", 303 | " # Inicializamos la entrada de datos\n", 304 | " symbol = symbols_list[asset][0]\n", 305 | " lot = symbols_list[asset][1]\n", 306 | "\n", 307 | " # Creamos la señal\n", 308 | " buy, sell = random()\n", 309 | "\n", 310 | " # Ejecutamos el algoritmo\n", 311 | " MT5.run(symbol, buy, sell, lot)\n" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "id": "36b23b8b", 318 | "metadata": {}, 319 | "outputs": [], 320 | "source": [] 321 | } 322 | ], 323 | "metadata": { 324 | "kernelspec": { 325 | "display_name": "Python 3", 326 | "language": "python", 327 | "name": "python3" 328 | }, 329 | "language_info": { 330 | "codemirror_mode": { 331 | "name": "ipython", 332 | "version": 3 333 | }, 334 | "file_extension": ".py", 335 | "mimetype": "text/x-python", 336 | "name": "python", 337 | "nbconvert_exporter": "python", 338 | "pygments_lexer": "ipython3", 339 | "version": "3.8.8" 340 | } 341 | }, 342 | "nbformat": 4, 343 | "nbformat_minor": 5 344 | } 345 | -------------------------------------------------------------------------------- /ES_TA_Capítulo_09_MT5_Trading_en_Vivo_SMA.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "id": "dbea0502", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Collecting numpy==1.19.2\n", 14 | " Downloading numpy-1.19.2-cp38-cp38-macosx_10_9_x86_64.whl (15.3 MB)\n", 15 | "\u001b[K |████████████████████████████████| 15.3 MB 23.6 MB/s eta 0:00:01\n", 16 | "\u001b[?25hInstalling collected packages: numpy\n", 17 | "\u001b[33m WARNING: The scripts f2py, f2py3 and f2py3.8 are installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 18 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 19 | "Successfully installed numpy-1.19.2\n", 20 | "Requirement already satisfied: scipy==1.6.2 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (1.6.2)\n", 21 | "Requirement already satisfied: numpy<1.23.0,>=1.16.5 in /Users/juangabriel/.local/lib/python3.8/site-packages (from scipy==1.6.2) (1.19.2)\n", 22 | "Collecting ta\n", 23 | " Downloading ta-0.10.1.tar.gz (24 kB)\n", 24 | "Requirement already satisfied: numpy in /Users/juangabriel/.local/lib/python3.8/site-packages (from ta) (1.19.2)\n", 25 | "Requirement already satisfied: pandas in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from ta) (1.2.4)\n", 26 | "Requirement already satisfied: python-dateutil>=2.7.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas->ta) (2.8.1)\n", 27 | "Requirement already satisfied: pytz>=2017.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas->ta) (2021.1)\n", 28 | "Requirement already satisfied: six>=1.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas->ta) (1.15.0)\n", 29 | "Building wheels for collected packages: ta\n", 30 | " Building wheel for ta (setup.py) ... \u001b[?25ldone\n", 31 | "\u001b[?25h Created wheel for ta: filename=ta-0.10.1-py3-none-any.whl size=28986 sha256=19fa3fed51f7d63585b496cb389f80668b180259336e597d8daea515771e391c\n", 32 | " Stored in directory: /Users/juangabriel/Library/Caches/pip/wheels/18/9a/81/694fa8602da445fa009fd13c8da25001be19efdfb67a9cc348\n", 33 | "Successfully built ta\n", 34 | "Installing collected packages: ta\n", 35 | "Successfully installed ta-0.10.1\n", 36 | "Collecting datetime\n", 37 | " Downloading DateTime-4.4-py2.py3-none-any.whl (51 kB)\n", 38 | "\u001b[K |████████████████████████████████| 51 kB 1.1 MB/s eta 0:00:01\n", 39 | "\u001b[?25hRequirement already satisfied: pytz in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from datetime) (2021.1)\n", 40 | "Requirement already satisfied: zope.interface in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from datetime) (5.3.0)\n", 41 | "Requirement already satisfied: setuptools in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from zope.interface->datetime) (52.0.0.post20210125)\n", 42 | "Installing collected packages: datetime\n", 43 | "Successfully installed datetime-4.4\n", 44 | "Collecting yfinance\n", 45 | " Downloading yfinance-0.1.72-py2.py3-none-any.whl (27 kB)\n", 46 | "Collecting requests>=2.26\n", 47 | " Downloading requests-2.28.0-py3-none-any.whl (62 kB)\n", 48 | "\u001b[K |████████████████████████████████| 62 kB 2.0 MB/s eta 0:00:011\n", 49 | "\u001b[?25hRequirement already satisfied: lxml>=4.5.1 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from yfinance) (4.6.3)\n", 50 | "Requirement already satisfied: numpy>=1.15 in /Users/juangabriel/.local/lib/python3.8/site-packages (from yfinance) (1.19.2)\n", 51 | "Requirement already satisfied: pandas>=0.24.0 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from yfinance) (1.2.4)\n", 52 | "Collecting multitasking>=0.0.7\n", 53 | " Downloading multitasking-0.0.10.tar.gz (8.2 kB)\n", 54 | "Requirement already satisfied: python-dateutil>=2.7.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas>=0.24.0->yfinance) (2.8.1)\n", 55 | "Requirement already satisfied: pytz>=2017.3 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from pandas>=0.24.0->yfinance) (2021.1)\n", 56 | "Requirement already satisfied: six>=1.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.15.0)\n", 57 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (1.22)\n", 58 | "Requirement already satisfied: idna<4,>=2.5 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2.6)\n", 59 | "Requirement already satisfied: certifi>=2017.4.17 in /Users/juangabriel/opt/anaconda3/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2020.12.5)\n", 60 | "Collecting charset-normalizer~=2.0.0\n", 61 | " Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB)\n", 62 | "Building wheels for collected packages: multitasking\n", 63 | " Building wheel for multitasking (setup.py) ... \u001b[?25ldone\n", 64 | "\u001b[?25h Created wheel for multitasking: filename=multitasking-0.0.10-py3-none-any.whl size=8488 sha256=464b015e696019c4bbeb2915e0281594b248900ca08545b6be77a7fe15d58b1d\n", 65 | " Stored in directory: /Users/juangabriel/Library/Caches/pip/wheels/21/c9/66/b41c847de65c7985db52ec21d59996841598b8b0e93f2b9500\n", 66 | "Successfully built multitasking\n", 67 | "Installing collected packages: charset-normalizer, requests, multitasking, yfinance\n", 68 | "\u001b[33m WARNING: The script normalizer is installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 69 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 70 | "\u001b[33m WARNING: The script sample is installed in '/Users/juangabriel/.local/bin' which is not on PATH.\n", 71 | " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n", 72 | "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", 73 | "conda-repo-cli 1.0.4 requires pathlib, which is not installed.\n", 74 | "anaconda-project 0.9.1 requires ruamel-yaml, which is not installed.\n", 75 | "sphinx 4.0.1 requires Jinja2<3.0,>=2.3, but you have jinja2 3.0.1 which is incompatible.\n", 76 | "sphinx 4.0.1 requires MarkupSafe<2.0, but you have markupsafe 2.0.1 which is incompatible.\u001b[0m\n", 77 | "Successfully installed charset-normalizer-2.0.12 multitasking-0.0.10 requests-2.28.0 yfinance-0.1.72\n", 78 | "\u001b[31mERROR: Could not find a version that satisfies the requirement MetaTrader5--user\u001b[0m\n", 79 | "\u001b[31mERROR: No matching distribution found for MetaTrader5--user\u001b[0m\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "import sys\n", 85 | "\n", 86 | "!{sys.executable} -m pip install numpy==1.19.2 --user\n", 87 | "!{sys.executable} -m pip install scipy==1.6.2 --user\n", 88 | "!{sys.executable} -m pip install ta --user\n", 89 | "!{sys.executable} -m pip install datetime --user\n", 90 | "!{sys.executable} -m pip install yfinance --user\n", 91 | "!{sys.executable} -m pip install MetaTrader5 --user" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 12, 97 | "id": "021d25d0", 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "------------------------------------------------------------------\n", 105 | "Date: 2022-01-14 08:33:32\n", 106 | "Balance: 10000.0 USD, \tEquity: 9991.28 USD, \tProfit: -8.72 USD\n", 107 | "------------------------------------------------------------------\n", 108 | "------------------------------------------------------------------\n", 109 | "Date: 2022-01-14 08:33:32\n", 110 | "SYMBOL: BTCUSD\n", 111 | "BUY: False \t SHORT: False\n", 112 | "POSITION: None \t ID: None\n", 113 | "------------------------------------------------------------------\n", 114 | "------------------------------------------------------------------\n", 115 | "Date: 2022-01-14 08:33:32\n", 116 | "SYMBOL: XMRUSD\n", 117 | "BUY: False \t SHORT: False\n", 118 | "POSITION: None \t ID: None\n", 119 | "------------------------------------------------------------------\n", 120 | "------------------------------------------------------------------\n", 121 | "Date: 2022-01-14 08:33:32\n", 122 | "SYMBOL: ETHUSD\n", 123 | "BUY: False \t SHORT: False\n", 124 | "POSITION: None \t ID: None\n", 125 | "------------------------------------------------------------------\n", 126 | "------------------------------------------------------------------\n", 127 | "Date: 2022-01-14 08:33:32\n", 128 | "SYMBOL: ETCUSD\n", 129 | "BUY: False \t SHORT: False\n", 130 | "POSITION: None \t ID: None\n", 131 | "------------------------------------------------------------------\n", 132 | "------------------------------------------------------------------\n", 133 | "Date: 2022-01-14 08:33:32\n", 134 | "SYMBOL: ZECUSD\n", 135 | "BUY: False \t SHORT: False\n", 136 | "POSITION: None \t ID: None\n", 137 | "------------------------------------------------------------------\n", 138 | "------------------------------------------------------------------\n", 139 | "Date: 2022-01-14 08:33:32\n", 140 | "SYMBOL: BTCUSD\n", 141 | "BUY: False \t SHORT: False\n", 142 | "POSITION: None \t ID: None\n", 143 | "------------------------------------------------------------------\n", 144 | "------------------------------------------------------------------\n", 145 | "Date: 2022-01-14 08:33:32\n", 146 | "SYMBOL: XMRUSD\n", 147 | "BUY: False \t SHORT: False\n", 148 | "POSITION: None \t ID: None\n", 149 | "------------------------------------------------------------------\n", 150 | "------------------------------------------------------------------\n", 151 | "Date: 2022-01-14 08:33:32\n", 152 | "SYMBOL: ETHUSD\n", 153 | "BUY: False \t SHORT: False\n", 154 | "POSITION: None \t ID: None\n", 155 | "------------------------------------------------------------------\n", 156 | "------------------------------------------------------------------\n", 157 | "Date: 2022-01-14 08:33:32\n", 158 | "SYMBOL: ETCUSD\n", 159 | "BUY: False \t SHORT: False\n", 160 | "POSITION: None \t ID: None\n", 161 | "------------------------------------------------------------------\n", 162 | "------------------------------------------------------------------\n", 163 | "Date: 2022-01-14 08:33:32\n", 164 | "SYMBOL: ZECUSD\n", 165 | "BUY: False \t SHORT: False\n", 166 | "POSITION: None \t ID: None\n", 167 | "------------------------------------------------------------------\n", 168 | "------------------------------------------------------------------\n", 169 | "Date: 2022-01-14 08:33:32\n", 170 | "SYMBOL: BTCUSD\n", 171 | "BUY: False \t SHORT: False\n", 172 | "POSITION: None \t ID: None\n", 173 | "------------------------------------------------------------------\n", 174 | "------------------------------------------------------------------\n", 175 | "Date: 2022-01-14 08:33:32\n", 176 | "SYMBOL: XMRUSD\n", 177 | "BUY: False \t SHORT: False\n", 178 | "POSITION: None \t ID: None\n", 179 | "------------------------------------------------------------------\n", 180 | "------------------------------------------------------------------\n", 181 | "Date: 2022-01-14 08:33:32\n", 182 | "SYMBOL: ETHUSD\n", 183 | "BUY: False \t SHORT: False\n", 184 | "POSITION: None \t ID: None\n", 185 | "------------------------------------------------------------------\n", 186 | "------------------------------------------------------------------\n", 187 | "Date: 2022-01-14 08:33:32\n", 188 | "SYMBOL: ETCUSD\n", 189 | "BUY: False \t SHORT: False\n", 190 | "POSITION: None \t ID: None\n", 191 | "------------------------------------------------------------------\n", 192 | "------------------------------------------------------------------\n", 193 | "Date: 2022-01-14 08:33:32\n", 194 | "SYMBOL: ZECUSD\n", 195 | "BUY: False \t SHORT: False\n", 196 | "POSITION: None \t ID: None\n", 197 | "------------------------------------------------------------------\n", 198 | "------------------------------------------------------------------\n", 199 | "Date: 2022-01-14 08:33:32\n", 200 | "SYMBOL: BTCUSD\n", 201 | "BUY: False \t SHORT: False\n", 202 | "POSITION: None \t ID: None\n", 203 | "------------------------------------------------------------------\n", 204 | "------------------------------------------------------------------\n", 205 | "Date: 2022-01-14 08:33:32\n", 206 | "SYMBOL: XMRUSD\n", 207 | "BUY: False \t SHORT: False\n", 208 | "POSITION: None \t ID: None\n", 209 | "------------------------------------------------------------------\n", 210 | "------------------------------------------------------------------\n", 211 | "Date: 2022-01-14 08:33:32\n", 212 | "SYMBOL: ETHUSD\n", 213 | "BUY: False \t SHORT: False\n", 214 | "POSITION: None \t ID: None\n", 215 | "------------------------------------------------------------------\n", 216 | "------------------------------------------------------------------\n", 217 | "Date: 2022-01-14 08:33:33\n", 218 | "SYMBOL: ETCUSD\n", 219 | "BUY: False \t SHORT: False\n", 220 | "POSITION: None \t ID: None\n", 221 | "------------------------------------------------------------------\n", 222 | "------------------------------------------------------------------\n", 223 | "Date: 2022-01-14 08:33:33\n", 224 | "SYMBOL: ZECUSD\n", 225 | "BUY: False \t SHORT: False\n", 226 | "POSITION: None \t ID: None\n", 227 | "------------------------------------------------------------------\n" 228 | ] 229 | }, 230 | { 231 | "ename": "KeyboardInterrupt", 232 | "evalue": "", 233 | "output_type": "error", 234 | "traceback": [ 235 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 236 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 237 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_5916/4152256203.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[1;31m# Verfication for launch\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 73\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mweekday\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# If you want to trade only from Monday to Friday\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 74\u001b[1;33m \u001b[0mis_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrftime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%H:%M:%S\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mstart\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 75\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[0mis_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 238 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: " 239 | ] 240 | } 241 | ], 242 | "source": [ 243 | "import ta\n", 244 | "import numpy as np\n", 245 | "import warnings\n", 246 | "from datetime import datetime\n", 247 | "import pandas as pd\n", 248 | "import MetaTrader5 as mt5\n", 249 | "warnings.filterwarnings(\"ignore\")\n", 250 | "mt5.initialize()\n", 251 | "from Chapter_09_MT5 import *\n", 252 | "import time\n", 253 | "\n", 254 | "\n", 255 | "\n", 256 | "\n", 257 | "\n", 258 | "def SMA_strategy(symbol):\n", 259 | " \n", 260 | " # Import / Features engineering\n", 261 | " df = MT5.get_data(symbol, 70)\n", 262 | " \n", 263 | " # Creamos la Resistencia haciendo un rolling máximo\n", 264 | " df[\"SMA fast\"] = df[\"close\"].rolling(30).mean()\n", 265 | "\n", 266 | " # Creamos el Soporte haciendo un rolling mínimo\n", 267 | " df[\"SMA slow\"] = df[\"close\"].rolling(60).mean()\n", 268 | "\n", 269 | "\n", 270 | " # Señales\n", 271 | " condition_1_buy = df[\"SMA fast\"].iloc[-1] > df[\"SMA slow\"].iloc[-1]\n", 272 | " \n", 273 | " condition_1_sell = df[\"SMA fast\"].iloc[-1] < df[\"SMA slow\"].iloc[-1]\n", 274 | " \n", 275 | " buy = condition_1_buy\n", 276 | " sell = condition_1_sell\n", 277 | " \n", 278 | "\n", 279 | " return buy, sell\n", 280 | "\n", 281 | "\n", 282 | "mt5.initialize()\n", 283 | "# True = Live Trading and False = Screener\n", 284 | "live = True\n", 285 | "\n", 286 | "if live:\n", 287 | " current_account_info = mt5.account_info()\n", 288 | " print(\"------------------------------------------------------------------\")\n", 289 | " print(\"Date: \", datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))\n", 290 | " print(f\"Balance: {current_account_info.balance} USD, \\t\"\n", 291 | " f\"Equity: {current_account_info.equity} USD, \\t\"\n", 292 | " f\"Profit: {current_account_info.profit} USD\")\n", 293 | " print(\"------------------------------------------------------------------\")\n", 294 | "\n", 295 | "\n", 296 | "info_order = {\n", 297 | " \"Bitcoin\": [\"EURUSD\", 1.]\n", 298 | " \n", 299 | "}\n", 300 | "\n", 301 | "\n", 302 | "start = datetime.now().strftime(\"%H:%M:%S\") #\"23:59:59\"\n", 303 | "while True:\n", 304 | "\n", 305 | " # Verificación para lanzar el algoritmo\n", 306 | " if datetime.now().weekday() not in (5,6): # Para hacer trading solamente de Lunes a Viernes\n", 307 | " is_time = datetime.now().strftime(\"%H:%M:%S\") == start \n", 308 | " else:\n", 309 | " is_time = False\n", 310 | "\n", 311 | " \n", 312 | " # Lanzamos el algoritmo\n", 313 | " if is_time:\n", 314 | "\n", 315 | " # Abrimos el trading\n", 316 | " for asset in info_order.keys():\n", 317 | "\n", 318 | " # Inicializamos la entrada de datos\n", 319 | " symbol = info_order[asset][0]\n", 320 | " lot = info_order[asset][1]\n", 321 | "\n", 322 | " # Creamos la señal\n", 323 | " buy, sell = SMA_strategy(symbol)\n", 324 | "\n", 325 | " # Ejecutamos el algoritmo\n", 326 | " if live:\n", 327 | " MT5.run(symbol, buy, sell,lot)\n", 328 | "\n", 329 | " else:\n", 330 | " print(f\"Symbol: {symbol}\\t\"\n", 331 | " f\"Buy: {buy}\\t\"\n", 332 | " f\"Sell: {sell}\")\n" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "id": "36b23b8b", 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [] 342 | } 343 | ], 344 | "metadata": { 345 | "kernelspec": { 346 | "display_name": "Python 3", 347 | "language": "python", 348 | "name": "python3" 349 | }, 350 | "language_info": { 351 | "codemirror_mode": { 352 | "name": "ipython", 353 | "version": 3 354 | }, 355 | "file_extension": ".py", 356 | "mimetype": "text/x-python", 357 | "name": "python", 358 | "nbconvert_exporter": "python", 359 | "pygments_lexer": "ipython3", 360 | "version": "3.8.8" 361 | } 362 | }, 363 | "nbformat": 4, 364 | "nbformat_minor": 5 365 | } 366 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Trading Algorítmico de la A a la Z con Python 2 | 🇪🇸 Puedes apuntarte en nuestro curso en: https://cursos.frogamesformacion.com/courses/trading-algoritmico-1 3 | 4 | O obtener la Ruta completa de trading algorítmico en: https://cursos.frogamesformacion.com/bundles/ruta-trading 5 | 6 | ### Recursos 7 | 8 | 9 | 💰 Únete a la comunidad de [Discord](https://discord.gg/z3dx5XpkX4) 10 | 11 | 📚 Puedes leer nuestro libro en [Amazon](https://www.amazon.es/Python-para-finanzas-trading-algor%C3%ADtmico-ebook/dp/B0BT4ZS9Q3) 12 | 13 | 🖥️ El canal de [YouTube de Quantreo's](https://www.youtube.com/channel/UCp7jckfiEglNf_Gj62VR0pw) (en inglés) y el de [Frogames](https://www.youtube.com/channel/UCMUxXNYrVCv6-bQakhomvBg) en Español 14 | 15 | 16 | 17 | ### VPS / Instalar Windows en tu Mac 18 | 19 | VPS: https://billing.virmach.com/aff.php?aff=10561 20 | 21 | BOOT CAMP MAC: https://www.youtube.com/watch?v=Hmm9Q-T0oTo 22 | 23 | Parallels Desktop: https://www.parallels.com/ 24 | 25 | 26 | ### Recursos 27 | 28 | 💰 Únete a la comunidad de Discord: https://discord.gg/wXjNPAc5BH 29 | 30 | 📚 Puedes leer nuestro libro en: https://www.amazon.com/gp/product/B09HG18CYL 31 | 32 | 🖥️ El canal de YouTube de Quantreo's (en inglés): https://www.youtube.com/channel/UCp7jckfiEglNf_Gj62VR0pw 33 | 34 | 35 | 36 | ### VPS / Instalar Windows en tu Mac 37 | 38 | VPS: https://billing.virmach.com/aff.php?aff=10561 39 | 40 | BOOT CAMP MAC: https://www.youtube.com/watch?v=Hmm9Q-T0oTo 41 | 42 | Parallels Desktop: https://www.parallels.com/ 43 | --------------------------------------------------------------------------------