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.

315

ClusterTomorrow to contain tomorrow’s cluster value. A ClusterMatrix column is then created

by forming a tuple of today’s cluster index and tomorrow’s cluster index. The Pandas

value_counts method is then used to create a frequency distribution of these pairs. Finally,

the K × K NumPy matrix is created and filled with the percentage frequency of occurance of

each cluster follow-on:

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)

sp500["ClusterMatrix"] = list(

zip(data["Cluster"], data["ClusterTomorrow"])

)

cmvc = data["ClusterMatrix"].value_counts()

clust_mat = np.zeros( (k, k) )

for row in cmvc.iteritems():

clust_mat[row[0]] = row[1]*100.0/len(data)

print("Cluster Follow-on Matrix:")

print(clust_mat)

The __main__ function ties all of the above functions together. It carries out the K-Means

algorithm and uses these cluster membership values in all subsequent functions:

if __name__ == "__main__":

# Obtain S&P500 pricing data from Yahoo Finance

start = datetime.datetime(2013, 1, 1)

end = datetime.datetime(2015, 12, 31)

sp500 = web.DataReader("^GSPC", "yahoo", start, end)

# Plot last year of price "candles"

plot_candlesticks(sp500, datetime.datetime(2015, 1, 1))

# Carry out K-Means clustering with five clusters on the

# three-dimensional data H/O, L/O and C/O

sp500_norm = get_open_normalised_prices(sp500, start, end)

k = 5

km = KMeans(n_clusters=k, random_state=42)

km.fit(sp500_norm)

labels = km.labels_

sp500["Cluster"] = labels

# Plot the 3D normalised candles using H/O, L/O, C/O

plot_3d_normalised_candles(sp500_norm)

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

Saved successfully!

Ooh no, something went wrong!