{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "WeightsGeneration.ipynb", "provenance": [], "authorship_tag": "ABX9TyNh42nx5XeUhi5bLVY4JfQ6", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "title", "colab_type": "text" }, "source": [ "## Weights Generation" ] }, { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "metadata": { "id": "3AtXFs60NdKV", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "outputId": "1e462b8b-7ccb-4bc5-8c91-7a613a52b10e" }, "source": [ "!pip install mlfinlab\n", "!pip install yfinance\n", "# See https://plotly.com/python/static-image-export/\n", "!wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O /usr/local/bin/orca\n", "!chmod +x /usr/local/bin/orca\n", "!apt-get install xvfb libgtk2.0-0 libgconf-2-4" ], "execution_count": 1, "outputs": [ { "output_type": "stream", "text": [ "Collecting mlfinlab\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/a9/9b/d6a2e5ec3818c9294d6e9e34a522c6ca7746efd4bd4e9640bbc4233ab2f0/mlfinlab-0.11.2-py3-none-any.whl (800kB)\n", "\u001b[K |████████████████████████████████| 808kB 2.8MB/s \n", "\u001b[?25hCollecting xmlrunner==1.7.7\n", " Downloading https://files.pythonhosted.org/packages/57/c0/a19e29bc6038a56bb690549573af6ea11a9d2a5c07aff2e27ed308c2cab9/xmlrunner-1.7.7.tar.gz\n", "Collecting numpy==1.17.3\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/0e/46/ae6773894f7eacf53308086287897ec568eac9768918d913d5b9d366c5db/numpy-1.17.3-cp36-cp36m-manylinux1_x86_64.whl (20.0MB)\n", "\u001b[K |████████████████████████████████| 20.0MB 1.3MB/s \n", "\u001b[?25hCollecting numba==0.49.1\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/56/dc/0e3d3732fd62c73fbb3317fc7bba22574832ab7a8e075620557bd4311641/numba-0.49.1-cp36-cp36m-manylinux2014_x86_64.whl (3.6MB)\n", "\u001b[K |████████████████████████████████| 3.6MB 43.8MB/s \n", "\u001b[?25hCollecting pandas==0.25.3\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/52/3f/f6a428599e0d4497e1595030965b5ba455fd8ade6e977e3c819973c4b41d/pandas-0.25.3-cp36-cp36m-manylinux1_x86_64.whl (10.4MB)\n", "\u001b[K |████████████████████████████████| 10.4MB 44.8MB/s \n", "\u001b[?25hCollecting matplotlib==3.1.1\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/57/4f/dd381ecf6c6ab9bcdaa8ea912e866dedc6e696756156d8ecc087e20817e2/matplotlib-3.1.1-cp36-cp36m-manylinux1_x86_64.whl (13.1MB)\n", "\u001b[K |████████████████████████████████| 13.1MB 321kB/s \n", "\u001b[?25hCollecting scipy==1.3.1\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/29/50/a552a5aff252ae915f522e44642bb49a7b7b31677f9580cfd11bcc869976/scipy-1.3.1-cp36-cp36m-manylinux1_x86_64.whl (25.2MB)\n", "\u001b[K |████████████████████████████████| 25.2MB 70.6MB/s \n", "\u001b[?25hCollecting cvxpy==1.0.25\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/d9/ed/90e0a13ad7ac4e7cdc2aeaefed26cebb4922f205bb778199268863fa2fbe/cvxpy-1.0.25.tar.gz (906kB)\n", "\u001b[K |████████████████████████████████| 911kB 44.4MB/s \n", "\u001b[?25hCollecting scikit-learn==0.21.3\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/a0/c5/d2238762d780dde84a20b8c761f563fe882b88c5a5fb03c056547c442a19/scikit_learn-0.21.3-cp36-cp36m-manylinux1_x86_64.whl (6.7MB)\n", "\u001b[K |████████████████████████████████| 6.7MB 40.0MB/s \n", "\u001b[?25hCollecting statsmodels==0.11.1\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/cb/83/540fd83238a18abe6c2d280fa8e489ac5fcefa1f370f0ca1acd16ae1b860/statsmodels-0.11.1-cp36-cp36m-manylinux1_x86_64.whl (8.7MB)\n", "\u001b[K |████████████████████████████████| 8.7MB 40.8MB/s \n", "\u001b[?25hRequirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from numba==0.49.1->mlfinlab) (47.1.1)\n", "Requirement already satisfied: llvmlite<=0.33.0.dev0,>=0.31.0.dev0 in /usr/local/lib/python3.6/dist-packages (from numba==0.49.1->mlfinlab) (0.31.0)\n", "Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas==0.25.3->mlfinlab) (2.8.1)\n", "Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas==0.25.3->mlfinlab) (2018.9)\n", "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.1.1->mlfinlab) (2.4.7)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.1.1->mlfinlab) (0.10.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.1.1->mlfinlab) (1.2.0)\n", "Requirement already satisfied: osqp>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (0.6.1)\n", "Requirement already satisfied: ecos>=2 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (2.0.7.post1)\n", "Requirement already satisfied: scs>=1.1.3 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (2.1.2)\n", "Requirement already satisfied: multiprocess in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (0.70.9)\n", "Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (1.12.0)\n", "Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.6/dist-packages (from scikit-learn==0.21.3->mlfinlab) (0.15.1)\n", "Requirement already satisfied: patsy>=0.5 in /usr/local/lib/python3.6/dist-packages (from statsmodels==0.11.1->mlfinlab) (0.5.1)\n", "Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from osqp>=0.4.1->cvxpy==1.0.25->mlfinlab) (0.16.0)\n", "Requirement already satisfied: dill>=0.3.1 in /usr/local/lib/python3.6/dist-packages (from multiprocess->cvxpy==1.0.25->mlfinlab) (0.3.1.1)\n", "Building wheels for collected packages: xmlrunner, cvxpy\n", " Building wheel for xmlrunner (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for xmlrunner: filename=xmlrunner-1.7.7-cp36-none-any.whl size=6235 sha256=d3de0603af5ce7c55cb4e3d3b4488ddcff5646f1d3ef9c62efc0499342c019b3\n", " Stored in directory: /root/.cache/pip/wheels/c8/0e/05/28e4ff5b683c7a319756b412c7f4dc508a7ffa654e12c54ef5\n", " Building wheel for cvxpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for cvxpy: filename=cvxpy-1.0.25-cp36-cp36m-linux_x86_64.whl size=2044780 sha256=3ac17e96db36af7bac66d4ec39673d18ae1e8698e71b8fc727459a8a8d2a404f\n", " Stored in directory: /root/.cache/pip/wheels/f1/b0/c3/0d3e98435c5070047e033ead500eb4f93a87c4f3ab6ea6dc1e\n", "Successfully built xmlrunner cvxpy\n", "\u001b[31mERROR: tensorflow 2.2.0 has requirement scipy==1.4.1; python_version >= \"3\", but you'll have scipy 1.3.1 which is incompatible.\u001b[0m\n", "\u001b[31mERROR: google-colab 1.0.0 has requirement pandas~=1.0.0; python_version >= \"3.0\", but you'll have pandas 0.25.3 which is incompatible.\u001b[0m\n", "\u001b[31mERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.\u001b[0m\n", "\u001b[31mERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.\u001b[0m\n", "Installing collected packages: xmlrunner, numpy, numba, pandas, matplotlib, scipy, cvxpy, scikit-learn, statsmodels, mlfinlab\n", " Found existing installation: numpy 1.18.4\n", " Uninstalling numpy-1.18.4:\n", " Successfully uninstalled numpy-1.18.4\n", " Found existing installation: numba 0.48.0\n", " Uninstalling numba-0.48.0:\n", " Successfully uninstalled numba-0.48.0\n", " Found existing installation: pandas 1.0.4\n", " Uninstalling pandas-1.0.4:\n", " Successfully uninstalled pandas-1.0.4\n", " Found existing installation: matplotlib 3.2.1\n", " Uninstalling matplotlib-3.2.1:\n", " Successfully uninstalled matplotlib-3.2.1\n", " Found existing installation: scipy 1.4.1\n", " Uninstalling scipy-1.4.1:\n", " Successfully uninstalled scipy-1.4.1\n", " Found existing installation: cvxpy 1.0.31\n", " Uninstalling cvxpy-1.0.31:\n", " Successfully uninstalled cvxpy-1.0.31\n", " Found existing installation: scikit-learn 0.22.2.post1\n", " Uninstalling scikit-learn-0.22.2.post1:\n", " Successfully uninstalled scikit-learn-0.22.2.post1\n", " Found existing installation: statsmodels 0.10.2\n", " Uninstalling statsmodels-0.10.2:\n", " Successfully uninstalled statsmodels-0.10.2\n", "Successfully installed cvxpy-1.0.25 matplotlib-3.1.1 mlfinlab-0.11.2 numba-0.49.1 numpy-1.17.3 pandas-0.25.3 scikit-learn-0.21.3 scipy-1.3.1 statsmodels-0.11.1 xmlrunner-1.7.7\n" ], "name": "stdout" }, { "output_type": "display_data", "data": { "application/vnd.colab-display-data+json": { "pip_warning": { "packages": [ "matplotlib", "mpl_toolkits", "numpy", "pandas" ] } } }, "metadata": { "tags": [] } }, { "output_type": "stream", "text": [ "Collecting yfinance\n", " Downloading https://files.pythonhosted.org/packages/c2/31/8b374a12b90def92a4e27d0fc595fc43635f395984e36a075244d98bd265/yfinance-0.1.54.tar.gz\n", "Requirement already satisfied: pandas>=0.24 in /usr/local/lib/python3.6/dist-packages (from yfinance) (0.25.3)\n", "Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.6/dist-packages (from yfinance) (1.17.3)\n", "Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.6/dist-packages (from yfinance) (2.23.0)\n", "Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.6/dist-packages (from yfinance) (0.0.9)\n", "Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.24->yfinance) (2018.9)\n", "Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.24->yfinance) (2.8.1)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (1.24.3)\n", "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (2.9)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (2020.4.5.1)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (3.0.4)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.6.1->pandas>=0.24->yfinance) (1.12.0)\n", "Building wheels for collected packages: yfinance\n", " Building wheel for yfinance (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for yfinance: filename=yfinance-0.1.54-py2.py3-none-any.whl size=22409 sha256=ad55e9ac0a0eb1fc07e87193b87cabcd8a65ee4032fd84ae0882441dc2ce2969\n", " Stored in directory: /root/.cache/pip/wheels/f9/e3/5b/ec24dd2984b12d61e0abf26289746c2436a0e7844f26f2515c\n", "Successfully built yfinance\n", "Installing collected packages: yfinance\n", "Successfully installed yfinance-0.1.54\n", "--2020-06-08 04:52:53-- https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage\n", "Resolving github.com (github.com)... 140.82.114.4\n", "Connecting to github.com (github.com)|140.82.114.4|:443... connected.\n", "HTTP request sent, awaiting response... 302 Found\n", "Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/99037241/9dc3a580-286a-11e9-8a21-4312b7c8a512?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200608%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200608T045254Z&X-Amz-Expires=300&X-Amz-Signature=09b9e9082f9e3e592a25b647acfc4714e8d4440c0b2e4040aec4a0ccebf70282&X-Amz-SignedHeaders=host&actor_id=0&repo_id=99037241&response-content-disposition=attachment%3B%20filename%3Dorca-1.2.1-x86_64.AppImage&response-content-type=application%2Foctet-stream [following]\n", "--2020-06-08 04:52:54-- https://github-production-release-asset-2e65be.s3.amazonaws.com/99037241/9dc3a580-286a-11e9-8a21-4312b7c8a512?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200608%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200608T045254Z&X-Amz-Expires=300&X-Amz-Signature=09b9e9082f9e3e592a25b647acfc4714e8d4440c0b2e4040aec4a0ccebf70282&X-Amz-SignedHeaders=host&actor_id=0&repo_id=99037241&response-content-disposition=attachment%3B%20filename%3Dorca-1.2.1-x86_64.AppImage&response-content-type=application%2Foctet-stream\n", "Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.216.241.188\n", "Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.241.188|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 51607939 (49M) [application/octet-stream]\n", "Saving to: ‘/usr/local/bin/orca’\n", "\n", "/usr/local/bin/orca 100%[===================>] 49.22M 13.5MB/s in 4.2s \n", "\n", "2020-06-08 04:52:59 (11.8 MB/s) - ‘/usr/local/bin/orca’ saved [51607939/51607939]\n", "\n", "Reading package lists... Done\n", "Building dependency tree \n", "Reading state information... Done\n", "libgtk2.0-0 is already the newest version (2.24.32-1ubuntu1).\n", "libgtk2.0-0 set to manually installed.\n", "The following additional packages will be installed:\n", " gconf-service gconf-service-backend gconf2-common libdbus-glib-1-2\n", "The following NEW packages will be installed:\n", " gconf-service gconf-service-backend gconf2-common libdbus-glib-1-2\n", " libgconf-2-4 xvfb\n", "0 upgraded, 6 newly installed, 0 to remove and 32 not upgraded.\n", "Need to get 1,687 kB of archives.\n", "After this operation, 10.5 MB of additional disk space will be used.\n", "Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 libdbus-glib-1-2 amd64 0.110-2 [58.3 kB]\n", "Get:2 http://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf2-common all 3.2.6-4ubuntu1 [700 kB]\n", "Get:3 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libgconf-2-4 amd64 3.2.6-4ubuntu1 [84.8 kB]\n", "Get:4 http://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf-service-backend amd64 3.2.6-4ubuntu1 [58.1 kB]\n", "Get:5 http://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf-service amd64 3.2.6-4ubuntu1 [2,036 B]\n", "Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 xvfb amd64 2:1.19.6-1ubuntu4.4 [784 kB]\n", "Fetched 1,687 kB in 3s (593 kB/s)\n", "Selecting previously unselected package libdbus-glib-1-2:amd64.\n", "(Reading database ... 144467 files and directories currently installed.)\n", "Preparing to unpack .../0-libdbus-glib-1-2_0.110-2_amd64.deb ...\n", "Unpacking libdbus-glib-1-2:amd64 (0.110-2) ...\n", "Selecting previously unselected package gconf2-common.\n", "Preparing to unpack .../1-gconf2-common_3.2.6-4ubuntu1_all.deb ...\n", "Unpacking gconf2-common (3.2.6-4ubuntu1) ...\n", "Selecting previously unselected package libgconf-2-4:amd64.\n", "Preparing to unpack .../2-libgconf-2-4_3.2.6-4ubuntu1_amd64.deb ...\n", "Unpacking libgconf-2-4:amd64 (3.2.6-4ubuntu1) ...\n", "Selecting previously unselected package gconf-service-backend.\n", "Preparing to unpack .../3-gconf-service-backend_3.2.6-4ubuntu1_amd64.deb ...\n", "Unpacking gconf-service-backend (3.2.6-4ubuntu1) ...\n", "Selecting previously unselected package gconf-service.\n", "Preparing to unpack .../4-gconf-service_3.2.6-4ubuntu1_amd64.deb ...\n", "Unpacking gconf-service (3.2.6-4ubuntu1) ...\n", "Selecting previously unselected package xvfb.\n", "Preparing to unpack .../5-xvfb_2%3a1.19.6-1ubuntu4.4_amd64.deb ...\n", "Unpacking xvfb (2:1.19.6-1ubuntu4.4) ...\n", "Setting up gconf2-common (3.2.6-4ubuntu1) ...\n", "\n", "Creating config file /etc/gconf/2/path with new version\n", "Setting up libdbus-glib-1-2:amd64 (0.110-2) ...\n", "Setting up xvfb (2:1.19.6-1ubuntu4.4) ...\n", "Setting up libgconf-2-4:amd64 (3.2.6-4ubuntu1) ...\n", "Setting up gconf-service-backend (3.2.6-4ubuntu1) ...\n", "Setting up gconf-service (3.2.6-4ubuntu1) ...\n", "Processing triggers for libc-bin (2.27-3ubuntu1) ...\n", "/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link\n", "\n", "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "1pXyxQPXRMuE", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 119 }, "outputId": "6eb0d648-ae42-483f-f5fe-8ffe3868ca10" }, "source": [ "import yfinance as yf\n", "import pandas as pd\n", "from mlfinlab.portfolio_optimization.mean_variance import MeanVarianceOptimisation\n", "\n", "def get_prices(stocks, start_date=\"2019-03-01\", end_date=\"2020-05-30\"):\n", " stocks_string = \" \".join(stocks)\n", " data = yf.download(stocks_string, start=start_date, end=end_date,\n", " group_by=\"ticker\")\n", " data = data.fillna(method='ffill')\n", " # Drop columns with no entries\n", " data = data.dropna(axis='columns', how='all')\n", "\n", " prices_df = pd.concat([data[ticker][\"Close\"] for ticker in stocks], axis=1)\n", " prices_df.columns = stocks\n", " return prices_df\n", "stocks = [\"NEXCF\", \"ACB.TO\", \"BB.TO\", \"N.V\", \"APHA.TO\", \"TTT.CN\", \"HUT.TO\", \"HIVE.V\", \"AC.TO\", \"CVE.TO\"]\n", "asset_prices = get_prices(stocks) \n", "# Compute IVP weights\n", "mvo = MeanVarianceOptimisation()\n", "mvo.allocate(asset_names=asset_prices.columns, asset_prices=asset_prices,\n", " solution='max_sharpe')\n", "ivp_weights = mvo.weights.sort_values(by=0, ascending=False, axis=1)\n", "with pd.option_context('display.max_rows', None, 'display.max_columns', None): \n", " print(ivp_weights)" ], "execution_count": 8, "outputs": [ { "output_type": "stream", "text": [ "[*********************100%***********************] 10 of 10 completed\n", " NEXCF HUT.TO HIVE.V ACB.TO BB.TO N.V APHA.TO TTT.CN \\\n", "0 0.690542 0.309458 9.412092e-23 0.0 0.0 0.0 0.0 0.0 \n", "\n", " AC.TO CVE.TO \n", "0 0.0 0.0 \n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "laaencmKSztN", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": 0, "outputs": [] } ] }