This file contains hidden or 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 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# Define a list of stock tickers for the portfolio | |
tickers = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'TSLA'] | |
# Set the date range for historical data | |
start_date = '2010-01-01' |
This file contains hidden or 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 necessary libraries | |
import quandl | |
import matplotlib.pyplot as plt | |
# Set your Quandl API key (you need to sign up on Quandl's website to get your API key) | |
quandl.ApiConfig.api_key = 'YOUR_API_KEY' | |
# Define the dataset codes for the BRICS nations' GDP | |
brics_gdp_datasets = { | |
'Brazil': 'ODA/BRA_NGDPD', |
This file contains hidden or 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 necessary libraries | |
import quandl | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# Set your Quandl API key (you need to sign up on Quandl's website to get your API key) | |
quandl.ApiConfig.api_key = 'YOUR_API_KEY' | |
# Define the dataset codes for Google and Yahoo | |
google_dataset = 'WIKI/GOOGL' # Replace with the correct dataset code for Google |
This file contains hidden or 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 necessary libraries | |
import quandl | |
import matplotlib.pyplot as plt | |
# Set your Quandl API key (you need to sign up on Quandl's website to get your API key) | |
quandl.ApiConfig.api_key = 'YOUR_API_KEY' | |
# Define the companies and their respective dataset codes | |
companies = { | |
'Google': 'WIKI/GOOGL', # Replace with the correct dataset code for Google |
This file contains hidden or 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
#Here's a full example of Perry Kaufman's market efficiency strategy implemented in Python, including backtesting capabilities | |
#using historical data. Remember that this is a simplified example for educational purposes, and it does not include considerations | |
#such as transaction costs or slippage. | |
pip install pandas numpy yfinance matplotlib | |
import pandas as pd | |
import numpy as np | |
import yfinance as yf | |
import matplotlib.pyplot as plt |
This file contains hidden or 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
library(quantmod) | |
# Download SPY data from Yahoo Finance | |
getSymbols("SPY", src = "yahoo", from = "2007-01-01", to = "2022-04-07") | |
# Calculate moving average | |
ma_length <- 38 | |
ma <- SMA(Cl(SPY), n = ma_length) | |
# Calculate OBV |
This file contains hidden or 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 pandas as pd | |
import backtrader as bt | |
# Define the strategy | |
class MyStrategy(bt.Strategy): | |
params = dict( | |
ma_length=50, | |
obv_ma_length=200, | |
target=5000, | |
stop=4000 |
This file contains hidden or 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 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"] |
This file contains hidden or 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 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') |
This file contains hidden or 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 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") |
NewerOlder