Turning data into insights and building strategy using Python

11 min read

This article will help you learn how to analyse data to acquire insights via Live stock market data and datasets. It will also help you to learn how you can create your own trading strategy using Python programming.

The complete data files and python code used in this project are also available in a downloadable format at the end of the article.

This article is the final project submitted by the author as a part of his coursework in the Executive Programme in Algorithmic Trading (EPAT®) at QuantInsti®. Do check our Projects page and have a look at what our students are building.


About the Author

Lokesh Kumar is a risk manager at one of the largest US Bank located in Mumbai. He has over 6 years of experience in the banking and financial sector, having worked in the field of regulatory models, credit decision models, macroeconomic research and analytics.

Lokesh holds BA Economics Honors from the University of Delhi and an MSc in Financial Economics from Madras School of Economics. Additionally, he holds a certification in risk management (FRM – US GARP). He is also an EPAT alumnus.


Motivation for the study

I am an economics student and risk manager. I have been doing data analysis, statistical, econometric, machine learning modelling using various statistical software (R, SAS, STATA, EVIEWS) for the last 6 years but never got a chance to work on Indian equity data.

This project proved to be a stepping stone to follow my interest and make a career in the Indian equity market. In addition, I have been tracking the Indian financial market and news across the globe for 5 years.


Data

  • The data for the analysis includes these variables.
  • 2 (liquid, volatile, positive annual return) stocks from each sector.
  • The frequency of data is daily.

Models

Momentum, Mean Reversion and Pair Trading: Analysis – Excess Return, Sharpe Ratio, Maximum drawdown, drawdown duration, In-sample and out-of-sample testing, Absolute return, relative return, profitability analysis.


Backtest models

Evaluating strategy performance


Project Abstract

This project examines stock prices behaviour and movement. We analyzed 20 stocks from an exhaustive list of stocks from Nifty sectoral indices for the period October 2010 to December 2018; considering 2 stocks from each sector based on high annualized average rolling return and volatility.

We explored the stock prices and volume data and built 3 strategies – Volume and Price based trading, Mean Reversion and Trend following- RSI. We found that there is no one particular strategy which works for all the stocks.

We evaluated the strategy performance and found a high return and hit ratio. We also did an out of sample test for the small sample period January 2019 to March 2019 to observe if we could have used these strategies in the live market then how much profit we could have generated.


Introduction

The objective is to determine the entry & exit point, long & short the trade with high confidence using a different modelling approach. We focused on building strategies which generate high returns and are easily implementable.

First, we explored the volume and price relationship as there are many technical indicators available based on volume and price. An uptrend is strong and healthy if volume increases as price increases, and decreases when the market moves into a counter-trend. If you wish to learn more about using technical indicators for trading strategies, you must enrol into our course on Technical Indicators Python.

Second, we explored the cross-sectional mean reversion strategy which attempts to exploit temporary mispricing between two securities from the same sector.

Third, we explored the trend following strategy where you follow the trend i.e. long when the price is going up and short when the price is going down. We computed the relative strength index (RSI) which evaluates overbought and oversold conditions in stock prices.


Data Mining

We used 2 stocks from each sector of India’s equity market.  We first looked at Nifty sectoral indices and the list of stocks included in each index. The indices comprise the most liquid, large capitalized stocks which reflect the behaviour and performance of each sector.

We analyzed a total of 126 stocks based on average annualized rolling return and rolling volatility using the 20 days rolling window.

We finalized the 2 stocks from each sector based on high volatility, high and positive return.

We extracted the stock prices and volume data from yahoo finance for the period from January 2010 to December 2018.

The final dataset consists of 20 stocks for the period from October 2010 to December 2018. The data was available from October 2010 for all 20 stocks.

The list of stocks used for the analysis:

Sr. No.

Symbol

Company Name

Industry

1

APOLLOTYRE

Apollo Tyres Ltd.

AUTOMOBILE

2

ASHOKLEY

Ashok Leyland Ltd.

AUTOMOBILE

3

PRESTIGE

Prestige Estates Projects Ltd.

CONSTRUCTION

4

OBEROIRLTY

Oberoi Realty Ltd.

CONSTRUCTION

5

JUBLFOOD

Jubilant Foodworks Ltd.

