Quantitative Trading and Systematic Investing

Letian Wang Blog on Quant Trading and Portfolio Management

0%

Thank you for visiting my blog, a place dedicated to quantitative trading and systematic investing.

You may also like the open-source trading system quanttrader, which is a pure python-based event-driven backtest and live trading package for quant traders. The package is published here on pypi and is ready to be pip installed. The document is hosted here on readthedocs.

Most of the quantitative research source codes are hosted in the QuantResearch project on Github.

Introduction

Market Profile and Volume Profile in Python -- Free yet powerful trade flow profiling tools for intraday stock market analysis is published here on medium. It illustrates how to combine Yahoo Finance, Google Colab, and Python Plotly to generate a free yet very powerful interactive charting tool for intraday market profiling analysis.

Market profile, as its name suggests, is a tool to profile stock market intraday trade activities. It was introduced by CBOT trader J. Peter Steidlmayer. It shows how much time the trading session is spent on a particular price level. Similarly, the volume profile depicts how many shares have been transacted at a particular price.

For details, see this post.

Read more »

Historical market data is essential for financial analysis and strategy backtesting. Professional data vendors sometimes are not an economically viable option for retail investors or startups. Fortunately, with the development of financial technologies or FinTech and the movement of inclusive finance, there are choices of free market data sources available online.

This post discusses three of them and demonstrate how to get data with Python. The three data sources and APIs discussed here are:

  • Yahoo Finance

  • Pandas DataReader

  • Quandl

  • Interactive Brokers (Supplemental)

The full post can be found here on medium.

Introduction

I decided to write a story discussing some machine learning in finance practices I see online. There are a lot of articles and books about this topic. This post is different in that the concepts described here may not be completely correct or mathematically tight. It is just my current understanding of machine learning and data science, and I’m prepared to come back updating some concepts based on feedback and new developments.

The full post is on medium now.

Read more »

Introduction

This post domonstrates how to use quanttrader to connect to Interactive Brokers and do live algorithmic trade. Quanttrader is pure Python and the brokerage API is also native Python so in total the solution is 100% Python. Interactive Brokers is a popular brokerage among quant traders thanks to its powerful and robust Application Programming Interface (API). A few years ago, I open sourced a trading system with connection to IB C# API. Now with IB's new Native Python API library, it is a good idea to build strategies in Python in order to leverage Python's machine learning toolkits.

The demo video is located here on Youtube.

For quanttrader backtest, check out this post.

Read more »

Introduction

This post demonstrates how to use reinforcement learning to price an American Option. An option is a derivative contract that gives its owner the right but not the obligation to buy or sell an underlying asset. Unlike its European-style counterpart, American-style option may exercise at any time before expiry.

American Option is known to be an optimal control MDP (Markov Decision Process) problem where the underlying process is Geometric Brownian motion ([1]). The Markovian state is a price-time tuple and the control is a binary action that decides on each day whether to exercise the option or not.

This post is published on medium here. The accompanying Jupyter notebook is located here on Github.

Read more »

Introduction

Exponentially Weighted Moving Average (EWMA or EWA) is a simple and powerful tool in quantitative trading. There are a lot of technical analysis articles about this concept. There are some articles explaining the situation under varying or irregular intervals. I write this note in order to apply it to intra-day trading.

Read more »

This post discusses the risk parity framework and maximum diversification portfolio. Then backtest the monthly portfolio rebalance strategy across five portfolios: minimum-variance, maximum-Sharpe, most-diversified, risk-parity, and equal-weights.

Introduction

In previous post we reviewed the basics of mean-variance optimization (MVO), and portfolios such as minimum variance and maximmum sharpe. This post continues to discuss some popular practices in asset allocation, namely risk parity and maximum diversification. Then we evaluate these allocation strategies using historical market data.

It is known that all these portfolios are special cases of MVO under some conditions. For example, MVO becomes minimum variance if expected reutrns are equal; it coincides with maximum diversification if return-risk ratios are the same across assets ([2]). The portfolio selection depends on our information and knowledge about the market. If expected return and risks are known with certainty, a maximum sharpe ratio is good choice. If expected return is hard to measure, covariance knowledge can be leveraged to construct minimum variance or risk parity portfolios. If we have no knowledge at all about the market, a naive equal-weighting portfolio will be a default option, which is also served as benchmark in our backtest.

Read more »

This post demonstrates how to use Expecation-Maximization (EM) Algorithm, Gaussian Mixture Model (GMM) and Markov Regime Switching Model (MRSM) to detect the latent stock market regime switches.

Introduction

It's been a while since the post of Hidden Markov Chain (HMM), this time let's continue to explore some other popular regime switching models, including Gaussian Mixture model and Markov Regime Switching model (MRSM). Similar to HMM, the market regime is served as hidden states so they are all approached by some sort of Expectation-Maximization (EM) machine learning techniques. It's in place to have a brief discussion on EM algorithm first.

The accompanying notebook can be found here.

Read more »

This post discusses the AutoRegressive Integrated Moving Average model (ARIMA) and the Autoregressive conditional heteroskedasticity model (GARCH) and their applications in stock market prediction.

Introduction

An ARMA (AutoRegressive-Moving Average) has two parts, the AR(p) part and MA(q) part, expressed as below

\[ \begin{aligned} X_t &= c + \epsilon_t+\sum_{i=1}^p\varphi_iX_{t-i}+\sum_{i=1}^q\theta_i\epsilon_{t-i}\\\\ \left( 1-\sum_{i=1}^p\varphi_iL^i \right)X_t&=c+\left(1+\sum_{i=1}^q\theta_iL^i \right)\epsilon_i \end{aligned} \]

where \(L\) is the lag operator and \(\epsilon_i\) is white noise. It can be approached by Box-Jenkins method. We may use PACF plot to identify AR lag order \(p\), and ACF plot to identify MA lag order \(q\); or use information such as AIC and BIC to do the model selection.

ARIMA (AutoRegressive Integrated Moving Average) is a generalization of ARMA by adding an integrated part with order \(d\) for non-stationary processes.

While ARIMA works on price level or returns, GARCH (Generalized AutoRegressive Conditional heteroskedasticity) tries to model the clustering in volatility or squared returns. It extends ARMA terms to the variance front

Read more »