Hands-On Financial Trading with Python - Jiri Pik - E-Book

Hands-On Financial Trading with Python E-Book

Jiri Pik

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Creating an effective system to automate your trading can help you achieve two of every trader’s key goals; saving time and making money. But to devise a system that will work for you, you need guidance to show you the ropes around building a system and monitoring its performance. This is where Hands-on Financial Trading with Python can give you the advantage.

This practical Python book will introduce you to Python and tell you exactly why it’s the best platform for developing trading strategies. You’ll then cover quantitative analysis using Python, and learn how to build algorithmic trading strategies with Zipline using various market data sources.

Using Zipline as the backtesting library allows access to complimentary US historical daily market data until 2018. As you advance, you will gain an in-depth understanding of Python libraries such as NumPy and pandas for analyzing financial datasets, and explore Matplotlib, statsmodels, and scikit-learn libraries for advanced analytics.

As you progress, you’ll pick up lots of skills like time series forecasting, covering pmdarima and Facebook Prophet.
By the end of this trading book, you will be able to build predictive trading signals, adopt basic and advanced algorithmic trading strategies, and perform portfolio optimization to help you get —and stay—ahead of the markets.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 236

Veröffentlichungsjahr: 2021

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Hands-On Financial Trading with Python

A practical guide to using Zipline and other Python libraries for backtesting trading strategies

Jiri Pik

Sourav Ghosh

BIRMINGHAM—MUMBAI

Hands-On Financial Trading with Python

Copyright © 2021 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Kunal Parikh

Publishing Product Manager: Aditi Gour

Senior Editor: Mohammed Yusuf Imaratwale

Content Development Editor: Athikho Sapuni Rishana

Technical Editor: Manikandan Kurup

Copy Editor: Safis Editing

Project Coordinator: Aishwarya Mohan

Proofreader: Safis Editing

Indexer: Priyanka Dhadke

Production Designer: Nilesh Mohite

First published: April 2021

Production reference: 1290421

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-83898-288-1

www.packt.com

Contributors

About the authors

Jiri Pik is an artificial intelligence architect and strategist who works with major investment banks, hedge funds, and other players. He has architected and delivered breakthrough trading, portfolio, and risk management systems, as well as decision support systems, across numerous industries. His consulting firm, Jiri Pik—RocketEdge, provides its clients with certified expertise, judgment, and execution at lightspeed.

Sourav Ghosh has worked in several proprietary high-frequency algorithmic trading firms over the last decade. He has built and deployed extremely low-latency, high-throughput automated trading systems for trading exchanges around the world, across multiple asset classes. He specializes in statistical arbitrage market-making and pairs trading strategies for the most liquid global futures contracts. He works as a senior quantitative developer at a trading firm in Chicago. He holds a master's in computer science from the University of Southern California. His areas of interest include computer architecture, FinTech, probability theory and stochastic processes, statistical learning and inference methods, and natural language processing.

About the reviewer

Ratanlal Mahanta is currently working as a quantitative analyst at bittQsrv, a global quantitative research company offering quant models for its investors. He has several years of experience in the modeling and simulation of quantitative trading. He holds a master's degree in science in computational finance, and his research areas include quant trading, optimal execution, and high-frequency trading. He has over 9 years' experience in the finance industry and is gifted at solving difficult problems that lie at the intersection of markets, technology, research, and design.

Table of Contents

Preface

Section 1: Introduction to Algorithmic Trading

Chapter 1: Introduction to Algorithmic Trading

Walking through the evolution of algorithmic trading

Understanding financial asset classes

Going through the modern electronic trading exchange

Order types

Limit order books

The exchange matching engine

Understanding the components of an algorithmic trading system

The core infrastructure of an algorithmic trading system

The quantitative infrastructure of an algorithmic trading system

Summary

Section 2: In-Depth Look at Python Libraries for the Analysis of Financial Datasets

Chapter 2: Exploratory Data Analysis in Python

Technical requirements

Introduction to EDA

Steps in EDA

Revelation of the identity of A, B, and C and EDA's conclusions

Special Python libraries for EDA

Summary

Chapter 3: High-Speed Scientific Computing Using NumPy

Technical requirements

Introduction to NumPy

Creating NumPy ndarrays

Creating 1D ndarrays

Creating 2D ndarrays

Creating any-dimension ndarrays

Creating an ndarray with np.zeros(...)

Creating an ndarray with np.ones(...)

Creating an ndarray with np.identity(...)

Creating an ndarray with np.arange(...)

Creating an ndarray with np.random.randn(…)

Data types used with NumPy ndarrays

Creating a numpy.float64 array

