logo

... cvxpy two.


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.


Episode Notes

Before starting, make sure you've installed the dependencies;

pip install pandas cvxpy matplotlib jupyterlab

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

wget https://calmcode.io/datasets/stocks.csv

Given all these preparations, you can use the following code to make the same visual;

import cvxpy as cp
import pandas as pd
df_stocks = pd.read_csv("stocks.csv").iloc[500:1800]
df_stocks.set_index('Date').plot(figsize=(12, 4));

To make the first order differences you need to run;

df_returns = df_stocks.set_index('Date').diff()
df_returns.mean()
df_returns.std()

Feedback? See an issue? Feel free to mention it here.

If you want to be kept up to date, consider getting the newsletter.