13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

316

# Plot the full OHLC candles re-ordered

# into their respective clusters

plot_cluster_ordered_candles(sp500)

# Create and output the cluster follow-on matrix

create_follow_cluster_matrix(sp500)

The output of the cluster follow-on matrix is as follows:

Cluster Follow-on Matrix:

[[ 14.70198675 4.37086093 1.05960265 5.43046358 12.45033113]

[ 4.76821192 1.7218543 0.66225166 1.45695364 3.31125828]

[ 0.52980132 0.92715232 0.52980132 0.66225166 1.7218543 ]

[ 3.57615894 2.78145695 1.05960265 2.51655629 4.2384106 ]

[ 14.43708609 1.98675497 1.05960265 4.2384106 9.8013245 ]]

It can be seen that this is certainly not an evenly distributed matrix. That is, certain "candles"

are likely to follow others with more frequency. This motivates the possibility of forming trading

strategies around cluster identification and prediction of subsequent clusters.

Figure 22.3 displays the candles for a years worth of the S&P500 OHLC prices for 2015. Note

the steep drop around late August and subsequent slow recovery in October/November:

Figure 22.3: S&P500 candlestick bars for the year 2015

Figure 22.4 is a three-dimensional plot of High/Open, Low/Open and Close/Open plotted

against each other. Each of the K = 5 clusters has been coloured. It is clear the the majority of

the bars are located around (1.0, 1.0, 1.0). This makes sense as most days are not hugely volatile

and hence the prices do not trade in too large a range.

However, there are many days when the closing price is substantially above the opening price

as is evidenced by the light blue cluster in the top of the figure. In addition there are many days

when the low point is substantially below the opening price, indicated by the light green cluster:

Figure 22.5 displays the candles for 2013-2015 inclusive ordered by cluster membership. This

visualisation makes it clear how the K-Means algorithm works on candle data. There are two

large clusters at either end of the chart that represent slight down days and slight up days,

respectively. Within the middle of the chart more severe gains and drops can be seen.

One interesting point to note is that the cluster membership is highly unequal. There are

many more lesser volatile days than there are higher volatile days. The central cluster in particular

contains days with steep declines:

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

Saved successfully!

Ooh no, something went wrong!