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.

429

SignalEvent(self.tickers[0], "BOT", self.qty)

)

self.invested = True

if self.invested and pred == -1:

print("CLOSING LONG: %s" % event.time)

self.events_queue.put(

SignalEvent(self.tickers[0], "SLD", self.qty)

)

self.invested = False

Unfortunately for more complex models the model.predict call can be extremely slow. For

simpler models with minimal parameters (e.g. linear regression) the call is fast. On the desktop

workstation used here to generate the results it was possible to obtain approximately 5,000

bars/second for simple models.

For more complex models such as Random Forest classifiers with many deeply grown trees

this prediction speed can drop substantially, often by up to three orders of magnitude. For the

Random Forest ensemble in particular the initial model generated in this example produced a

4.1Gb pickle file, which is beyond the RAM capacity of some earlier PCs. Thus the method

has no choice but to swap to disk when predicting new data points, which can heavily reduce

performance.

One of the goals with optimising a model is to make sure that it is possible to actually

generate predictions in a reasonable time frame. In the case of the Random Forest classifier, it

was necessary to experiment with the maximum depth of the tree in order to reduce the final

pickle file size but still achieve reasonable prediction performance. The final model is a tradeoff

between prediction accuracy and backtest execution speed, which ran at approximately 60

bars/second in the example below.

29.4 QSTrader Backtest Script

Now that the Strategy class has been developed it is necessary to wrap it in an intraday backtest.

This code is given below in intraday_ml_backtest.py. It is very similar to other QSTrader

implementations.

The main differences are that a new price handler IQFeedIntradayCsvBarPriceHandler

is now imported instead of one designed for Yahoo Finance daily data, as well as the periods

parameter for the TearsheetStatistics class, which has been set to 252 × 6.5 × 60 = 98280.

This ensures that the calculated Sharpe Ratio is correct for the number of periods being used:

# intraday_ml_backtest.py

import click

import datetime

from qstrader import settings

from qstrader.compat import queue

from qstrader.price_parser import PriceParser

from qstrader.price_handler.iq_feed_intraday_csv_bar import \

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

Saved successfully!

Ooh no, something went wrong!