A Guide to Trading Systems

Lesson -> Taking a Live Example - 1

13.1 – Pair data Track

We are now done with the theory background required to trade pair options. This is the moment most of you have waited for.

This chapter will discuss the factors that affect pair trading and give an example of live trading.

Here's a quick overview of pre-trade theory.

  1. A basic overview of linear regression. How to do it.
  2. Linear regression is when you compare an independent variable X with a dependent variable.
  3. Linear regression produces the intercept, slope and residuals as well as the standard error and intercept.
  4. The error ratio is a key factor in deciding whether to classify a stock dependent (Y), or independent (X).
  5. The error ratio is the ratio between standard error of intercept and standard error
  6. The error ratio is calculated by combining both X AND Y. The stock with the lowest error ratio is what will be assigned X stock and which stock as Y.
  7. Regression residuals should be stationary. If they are stationary, we can conclude that both stocks are co-integrated
  8. If the stocks are integrated, they can move together
  9. An ADF test can be used to determine the stationarity of a series.
  10. A pair of ideal partners should have an ADF value less than 0.05

Each point has been covered in detail over the past few chapters. These points will help you understand which pairs are worth trading. Take two stocks from the same industry, do a linear regression on them and check the error ratio to identify the stock. To determine the residual, we perform an ADF test. A pair cannot be considered worth trading if its ADF falls below 0.05. We track residuals daily to identify trading opportunities for eligible pairs.

When - is available, a pair trade opportunity will arise.

  1. The residuals reached -2 standard deviations (-2SD). This signal is long on the pair so we sell X and buy Y
  2. The residuals reached +2 standard deviation (+2SD). This signal is short on the pair. We sell Y to buy X

As such, I prefer to initiate trades when residuals are at 2.5 SD or less. The stop loss for long trades is -3 SD, and +3SD if they are short. The target is -1 SD or +1 SD respectively. To plan your trades and track the residual value once you have initiated a pair trade, this means that you must also monitor it after you have initiated it. This is something we'll discuss later in the chapter.

13.2 -Guide to Programmers

Chapter 11 introduced the 'Pair Data’ sheet. This is the output of the Pair trading algorithm. This sheet is basically the output of the Pair Trading Algo.

  1. Downloads the closing prices for the underlying in the last 200 days. This can be done using NSE's Bhavcopy. You can also automate it by running a script.
  2. Already completed is the stock list and sector classification. The download is therefore more organized
  3. It runs a series regressions and calculates each regression's "error ratio". If we're talking about RBL Bank or Kotak Bank then the regression module would regress RBL(X) and Kotak (“Y”) and RBL(Y). The combination with the lowest error rate is taken into consideration and the others are ignored
  4. The residuals are subject to the adf test, which is used for the combination with the lowest error rate.
  5.  A report (pair-data) is generated. This includes all viable XY combinations along with their respective intercepts. Beta and adf value. The standard error and the sigma are also mentioned. However, we will soon be discussing sigma.  

This guideline is for programmers who are interested in developing their own pair trading algorithm.

I briefly discussed how to read the Pair data in Chapter 11, but now it is time to get into the details. This is the Pair Data Excel Sheet -

Take a look at the highlighted data. Bajaj Auto is the Y stock and TVS is the X stock. This combination can be found in the report. It implies that Bajaj Auto as Y and TVS As X have a lower standard error rate. TVS and Bajaj are not viable pairs owing to a higher error ratio. Therefore, you won't find this combination (Bajaj Auto as X, TVS, Y) in the report.

The report will identify which one is X or Y and give you the following information:

  1. Intercept - 1172.72
  2. Beta - 2.804
  3. ADF value 0.012
  4. Std_err: -0.77
  5. Sigma - 103.94

Assuming (and hoping) that you are familiar with the first three variables, i.e. intercept, beta, and ADF value, I won't go into detail. Let me quickly address the second and third variables.

The Standard Error, or Std_err as it is known in the report, is basically a ratio of Today’s residual to the standard error. This can be confusing because we are referring to two standard errors. The standard error of residual 2 is reported in the regression output. Let me give you an example.

Take a look at this snapshot -

Here is the summary of regression output for Yes Bank versus South Indian Bank. I've highlighted standard error (22.776). This is the standard error for residuals. This is the standard error of residuals.

The second highlight is 20.914 which is the residual.

The report's std_err simply refers to a ratio of -

Today's residual/Standard Error of residual

= 20.92404/22.776

= 0.91822

Yes, I do agree that calling this number "std_err" is not the best option, but please be patient

This number provides me with information about the position of today's residual in relation to the standard distribution. This number is the trigger for the trade. If this number is lower than -2.5, a long position can be initiated with a stop loss of -3.0. If this number is +2.5 or greater, with a stop loss of +3.0, a short position can be initiated. If you are long, your target is at -1, and if you are short, it is at +1.

Also, this means that the daily std_err value must be calculated and tracked in order to identify trading opportunities. We'll get to this later.

In the pair data report, the sigma value is the standard error of residual. The above example is 22.776.

Now, if you carefully read the pair data sheet, then you should be able understand all the details.

Okay, let's get to the trade!

13.3 - Consider a Live example

To find opportunities in pair trading, I've been using the algo and found one on 10 May 2018. This is the snapshot of the pair data. You can download it towards the end. Remember that this pair trading algorithm was created using the closing prices for 10 May.
(IMAGe 3).

Take a look at the red data. This is Tata Motors Ltd (dependent) and Tata Motors VVR (independent).