CONSUMER GOODS

6

UBL

United Breweries Ltd.

CONSUMER GOODS

7

PFC

Power Finance Corporation Ltd.

FINANCIAL SERVICES

8

RECLTD

REC Ltd.

FINANCIAL SERVICES

9

NIITTECH

NIIT Technologies Ltd.

IT

10

MINDTREE

MindTree Ltd.

IT

11

TVTODAY

TV Today Network Ltd.

MEDIA & ENTERTAINMENT

12

INOXLEISUR

Inox Leisure Ltd.

MEDIA & ENTERTAINMENT

13

WELCORP

Welspun Corp Ltd.

METALS

14

HINDALCO

Hindalco Industries Ltd.

METALS

15

AUROPHARMA

Aurobindo Pharma Ltd.

PHARMA

16

BIOCON

Biocon Ltd.

PHARMA

17

INDIANB

Indian Bank

PUBLIC BANK

18

BANKBARODA

Bank of Baroda

PUBLIC BANK

19

FEDERALBNK

Federal Bank Ltd.

PRIVATE BANK

20

AXISBANK

Axis Bank Ltd.

PRIVATE BANK


Data Analysis

We computed the descriptive statistics of stocks returns, hit ratio and cumulative profit/loss.

Following is the technical description of each strategy used in the project:

In Volume and price-based trading strategy, we generated a short signal if the current volume is less than the last 3 days of moving average volume and the current price is less than the last 3 days of moving average price. We tried different short time frames such as 3,4,5,14 but 3 days was giving best results and also tried to generate long signal but that was not giving positive results in any of the stocks so we went ahead with short sell trade only.

In Mean-reversion pair trading strategy, we considered 2 similar stocks from each sector, computed the price ratio and checked if the price ratio (spread or PS1/PS2) is cointegrated then the stocks are mean-reverting and can apply the pair-trading strategy.

We found many of the price ratios were not cointegrated. We checked cointegration using the Augmented Dicky Fuller (ADF) test. We created the signals – long entry in S1 when the spread goes below the lower band and long exit in S1 when the spread goes above the moving average, a short entry in S1 when the spread goes above the upper band and short exit in S1 when the spread goes below the moving average, and take the reverse position in S2.

We tried different time frames such as 5, 14, 21 days to compute rolling return and volatility but 5 days was giving best results and also tried +/- 1 or +/- 2 standard deviations to compute upper and lower band, and finalized +/- 1 standard deviation based on the results.

In Trend-following RSI strategy, the value of 70 or above indicates that the security is overbought so short the stock and value of 30 or below indicate that the security is oversold so long the stock. We use the 21 days period to compute RSI. However, we explored different time frames such as 5, 14, 21,30, 60 days but 21 days was giving the best results.


Key Findings

We provide the summary statistics of 20 stocks. Negative skewness indicates that the distribution of returns is negatively biased.

Excess Kurtosis (Kurtosis greater than 3) indicates the distribution of returns has fatter tails (higher extreme outcomes).

Symbol

Mean

Std. Deviation

Minimum

Maximum

Percentile 25th

Percentile 50th

Percentile 75th

Skewness

Kurtosis

APOLLOTYRE

0.06

2.51

-29.37

10.16

-1.34

0.06

1.43

-0.96

11.07

ASHOKLEY

0.05

2.39

-15.09

12.81

-1.30

0.00

1.34

0.02

2.92

PRESTIGE

0.01

2.82

-13.35

16.62

-1.69

-0.12

1.50

0.43

2.12

OBEROIRLTY

0.02

2.35

-11.70

16.06

-1.32

-0.10

1.13

0.72

3.83

JUBLFOOD

0.08

2.49

-11.91

15.03

-1.25

0.00

1.34

0.16

3.12

UBL

0.06

2.53

-12.67

17.18

-1.16

-0.02

1.08

0.78

6.61

PFC

-0.03

2.72

-15.52

13.40

-1.62

-0.08

1.57

0.07

1.66

RECLTD

-0.02

2.59

-13.55

12.99

-1.47

-0.07

1.50

-0.04

1.57

NIITTECH

0.08

2.36

-12.60

13.91

-1.26

-0.04

1.26

0.35

3.05

