Algorithmic Trading 101 — Lesson 3: Introduction to Arbitrage Strategies

The Ocean
The Ocean
8 min readMay 21, 2018

--

When building algorithms through models or regressions — as we’ve done in our last two lessons — we often rely on a variety of dependent (explanatory) variables. We use these dependent variables to estimate trends in price behavior. Then, we can create a predictive model of the future price. Finally, by assessing the statistical significance of our prediction, we make trades based on whether the price is overvalued or undervalued. If we assume everyone is doing this all the time, and there are no market frictions, then we get something called the efficient markets hypothesis — that investors continuously update their models with latest and greatest information, and asset prices reach their ‘equilibrium’ level as investors quickly buy or sell based on new information that reveals whether assets are undervalued or overvalued. An implication of the efficient markets hypothesis (which is itself controversial) is that it’s impossible to beat the market in the long term.

However, in the short term, prices do indeed deviate from their modeled equilibrium, and because of a variety of frictions, might linger long enough for you — the trader — to exploit. This is especially true in the nascent crypto markets, where everyday, there are intra- and inter-exchange arbitrage opportunities.

Arbitrage is the ability to simultaneously buy and sell an asset based on price discrepancy. If done correctly, we can simultaneously buy the same asset at a lower price and sell at a higher price, thus locking in a ‘free profit.’ But this is easier said than done — these arbitrage opportunities might exist for only a short period of time, it can be hard (particularly in the crypto market) to buy and sell ‘simultaneously,’ and transaction costs can eat into profits (just to name three issues). This is where algorithmic trading comes in — it can account for many of the timing, risk, and transaction costs issues more easily than a human trader and let you capture more of the risk-free spread.

Types of arbitrage

Arbitrage models vary in difficulty just like any other model. The most basic arbitrage is a two-fold model where you buy and sell simultaneously. An example would be we observe the price of ZRX is 1.34 on one platform and 1.36 on a different platform. If you place a buy market order at 1.34 and at the same time place a sell market order of the same size at 1.36, you would make an instant profit of 0.02, once the orders have been executed. Of course, this simple example assumes each order is placed instantaneously. Since latency exists in all order systems, you might need some extra ZRX in your wallet to take advantage of this opportunity, since your sell order might execute before your buy order.

A more complicated model is known as a triangular model. This arbitrage opportunity might occur intra-exchange across different pairs, or across different platforms with say a different number of decimal places (this is especially true in traditional FX markets).

Let’s say you observe the following prices on three different exchanges:

  • EXAMPLE: BUY/AT WHAT PRICE
  • Exchange 1: ZRX/USD = 0.746
  • Exchange 2: ZRX/BTC = 0.000165
  • Exchange 3: USD/BTC = 8,200.00

Let’s say you have $100,000 and you convert it to ZRX at a rate of 0.746, giving yourself 74,600 ZRX. Now by converting your 74,600 ZRX to BTC at a rate of 0.000165, you would obtain 12.309 BTC. Finally convert your 12.309 BTC back to USD at a rate of 8,200, which gives you $100933.80. So these three back-to-back-to-back trades resulted in a risk-free profit of $933.80! Note that your total profit is a function of the size of your bank roll — this is why, especially in FX markets as an example, many trades use leverage to boost their trade size. Even when the spread is small, with leverage and continuous trading, it’s possible to generate outsized returns — but as with anything, be sure you understand the risks before trading.

Modeling arbitrage

‘Simultaneous’ arbitrage like that described above is the least risky form of arbitrage, but outside of crypto markets, it typically doesn’t exist very often or for very long. More common ‘arbitrage modeling’ centers around estimating the extent to which the price of an asset deviates from its ‘equilibrium’ price.

Mathematically, we can write the Arbitrage Pricing Theory model as:

This model views the returns of an asset as a linear combination of the regressors. Once the model computes an expected return, we would then compare it against the fair market price. For instances if the APT model determines that the price is $1.30 and the current market price is $1.20, it would indicate to take a long position as we believe that the market will return to the $1.30 level. It is important to note that the APT model does not guarantee profits like a simultaneous trade does, but it does provide high probability arbitrage opportunities if the model is correctly back tested. You can read more about this model here.

Pairs trading

