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.

394

adf.test(comb$residuals, k=1)

# 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

class CointegrationBollingerBandsStrategy(AbstractStrategy):

"""

Requires:

tickers - The list of ticker symbols

events_queue - A handle to the system events queue

lookback - Lookback period for moving avg and moving std

weights - The weight vector describing

a "unit" of the portfolio

entry_z - The z-score trade entry threshold

exit_z - The z-score trade exit threshold

base_quantity - Number of "units" of the portfolio

to be traded

"""

def __init__(

self, tickers, events_queue,

lookback, weights, entry_z, exit_z,

base_quantity

):

self.tickers = tickers

self.events_queue = events_queue

self.lookback = lookback

self.weights = weights

self.entry_z = entry_z

self.exit_z = exit_z

self.qty = base_quantity

self.time = None

self.latest_prices = np.full(len(self.tickers), -1.0)

self.port_mkt_val = deque(maxlen=self.lookback)

self.invested = None

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

Saved successfully!

Ooh no, something went wrong!