Skip to content

Instantly share code, notes, and snippets.

View 18182324's full-sized avatar
🎯
Focusing

Wealth Hub 18182324

🎯
Focusing
View GitHub Profile
@18182324
18182324 / Pairs Trading Z Score Top 10 Pairs.py
Last active February 1, 2023 18:57
Pairs Trading S&P 500 Tickers
import pandas as pd
import numpy as np
from pandas_datareader import data as pdr
import statsmodels
from statsmodels.tsa.stattools import coint
import matplotlib.pyplot as plt
# Step 1: Download the stock data from yahoo finance for all stocks in the S&P 500 Index
tickers = pd.read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")[0]["Symbol"].tolist()
data = pdr.get_data_yahoo(tickers, start="2010-01-01", end="2022-12-31")["Adj Close"]
@18182324
18182324 / Framework fixed income asset allocation model.py
Created January 5, 2023 21:55
Quant Fidelity Fixed Income Asset Allocation Model
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf
import requests
# Load the data for the macro, fundamentals, sentiment, and valuation pillars
macro_data = pd.read_csv('macro_data.csv')
fundamentals_data = pd.read_csv('fundamentals_data.csv')
sentiment_data = pd.read_csv('sentiment_data.csv')
valuation_data = pd.read_csv('valuation_data.csv')
@18182324
18182324 / Highest return optimization algorithm.py
Created January 5, 2023 21:04
Tactical Asset Allocation and Automatic Optimization
import numpy as np
import pandas as pd
import yfinance as yf
from scipy.optimize import minimize
# Download the ETF data from Yahoo Finance
etf_data = {}
for etf in ['SPY', 'MDY', 'EFA', 'EEM', 'TLT']:
etf_data[etf] = yf.Ticker(etf).history(period="10y")
import pandas as pd
import yfinance as yf
# Download the ETF data from Yahoo Finance
etf_data = {}
for etf in ['SPY', 'MDY', 'EFA', 'EEM', 'TLT']:
etf_data[etf] = yf.Ticker(etf).history(period="10y")
# Calculate the returns for each ETF
etf_returns = {}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Define a class for a portfolio asset
class Asset {
public:
string name;
import pandas as pd
import yfinance as yf
# Download the ETF data from Yahoo Finance
etf_data = {}
for etf in ['ETF1', 'ETF2', 'ETF3', 'ETF4', 'ETF5', 'ETF6', 'ETF7', 'ETF8', 'ETF9', 'ETF10']:
etf_data[etf] = yf.Ticker(etf).history(period="10y")
# Calculate the returns for each ETF
etf_returns = {}
@18182324
18182324 / MSVAR_DCC_models.py
Created January 4, 2023 18:43
Statistical models to Predict Recession
#Markov-Switching Vector Autoregression (MSVAR) model
import numpy as np
import statsmodels.tsa.api as smt
# Set the number of lags to consider
nlags = 4
# Create the model
model = smt.MSVAR(data, lags=nlags)
@18182324
18182324 / Machine Learning Predicting Stagflation.py
Last active January 4, 2023 17:06
Predicting Economic Cycles with Machine Learning
#Scikit-learn library to build and train a linear regression model
# First, we'll import the necessary libraries
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# Next, we'll load our data and split it into training and testing sets
X = # Your input data (economic indicators)
y = # Your output data (stagflation probability)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
@18182324
18182324 / Bitcoin Trading System.py
Created November 6, 2022 17:39
Development Framework Trading and Backtesting Trading Strategies
# Load libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import read_csv, set_option
from pandas.plotting import scatter_matrix
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV
from sklearn.linear_model import LogisticRegression
# The investment universe consists of 24 commodity futures, 12 cross-currency pairs (with 9 underlying currencies), 9 developed equity indices, and 13
# government bond futures.
# Every month, the investor considers whether the excess return of each asset over the past 12 months is positive or negative and goes long on the
# contract if it is positive and short if negative. The position size is set to be inversely proportional to the instrument’s volatility. A univariate
# GARCH model is used to estimated ex-ante volatility in the source paper. However, other simple models could probably be easily used with good results
# (for example, the easiest one would be using historical volatility instead of estimated volatility). The portfolio is rebalanced monthly.
# QC implementation changes:
# instead of GARCH model volatility, simple historical volatility is used in this code.
# for more info on trading strategies visit miltonfmr.com