Herron Topic 2 - Practice

FINA 6333 for Spring 2025

Author

Richard Herron

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pandas_datareader as pdr
import statsmodels.api as sm
import yfinance as yf
%precision 4
pd.options.display.float_format = '{:.4f}'.format
# %config InlineBackend.figure_format = 'retina'

Announcements

Five-Minute Review

Practice

Implement the SMA(20) strategy with Bitcoin from the lecture notebook

Try to create the btc data frame in one code cell with one assignment (i.e., one =).

Investigate how SMA(20) generates returns

Consider the following:

  1. Does SMA(20) avoid the worst performing days? How many of the worst 20 days does SMA(20) avoid? Try the .sort_values() or .nlargest() method.
  2. Does SMA(20) preferentially avoid low-return days? Try to combine the .groupby() method and pd.qcut() function.
  3. Does SMA(20) preferentially avoid high-volatility days? Try to combine the .groupby() method and pd.qcut() function.

Implement the SMA(20) strategy with IBM

How often does SMA(20) outperform buy-and-hold with 10-year rolling windows?

Implement a long-only BB(20, 2) strategy with Bitcoin

Bollinger Bands are bands around a trend, typically defined in terms of simple moving averages and volatilities. A long-only BB(20, 2) strategy has upper and lower bands at 2 standard deviations above and below the SMA(20). It invests as follows:

  1. Buy when the closing price crosses LB(20) from below
  2. Sell when the closing price crosses UB(20) from above
  3. No short-selling

The long-only BB(20, 2) is more difficult to implement than the long-only SMA(20) because we need to track buys and sells. For example, if the closing price is between LB(20) and BB(20), we need to know if our last trade was a buy or a sell. Further, if the closing price is below LB(20), we can still be long because we sell when the closing price crosses UB(20) from above.

More on Bollinger Bands here and here.

Implement a long-short RSI(14) strategy with Bitcoin

From Fidelity:

The Relative Strength Index (RSI), developed by J. Welles Wilder, is a momentum oscillator that measures the speed and change of price movements. The RSI oscillates between zero and 100. Traditionally the RSI is considered overbought when above 70 and oversold when below 30. Signals can be generated by looking for divergences and failure swings. RSI can also be used to identify the general trend.

The RSI formula: \(RSI(n) = 100 - \frac{100}{1 + RS(n)}\), where \(RS(n) = \frac{SMA(U, n)}{SMA(D, n)}\). For “up days”, \(U = \Delta \text{Adj\ Close}\) and \(D = 0\). For “down days”, \(U = 0\) and \(D = - \Delta \text{Adj\ Close}\).

We will implement a long-short RSI(14) as follows:

  1. Buy when the RSI crosses 30 from below, and sell when the RSI crosses 50 from below
  2. Short when the RSI crosses 70 from above, and cover when the RSI crosses 50 from above

More about RSI here.

Implement a golden cross with Bitcoin

From Grok:

In technical analysis, a golden cross is a bullish chart pattern that occurs when a short-term moving average (typically the 50-day moving average) crosses above a long-term moving average (typically the 200-day moving average). This crossover is considered a signal that a stock, index, or other asset may be entering a sustained upward trend, suggesting potential buying opportunities for traders and investors.

More here.

Compare all strategies with Bitcoin