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.

319

# Apply the K-Means Algorithm for k=3, which is

# equal to the number of true Gaussian clusters

km3 = KMeans(n_clusters=3)

km3.fit(X)

km3_labels = km3.labels_

# Apply the K-Means Algorithm for k=4, which is

# larger than the number of true Gaussian clusters

km4 = KMeans(n_clusters=4)

km4.fit(X)

km4_labels = km4.labels_

# Create a subplot comparing k=3 and k=4

# for the K-Means Algorithm

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14,6))

ax1.scatter(X[:, 0], X[:, 1], c=km3_labels.astype(np.float))

ax1.set_xlabel("$x_1$")

ax1.set_ylabel("$x_2$")

ax1.set_title("K-Means with $k=3$")

ax2.scatter(X[:, 0], X[:, 1], c=km4_labels.astype(np.float))

ax2.set_xlabel("$x_1$")

ax2.set_ylabel("$x_2$")

ax2.set_title("K-Means with $k=4$")

plt.show()

# ohlc_clustering.py

import copy

import datetime

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

from matplotlib.finance import candlestick_ohlc

import matplotlib.dates as mdates

from matplotlib.dates import (

DateFormatter, WeekdayLocator, DayLocator, MONDAY

)

import numpy as np

import pandas as pd

import pandas_datareader.data as web

from sklearn.cluster import KMeans

def get_open_normalised_prices(symbol, start, end):

"""

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

Saved successfully!

Ooh no, something went wrong!