MINDTREE

0.09

2.04

-18.36

9.42

-0.99

0.03

1.09

-0.13

6.15

TVTODAY

0.08

3.04

-18.15

18.23

-1.44

-0.19

1.30

1.16

7.45

INOXLEISUR

0.06

2.64

-15.21

16.90

-1.38

-0.09

1.26

0.74

4.70

WELCORP

-0.03

3.31

-31.45

18.23

-1.76

-0.19

1.64

-0.24

8.19

HINDALCO

0.00

2.52

-10.17

12.68

-1.57

-0.06

1.52

0.13

1.25

AUROPHARMA

0.09

2.51

-18.44

12.44

-1.31

0.00

1.48

-0.03

3.13

BIOCON

0.07

2.04

-12.46

14.47

-1.02

0.01

1.08

0.56

5.12

INDIANB

-0.01

2.74

-13.11

19.12

-1.56

-0.11

1.27

0.93

5.66

BANKBARODA

-0.02

2.53

-17.89

27.33

-1.38

-0.03

1.28

0.85

12.06

FEDERALBNK

0.03

2.19

-13.04

17.47

-1.21

-0.04

1.18

0.30

4.78

AXISBANK

0.04

2.13

-9.96

14.60

-1.15

-0.02

1.19

0.20

3.03

Volume/Price Based Short Sell Strategy

We deploy this strategy on all 20 stocks. The data shows that this strategy leads to a 55% hit ratio and a 9.7% annualized return on an average. Cumulative PnL shows that the amount invested (INR 1) in October 2010 turned out to be (INR 2.291) in December 2018 on an average.

Symbol

# Positive Trades

# Total Trades

Hit Ratio

Cumulative PnL

Avg. Annualized Return

APOLLOTYRE

324

605

54%

2.294

10.9%

ASHOKLEY

328

576

57%

3.089

15.1%

PRESTIGE

421

695

61%

4.703

21.4%

OBEROIRLTY

405

683

59%

4.251

19.8%

JUBLFOOD

340

601

57%

2.325

11.1%

UBL

381

669

57%

2.712

13.3%

PFC

292

562

52%

1.486

5.1%

RECLTD

303

605

50%

1.225

2.6%

NIITTECH

346

669

52%

1.052

0.6%

MINDTREE

343

617

56%

2.105

9.8%

TVTODAY

436

751

58%

2.446

11.8%

INOXLEISUR

384

749

51%

1.002

0.0%

WELCORP

416

691

60%

4.271

19.9%

HINDALCO

291

573

51%

1.402

4.3%

AUROPHARMA

295

563

52%

2.089

9.6%

BIOCON

329

622

53%

1.886

8.3%

INDIANB

372

639

58%

2.750

13.5%

BANKBARODA

313

601

52%

1.455

4.8%

FEDERALBNK

324

595

54%

1.876

8.2%

AXISBANK

296

553

54%

1.402

4.3%

Mean Reversion Pair Trading Strategy

We found only 2 pair of stocks cointegrated in our analysis. The p-value is less than a 10% significance level for PFC & RECLLTD and less than 1% significance level for TVTODAY & INOXLEISUR.

The data shows that this strategy leads to a 52% hit ratio and 13.7% annualized return on an average. Cumulative PnL shows that the amount invested (INR 2) in October 2010 became (INR 5.677) in December 2018 on an average.

Symbol

# Positive Trades

# Total Trades

Hit Ratio

Cumulative PnL

Avg. Annualized Return

PFC

1475

2896

51%

4.601

11.0%

RECLTD

TVTODAY

1521

2879

53%

6.753

16.4%

INOXLEISUR

Trend Following RSI Strategy

We developed this strategy for NIITTECH because the volume and price based short selling strategy failed to generate good returns and the IT pair was not cointegrated.

The data shows that this strategy leads to a 69% hit ratio and 6.5% annualized return on an average. Cumulative PnL shows that the amount invested (INR 1) in October 2010 turned out to be (INR 1.652) in December 2018 on an average.

Symbol

# Positive Trades

# Total Trades

Hit Ratio

Cumulative PnL

Avg. Annualized Return

NIITTECH

46

67

69%

1.652

6.5%

Out of Sample Backtest

