Weights Generation

Weights Generation#

Open In Colab

!pip install mlfinlab
!pip install yfinance
# See https://plotly.com/python/static-image-export/
!wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O /usr/local/bin/orca
!chmod +x /usr/local/bin/orca
!apt-get install xvfb libgtk2.0-0 libgconf-2-4
ERROR: Could not find a version that satisfies the requirement mlfinlab (from versions: none)

ERROR: No matching distribution found for mlfinlab

Requirement already satisfied: yfinance in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (0.2.44)
Requirement already satisfied: pandas>=1.3.0 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (2.2.3)
Requirement already satisfied: numpy>=1.16.5 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (2.1.1)
Requirement already satisfied: requests>=2.31 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (2.32.3)
Requirement already satisfied: multitasking>=0.0.7 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (0.0.11)
Requirement already satisfied: lxml>=4.9.1 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (5.3.0)
Requirement already satisfied: platformdirs>=2.0.0 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (4.3.6)
Requirement already satisfied: pytz>=2022.5 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (2024.2)
Requirement already satisfied: frozendict>=2.3.4 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (2.4.4)
Requirement already satisfied: peewee>=3.16.2 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (3.17.6)
Requirement already satisfied: beautifulsoup4>=4.11.1 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (4.12.3)
Requirement already satisfied: html5lib>=1.1 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from yfinance) (1.1)
Requirement already satisfied: soupsieve>1.2 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.6)
Requirement already satisfied: six>=1.9 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from html5lib>=1.1->yfinance) (1.16.0)
Requirement already satisfied: webencodings in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from html5lib>=1.1->yfinance) (0.5.1)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from pandas>=1.3.0->yfinance) (2.9.0.post0)
Requirement already satisfied: tzdata>=2022.7 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from pandas>=1.3.0->yfinance) (2024.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from requests>=2.31->yfinance) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from requests>=2.31->yfinance) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from requests>=2.31->yfinance) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in /opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages (from requests>=2.31->yfinance) (2024.8.30)
--2024-10-02 01:16:38--  https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage
Resolving github.com (github.com)... 140.82.116.3
Connecting to github.com (github.com)|140.82.116.3|:443... 
connected.
HTTP request sent, awaiting response... 
302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/99037241/9dc3a580-286a-11e9-8a21-4312b7c8a512?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20241002%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241002T011638Z&X-Amz-Expires=300&X-Amz-Signature=5a5d61eb48d7c7eb5fe36b1eb52c3574706605dc296b723959942a7de46f23c6&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dorca-1.2.1-x86_64.AppImage&response-content-type=application%2Foctet-stream [following]
--2024-10-02 01:16:38--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/99037241/9dc3a580-286a-11e9-8a21-4312b7c8a512?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20241002%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241002T011638Z&X-Amz-Expires=300&X-Amz-Signature=5a5d61eb48d7c7eb5fe36b1eb52c3574706605dc296b723959942a7de46f23c6&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dorca-1.2.1-x86_64.AppImage&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 
200 OK
Length: 51607939 (49M) [application/octet-stream]
Saving to: ‘/usr/local/bin/orca’


/usr/local/bin/orca   0%[                    ]       0  --.-KB/s               
/usr/local/bin/orca  40%[=======>            ]  20.02M  77.0MB/s               
/usr/local/bin/orca  81%[===============>    ]  40.02M  76.0MB/s               
/usr/local/bin/orca 100%[===================>]  49.22M  89.1MB/s    in 0.6s    

2024-10-02 01:16:39 (89.1 MB/s) - ‘/usr/local/bin/orca’ saved [51607939/51607939]
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
import yfinance as yf
import pandas as pd
from mlfinlab.portfolio_optimization.mean_variance import MeanVarianceOptimisation

def get_prices(stocks, start_date="2019-03-01", end_date="2020-05-30"):
    stocks_string = " ".join(stocks)
    data = yf.download(stocks_string, start=start_date, end=end_date,
                      group_by="ticker")
    data = data.fillna(method='ffill')
    # Drop columns with no entries
    data = data.dropna(axis='columns', how='all')

    prices_df = pd.concat([data[ticker]["Close"] for ticker in stocks], axis=1)
    prices_df.columns = stocks
    return prices_df
stocks = ["NEXCF", "ACB.TO", "BB.TO", "N.V", "APHA.TO", "TTT.CN", "HUT.TO", "HIVE.V", "AC.TO", "CVE.TO"]
asset_prices = get_prices(stocks) 
# Compute IVP weights
mvo = MeanVarianceOptimisation()
mvo.allocate(asset_names=asset_prices.columns, asset_prices=asset_prices,
             solution='max_sharpe')
ivp_weights = mvo.weights.sort_values(by=0, ascending=False, axis=1)
with pd.option_context('display.max_rows', None, 'display.max_columns', None): 
  print(ivp_weights)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 3
      1 import yfinance as yf
      2 import pandas as pd
----> 3 from mlfinlab.portfolio_optimization.mean_variance import MeanVarianceOptimisation
      5 def get_prices(stocks, start_date="2019-03-01", end_date="2020-05-30"):
      6     stocks_string = " ".join(stocks)

ModuleNotFoundError: No module named 'mlfinlab'