33 Introducing You to the World of Quantitative Analysis

33 Introducing You to the World of Quantitative Analysis #

Hello, I’m Jingxiao.

During the peak in 2000, Goldman Sachs employed 600 traders to buy and sell cash stocks for institutional clients. But today, that number is down to only 2 traders (Ref. The Economist). By 2009, in the aftermath of the financial crisis, experts warned about the increasing involvement of machines in stock and securities trading, as the rise of machines gradually led to the disappearance of manual trading jobs.

A long time ago, the trading floor at UBS looked like this:

Eight years later, the trading floor only had these people:

In fact, with the rapid development of data processing technology and the gradually matured research on quantitative trading models, cash stock trading, bond markets, futures markets, and a considerable part of investment banking have all been moving towards automation.

And by 2017, when WannyCry swept the globe, Bitcoin emerged into the public eye within just a few months, with its price skyrocketing. Many people who traded Bitcoin made a fortune. Some people even applied quantitative strategies from the financial industry to Bitcoin trading, profiting from arbitrage or short-term trading in the early stages of the immature market.

In this lesson, we will explore the world of quantitative trading. As part of our comprehensive practice module in the Python column, I hope you can gain something unique from this content.

What is trading? #

The market is one of the greatest inventions in human history. Adam Smith vividly explained the interaction between the market and rational individuals, ultimately benefiting the entire society, using the concept of the “invisible hand” in The Wealth of Nations.

At the heart of the market is exchange. Initially, human development involved barter, the primitive “trade of goods for goods,” before the birth of money. However, this approach was highly inefficient and inconvenient for circulation and exchange. Often, one would have to go through a long chain of exchange to obtain the desired item. Consequently, the concept of a medium of exchange emerged, and division of labor gradually appeared. People would exchange the goods they produced for a medium of exchange, and then exchange that for other goods they needed.

And at the core of exchange is buying and selling. When both buyers and sellers expect equal prices, a transaction is made. With the development of finance and technology, an increasing number of financial instruments such as stocks, bonds, options, and futures have emerged, and financial derivatives have become more complex.

In investment banks, one role that we often hear about is that of quantitative fund traders. They analyze the value of investment targets based on these complex derivatives and manage their positions through buying and selling using certain strategies.

Why trading can be profitable is a puzzling question for many. Does the market indeed follow any patterns? It definitely does, but although there are traces to follow, they cannot be fully relied upon. The diversity of trading and the complexity of human nature lead to significant noise in financial data, making it impossible for us to simply derive market changes from one or two factors.

Therefore, the trading industry itself demands exceptionally high qualities from its participants. Besides having solid professional qualifications (including financial knowledge, quantitative logic, analytical skills, and decision-making abilities), there is also a high requirement for psychological qualities. This industry, which deals directly with money and confronts the deepest desires of human nature, has attracted countless experts. Many have become extremely wealthy, while others have gone bankrupt and lost everything.

So, what are the ways to mitigate the risks brought by psychological qualities?

Quantitative Trading #

Before answering this question, let me start with a side note. Friends who have just been exposed to quantitative trading are often confused by several terms: quantitative trading, program trading, algo-trading, high-frequency trading, and automated trading systems.

Although I have come across many people in the industry who mix these terms, it is still helpful for beginners to clarify these terms. At least, when others mention these fancy terms, we don’t have to feel intimidated.

Let’s start with program trading, which usually uses computer programs instead of traders to execute buy and sell orders of financial products. For example, if a fund manager needs to sell a large number of stocks, directly placing a large sell order may impact the market. In this case, the order can be split into smaller orders and executed gradually using computer programs. Therefore, program trading is often the underlying layer of quantitative trading.

Algo-trading, on the other hand, is usually used in high-frequency trading. It refers to the use of algorithms to quickly determine the timing of buying and selling and to execute multiple trades rapidly.

Quantitative trading, on the other hand, usually refers to the use of mathematical, statistical, and even machine learning methods to identify suitable buying and selling opportunities. Therefore, under this definition, algo-trading, high-frequency trading, and statistical arbitrage can all be considered forms of quantitative trading.

In simple terms, we can consider quantitative trading to have the widest scope. Therefore, when you are unsure which term to use, you can use quantitative trading.

Now let’s get back to our original question. The method to avoid risks caused by psychological factors is naturally quantitative trading. The benefits of quantitative trading are obvious. At first glance, computers can work tirelessly without the need for real-time trading by traders, fulfilling people’s desire to “make money while lying down.” Of course, this is just an ideal scenario. If we actually do this, we will soon have to go back to work. In reality, mature quantitative trading still requires human intervention to timely intervene and prevent huge trading losses caused by sudden algorithm failures.

This is even more significant in the field of cryptocurrency trading. Cryptocurrency trades are conducted on numerous exchanges around the world. Unlike stocks, which are traded on only a few exchanges, a cryptocurrency can be traded simultaneously on all exchanges. Additionally, due to the absence of opening and closing restrictions in the cryptocurrency market, trading occurs 24/7, making it much more demanding than the “996” work schedule. If there were traders who could survive in this market, they would deserve the title of “gods.”

Trading on multiple exchanges means that the global cryptocurrency market is closely interconnected at all times. Any significant changes in one market or region will affect all markets. For example, on the day when North Korea detonated a hydrogen bomb in 2017, even before the news was released, bitcoin prices immediately surged in neighboring South Korea and Japan. Another example is when negative news about bitcoin broke in the middle of the night, its price also immediately plummeted.

Bitcoin Prices on Leading Global Exchanges in June 2019

The highly fluctuating nature of bitcoin prices is often observed. Today, you may be financially free, but tomorrow, your financial freedom may not be as certain. Obviously, in such a market, human labor is difficult to sustain, and quantitative trading is a suitable approach.

