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.

403

• TLT - For the period 3rd August 2009 to 1st August 2016 (link here)

• IEI For the period 3rd August 2009 to 1st August 2016 (link here).

This data will need to placed in the directory specified by the QSTrader settings file if you

wish to replicate the results.

28.2 Python QSTrader Implementation

Since QSTrader handles the position tracking, portfolio management, data ingestion and order

management the only code we need to write involves the Strategy object itself.

The Strategy communicates with the PortfolioHandler via the event queue, making use

of SignalEvent objects to do so. In addition we must import the base abstract strategy class,

AbstractStrategy.

Note that in the current alpha version of QSTrader we must also import the PriceParser

class. This is used to multiply all prices on input by a large multiple (10 8 ) and perform integer

arithmetic when tracking positions. This avoids floating point rounding issues that can accumulate

over the long period of a backtest. We must divide all the prices by PriceParser.PRICE_MULTIPLIER

to obtain the correct values:

from __future__ import print_function

from math import floor

import numpy as np

from qstrader.price_parser import PriceParser

from qstrader.event import (SignalEvent, EventType)

from qstrader.strategy.base import AbstractStrategy

The next step is to create the KalmanPairsTradingStrategy class. The job of this class is

to determine when to create SignalEvent objects based on received BarEvents from the daily

OHLCV bars of TLT and IEI from Yahoo Finance.

There are many different ways to organise this class. I have opted to hardcode all of the

parameters in the class for clarity of the explanation. Notably I have fixed the value of δ =

10 −4 and v t = 10 −3 . They represent the system noise and measurement noise variance in the

Kalman Filter model. This could also be implemented as a keyword argument in the __init__

constructor of the class. Such an approach would allow straightforward parameter optimisation.

The first task is to set the time and invested members to be equal to None, as they will be

updated as market data is accepted and trade signals generated. latest_prices is an array of

length two containing the current prices of TLT and IEI, used for convenience throughout the

class.

The next set of parameters all relate to the Kalman Filter and are explained in depth in the

previous chapters.

The final set of parameters include days, used to track how many days have passed as well

as qty and cur_hedge_qty, used to track the absolute quantities of ETFs to purchase for both

the long and short side. I have set this to be 2,000 units on an account equity of 100,000 USD.

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

Saved successfully!

Ooh no, something went wrong!