Blockchain Quantitative Investment Series Course (3) — Calender Spread Arbitrage

NO.1

In the book “Financial Alchemy” written by Soros in 1987, an important proposition was put forward: I believe the market prices are always wrong in the sense that they present a biased view of the future.

The market validity hypothesis is only a theoretical assumption. In fact, market participants are not always rational, and at each point of time, participants cannot fully acquire and objectively interpret all information. Even if it is the same information, everyone’s feedback is different.

In other words, the price itself already contains the wrong expectations of market participants, so in essence the market price is always wrong. This may be the source of profit for the arbitrageurs.

NO.2

Based on the above principles, we also know that in a non-effective futures market, the market impact of delivery contracts in different periods is not always synchronized, and the pricing is not the reason of completely effective.

Then, based on the price of the delivery contract at different times of the same transaction target, if there is a large spread between the two prices, it is possible to simultaneously trade futures contracts of different periods and carry out intertemporal arbitrage.

Like commodity futures, digital currencies also have an intertemporal arbitrage contract portfolio associated with them. For example, in the OkEX exchange: ETC week, ETC next week, ETC quarter.

For example, suppose the spread between ETC week and ETC quarter is maintained at around 5 for a long time. If the spread reaches 7, we expect the spread to return to 5 at some time in the future. Then you can sell the ETC week and buy the ETC quarter to short the spread. vice versa.

NO.3

Although this spread exists, there are often many uncertainties in manual arbitrage due to the time-consuming, poorly accurate of manual operations and price-changing effects.

Through the quantitative model to capture arbitrage opportunities and develop arbitrage trading strategies, as well as programmatic algorithms automatically release trading orders to the exchange, to quickly and accurately capture opportunities and efficiently earn income, which is the charm of quantitative arbitrage.

This article will teach you how to use FMZ quantitative trading platform and the ETC futures contract in the OkEX exchange in digital currency trading, with a simple arbitrage strategy to demonstrate how to capture the instantaneous arbitrage opportunities, and seize every visible profit while simultaneously hedging the risks that may be encountered.

NO.4

Create a digital currency intertemporal arbitrage strategy

Difficulty: ordinary level

Strategic environment:

Transaction target: Ethereum (ETC)

Spread data: ETC weekly — ETC quarterly

Trading period: 5 minutes

Position match: 1:1

Transaction type: same variety intertemporal

Strategy logic:

Buying-long spread position conditions: If the current account has no positions, and the spread is less than the down trail of boll indicator, place a spread order, which is: buying long ETC weekly, selling short ETC quarterly.

Short-selling spread position conditions: If the current account has no positions and the spread is higher than the up rail of boll indicator, place a spread order, which is: selling short ETC weekly, buying long ETC quarterly.

Closing the buying-long spread position condition: If the current account holds a long ETC weekly position and holds a short ETC quarterly position, and the spread is higher than the middle rail of boll indicator, place a close spread order, which is: selling ETC weekly, buy to cover the ETC quarterly.

Closing the Short-selling spread position condition: If the current account holds a short ETC weekly position and holds a long ETC quarterly position, and the spread is lower than the middle rail of boll indicator, place a close spread order, which is: buy to cover the ETC weekly, selling ETC quarterly.

NO.5

The above is a simple description of digital currency intertemporal arbitrage strategy logic, then how to implement your own ideas in the program? We tried to build the framework first on FMZ quantitative trading platform.

Strategy framework:

The strategy framework can be easily built up according to the strategic thinking and transaction process. The entire strategy can be simplified into three steps:

1. Pre-processing before transaction.

2. Get and calculate data.

3. Place an order and follow up.

NO.6

Next, we need to fill in the necessary details in the strategy framework based on the actual transaction process and transaction details.

First, pre-processing before transaction

Step 1: In the global environment, declare the necessary global variables.

Declare a chart object that configures the chart

Var chart = { }

Call the Chart function to initialize the chart

Var ObjChart = Chart ( chart )

Declare an empty array to store the spread sequence

Var bars = [ ]

Declare a timestamp variable that records historical data

Var oldTime = 0

Step 2: Configure external parameters for the strategy.

Step 3: Define the data processing function

Basic data function: Data ( )

Create a constructor Data and define its internal properties, including: account data, position data, K-line data timestamp, the latest buying/selling price of arbitrage contract A/B, positive/reverse arbitrage spread

Get the position function: mp ( )

Check through the entire array of positions, return the specified contract and the number of positions in the specified direction. Return false if there is none.

K line and indicator function: boll ( )

Synthesize a new K-line sequence based on the data of positive/reverse arbitrage spread. And return the up/middle/down rail data calculated by the boll indicator.

Order function: trade ( )

Insert the order contract name and trade type, then place the order in the latest buying/selling price and return the result after placing the order. Since it is necessary to simultaneously order two different directions of orders, the latest buying/selling price are converted within the function according to the order contract name.

Cancel order function: cancelOrders ( )

Get array of all pending orders and cancel them one by one. And if there is pending order, return false, and if there is none, return true.

Process holding single contract: isEven ( )

In the case of single-leg situation in the arbitrage trading, it is directly handled by simply closing all the positions. Of course, you can also change into chasing price.

Drawing chart function: drawingChart ( )

Call ObjChart.add ( ) method to draw the necessary market data and indicator data in the chart: up, middle, down rail, positive/reverse arbitrage spread.

Step 4: In the entry function main ( ), execute the pre-processing code before transaction, which only run once after the program starts, including:

filter the information that is not very important in the console SetErrorFilter ( )

Set the digital currency type to be traded http://exchange.IO ( )

Empty the drawn charts before the program starts ObjChart.reset ( )

Empty the status bar information before the program starts LogProfitReset ( )

NO.7

After defining the above pre-processing before transaction, it is necessary to proceed to the next step, enter the polling mode, and repeat the onTick ( ) function.

And set the sleep time when polling, because some of the digital currency exchange’s API has a built-in access limit for a certain period of time.

Second, get and calculate data

Step 1: Get the underlying data object, account balance and boll indicator data for trading logic.

Third, place an order and follow-up

Step 1: Perform the buying and selling operation according to the above strategy logic. First, see if the price and indicator conditions are true, and then see if the position conditions are true, and finally execute the trade ( ) order function.

Step 2: After placing order, it is necessary to deal with abnormal situations such as pending orders and holding single contract, and draw charts.

NO.8

Above, we have created a simple digital currency intertemporal arbitrage strategy through more than 200 lines. The complete code is as follows:

NO.9

This strategy just serves as a trigger. The real market is not that simple, but you can use this example to play with your imagination.

What needs to be reminded is that, based on my limited experience, the pure period arbitrage strategy is basically not worth running in the current digital currency market situation, whether it is risk-free triangle arbitrage or cross-market arbitrage.

The reason is that no matter in which digital currency exchange’s futures market, the margin is not fiat. Almost all digital currencies have fallen by about 70% since the beginning of the year. In other words, the strategy is always making currency, but the price of the currency is falling.

Take a broad view, the digital currency market has already left the blockchain. Just like the tulips then, the price always comes from people’s expectations and confidence, and the confidence comes from the price…

visit this to get complete code: https://www.fmz.com/bbs-topic/2281