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.

311

clustered using K-Means and then plotted in a three-dimensional scatterplot to visualise cluster

membership.

These cluster labels will then be applied back to the original candle data and used to visualise

cluster membership (as well as boundaries) on a cluster label-ordered candlestick chart. Finally,

a "follow-on matrix" will be created, which describes the frequency of tomorrow’s cluster being

j, if today’s cluster is i. Such a matrix is useful for ascertaining whether there is any scope for

forming a predictive trading strategy based on today’s cluster membership.

This section of code requires many imports. The majority of these are due to necessary

formatting options for Matplotlib. The copy standard library is brought in to make deep copies

of DataFrames so that they are not overwritten by each subsequent plot function. In addition

NumPy and Pandas are imported for data manipulation. Finally the KMeans module is imported

from Scikit-Learn:

# 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

The first function takes a symbol string for an equities ticker, as well as starting and ending

dates, and uses these to create a three-dimensional time series. Each dimension represents the

High, Low and Close price normalised by the Open price, respectively. The remaining columns

are dropped and the DataFrame is returned:

def get_open_normalised_prices(symbol, start, end):

"""

Obtains a pandas DataFrame containing open normalised prices

for high, low and close for a particular equities symbol

from Yahoo Finance. That is, it creates High/Open, Low/Open

and Close/Open columns.

"""

df = web.DataReader(symbol, "yahoo", start, end)

df["H/O"] = df["High"]/df["Open"]

df["L/O"] = df["Low"]/df["Open"]

df["C/O"] = df["Close"]/df["Open"]

df.drop(

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

Saved successfully!

Ooh no, something went wrong!