Skip to content

Instantly share code, notes, and snippets.

# Summary statistics for the Buy-and-Hold strategy
# Summary statistics for the 4-day SMA strategy
# Summary statistics for the HMM-DC based 4-day SMA strategy
# Set the figure size
# Plot both the Buy-and-hold, SMA and HMM-DC-based strategy cumulative returns
plt.plot(plot_data.index, plot_data['buy_hold_cum_rets'], label = "Buy and Hold Returns")
plt.plot(plot_data.index, plot_data['basic_stra_cum_rets'], label = "SMA Strategy Returns", color='g')
plt.plot(plot_data.index, plot_data['hmm_dc_stra_cum_rets'], label = "HMM-DC-based Strategy Returns", color='r')
#plt.plot(plot_data.index, plot_data['hmm_stra_cum_rets'], label = "HMM-based Strategy Returns", color='y')
# Set the title of the graph
# Compute the Buy-and-Hold cumulative returns
plot_data['buy_hold_cum_rets'] = np.exp(plot_data['returns'].cumsum())
# Compute the simple 4-day moving average strategy returns
plot_data['basic_stra_rets'] = plot_data['returns']*plot_data['signal'].shift(1)
# Compute the simple 4-day moving average strategy cumulative returns
plot_data['basic_stra_cum_rets'] = np.exp(plot_data['basic_stra_rets'].cumsum())
# Compute the HMM-DC-based strategy returns
# Subset the data for plotting
plot_data = data.loc['2018':]
# Count the number of days in which we face different leverages
# Set the initial day to start the trading backtesting loop
initial_t = data.index.get_loc(data.loc['2018':].index[0])
# Print the initial day
# Create the backtesting loop
for t in range(initial_t, (len(data.index)-1)):
# Create a data sample to be used for the trading computations
data_sample = data[['R','returns']].iloc[:(t+1)]
# Create an HMM model object
dc_model = hmm.GaussianHMM(n_components = 2, covariance_type = "diag", n_iter = 200, random_state = 100)
# Create the array input to be used for the HMM model
dc_X = data_sample[['R']].values
# Create a simple 4-day moving average
data['sma'] = data['Close'].rolling(4).mean()
# Create the strategy signal
data['signal'] = np.where(data['Close']>data['sma'],1.0,-1.0)
# Create the leverage column
data['dc_leverage'] = 0.0
# Create the next state column
# Find the returns standard deviation for state 0
state0_vol = data['returns'][data['states']==0].std()*np.sqrt(252)*100
# Find the returns standard deviation for state 1
state1_vol = data['returns'][data['states']==1].std()*np.sqrt(252)*100
# Print the returns volatility for both states
print(f'Volatility for state 0 and 1 are {state0_vol:.2f} and {state1_vol:.2f}, respectively')
# Find the R mean for state 0
state0_R_vol = data['R'][data['states']==0].mean()
# Find the R mean for state 1
state1_R_vol = data['R'][data['states']==1].mean()
# Print the R volatility for both states
print(f'Volatility for state 0, 1 and 2 are {state0_R_vol:.2f} and {state1_R_vol:.2f}, respectively')
# Create an HMM object with two hidden states
model = hmm.GaussianHMM(n_components = 2, covariance_type = "diag", n_iter = 100, random_state = 42)
# Create an array input for the HMM model
X = data[['R']].values
# Estimate the HMM model
results =
# Use the Viterbi algorithm to find the fitted hidden states