We have optimized the strategy based on historical data from October 2010- December 2018 period.

We run an out of sample backtest and see how the strategy performs if we could have used those strategies in the market to generate profits. The out of sample backtest period used in the analysis is January 2019 to March 2019.

Volume/Price Based Short Sell Strategy

According to historical data results, we test this strategy on those stocks which have generated more than 10% return in the past and observed how this strategy could have performed if we have invested our money in the market for the period January 2019 to March 2019.

The data shows that this strategy leads to a 55% hit ratio and 25.3% annualized return on an average.

Symbol

# Positive Trades

# Total Trades

Hit Ratio

Cumulative PnL

Avg. Annualized Return

APOLLOTYRE

12

19

63%

1.002

0.8%

ASHOKLEY

10

17

59%

1.025

10.8%

PRESTIGE

16

24

67%

1.173

93.1%

OBEROIRLTY

7

16

44%

0.960

-15.4%

JUBLFOOD

6

13

46%

0.996

-1.8%

UBL

6

16

38%

0.946

-20.5%

TVTODAY

16

22

73%

1.156

82.3%

WELCORP

17

27

63%

1.168

90.0%

INDIANB

7

15

47%

0.971

-11.3%

Mean Reversion Pair Trading Strategy

The data shows that this strategy leads to a 45% hit ratio and 5.4% annualized return on an average.

Symbol

# Positive Trades

# Total Trades

Hit Ratio

Cumulative PnL

Avg. Annualized Return

PFC

40

83

48%

2.202

48.8%

RECLTD

TVTODAY

40

95

42%

1.781

-38.1%

INOXLEISUR

Trend Following RSI Strategy

The data shows that this strategy leads to a 50% hit ratio and 4.5% annualized return on an average.

Symbol

# Positive Trades

# Total Trades

Hit Ratio

Cumulative PnL

Avg. Annualized Return

NIITTECH

2

4

50%

1.011

4.5%


Limitations

We haven’t included transaction costs in order to compute return.


Implementation Methodology

This is a completely live project and can be easily implementable in the trading system.

Volume and Price based Short Selling Strategy

If current day volume goes below last 3 days of average volume and current day closing price goes below last 3 days of average closing price, buy the stock at the open price and sell the stock at the close price on the next day.

Mean Reversion Pair Trading Strategy

If current-day spread (price ratio of stock1/stock2) goes below lower band (5 days moving average of spread – 5 days moving standard deviation of spread), make a long entry in stock1 the next day.

If current-day spread (price ratio of stock1/stock2) goes above 5 days moving average of spread, make a long exit in stock1 on the next day.

If current-day spread (price ratio of stock1/stock2) goes above the upper band (5 days moving average of spread + 5 days moving standard deviation of spread), make a short entry in stock1 the next day.

If current-day spread (price ratio of stock1/stock2) goes below 5 days moving average of spread, make a short exit in stock1 on the next day. And take the reverse position in stock2 simultaneously.

If the long and short signal is fresh, trade at an open price otherwise trades at a close price.

Trend Following RSI Strategy

If current-day RSI goes below 30, long the stock the next day.

If current-day RSI goes above70, short the stock the next day.

If the long and short signal is fresh, trade at an open price otherwise trades at a close price.


Conclusion

We explored the stock prices and volume data and built 3 strategies – Volume and Price based trading, Mean Reversion and Trend following- RSI.

We find that there is no one particular strategy which works for all the stocks. We evaluated the strategy performance and found high overall return and hit ratio.

However, we can explore a lot of other strategies which we learnt in the course and generate a much higher return than what these strategies accomplished.

If you want to learn various aspects of Algorithmic trading then check out the Executive Programme in Algorithmic Trading (EPAT). The course covers various training modules and equips you with the required skill sets to build a promising career in algorithmic trading.


Bibliography


Files in the download

  • Project code strategy in Python
  • Project code data extract in R


Disclaimer: The information in this project is true and complete to the best of our Student’s knowledge. All recommendations are made without guarantee on the part of the student or QuantInsti®. The student and QuantInsti® disclaim any liability in connection with the use of this information. All content provided in this project is for informational purposes only and we do not guarantee that by using the guidance you will derive a certain profit.

Advanced Algorithmic Trading Course