Creating a numpy.bool array

ndarrays' dtype attribute

Converting underlying data types of ndarray with numpy.ndarrays.astype(...)

Indexing of ndarrays

Direct access to an ndarray's element

ndarray slicing

Boolean indexing

Indexing with arrays

Basic ndarray operations

Scalar multiplication with an ndarray

Linear combinations of ndarrays

Exponentiation of ndarrays

Addition of an ndarray with a scalar

Transposing a matrix

Changing the layout of an ndarray

Finding the minimum value in an ndarray

Calculating the absolute value

Calculating the mean of an ndarray

Finding the index of the maximum value in an ndarray

Calculating the cumulative sum of elements of an ndarray

Finding NaNs in an ndarray

Finding the truth values of x1>x2 of two ndarrays

any and all Boolean operations on ndarrays

Sorting ndarrays

Searching within ndarrays

File operations on ndarrays

File operations with text files

File operations with binary files

Summary

Chapter 4: Data Manipulation and Analysis with pandas

Technical requirements

Introducing pandas Series, pandas DataFrames, and pandas Indexes

pandas.Series

pandas.DataFrame

pandas.Index

Learning essential pandas.DataFrame operations

Indexing, selection, and filtering of DataFrames

Dropping rows and columns from a DataFrame

Sorting values and ranking the values' order within a DataFrame

Arithmetic operations on DataFrames

Merging and combining multiple DataFrames into a single DataFrame

Hierarchical indexing

Grouping operations in DataFrames

Transforming values in DataFrames' axis indices

Handling missing data in DataFrames

The transformation of DataFrames with functions and mappings

Discretization/bucketing of DataFrame values

Permuting and sampling DataFrame values to generate new DataFrames

Exploring file operations with pandas.DataFrames

CSV files

JSON files

Summary

Chapter 5: Data Visualization Using Matplotlib

Technical requirements

Creating figures and subplots

Defining figures' subplots

Plotting in subplots

Enriching plots with colors, markers, and line styles

Enriching axes with ticks, labels, and legends

Enriching data points with annotations

Saving plots to files

Charting a pandas DataFrame with Matplotlib

Creating line plots of a DataFrame column

Creating bar plots of a DataFrame column

Creating histogram and density plots of a DataFrame column

Creating scatter plots of two DataFrame columns

Plotting time series data

Summary

Chapter 6: Statistical Estimation, Inference, and Prediction

Technical requirements

Introduction to statsmodels

Normal distribution test with Q-Q plots

Time series modeling with statsmodels

ETS analysis of a time series

Augmented Dickey-Fuller test for stationarity of a time series

Autocorrelation and partial autocorrelation of a time series

ARIMA time series model

Using a SARIMAX time series model with pmdarima

Time series forecasting with Facebook's Prophet library

Introduction to scikit-learn regression and classification

Generating the dataset

Running RidgeCV regression on the dataset

Running a classification method on the dataset

Summary

Section 3: Algorithmic Trading in Python

Chapter 7: Financial Market Data Access in Python

Technical requirements

Exploring the yahoofinancials Python library

Single-ticker retrieval

Multiple-tickers retrieval

Exploring the pandas_datareader Python library

Access to Yahoo Finance

Access to EconDB

Access to the Federal Reserve Bank of St Louis' FRED

Caching queries

Exploring the Quandl data source

Exploring the IEX Cloud data source

Exploring the MarketStack data source

Summary

Chapter 8: Introduction to Zipline and PyFolio

Technical requirements

Introduction to Zipline and PyFolio

Installing Zipline and PyFolio

Installing Zipline

Installing PyFolio

Importing market data into a Zipline/PyFolio backtesting system

Importing data from the historical Quandl bundle

Importing data from the CSV files bundle

Importing data from custom bundles

Structuring Zipline/PyFolio backtesting modules

Trading happens every day

Trading happens on a custom schedule

Reviewing the key Zipline API reference

Types of orders

Commission models

Slippage models

Running Zipline backtesting from the command line

Introduction to risk management with PyFolio

Market volatility, PnL variance, and PnL standard deviation

Trade-level Sharpe ratio

Maximum drawdown

Summary

Chapter 9: Fundamental Algorithmic Trading Strategies

Technical requirements

What is an algorithmic trading strategy?

Learning momentum-based/trend-following strategies

Rolling window mean strategy

Simple moving averages strategy

Exponentially weighted moving averages strategy

RSI strategy

MACD crossover strategy

RSI and MACD strategies

Triple exponential average strategy

Williams R% strategy

Learning mean-reversion strategies

Bollinger band strategy

Pairs trading strategy

