Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a cross-cycle arbitrage strategy based on EG cointegration method with Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to use Python to implement a cross-cycle arbitrage strategy based on EG cointegration method? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Some background in finance and mathematics

As we all know, the main premise of the carry trade is the mean return of the underlying prices of two trades with the same attributes, which is a carry trade that earns such a mean return. The classical regression model is based on stationary data variables, for non-stationary variables, we can not use the classical regression model, otherwise there will be many problems such as false regression.

As the price correlation of many related transactions is non-stationary, this brings great limitations to the classical regression analysis methods. In practical application, most of the time series are actually non-stationary. The difference method is usually used to eliminate the non-stationary trend in the series, so as to establish a model after the series is stabilized, such as using the ARIMA model. However, the transformed series limits the range of the underlying price of the transaction, and sometimes the transformed series does not have direct analytical significance, so the time series model established after it is transformed into a stationary series is not easy to explain.

The cointegration theory and its method proposed by Engle and Granger in 1987 provide another way for the modeling of non-stationary series. Although some economic variables are non-stationary series themselves, their linear combinations may be stationary series. This stationary linear combination is called cointegration equation and can be interpreted as a long-term stable equilibrium relationship between variables.

For example, consumption and income are non-stationary time series, but have cointegration relationship. If they do not, then long-term consumption may be higher or lower than income, so consumers will irrationally spend or accumulate savings.

Assuming that some economic indicators are linked by an economic system, these variables should have an equilibrium relationship in the long run, which is the basic starting point for establishing and testing the model. In the short term, these variables may deviate from the mean due to seasonal effects or random disturbances. If the deviation is temporary, it will return to equilibrium over time; if the deviation is persistent, it cannot be said that there is an equilibrium relationship between these variables. Cointegration (co-integration) can be regarded as a statistical representation of the nature of this equilibrium relationship.

The concept of cointegration is a powerful concept. Because cointegration allows us to describe the balanced or stationary relationship between two or more sequences. For each series alone, it may be non-stationary, and the moments of these series, such as mean, variance or covariance, vary with time, while the linear combination series of these time series may have the property of not changing with time.

Apply the above concepts to futures trading

In the arbitrage trade of commodities, intertemporal contracts of the same variety have a strong cointegration relationship (which also includes spot and futures arbitrage). It is best to use this cointegration theory and method proposed by Engle and Granger for quantitative analysis, which is also the theoretical cornerstone of many top quantitative teams in the world when carrying out intertemporal arbitrage with commodities.

In this paper, we will use this theory to practice on rebar futures. In terms of code implementation, we use Python, the most powerful language in quantitative finance, to code.

Before we start coding, we need to know about a Python library that is very popular in data science around the world, called statsmodels.

Statsmodels (http://www.statsmodels.org) is a Python library for fitting a variety of statistical models, performing statistical tests and data exploration and visualization. It is an indispensable weapon in econometrics and data science research. It contains many classical frequency school statistical methods, and the hot Bayesian methods and machine learning models in the field of artificial intelligence can also be found in it.

For example:

Linear model, generalized linear model and robust linear model

Linear mixed effect model

Analysis of variance (ANOVA) method

Time series process and state space model

Generalized method of moment

With this library, coupled with the famous scientific computing NumPy library, we can use Python to encode the basic framework of this theory for quantitative analysis, and at the same time realize automated transactions on the inventor quantitative platform.

Note:

About the installation method of this library and the installation method of NumPy library, see the installation and deployment of the custodian in the inventor's quantitative website library. Neither of these libraries is the official library of Python, and readers need to install them on the local computer or cloud computing server deployed by the custodian themselves. Readers who do not understand can go to the inventor's quantitative website to search for the method. It is very simple to use pip to install after the deployment of the custodian. I won't repeat it here.

Policy logic

This strategy is based on the EG two-step method.

The first step: sequence same order simple integration

Step 2: OLS residual is stable

After judging that the sequence has a cointegration relationship according to the above two steps (if there is no cointegration relationship, the full flat position will not be operated)

By calculating 0.9 standard deviations of the regression residuals of the two real price series, and shorting the price difference when the price difference breaks through the upper track, making multiple price differences when the price difference breaks through the lower track and closing the position when it returns to the level of standard deviation.

Code implementation with Python

As a first step, we need to create a function for cointegration checking. Knowledge about the statsmodels library is needed in the following code.

# create a function to check def cointegration_check (series01, series02): urt_rb2001 = ts.adfuller (np.array (series01), 1) [1] urt_rb2005 = ts.adfuller (np.array (series01), 1) [1] # if it is stationary or unstable at the same time, the difference test if (urt_rb2001 > 0.1 and urt_rb2005 > 0.1) or (urt_rb2001)

< 0.1 and urt_rb2005 < 0.1): urt_diff_rb2001 = ts.adfuller(np.diff(np.array(series01)), 1)[1] urt_diff_rb2005 = ts.adfuller(np.diff(np.array(series01), 1))[1] # 同时差分平稳进行OLS回归的残差平稳检验 if urt_diff_rb2001 < 0.1 and urt_diff_rb2005 < 0.1: matrix = np.vstack([series02, np.ones(len(series02))]).T beta, c = np.linalg.lstsq(matrix, series01)[0] resid = series01 - beta * series02 - c if ts.adfuller(np.array(resid), 1)[1] >

