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.

9

28 Kalman Filter-Based Pairs Trading using QSTrader . . . . . . . . . . . . . . . 401

28.1 The Trading Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

28.1.1 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402

28.2 Python QSTrader Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 403

28.3 Strategy Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

28.4 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

28.5 Full Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

29 Supervised Learning for Intraday Returns Prediction using QSTrader . . . . 419

29.1 Prediction Goals with Machine Learning . . . . . . . . . . . . . . . . . . . . . . . 419

29.1.1 Class Imbalance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420

29.2 Building a Prediction Model on Historical Data . . . . . . . . . . . . . . . . . . . 421

29.3 QSTrader Strategy Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

29.4 QSTrader Backtest Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

29.5 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432

29.6 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435

29.7 Full Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435

30 Sentiment Analysis via Sentdex Vendor Sentiment Data with QSTrader . . . 445

30.1 Sentiment Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

30.1.1 Sentdex API and Sample File . . . . . . . . . . . . . . . . . . . . . . . . . 446

30.2 The Trading Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

30.3 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

30.4 Python Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

30.4.1 Sentiment Handling with QSTrader . . . . . . . . . . . . . . . . . . . . . 449

30.4.2 Sentiment Analysis Strategy Code . . . . . . . . . . . . . . . . . . . . . . 453

30.5 Strategy Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456

30.5.1 Transaction Costs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456

30.5.2 Sentiment on S&P500 Tech Stocks . . . . . . . . . . . . . . . . . . . . . . 456

30.5.3 Sentiment on S&P500 Energy Stocks . . . . . . . . . . . . . . . . . . . . . 457

30.5.4 Sentiment on S&P500 Defence Stocks . . . . . . . . . . . . . . . . . . . . 458

30.6 Full Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460

31 Market Regime Detection with Hidden Markov Models using QSTrader . . 465

31.1 Regime Detection with Hidden Markov Models . . . . . . . . . . . . . . . . . . . 465

31.2 The Trading Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466

31.3 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466

31.4 Python Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467

31.4.1 Returns Calculation with QSTrader . . . . . . . . . . . . . . . . . . . . . 467

31.4.2 Regime Detection Implementation . . . . . . . . . . . . . . . . . . . . . . 468

31.5 Strategy Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478

31.5.1 Transaction Costs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478

31.5.2 No Regime Detection Filter . . . . . . . . . . . . . . . . . . . . . . . . . . 478

31.5.3 HMM Regime Detection Filter . . . . . . . . . . . . . . . . . . . . . . . . 479

31.6 Full Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

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

Saved successfully!

Ooh no, something went wrong!