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.

408

It also changes the FixedPositionSizer to the NaivePositionSizer. The latter is used

to "naively" accept the suggestions of absolute quantities of ETF units to trade as determined

in the KalmanPairsTradingStrategy class. In a production environment it would be necessary

to adjust this depending upon the risk management goals of the portfolio.

Here is the full code for the kalman_qstrader_backtest.py:

import datetime

import click

from qstrader import settings

from qstrader.compat import queue

from qstrader.price_parser import PriceParser

from qstrader.price_handler.yahoo_daily_csv_bar import \

YahooDailyCsvBarPriceHandler

from qstrader.strategy import Strategies, DisplayStrategy

from qstrader.position_sizer.naive import NaivePositionSizer

from qstrader.risk_manager.example import ExampleRiskManager

from qstrader.portfolio_handler import PortfolioHandler

from qstrader.compliance.example import ExampleCompliance

from qstrader.execution_handler.ib_simulated import \

IBSimulatedExecutionHandler

from qstrader.statistics.tearsheet import TearsheetStatistics

from qstrader.trading_session.backtest import Backtest

from kalman_qstrader_strategy import KalmanPairsTradingStrategy

def run(config, testing, tickers, filename):

# Set up variables needed for backtest

events_queue = queue.Queue()

csv_dir = config.CSV_DATA_DIR

initial_equity = PriceParser.parse(100000.00)

# Use Yahoo Daily Price Handler

start_date = datetime.datetime(2009, 8, 3)

end_date = datetime.datetime(2016, 8, 1)

price_handler = YahooDailyCsvBarPriceHandler(

csv_dir, events_queue, tickers,

start_date=start_date, end_date=end_date

)

# Use the KalmanPairsTrading Strategy

strategy = KalmanPairsTradingStrategy(tickers, events_queue)

strategy = Strategies(strategy, DisplayStrategy())

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

Saved successfully!

Ooh no, something went wrong!