Calmcode - cvxpy two: introduction

Introduction

1 2 3 4 5 6 7 8

When you invest in stocks you want to get a high return, but a low risk. There's also going to be a few constraints. That sounds like a job for cvxpy.

You can use a dataset that we provide in this video but you're also free to download your own dataset using the yahoo finance python api. We've used the following code;

import pandas as pd
import yfinance as yf
from functools import reduce

dataframes = []
tickets = ["MSFT", "KLM", "ING", "MOS"]
for tick in tickets:
    hist = yf.Ticker(tick).history(period="max")
    hist = (hist['Open']
            .reset_index()
            .rename(columns={'Open': tick})
            .set_index('Date'))
    dataframes.append(hist)
df = (reduce(lambda d1, d2: d1.join(d2), dataframes)
  .dropna()
  .reset_index())

If you prefer to use the same dataset to follow along you can download it via our datasets page or you can fetch it via;

wget https://calmcode.io/static/data/stocks.csv