There is always some correlation — the extent to which prices move in the same direction — between any two different assets. Generally, the prices for two assets in the same sector have a high correlation. For example, if there is new car regulation, then most likely the stock prices for both Tesla and Ford decline — meaning they are highly correlated. You see similar behavior when crypto regulatory news is announced — prices of many crypto assets increase or decrease in unison. Arbitrage opportunities exist when correlations diverge from the norm. Suppose two virtual currencies usually move in lock step, but after some news announcement, the price of currency A decreases by 20%, but the price of currency B decreases by only 5%. We could buy the underperforming coin (currency A) and sell the over-performing coin (currency B), under the assumption that at equilibrium, both the price of both coins will settle around a decrease of 10% on the day.

This trading methodology is called pairs trading. This is similar to the (single asset) mean reversion model that we discussed last time. In pairs trading, we assume that the two assets will revert back to their equilibrium correlation levels, as opposed to price level (as in the mean reversion model). But by taking two opposing positions — selling one asset, and buying the other — we don’t have to worry about both moving in the same direction, both increasing or decreasing in price. Instead, we only care about the relationship between them, which (often but not always) is statistically more significant than any individual price level prediction. And since we’re taking two positions instead of one, if the prices of the pair converge like they have historically, then might be able to get double the profit vs. the mean reversion case.

The risk in this model is that the pair diverges instead of converges once you take your positions, which can cause you to loose on both legs of your trade. This model also requires multiple initial parameters: you must choose your assets of interest, determine their current correlation, set an entry level when you have a correlation ‘break’, and manage a take profit and stop loss level for both of the positions that you took. Convergence and divergence is often determined by confidence intervals which can be created using standard deviations, Bollinger bands, and range trading, among others.

Here are some links that you might find useful to build a pairs trading strategy:

Trading between centralized and decentralized crypto exchanges

Arbitrage between exchanges requires precise accuracy for orders to ensure we maximize the spread we can capture by minimizing the time and cost it takes to execute the trades. We’ve built The Ocean with such strategies in mind. Our liquidity, low fees, low latency, market order system, and non-custodial execution will make it faster, safer, and cheaper for you to trade — which makes arbitrage strategies especially profitable. We’ll be announcing our liquidity partners, fee structure, and execution advantages soon.

Challenge #3 — Arbitrage Bot

In the meantime, we want to give you the tools to build a simple ‘arbitrage bot’ to trade between us and other platforms. To build such a bot, we need to:

  • Obtain APIs from both The Ocean and another exchange like Binance
  • For The Ocean, see our Getting Started Guide and API documentation
  • For Binance, try the official API or a Python wrapper
  • Create a loop to check for price mismatches
  • Execute a buy order on the ‘cheaper’ exchange and a sell order on the ‘overpriced exchange’ of the same quantity

Arbitrage opportunities often only exist for very short periods of time. If you see the opportunity, chances are others do too, and so the market tends to be self-correcting. Execution time on market orders becomes crucial to ensure that the prices you see are the prices you get — as you don’t want to accidentally lock in losses. If the market isn’t liquid, it can’t support large orders that let you maximize your profit. And if you have to wait minutes, even hours, on centralized platforms for your trades to settle, you’re exposing yourself to risk and headaches in managing your tokens. The Ocean provides lots of advantages to traders who want to take advantage of arbitrage opportunities, so we’re here to help get you started! Try the challenge below, and if you get stuck, ask us on Telegram or send us an email at hello@theocean.trade.

Challenge: Create an arbitrage model between The Ocean and Binance

Bonus: Using Binance data, create a correlation matrix between pairs of tokens to find ‘equilibrium’ levels

Answer to Challenge #3 — Arbitrage between The Ocean & Binance

Our solution is now available on GitHub. Check it out! 👍

*Since we’re not live (yet!), feel free to learn from and tweak this code to meet your current needs. This course is designed to give you the fundamental knowledge to craft your own strategies, both on and off The Ocean.

*Remember, anyone that participates on Telegram or sends us a solution anytime during the course of our Algorithmic Trading 101 series is eligible to receive part of $5000 in cryptocurrency prizes.

__________________________

🤖 Links to Lessons 🤖
The Syllabus & How to Win
Lesson 1: Time Series Analysis
Lesson 2: Data, Strategy Design, and Mean Reversion
Lesson 3: Intro to Arbitrage Strategies
Lesson 4: Portfolio Management and Machine Learning in Python
Lesson 5: More Machine Learning

Having trouble or just want to discuss your strategy? Join our Telegram to get real-time feedback and answers.

Follow us on Twitter at @TheOceanTrade or subscribe to our newsletter to stay in the loop.

--

--

The Ocean
The Ocean

The Ocean is a high performance 0x-based Ethereum ERC20 token trading platform. Sign up for launch news: www.theocean.trade