Skip to content

Instantly share code, notes, and snippets.

View 18182324's full-sized avatar
🎯
Focusing

Milton_FMR 18182324

🎯
Focusing
View GitHub Profile
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
@18182324
18182324 / Backtesting MA in R.r
Last active May 7, 2022 07:13
Backtesting MA in R
# Step 1: Load libraries and data
library(quantmod)
library(PerformanceAnalytics)
##
##Attaching package: 'PerformanceAnalytics'
##The following object is masked from 'package:graphics':
##legend
getSymbols('NFCI', src = 'FRED', , from = '2000-01-01')
##[1] "NFCI"
NFCI <- na.omit(lag(NFCI)) # we can only act on the signal after release, i.e. the next day
#requirements
#alpaca-trade-api>=0.25
#ta
#sklearn
import alpaca_trade_api as tradeapi
import requests
import time
from ta import macd
import numpy as np
@18182324
18182324 / pairs-trading-advanced-strategy
Created November 14, 2021 10:01
Pairs Trading Advanced K-Means Algorithm Clustering
companies_dict = {
'Amazon':'AMZN',
'Apple':'AAPL',
'Walgreen':'WBA',
'Northrop Grumman':'NOC',
'Boeing':'BA',
'Lockheed Martin':'LMT',
'McDonalds':'MCD',
'Intel':'INTC',
'Navistar':'NAV',