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.

313

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

ax.xaxis_date()

plt.setp(

plt.gca().get_xticklabels(),

rotation=45, horizontalalignment=’right’

)

plt.show()

The following function plot_3d_normalised_candles makes a scatter plot in three-dimensional

space of all of the candles, normalised by the open price. Each daily candle bar is coloured according

to cluster membership (which is determined in subsequent code snippets below):

def plot_3d_normalised_candles(data):

"""

Plot a 3D scatterchart of the open-normalised bars

highlighting the separate clusters by colour

"""

fig = plt.figure(figsize=(12, 9))

ax = Axes3D(fig, elev=21, azim=-136)

ax.scatter(

data["H/O"], data["L/O"], data["C/O"],

c=labels.astype(np.float)

)

ax.set_xlabel(’High/Open’)

ax.set_ylabel(’Low/Open’)

ax.set_zlabel(’Close/Open’)

plt.show()

The next function plot_cluster_ordered_candles is similar to the above candlestick plot,

except that it is now ordered by cluster membership, rather than date. In addition each cluster

boundary is visualised with a blue dotted line. The function is somewhat complex, but once

again this is mainly due to formatting issues with Matplotlib.

The latter section of the function involves creating a separate DataFrame called change_indices.

Its job is to determine the index at which a new cluster boundary is located. This is done by

sorting all elements by their cluster index and then using the diff method to obtain the change

points. This is then filtered by all values that do not equal zero, which returns a DataFrame

consisting of five rows, one for each boundary. This is then used by the Matplotlib axvline

method to plot the dotted blue line:

def plot_cluster_ordered_candles(data):

"""

Plot a candlestick chart ordered by cluster membership

with the dotted blue line representing each cluster

boundary.

"""

# Set the format for the axis to account for dates

# correctly, particularly Monday as a major tick

mondays = WeekdayLocator(MONDAY)

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

Saved successfully!

Ooh no, something went wrong!