This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import pandas as pd | |
import numpy as np | |
from matplotlib import pyplot as plt | |
factors = pd.read_csv("./factors.csv", index_col="Date") | |
# convert Date index to datetime object | |
factors.index= pd.to_datetime(factors.index, format="%Y%m%d") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yfinance as yf | |
import pandas as pd | |
amzn = yf.Ticker("AMZN") | |
hist = amzn.history(period="max", auto_adjust=True, rounding=False) | |
price = hist[["Close"]] | |
monthly_price = price.resample('BM').last() | |
# calculate monthly returns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
factors = pd.read_csv("./data/factors.csv", index_col="Date") | |
# convert Date index to datetime object | |
factors.index= pd.to_datetime(factors.index, format="%Y%m%d") | |
# get monthly avg | |
factors = factors.resample('BM').mean() | |
factors_cum = factors.cumsum() | |
factors_cum.plot.line() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yfinance as yf | |
import pandas as pd | |
etsy = yf.Ticker("ETSY") | |
hist = etsy.history(period="max", auto_adjust=True, rounding=False) | |
price = hist[["Close"]] | |
# resample by latest monthly price | |
monthly_price = price.resample('BM').last() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import statsmodels.api as sm | |
# join factors to returns dfs | |
data = factors.join(excess_returns).dropna() | |
y = data[["Returns"]] | |
x = data[["Mkt-RF","SMB","HML"]] | |
ols = sm.OLS(y,x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
latest_factors = factors.tail(1).to_dict('r')[0] | |
RRR = latest_factors["RF"] | |
RRR += betas["Mkt-RF"] * latest_factors["Mkt-RF"] | |
RRR += betas["SMB"] * latest_factors["SMB"] | |
RRR += betas["HML"] * latest_factors["HML"] | |
print(f"Required rate of return is: {RRR}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# src/constants.py | |
DJIA_CONSTITUENTS_URL = "https://financialmodelingprep.com/api/v3/dowjones_constituent" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# src/models/constituent.py | |
from pydantic import BaseModel | |
class Constituent(BaseModel): | |
"""Represents a ticker received from FMP API when retrieving constituents of an index; see `price.load_djia_constituents` method.""" | |
symbol: str | |
name: str | |
sector: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# src/price/djia.py | |
import os | |
import csv | |
from typing import List | |
import requests | |
from dotenv import load_dotenv | |
from pydantic import parse_obj_as |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# src/price/endpoints.py | |
DJIA_CONSTITUENTS = "https://financialmodelingprep.com/api/v3/dowjones_constituent" |
OlderNewer