Learning mathematical model-based strategies

Minimization of the portfolio volatility strategy with monthly trading

Maximum Sharpe ratio strategy with monthly trading

Learning time series prediction-based strategies

SARIMAX strategy

Prophet strategy

Summary

Appendix A: How to Setup a Python Environment

Technical requirements

Initial setup

Downloading the complimentary Quandl data bundle

Other Books You May Enjoy

Preface

Algorithmic trading helps you stay ahead of the market by devising strategies in quantitative analysis to gain profits and cut losses. This book will help you to understand financial theories and execute a range of algorithmic trading strategies confidently.

The book starts by introducing you to algorithmic trading, the pyfinance ecosystem, and Quantopian. You'll then cover algorithmic trading and quantitative analysis using Python, and learn how to build algorithmic trading strategies on Quantopian. As you advance, you'll gain an in-depth understanding of Python libraries such as NumPy and pandas for analyzing financial datasets, and also explore the matplotlib, statsmodels, and scikit-learn libraries for advanced analytics. Moving on, you'll explore useful financial concepts and theories such as financial statistics, leveraging and hedging, and short selling, which will help you understand how financial markets operate. Finally, you will discover mathematical models and approaches for analyzing and understanding financial time series data.

By the end of this trading book, you will be able to build predictive trading signals, adopt basic and advanced algorithmic trading strategies, and perform portfolio optimization on the Quantopian platform.

Who this book is for

This book is for data analysts and financial traders who want to explore algorithmic trading using Python core libraries. If you are looking for a practical guide to execute various algorithmic trading strategies, then this book is for you. Basic working knowledge of Python programming and statistics will be helpful.

What this book covers

Chapter 1, Introduction to Algorithmic Trading and Python, introduces the key financial trading concepts and explains why Python is best suited for algorithmic trading.

Chapter 2, Exploratory Data Analysis in Python, provides an overview of the first step in processing any dataset, exploratory data analysis.

Chapter 3, High-Speed Scientific Computing Using NumPy, takes a detailed look at NumPy, a library for fast and scalable structured arrays and vectorized computations.

Chapter 4, Data Manipulation and Analysis with pandas, introduces the pandas library, built on top of NumPy, which provides data manipulation and analysis methods to structured DataFrames.

Chapter 5, Data Visualization Using Matplotlib, focuses on one of the primary visualization libraries in Python, Matplotlib.

Chapter 6, Statistical Estimation, Inference, and Prediction, discusses the statsmodels and scikit-learn libraries for advanced statistical analysis techniques, time series analysis techniques, as well as training and validating machine learning models.

Chapter 7, Financial Market Data Access in Python, describes alternative ways to retrieve market data in Python.

Chapter 8, Introduction to Zipline and PyFolio, covers Zipline and PyFolio, which are Python libraries that abstract away the complexities of actual backtesting and performance/risk analysis of algorithmic trading strategies. They allow you to entirely focus on the trading logic.

Chapter 9, Fundamental Algorithmic Trading Strategies, introduces the concept of an algorithmic strategy, and eight different trading algorithms representing the most used algorithms.

To get the most out of this book

Follow the instructions in the Appendix section on how to recreate the conda virtual environment using the environment.yml file stored in the book's GitHub's repository. One command restores the entire environment.

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Hands-On-Financial-Trading-with-Python. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781838982881_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Let's create a zipline_env virtual environment with Python 3.6."

A block of code is set as follows:

from zipline import run_algorithm

from zipline.api import order_target_percent, symbol

from datetime import datetime

import pytz

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

from . import quandl  # noqa

from . import csvdir  # noqa

from . import quandl_eod  # noqa

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Then, specify the variable in the Environment Variables... dialog."

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

Section 1: Introduction to Algorithmic Trading

This section will introduce you to important concepts in algorithmic trading and Python.

This section comprises the following chapter:

Chapter 1, Introduction to Algorithmic Trading and Python

Chapter 1: Introduction to Algorithmic Trading

In this chapter, we will take you through a brief history of trading and explain in which situations manual and algorithmic trading each make sense. Additionally, we will discuss financial asset classes, which are a categorization of the different types of financial assets. You will learn about the components of the modern electronic trading exchange, and, finally, we will outline the key components of an algorithmic trading system.

In this chapter, we will cover the following topics:

Walking through the evolution of algorithmic trading Understanding financial asset classesGoing through the modern electronic trading exchangeUnderstanding the components of an algorithmic trading system

Walking through the evolution of algorithmic trading