0.1: result = 0.0 else: result = 1.0 return beta, c, resid, result else: result = 0.0 return 0.0,0.0,0.0, result else: result = 0.0 return 0.0,0.0,0.0, result

In the second step, we need to calculate the relevant data of cointegration, mainly in order to get the upper and lower orbit values of the standard deviation of the residual, as follows:

# Computing cointegration def cointegration_calc (): # using the inventor quantization transaction class library obj = ext.NewPositionManager () # to show the results of the cointegration test of two price sequences beta, c, resid, result = cointegration_check (close_01, close_02) # if the result of the failed cointegration test is returned, the full flat position is waiting for if not result: print ('the cointegration test fails. Full leveling of all positions') obj.CoverAll () return # calculate the standard deviation of the upper and lower rails mean = np.mean (resid) up = mean + 0.9 * np.std (resid) down = mean-0.9 * np.std (resid) # calculate the new residual resid_new = close_01 [- 1]-beta * close_02 [- 1]-c

It should be noted here that we need to use the domestic commodity futures template of the inventor quantification platform. The address of the template is: https://www.fmz.com/strategy/24288. When you code the inventor quantification strategy page, you need to copy this template to your own policy base first, and then check it during the back test. Here, please pay attention.

For information on how to deploy custodians and robots, please refer to my previous article: https://www.fmz.com/bbs-topic/4140

Readers who want to buy their own cloud computing server deployment trustees can refer to this article: https://www.fmz.com/bbs-topic/2848

Complete policy code:

Import typesimport numpy as npimport statsmodels.tsa.stattools as ts# We first create a function for cointegration test def cointegration_check (series01, series02): urt_rb2001 = ts.adfuller (np.array (series01), 1) [1] urt_rb2005 = ts.adfuller (np.array (series01), 1) [1] # if it is stationary or unstable at the same time, the difference test if (urt_rb2001 > 0.1and urt_rb2005 > 0.1) or (urt_rb2001)

< 0.1 and urt_rb2005 < 0.1): urt_diff_rb2001 = ts.adfuller(np.diff(np.array(series01)), 1)[1] urt_diff_rb2005 = ts.adfuller(np.diff(np.array(series01), 1))[1] # 同时差分平稳进行OLS回归的残差平稳检验 if urt_diff_rb2001 < 0.1 and urt_diff_rb2005 < 0.1: matrix = np.vstack([series02, np.ones(len(series02))]).T beta, c = np.linalg.lstsq(matrix, series01)[0] resid = series01 - beta * series02 - c if ts.adfuller(np.array(resid), 1)[1] >

0.1: result = 0.0 else: result = 1.0 return beta, c, resid, result else: result = 0.0 return 0.0,0.0,0.0, result else: result = 0.0 return 0.0,0.0,0.0 Result# initialization contract data def init (): # subscribe to rebar 2001 contract and 2005 contract And get all the closing prices of the current cycle of the inventor quantification platform exchange.SetContractType ("rb2001") records = exchange.GetRecords () close_01 = records.Close exchange.SetContractType ("rb2005") records = exchange.GetRecords () close_02 = records.Close# calculation cointegration def cointegration_calc (): # use the inventor quantitative transaction class library obj = ext.NewPositionManager () # to show two The result of cointegration test of price series beta C, resid, result = cointegration_check (close_01, close_02) # if the result of failed cointegration test is returned, the position will be fully closed waiting for if not result: print ('cointegration test failed. Full leveling of all positions') obj.CoverAll () return # calculate the standard deviation of the upper and lower rails mean = np.mean (resid) up = mean + 0.9 * np.std (resid) down = mean-0.9 * np.std (resid) # calculate the new residual resid_new = close_01 [- 1]-beta * close_02 [- 1]-cdef trade (): obj = ext.NewPositionManager () # use inventor quantitative trading class library # here to obtain position information positions = exchange.GetPosition () # get position array if len (positions) = = 0: # if the length of the position array is 0 return 0 # it is proved to be short Return 0 for i in range (len (positions)): # iterate the position array if (positions [I] ['Type'] = = PD_LONG) or (positions [I] [' Type'] = = PD_LONG_YD): position_01_long = 1 # Mark position_01_long as 1 elif (positions [I] ['Type'] = PD_SHORT) or (positions [I] [' Type'] = PD _ SHORT_YD): position_01_short =-1 # Mark position_01_short as-1 if not position_01_long = 1 and not position_01_short =-1: # New residual if resid_new > up: obj.OpenLong ("rb2001") 1) # Buy obj.OpenShort ("rb2005", 1) # sell it # do a lot of new residual if resid_new when you go down the rails

< down: obj.OpenLong("rb2005", 1) # 买开 obj.OpenShort("rb2001", 1) # 卖开 # 新残差回归时平仓 elif position_01_short = -1: if resid_new = down: obj.CoverAll() print('价格回归,平所有仓位') # 突破上轨反向开仓 if resid_new >

Up: obj.OpenLong ("rb2005", 1) # Buy obj.OpenShort ("rb2001", 1) # sell def main (): while True: trade () Sleep (1000) this is the answer to the question on how to use Python to implement a cross-cycle arbitrage strategy based on EG cointegration. I hope the above content can be of some help to you. If you still have a lot of questions to solve, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report