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.

386

27.5 Python QSTrader Implementation

Note that the full listings of each of these Python files can be found at the end of

the chapter.

Note also that this strategy contains an implicit lookahead bias and so its performance

will be grossly exaggerated compared to a real implementation. The lookahead bias occurs due to

the use of calculating the hedging ratio across the same sample of data as the trading strategy is

simulated on. In a real implementation two separate sets of data will be needed in order to verify

that any structural relationship persists out-of-sample.

The implementation of the strategy is similar to other QSTrader strategies. It involves the

creation of a subclass of AbstractStrategy in the coint_bollinger_strategy.py file. This

class is then used by the coint_bollinger_backtest.py file to actually simulate the backtest.

coint_bollinger_strategy.py will be described first. NumPy is imported, as are the

necessary QSTrader libraries for handling signals and strategies. PriceParser is brought in

to adjust the internal handling of QSTrader’s price storage mechanism to avoid floating-point

round-off error.

The Python deque double-ended queue class is also imported and used to store a rolling

window of closing price bars. THis is necessary for the moving average and standard deviation

lookback calculations. More on this below.

# coint_bollinger_strategy.py

from __future__ import print_function

from collections import deque

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 define the CointegrationBollingerBandsStrategy subclass of AbstractStrategy,

which carries out the signals generation. As with all strategies it requires a list of tickers that

it acts upon as well as a handle to the events_queue upon which to place the SignalEvent

objects.

This subclass requires additional parameters. lookback is the integer number of bars over

which to perform the rolling moving average and standard deviation calculations. weights is

the set of fixed hedging ratios, or primary Johansen test eigenvector components, to use as the

"unit" of a portfolio of cointegrating assets.

entry_z describes the multiple of z-score entry threshold (i.e. number of standard deviations)

upon which to open a trade. exit_z is the corresponding number of standard deviations upon

which to exit the trade. base_quantity is the integer number of "units" of the portfolio to

trade.

In addition the class also keeps track of the latest prices of all tickers in a separate array in

self.latest_prices. It also contains a double-ended queue consisting of the last lookback val-

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

Saved successfully!

Ooh no, something went wrong!