Stock Delivery Analysis using Pandas

Well, I am not a Trader but I tried to learn some concepts of Swing Trading and while learning that I came across the concept of Stock Delivery and it's analysis.

In this Blog I will tell you about how you can do stock delivery analysis using Pandas Library based on stock delivery data from NSE website.


What is Stock Delivery

Stock delivery means buying a stock and it gets deposited in your Demat Account so basically any individual buy particular quantity of stock and hold it in his demat account. 


Basic Setup for running the Script


Install Following python package in your virtualenv 

- pandas

- nsepy


Use following command to install above package



pip install pandas

pip install nsepy


Get Stock Delivery Data

Now let's start working on analysis. In this blog we will explore how to get delivery data and calculate 10 days and 20 days delivery average of stock

  1. Import the necessary library

  2. 
      import pandas as pd
      from nsepy import get_history
      from datetime import datetime, timedelta, date
      import matplotlib.pyplot as plt
      
  3. Set start and end date to get data within date range

  4. 
      number_of_days = 50
      end_date = datetime.now().date() + timedelta(1)
      start_date = end_date - timedelta(number_of_days)
      # so here we set date range for last 50 days because we need to get data for last 50 days
      
  5. Now get the data into pandas data frame using get_history function which we impored earlier

  6. 
      df = get_history(symbol="tatasteel", start=start_date, end=end_date)
      

    so now we have data for last 50 days of tatasteel stock.Same way you can use any stock symbol for which you want to do analysis.

  7. Print df.head() which will print first 5 records

  8. 
      df.head()
      
    
                
    
    Symbol Series Prev Close Open High Low Last Close VWAP Volume Turnover Trades Deliverable Volume %Deliverble
    Date
    2021-12-20 TATASTEEL EQ 1132.10 1117.00 1117.20 1066.05 1073.00 1072.95 1082.82 7352650 7.961619e+14 236252 2370671 0.3224
    2021-12-21 TATASTEEL EQ 1072.95 1090.00 1124.25 1085.00 1108.85 1105.10 1109.16 7026849 7.793906e+14 171193 1594912 0.2270
    2021-12-22 TATASTEEL EQ 1105.10 1115.00 1131.00 1110.00 1130.00 1128.85 1123.02 4366917 4.904149e+14 114115 1005289 0.2302
    2021-12-23 TATASTEEL EQ 1128.85 1138.00 1146.80 1120.20 1126.45 1127.65 1134.88 4621478 5.244827e+14 114823 1226564 0.2654
    2021-12-24 TATASTEEL EQ 1127.65 1130.05 1133.45 1103.85 1117.00 1115.45 1115.11 4093630 4.564829e+14 96182 824805 0.2015

    In the above table get_history function return so many columns. For the scope of this blog, we will focus on the following column only 

     1. date

     2. Symbol 

     3. Series

     4. Volume 

      5. Deliverable Volume --> This is exactly what we need for our analysis 

     Let's create two new columns in data frame which will hold a delivery volume average of 10 days and 20 days each


df['delivery_SMA_10'] = df.iloc[:,12].rolling(window=10).mean()
df['delivery_SMA_20'] = df.iloc[:,12].rolling(window=20).mean()

In above code df.iloc[:,12] will return all values from 'Deliverable Volume' column and also we used rolling function to find out mean of underlying data having a window size of 10 and 20.

Now Print first 25 rows