The concept of trading one possession for another has been around since the beginning of time. In its earliest form, trading was useful for exchanging a less desirable possession for a more desirable possession. Eventually, with the passage of time, trading has evolved into participants trying to find a way to buy and hold trading instruments (that is, products) at prices perceived as lower than fair value in the hopes of being able to sell them in the future at a price higher than the purchase price. This buy-low-and-sell-high principle serves as the basis for all profitable trading to date; of course, how to achieve this is where the complexity and competition lies.

Markets are driven by the fundamental economic forces of supply and demand. As demand increases without a commensurate increase in supply, or supply decreases without a decrease in demand, a commodity becomes scarce and increases in value (that is, its market price). Conversely, if demand drops without a decrease in supply, or supply increases without an increase in demand, a commodity becomes more easily available and less valuable (a lower market price). Therefore, the market price of a commodity should reflect the equilibrium price based on available supply (sellers) and available demand (buyers).

There are many drawbacks to the manual trading approach, as follows:

Human traders are inherently slow at processing new market information, making them likely to miss information or to make errors in interpreting updated market data. This leads to bad trading decisions.Humans, in general, are also prone to distractions and biases that reduce profits and/or generate losses. For example, the fear of losing money and the joy of making money also causes us to deviate from the optimal systematic trading approach, which we understand in theory but fail to execute in practice. In addition, people are also naturally and non-uniformly biased against profitable trades versus losing trades; for instance, human traders are quick to increase the amount of risk after profitable trades and slow down to decrease the amount of risk after losing trades.Human traders learn by experiencing market conditions, for example, by being present and trading live markets. So, they cannot learn from and backtest over historical market data conditions – an important advantage of automated strategies, as we will see later.

With the advent of technology, trading has evolved from pit trading carried out by yelling and signaling buy and sell orders all the way to using sophisticated, efficient, and fast computer hardware and software to execute trades, often without much human intervention. Sophisticated algorithmic trading software systems have replaced human traders and engineers, and mathematicians who build, operate, and improve these systems, known as quants, have risen to power.

In particular, the key advantages of an automated, computer-driven systematic/algorithmic trading approach are as follows:

Computers are extremely good at performing clearly defined and repetitive rule-based tasks. They can perform these tasks extremely quickly and can handle massive throughputs.Additionally, computers do not get distracted, tired, or make mistakes (unless there is a software bug, which, technically, counts as a software developer error).Algorithmic trading strategies also have no emotions as far as trading through losses or profits; therefore, they can stick to a systematic trading plan no matter what.

All of these advantages make systematic algorithmic trading the perfect candidate to set up low-latency, high-throughput, scalable, and robust trading businesses.

However, algorithmic trading is not always better than manual trading:

Manual trading is better at dealing with significantly complex ideas and the complexities of real-world trading operations that are, sometimes, difficult to express as an automated software solution.Automated trading systems require significant investments in time and R&D costs, while manual trading strategies are often significantly faster to get to market.Algorithmic trading strategies are also prone to software development/operation bugs, which can have a significant impact on a trading business. Entire automated trading operations being wiped out in a matter of a few minutes is not unheard of.Often, automated quantitative trading systems are not good at dealing with extremely unlikely events termed as black swan events, such as the LTCM crash, the 2010 flash crash, the Knight Capital crash, and more.

In this section, we learned about the history of trading and when automated/algorithmic is better than manual trading. Now, let's proceed toward the next section, where we will learn about the actual subject of trading categorized into financial asset classes.

Understanding financial asset classes

Algorithmic trading deals with the trading of financial assets. A financial asset is a non-physical asset whose value arises from contractual agreements.

The major financial asset classes are as follows:

Equities (stocks): These allow market participants to invest directly in the company and become owners of the company.Fixed income (bonds): These represent a loan made by the investor to a borrower (for instance, a government or a firm). Each bond has its end date when the principal of the loan is due to be paid back and, usually, either fixed or variable interest payments made by the borrower over the lifetime of the bond.Real Estate Investment Trusts (REITs): These are publicly traded companies that own or operate or finance income-producing real estate. These can be used as a proxy to directly invest in the housing market, say, by purchasing a property.Commodities: Examples include metals (silver, gold, copper, and more) and agricultural produce (wheat, corn, milk, and more). They are financial assets tracking the price of the underlying commodities.Exchange-Traded Funds (ETFs): An EFT is an exchange-listed security that tracks a collection of other securities. ETFs, such as SPY, DIA, and QQQ, hold equity stocks to track the larger well-known S&P 500, Dow Jones Industrial Average, and Nasdaq stock indices. ETFs such as United States Oil Fund (USO) track oil prices by investing in short-term WTI crude oil futures. ETFs are a convenient investment vehicle for investors to invest in a wide range of asset classes at relatively lower costs.Foreign Exchange (FX) between different currency pairs, the major ones being the US Dollar (USD), Euro (EUR), Pound Sterling (GBP), Japanese Yen (JPY), Australian Dollar (AUD), New Zealand Dollar (NZD), Canadian Dollar (CAD), Swiss Franc (CHF), Norwegian Krone (NOK), and Swedish Krona (SEK). These are often referred to as the G10 currencies.The key Financial derivatives are options and futures – these are complex leveraged derivative products that can magnify the risk as well as the reward:

