13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

322

ax.xaxis.set_major_locator(mondays)

ax.xaxis.set_minor_locator(alldays)

ax.xaxis.set_major_formatter(weekFormatter)

# Sort the data by the cluster values and obtain

# a separate DataFrame listing the index values at

# which the cluster boundaries change

df = copy.deepcopy(data)

df.sort_values(by="Cluster", inplace=True)

df.reset_index(inplace=True)

df["clust_index"] = df.index

df["clust_change"] = df["Cluster"].diff()

change_indices = df[df["clust_change"] != 0]

# Plot the OHLC chart with cluster-ordered "candles"

csticks = candlestick_ohlc(

ax, df[

["clust_index", ’Open’, ’High’, ’Low’, ’Close’]

].values, width=0.6,

colorup=’#000000’, colordown=’#ff0000’

)

ax.set_axis_bgcolor((1,1,0.9))

# Add each of the cluster boundaries as a blue dotted line

for row in change_indices.iterrows():

plt.axvline(

row[1]["clust_index"],

linestyle="dashed", c="blue"

)

plt.xlim(0, len(df))

plt.setp(

plt.gca().get_xticklabels(),

rotation=45, horizontalalignment=’right’

)

plt.show()

def create_follow_cluster_matrix(data):

"""

Creates a k x k matrix, where k is the number of clusters

that shows when cluster j follows cluster i.

"""

data["ClusterTomorrow"] = data["Cluster"].shift(-1)

data.dropna(inplace=True)

data["ClusterTomorrow"] = data["ClusterTomorrow"].apply(int)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!