Python for Financial Data Science
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
FIN42330 Python for Financial Data Science
Assignment
(Please read all instructions very carefully)
Custom Functions & Applications
Moving average timing investment strategy
Suppose you are considering investing in an exchanged traded fund that tracks the S&P
500, the SPDR S&P 500 ETF Trust (ticker: SPY), using a moving average (MA) in
dicator. The MA indicator is one of the most popular timing strategies using technical
analysis.
Denote by Rt the simple return and by Pt the corresponding prices on SPY at time
t. The MA at day t of lag l is defined as
At,l =
P
t−(l−1) + Pt−(l−2) + · · · , +Pt−1 + Pt
l
,
(1)
which is the average price of the past l days including day t. Consider 10-day MAs. On
each trading day t, if the last closing price Pt−1 is above the MA price At−1, we will invest
in the SPY for the trading dy t; otherwise, we will not invest. So the MA provides an
investment timing signal with a lag of 1 day. The idea of the MA is that an investor
should hold an asset when the asset price is on an uninterrupted up trend. However,
when the trend is broken, the investor should then sell the asset.
Mathematically, in the absence of transaction costs, the excess rate of return on the
MA timing strategy on day t is
˜
Rt =
(
Rt − Rt
f
, if
Pt−1
> At−1,l;
0,
otherwise
,
(2)
where Rt
f
is the day t rate of return on the risk-free asset.
1. Download daily prices (Adj Close) of SPY spanning the period from 3rd December
2018 to 1st December 2023 from Yahoo Finance. As a proxy for the risk-free
asset, consider the daily data on the 1-month T-bill rate (DBT4WK) from the
U.S. Federal reserve economic database https://fred.stlouisfed.org/series/
DTB4WK. Assume 252 trading days per year.
2. Write Python scripts and functions, as appropriate, to implement the MA timing
strategy detailed above. Report the mean, volatility, and Sharpe ratio of the excess
returns on the trading strategy.
13. Write a Python function to evaluate the predictive performance of the MA timing
strategy (deployed in the task just above) using the market timing test of Pesaran
and Timmermann (1992). The test is defined in equation 6 of the paper.
Submit your source code (as a Python file or preferably, as a Jupyter notebook) and PDF
file of your report via the dedicated submission folder (Assignments via the Assessment
tab) on Brightspace by Tuesday, 12th December 2023 at 5pm. Please include your
solutions to all questions in one single document. Source code for all questions are required
and should be appropriately labelled to the extent that it is obvious which answers relate
to which questions.