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.

193

Note: You will likely need to run pip install pykalman to install the PyKalman library as

it is not bundled with Anaconda.

The next step is write the function draw_date_coloured_scatterplot to produce a scatterplot

of the asset adjusted closing prices (such a scatterplot is inspired by that produced by

Aidan O’Mahony[74]). The scatterplot will be coloured using a Matplotlib colour map, specifically

"Yellow To Red", where yellow represents price pairs closer to 2010, while red represents

price pairs closer to 2016:

def draw_date_coloured_scatterplot(etfs, prices):

"""

Create a scatterplot of the two ETF prices, which is

coloured by the date of the price to indicate the

changing relationship between the sets of prices

"""

# Create a yellow-to-red colourmap where yellow indicates

# early dates and red indicates later dates

plen = len(prices)

colour_map = plt.cm.get_cmap(’YlOrRd’)

colours = np.linspace(0.1, 1, plen)

# Create the scatterplot object

scatterplot = plt.scatter(

prices[etfs[0]], prices[etfs[1]],

s=30, c=colours, cmap=colour_map,

edgecolor=’k’, alpha=0.8

)

# Add a colour bar for the date colouring and set the

# corresponding axis tick labels to equal string-formatted dates

colourbar = plt.colorbar(scatterplot)

colourbar.ax.set_yticklabels(

[str(p.date()) for p in prices[::plen//9].index]

)

plt.xlabel(prices.columns[0])

plt.ylabel(prices.columns[1])

plt.show()

I have commented the code to make it fairly straightforward to see what all of the commands

are doing. The main work is being done within the colour_map, colours and scatterplot

variables. It is given in Figure 13.1.

13.3.5 Time-Varying Slope and Intercept

The next step is to actually use PyKalman to dynamically adjust the intercept and slope between

TFT and IEI. This function is more complex and requires some explanation.

Firstly we define a variable called delta, which is used to control the transition covariance

for the system noise. This controls the value of W t . We simply multiply such a value by the

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

Saved successfully!

Ooh no, something went wrong!