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.

412

• Asset Selection - Choosing additional, or alternative, pairs of ETFs would help to add

diversification to the portfolio, but increases the complexity of the strategy as well as the

number of trades (and thus transaction costs).

28.5 Full Code

# kalman_qstrader_strategy.py

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 KalmanPairsTradingStrategy(AbstractStrategy):

"""

Requires:

tickers - The list of ticker symbols

events_queue - A handle to the system events queue

"""

def __init__(

self, tickers, events_queue

):

self.tickers = tickers

self.events_queue = events_queue

self.time = None

self.latest_prices = np.array([-1.0, -1.0])

self.invested = None

self.delta = 1e-4

self.wt = self.delta / (1 - self.delta) * np.eye(2)

self.vt = 1e-3

self.theta = np.zeros(2)

self.P = np.zeros((2, 2))

self.R = None

self.days = 0

self.qty = 2000

self.cur_hedge_qty = self.qty

def _set_correct_time_and_price(self, event):

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

Saved successfully!

Ooh no, something went wrong!