df.head(25)

    
Symbol Series Prev Close Open High Low Last Close VWAP Volume Turnover Trades Deliverable Volume %Deliverble delivery_SMA_10 delivery_SMA_20
Date
2021-12-20 TATASTEEL EQ 1132.10 1117.00 1117.20 1066.05 1073.00 1072.95 1082.82 7352650 7.961619e+14 236252 2370671 0.3224 NaN NaN
2021-12-21 TATASTEEL EQ 1072.95 1090.00 1124.25 1085.00 1108.85 1105.10 1109.16 7026849 7.793906e+14 171193 1594912 0.2270 NaN NaN
2021-12-22 TATASTEEL EQ 1105.10 1115.00 1131.00 1110.00 1130.00 1128.85 1123.02 4366917 4.904149e+14 114115 1005289 0.2302 NaN NaN
2021-12-23 TATASTEEL EQ 1128.85 1138.00 1146.80 1120.20 1126.45 1127.65 1134.88 4621478 5.244827e+14 114823 1226564 0.2654 NaN NaN
2021-12-24 TATASTEEL EQ 1127.65 1130.05 1133.45 1103.85 1117.00 1115.45 1115.11 4093630 4.564829e+14 96182 824805 0.2015 NaN NaN
2021-12-27 TATASTEEL EQ 1115.45 1111.05 1124.70 1102.35 1124.00 1121.80 1114.49 2758851 3.074721e+14 74732 368587 0.1336 NaN NaN
2021-12-28 TATASTEEL EQ 1121.80 1127.00 1131.50 1122.00 1127.00 1127.45 1127.24 2880781 3.247338e+14 108720 1134146 0.3937 NaN NaN
2021-12-29 TATASTEEL EQ 1127.45 1120.00 1126.65 1108.00 1115.75 1116.25 1115.35 4193520 4.677260e+14 145576 1651673 0.3939 NaN NaN
2021-12-30 TATASTEEL EQ 1116.25 1117.90 1127.00 1099.00 1101.15 1101.00 1110.09 4114093 4.567033e+14 102093 1509421 0.3669 NaN NaN
2021-12-31 TATASTEEL EQ 1101.00 1105.00 1123.50 1102.65 1112.40 1111.45 1115.49 3687021 4.112829e+14 94613 1171618 0.3178 1285768.6 NaN
2022-01-03 TATASTEEL EQ 1111.45 1115.00 1151.00 1115.00 1150.00 1142.45 1129.46 3865803 4.366280e+14 87982 1215520 0.3144 1170253.5 NaN
2022-01-04 TATASTEEL EQ 1142.45 1153.00 1159.70 1136.50 1148.05 1148.80 1147.81 5975731 6.859027e+14 125400 1733910 0.2902 1184153.3 NaN
2022-01-05 TATASTEEL EQ 1148.80 1147.00 1180.80 1141.25 1176.00 1177.60 1166.60 6186176 7.216800e+14 134032 1997541 0.3229 1283378.5 NaN
2022-01-06 TATASTEEL EQ 1177.60 1172.00 1183.00 1155.55 1161.50 1163.25 1166.83 5335400 6.225489e+14 133187 1440528 0.2700 1304774.9 NaN
2022-01-07 TATASTEEL EQ 1163.25 1165.20 1174.00 1147.85 1158.15 1160.35 1160.62 3973857 4.612154e+14 87679 1167664 0.2938 1339060.8 NaN
2022-01-10 TATASTEEL EQ 1160.35 1165.00 1173.40 1153.85 1168.00 1169.05 1165.95 3822500 4.456832e+14 124004 1331943 0.3484 1435396.4 NaN
2022-01-11 TATASTEEL EQ 1169.05 1158.65 1162.70 1113.00 1134.90 1130.25 1137.02 9697885 1.102671e+15 245197 3285797 0.3388 1650561.5 NaN
2022-01-12 TATASTEEL EQ 1130.25 1139.85 1150.80 1132.40 1147.80 1147.20 1142.13 5035843 5.751572e+14 139132 1525010 0.3028 1637895.2 NaN
2022-01-13 TATASTEEL EQ 1147.20 1154.00 1225.50 1152.15 1219.00 1221.15 1200.08 15723116 1.886896e+15 350847 4326987 0.2752 1919651.8 NaN
2022-01-14 TATASTEEL EQ 1221.15 1213.00 1226.30 1206.10 1211.00 1213.60 1215.42 5267907 6.402741e+14 162565 1290762 0.2450 1931566.2 1608667.40
2022-01-17 TATASTEEL EQ 1213.60 1215.00 1245.00 1190.35 1230.80 1229.75 1221.77 8969607 1.095879e+15 202840 2210957 0.2465 2031109.9 1600681.70
2022-01-18 TATASTEEL EQ 1229.75 1230.00 1233.75 1190.00 1196.95 1194.85 1209.80 5840897 7.066337e+14 150353 1797537 0.3078 2037472.6 1610812.95
2022-01-19 TATASTEEL EQ 1194.85 1196.00 1220.90 1183.00 1208.00 1209.60 1204.89 7304321 8.800884e+14 186907 1335466 0.1828 1971265.1 1627321.80
2022-01-20 TATASTEEL EQ 1209.60 1210.10 1224.00 1197.00 1209.90 1206.70 1212.03 5920597 7.175913e+14 159207 1526225 0.2578 1979834.8 1642304.85
2022-01-21 TATASTEEL EQ 1206.70 1200.00 1204.30 1156.35 1169.10 1169.70 1177.49 7166267 8.438184e+14 170921 2048736 0.2859 2067942.0 1703501.40
2022-01-24 TATASTEEL EQ 1169.70 1160.00 1163.65 1090.00 1100.50 1099.20 1121.02 8392076 9.407700e+14 279320 1468792 0.2910 2081626.9 1758511.65
2022-01-25 TATASTEEL EQ 1099.20 1094.70 1121.00 1087.05 1105.95 1109.10 1107.84 5733063 6.351335e+14 206703 1555235 0.2713 1908570.7 1779566.10
2022-01-27 TATASTEEL EQ 1109.10 1090.00 1103.45 1061.30 1090.65 1088.35 1082.03 9056656 9.799578e+14 271554 2244835 0.2479 1980553.2 1809224.20
2022-01-28 TATASTEEL EQ 1088.35 1105.85 1125.75 1080.95 1086.00 1084.65 1108.99 7864893 8.722068e+14 192376 2044249 0.2599 1752279.4 1835965.60
2022-01-31 TATASTEEL EQ 1084.65 1109.90 1114.60 1080.50 1085.00 1085.55 1096.19 5570939 6.106809e+14 172294 1283038 0.2303 1751507.0 1841536.60
In above table two additional columns were added i.e 'delivery_SMA_10' and 'delivery_SMA_20' which hold average volume of previous 10 days and 20 days respectively. 
The first 10 records for delivery_SMA_10 and 20 records for delivery_SMA_20 are NaN because for those previous 10 days and 20 days volume data are not available. 