ADF value is 0.0179, which is less than the threshold of 0.0.05. It's an excellent value.Remember, an ADF value less than 0.05 means that the residual remains stationary. This is precisely what we want.

The std_err reading is -2.54, meaning that the residuals are close to the mean but have diverged sufficiently from the mean. Therefore, one could consider setting up a long trade. This trade is very long so you must first buy Tata Motors (the dependent stock) and then short Tata Motors DVR, the independent stock. Trade was scheduled to occur on Friday, May 11th Morning. The trade was canceled due to some reasons. Although the rate was slightly higher than the original, this was still a showcase deal and was not intended to chase the P&L.

These are the details of trade execution -
(IMAGE 4).

There may be two questions you have at this point. I will list them for your convenience.

Question Was the trade executed without looking at prices? So I didn’t even consider the stock’s price, nor did I look at support, resistance and RSI. It is not necessary.

Answer - None of this is necessary. Only what matters is the trading location of residuals, which is precisely what I was looking for.

Question- Why did I decide to trade one lot each?Why can't one trade 3lots of TMD & 2 lots of TM?

Answer This depends on the stock's beta. To ensure that we are neutral in this situation, we will use the beta to identify the stocks of X and Y. Beta neutrality means that we must have beta*X stocks of X for every stock of Y. The beta for Tata Motors (Y), Tata Motors DVR(X) is 1.59. This means that for every Tata Motors (Y) stock, I must have 1.59 Tata Motors DVRs (X).

This means that Tata Motors (Y), 1500 is the lot size, and we will need to purchase 1500*1.59 shares or 2385 Tata Motors DVRs (X). Since the lot size of Tata Motors (Y) is 2400, it's close to 2385, I decided to buy 1 lot each. However, I am aware that this trade is somewhat more long-term because I am buying an additional 115.

Please note that this constraint means that we can't trade pairs if beta is -ve.

Remember that I initiated the trade when the residual value was only -2.54. I wanted to keep the position open while waiting for either the stop loss (-3 or -1 on residual) or the target (-1.until then it was a waiting game.

A basic excel tracker was created to track the position of each individual. These accessories are great for programmers, but I have limited programming skills so I created a basic excel position tracker. This is the screenshot. You can download the sheet by clicking the link below.


The position tracker provides all information about the pair. This sheet is fairly simple to comprehend, I think. It's designed so that you can enter the current values of X & Y and the P&L.

Once the position has been taken, one only needs to track the residual's z-score. You will need to continue tracking the values as well as the respective z scores. This is exactly what I did. Faisal, my colleague, recorded all values except for the 14 th, and 15 _th. These are the logs.
(IMAGE 6).

As you can see the current values were kept track and the z-score calculated multiple times per day. This is very common in pair trading. The position was open for almost 7 trading sessions. Positions have been open for as long as 22 to 25 trading sessions, and I've seen them. Here's the thing: as long as you have the math right, you don't need to wait for the target (or SL) to trigger.

On 23 rd Mai morning, the Z-score fell to the target level, and there was an opportunity to close the trade. Here's a snapshot
(IMAGE 7).

You can see that the Tata Motors DVR gains are much greater than the losses. We will not know which position will give us the most money until we trade. However, the idea is that one will be in our favor while the other may not. However, it's impossible to predict which one will be the breadwinner.

This was the position tracker for day 23 rd Mai.
(IMAGE 8).

The P&L was approximately Rs.14,000/–, which is not bad for a low-risk trade.

13.4 - Last words on pair trading

Okay guys, I have covered everything I know about pairing trading over the past 13 chapters. This is my personal opinion, and I think it's a more exciting way to trade than blind speculative. Pair trading is less risky than speculative trading, but it comes with its own risks. You need to be aware. It is possible for the pair to continue to diverge even after you have initiated the trade, which can lead to a large loss. Because you have two contracts, your margin requirements may be slightly higher. To accommodate M2M daily, you will need some buffer money.

You may need to also take a spot market position in certain situations. On 23 rd Mai, there was a signal for shorting Allahabad Bank (Y), and longing Union Bank (X). This pair has a z-score of 2.64 and a beta of 0.437.

Beta neutrality dictates that for every 1 share in Allahabad Bank (Y), there are 0.437 shares in Union Bank (X). Since Allahabad Bank's lot size is 10,000, I would need to purchase 4378 shares from Union Bank. Union Bank's lot size is 4000 so I needed to purchase 370 shares on the spot market.
(IMAGE 9).

It's my hope that I can trade .

I am sure that most of you would like the pair data sheet. We are currently working to make this sheet available on a daily basis, so you can track the pairs. You can build the algo yourself, but I suggest that you wait. Please post any concerns below.

You don't have any other option than to learn programming from someone and convince them that there are opportunities to make money. This is exactly what I did.

Let me end with a thought.

  1. To determine if Stock A and Stock B are cointegrated, we run a linear regression with them to see if their residuals are stationary.
  2. What if Stock A and Stock B are not stationary? Instead, Stock A and stock B & C together become stationary.

Trading is more than just trading. There's a multivariate regression. This is not a difficult concept to grasp, but I can tell you that it is worth the effort.


  1. The residual's current value is the trigger for a trade to make a pair.
  2. To determine the stock needed in X or Y, check for beta neutrality.
  3. Negative beta may mean that it is not possible to trade the pair.
  4. Check the z-score movement once the trade has been initiated to see the current position.
  5. It doesn't really matter what the price of futures is, the focus is on the z score