Traditional electronic trading (such as stocks and cryptocurrencies) involves sending buy and sell orders directly to exchanges through brokerages or software. The underlying layer of algo-trading is to automate such operations using programs. Brokerages or exchanges usually provide API interfaces to investors. For example, the API of Interactive Brokers allows access to stock and options market data and trading. Similarly, exchanges like Gemini and OKCoin provide corresponding APIs for cryptocurrency market data retrieval and trading.

The public market data API of the Gemini exchange, for example, can retrieve the latest price and trading volume of bitcoin (BTC) against the US dollar (USD) using a simple HTTP GET request.

########## Gemini Market Data API ##########
## https://api.gemini.com/v1/pubticker/:symbol

import json
import requests

gemini_ticker = 'https://api.gemini.com/v1/pubticker/{}'
symbol = 'btcusd'
btc_data = requests.get(gemini_ticker.format(symbol)).json()
print(json.dumps(btc_data, indent=4))

The output of the script will be:

{
   "bid": "8825.88",
   "ask": "8827.52",
   "volume": {
       "BTC": "910.0838782726",
       "USD": "7972904.560901317851",
       "timestamp": 1560643800000
   },
   "last": "8838.45"
}

For algo-trading systems, APIs are only the lowest layer of the structure. Generally, a basic trading system should include: a market data module, a strategy module, and an execution module. Additionally, a backtesting system is usually used to assist strategy development. Their division of labor is roughly as follows:

Division of Labor in a Basic Trading System

In this diagram:

  • The market data module mainly attempts to retrieve market data and is usually responsible for obtaining the status of trading accounts.
  • The strategy module mainly subscribes to market data, generates buy and sell orders based on preset algorithms, and sends them to the execution module.
  • The execution module receives and encapsulates the buy and sell orders from the strategy module, forwards them to the exchanges, and supervises the complete execution of the strategy’s orders.

Python Algorithmic Trading #

After learning so much about quantitative trading, let’s talk about Python algorithmic trading. Python is widely used in various aspects of the finance industry, and it is playing an increasingly important role in the field of algorithmic trading. The popularity of Python in this industry can be attributed to the following four reasons.

Data Analysis Capability #

The first reason is Python’s data analysis capability. Efficient data processing is a fundamental requirement in algorithmic trading, and data processing happens to be one of Python’s strengths. In particular, the combination of NumPy and Pandas greatly improves the quality of life for algorithmic trading developers.

We can demonstrate this with a simple example of how to fetch, format, and plot the price curve of Bitcoin on Gemini exchange over the past hour. I have provided detailed comments in the relevant code, so I won’t go into much detail here. Take a look and understand it.

import matplotlib.pyplot as plt
import pandas as pd
import requests

# Select the time period for data retrieval
periods = '3600'

# Fetch historical price data of BTC through HTTP
resp = requests.get('https://api.cryptowat.ch/markets/gemini/btcusd/ohlc', 
  params={
    'periods': periods
  })
data = resp.json()

# Convert to pandas data frame
df = pd.DataFrame(
  data['result'][periods], 
  columns=[
    'CloseTime',
    'OpenPrice',
    'HighPrice',
    'LowPrice',
    'ClosePrice',
    'Volume',
    'NA'])

# Output the first few rows of the DataFrame
print(df.head())

# Plot the BTC price curve
df['ClosePrice'].plot(figsize=(14, 7))
plt.show()
########### Output ###############
   CloseTime  OpenPrice  HighPrice  ...  ClosePrice     Volume             NA
0  1558843200    8030.55    8046.30  ...     8011.20  11.642968   93432.459964
1  1558846800    8002.76    8050.33  ...     8034.48   8.575682   68870.145895
2  1558850400    8031.61    8036.14  ...     8000.00  15.659680  125384.519063
3  1558854000    8000.00    8016.29  ...     8001.46  38.171420  304342.048892
4  1558857600    8002.69    8023.11  ...     8009.24   3.582830   28716.385009

By running this code, we can get the price curve as shown in the following chart:

Abundance of Specialized Libraries #

In addition to its powerful data processing capability, Python also has numerous mature algorithmic trading libraries available for use. For example, you can use Zipline for strategy backtesting or Pyfolio for portfolio analysis. Many exchanges also provide Python-based API clients.

Convenient Trading Platforms #

The third reason is the convenience of trading platforms. There are algorithmic trading platforms that allow the execution of custom Python strategies without the need to build a quantitative trading framework. These trading platforms essentially serve as market data and execution modules for users. Users only need to define their strategy modules to engage in algorithmic trading and backtesting.

For example, Quantopian provides a standard backtesting environment based on Zipline. Users can choose Python as the development language and share their strategies with the community. In addition, there are similar platforms in China, such as BigQuant and guoren.com, which provide trading for different markets and financial products.

Wide Range of Industry Applications #

The last reason is the wide range of industry applications of Python itself. Currently, the trading departments of many investment institutions have started to use Python, thereby creating a higher demand for skilled Python developers. Naturally, learning Python has become a more meaningful “investment”.

Summary #

In this lesson, we introduced trading and the basic concepts of algorithmic trading. We also briefly discussed why learning Python is important for building quantitative trading systems. Quantitative trading is a major trend in the trading industry, and Python, as one of the most suitable languages for quantitative practitioners, holds a very important position for beginners.

In the upcoming lessons, we will dive into each module of quantitative trading in detail, gradually unveiling the mysterious veil of quantitative trading for you.

Reflection Question #

Finally, let me leave you with a reflection question. Which is more suitable for using Python, high-frequency trading or medium-low frequency trading? Why? Feel free to share your thoughts in the comments section. Also, you are welcome to share this article with more people who are interested in quantitative trading, so that we can exchange and discuss together.