Now we can plot the graph of volume average to make sense of the above data as "a picture tells a thousand words"



# Remove all Nan Value 
df.dropna(inplace=True)
# Plot 10 days average volume line with green color
plt.plot(df.index, df['delivery_SMA_10'], label='SMA 10 days delivery', color='g')

# # Plot 20 days average volume line with red color
plt.plot(df.index, df['delivery_SMA_20'], label='SMA 20 days delivery', color='r')

plt.legend()
plt.show() 


In the above graph, we plot lines of 10 days moving average of Deliverable Volume and 20 days moving average Deliverable Volume.

The above graph is showing us that as of the current date(2022-02-05) 10 days moving average is less than 20 days moving average so we can clearly understand Delivery starts decreasing when 10 days delivery line cross 20 days delivery line from above.

The delivery analysis is one of the ways to analyze stock data that you can include in your trading strategy.

In this article, I write about how we can use stock delivery data to calculate average and plot graph based on it. We can use this data in many ways to derive what is happening with the stock in the trading session.

Here we calculate 10 days and 20 days moving average of volume. But you can calculate the same for any number of days.

In this article I discussed about only Deliverable Volume data but you can do analysis on other data from table like Open, Close, High, Low, VWAP, Turnover and also plot graph based on your calculated data.

I will soon write on different kinds of analysis which we can do from stock history data using Pandas.

Comments