a) Futures are financial contracts to buy or sell an asset at a predetermined future date and price.

b) Options are financial contracts giving their owner the right, but not the obligation, to buy or sell an underlying asset at a stated price (strike price) prior to or on a specified date.

In this section, we learned about the financial asset classes and their unique properties. Now, let's discuss the order types and exchange matching algorithms of modern electronic trading exchanges.

Going through the modern electronic trading exchange

The first trading exchange was the Amsterdam Stock Exchange, which began in 1602. Here, the trading happened in person. The applications of technology to trading included using pigeons, telegraph systems, Morse code, telephones, computer terminals, and nowadays, high-speed computer networks and state-of-the-art computers. With the passage of time, the trading microstructure has evolved into the order types and matching algorithms that we are used to today.

Knowledge of the modern electronic trading exchange microstructure is important for the design of algorithmic strategies.

Order types

Financial trading strategies employ a variety of different order types, and some of the most common ones include Market orders, Market with Price Protection orders, Immediate-Or-Cancel (IOC) orders, Fill and Kill (FAK) orders, Good-'Till-Day (GTD) orders, Good-'Till-Canceled (GTC) orders, Stop orders, and Iceberg orders.

For the strategies that we will be exploring in this book, we will focus on Market orders, IOC, and GTC.

Market orders

Market orders are buy-or-sell orders that need to be executed instantly at the current market price and are used when the immediacy of execution is preferred to the execution price.

These orders will execute against all available orders on the opposite side at the order's price until all the quantity asked for is executed. If it runs out of available liquidity to match against, it can be configured to sit in the order book or expire. Sitting in the book means the order becomes a resting order that is added to the book for other participants to trade against. To expire means that the remaining order quantity is canceled instead of being added to the book so that new orders cannot match against the remaining quantity.

So, for instance, a buy market order will match against all sell orders sitting in the book from the best price to the worst price until the entire market order is executed.

These orders may suffer from extreme slippage, which is defined as the difference in the executed order's price and the market price at the time the order was sent.

IOC orders

IOC orders cannot execute at prices worse than what they were sent for, which means buy orders cannot execute higher than the order's price, and sell orders cannot execute lower than the order's price. This concept is known as limit price since that price is limited to the worst price the order can execute at.

An IOC order will continue matching against orders on the order side until one of the following happens:

The entire quantity on the IOC order is executed.The price of the passive order on the other side is worse than the IOC order's price.The IOC order is partially executed, and the remaining quantity expires.

An IOC order that is sent at a price better than the best available order on the other side (that is, the buy order is lower than the best offer price, or the sell order is higher than the best bid price) does not execute at all and just expires.

GTC orders

GTC orders can persist indefinitely and require a specific cancellation order.

Limit order books

The exchange accepts order requests from all market participants and maintains them in a limit order book. Limit order books are a view into all the market participant's visible orders available at the exchange at any point in time.

Buy orders (or bids) are arranged from the highest price (that is, the best price) to the lowest price (that is, the worst price), and Ask orders (that is, asks or offers) are arranged from the lowest price (that is, the best price) to the highest price (that is, the lowest price).

The highest bid prices are considered the best bid prices because buy orders with the highest buy prices are the first to be matched, and the reverse is true for ask prices, that is, sell orders with the lowest sell prices match first.

Orders on the same side and at the same price level are arranged in the First-In-First-Out (FIFO) order, which is also known as priority order – orders with better priority are ahead of orders with lower priority because the better priority orders have reached the exchange before the others. All else being equal (that is, the same order side, price, and quantity), orders with better priority will execute before orders with worse priority.

The exchange matching engine

The matching engine at the electronic trading exchange performs the matching of orders using exchange matching algorithms. The process of matching entails checking all active orders entered by market participants and matching the orders that cross each other in price until there are no unmatched orders that could be matched – so, buy orders with prices at or above other sell orders match against them, and the converse is true as well, that is, sell orders with prices at or below other buy orders match against them. The remaining orders remain in the exchange matching book until a new order flow comes in, leading to new matches if possible.