Hello Dear,
The jupyter lab states: " ## Read FED dates
To read the CSV file, we use "pandas.read_csv()".
Syntax:
<b>pd.read_csv(filename)</b>
filename: Name of the file in the string format
This data is available in the downloadable unit of this course. You can also download these dates on the official FED website."
Can you please provide instruction or guidance on how to download these dates from the official FED Website? I need to download the most recent dates as the one in the lab are out of date and can't really help me right now.
Thank you
Hey Pierre,
Unfortunately, the current version of the FED website does not have the download functionality. An easy way is to download the datafile given by Quantra from the downloadable unit at the end of the course. After downloading it you can update this file with the most recent dates which you will find here.
Hope this helps!
Can you please check this for me? After updating the data from Fed and SPY from yfinance, the CAGR is 0.00, max drawdown is 0.00… it seems like something is wrong.
# Import pandas
import pandas as pd
# Read FED days data
fed_days = pd.read_csv('../data_modules/fed_days_1993_2023_1.csv')
# Set Date to date-time format
fed_days.Date = pd.to_datetime(fed_days.Date, format='%d-%m-%Y')
fed_days.tail(5)
266 2023-03-05
267 2023-06-14
268 2023-07-26
269 2023-09-20
270 2023-01-11
# Import pandas
import pandas as pd
import yfinance as yf
# Read SPY prices
#spy_prices = pd.read_csv('../data_modules/spy_daily_1997_2023_1123_1.csv', delimiter=';')
spy_prices = pd.read_csv('../data_modules/spy_daily_1997_2023_1123_1.csv')
spy_prices.tail()
# Calculate daily changes of the adjusted close price
spy_prices['daily_changes'] = spy_prices['Adj Close'].pct_change()
spy_prices.tail()
Date Open High Low Close Adj Close Volume daily_changes
1479 2023-11-16 449.220001 450.559998 448.119995 450.230011 450.230011 66665800 0.001223
1480 2023-11-17 450.239990 451.420013 449.290009 450.790009 450.790009 83133200 0.001244
1481 2023-11-20 450.529999 455.130005 450.519989 454.260010 454.260010 69936200 0.007698
1482 2023-11-21 453.179993 454.130005 451.959991 453.269989 453.269989 49244600 -0.002179
1483 2023-11-22 454.980011 456.380005 453.890015 455.019989 455.019989 59394900 0.003861
# Import numpy
import numpy as np
# Generate FED signals
spy_prices['fed_signal'] = np.where((spy_prices['Date'].isin(fed_days['Date'])), 1, 0)
# Set Date column as index
spy_prices.set_index('Date', inplace=True)
spy_prices.tail()
Open High Low Close Adj Close Volume daily_changes fed_signal
Date
2023-11-16 449.220001 450.559998 448.119995 450.230011 450.230011 66665800 0.001223 0
2023-11-17 450.239990 451.420013 449.290009 450.790009 450.790009 83133200 0.001244 0
2023-11-20 450.529999 455.130005 450.519989 454.260010 454.260010 69936200 0.007698 0
2023-11-21 453.179993 454.130005 451.959991 453.269989 453.269989 49244600 -0.002179 0
2023-11-22 454.980011 456.380005 453.890015 455.019989 455.019989 59394900 0.003861 0
# Calculate strategy returns
spy_prices['strategy_returns'] = spy_prices['daily_changes'] * spy_prices['fed_signal']
# Calculate cumulative strategy returns
cumulative_strategy_returns = ((spy_prices['strategy_returns'] + 1).cumprod())
# Calculate startegy returns
total_strategy_returns = (cumulative_strategy_returns.iloc[-1]-1)*100
print("The strategy returns is %.2f" % total_strategy_returns)
The strategy returns is 0.00
# Total number of trading days
days = len(cumulative_strategy_returns)
# Calculate compounded annual growth rate
cagr = (cumulative_strategy_returns.iloc[-1]**(252/days)-1)*100
print('The CAGR from strategy is %.2f' % cagr)
The CAGR from strategy is 0.00
def calc_drawdown(cum_rets):
# Calculate the running maximum
running_max = np.maximum.accumulate(cum_rets.dropna())
# Ensure the value never drops below 1
running_max[running_max < 1] = 1
# Calculate the percentage drawdown
drawdown = (cum_rets)/running_max - 1
return drawdown
drawdown = calc_drawdown(cumulative_strategy_returns)
max_dd = drawdown.min()*100
print("The maximum drawdown is %.2f" % max_dd)
The maximum drawdown is 0.00
# Calculate strategy returns with trend factor
spy_prices['strategy_returns_trend_factor'] = spy_prices['daily_changes'] * spy_prices['fed_signal'] \
* spy_prices['sma_signal']
# Calculate cumulative strategy returns with trend factor
cumulative_strategy_returns_trend_factor = (
(spy_prices['strategy_returns_trend_factor']+1).cumprod())
# Calculate strategy returns with trend factor
total_strategy_returns_trend_factor = (
cumulative_strategy_returns_trend_factor.iloc[-1]-1)*100
print("The strategy returns is %.2f" % total_strategy_returns_trend_factor)
The strategy returns is 0.00
# Total number of trading days
days = len(cumulative_strategy_returns_trend_factor)
# Calculate CAGR with trend factor
cagr_trend_factor = (
cumulative_strategy_returns_trend_factor.iloc[-1]**(252/days)-1)*100
print('The CAGR from strategy is %.2f' % cagr_trend_factor)
The CAGR from strategy is 0.00
# Calculate drawdown with trend factors
drawdown = calc_drawdown(cumulative_strategy_returns_trend_factor)
max_dd_trend_factor = drawdown.min()*100
print("The maximum drawdown is %.2f" % max_dd_trend_factor)
The maximum drawdown is 0.00
# Import tabulate
from tabulate import tabulate
stats = tabulate(
[
['Strategy Returns', total_strategy_returns, total_strategy_returns_trend_factor],
['CAGR', cagr, cagr_trend_factor],
['Maximum Drawdown', max_dd, max_dd_trend_factor],
['CAGR/Maximum Drawdown', cagr/abs(max_dd), cagr_trend_factor/abs(max_dd_trend_factor)],
],
headers=['Stats Name','Strategy', 'Strategy with Trend Factor'], tablefmt='orgtbl')
p