
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import mplfinance as mpl
# pip install --upgrade mplfinance
tcs = pd.read_csv('TCS.NS.csv', index_col='Date', parse_dates=True)
tcs.head(10)
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| Date | ||||||
| 2017-11-02 | 1304.250000 | 1337.000000 | 1303.375000 | 1313.074951 | 1199.349854 | 1468422 |
| 2017-11-03 | 1316.199951 | 1318.349976 | 1301.400024 | 1310.050049 | 1196.586670 | 666886 |
| 2017-11-06 | 1310.000000 | 1335.900024 | 1305.875000 | 1332.775024 | 1217.343750 | 1377000 |
| 2017-11-07 | 1335.000000 | 1384.000000 | 1333.025024 | 1355.025024 | 1237.666748 | 2696892 |
| 2017-11-08 | 1361.500000 | 1379.500000 | 1359.125000 | 1367.500000 | 1249.061035 | 1402252 |
| 2017-11-09 | 1375.000000 | 1375.000000 | 1359.650024 | 1368.199951 | 1249.700439 | 1152432 |
| 2017-11-10 | 1361.099976 | 1363.000000 | 1346.675049 | 1354.375000 | 1237.072998 | 1356566 |
| 2017-11-13 | 1351.025024 | 1388.699951 | 1351.025024 | 1379.474976 | 1259.998901 | 1496996 |
| 2017-11-14 | 1378.275024 | 1378.275024 | 1353.750000 | 1357.300049 | 1239.744385 | 1657252 |
| 2017-11-15 | 1353.000000 | 1377.500000 | 1350.675049 | 1352.650024 | 1235.497314 | 6670834 |
.
tcs.info()
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 1235 entries, 2017-11-02 to 2022-11-01 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Open 1235 non-null float64 1 High 1235 non-null float64 2 Low 1235 non-null float64 3 Close 1235 non-null float64 4 Adj Close 1235 non-null float64 5 Volume 1235 non-null int64 dtypes: float64(5), int64(1) memory usage: 67.5 KB
.
tcs.isnull().sum()
Open 0 High 0 Low 0 Close 0 Adj Close 0 Volume 0 dtype: int64
.
tcs.describe()
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| count | 1235.000000 | 1235.000000 | 1235.000000 | 1235.000000 | 1235.000000 | 1.235000e+03 |
| mean | 2517.772529 | 2543.625319 | 2489.836221 | 2516.272792 | 2428.493448 | 3.127995e+06 |
| std | 734.088662 | 738.030266 | 727.998626 | 732.955559 | 766.403293 | 3.075381e+06 |
| min | 1268.500000 | 1292.224976 | 1247.175049 | 1272.800049 | 1162.562744 | 1.445300e+05 |
| 25% | 1984.000000 | 2002.000000 | 1962.525024 | 1983.099976 | 1847.940429 | 1.928873e+06 |
| 50% | 2217.850098 | 2244.500000 | 2194.199951 | 2215.600098 | 2107.700684 | 2.611493e+06 |
| 75% | 3198.474976 | 3224.924926 | 3162.075074 | 3195.474976 | 3150.350463 | 3.573045e+06 |
| max | 4033.949951 | 4043.000000 | 3980.000000 | 4019.149902 | 3964.502686 | 8.806715e+07 |
.
infy = pd.read_csv('INFY.NS.csv', index_col='Date', parse_dates=True)
infy.head(10)
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| Date | ||||||
| 2017-11-02 | 461.524994 | 463.000000 | 460.000000 | 461.450012 | 406.673004 | 4686138 |
| 2017-11-03 | 458.500000 | 464.950012 | 458.475006 | 463.325012 | 408.325439 | 3699086 |
| 2017-11-06 | 461.575012 | 467.500000 | 461.325012 | 464.350006 | 409.228668 | 5066534 |
| 2017-11-07 | 463.125000 | 480.000000 | 462.924988 | 477.750000 | 421.038086 | 13442820 |
| 2017-11-08 | 480.125000 | 480.924988 | 473.750000 | 476.149994 | 419.627991 | 5369994 |
| 2017-11-09 | 476.500000 | 481.500000 | 474.125000 | 477.250000 | 420.597412 | 4723232 |
| 2017-11-10 | 479.399994 | 482.174988 | 475.000000 | 481.024994 | 423.924316 | 6589428 |
| 2017-11-13 | 481.024994 | 484.700012 | 474.674988 | 475.975006 | 419.473785 | 5021216 |
| 2017-11-14 | 475.975006 | 477.424988 | 471.625000 | 474.549988 | 418.217926 | 5733710 |
| 2017-11-15 | 472.575012 | 476.750000 | 472.250000 | 475.899994 | 419.407684 | 7023516 |
.
infy.info()
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 1235 entries, 2017-11-02 to 2022-11-01 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Open 1235 non-null float64 1 High 1235 non-null float64 2 Low 1235 non-null float64 3 Close 1235 non-null float64 4 Adj Close 1235 non-null float64 5 Volume 1235 non-null int64 dtypes: float64(5), int64(1) memory usage: 67.5 KB
.
infy.isnull().sum()
Open 0 High 0 Low 0 Close 0 Adj Close 0 Volume 0 dtype: int64
.
infy.describe()
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| count | 1235.000000 | 1235.000000 | 1235.000000 | 1235.000000 | 1235.000000 | 1.235000e+03 |
| mean | 1040.153726 | 1051.354049 | 1029.087085 | 1040.137409 | 988.241594 | 8.488027e+06 |
| std | 433.301951 | 436.597649 | 429.126905 | 432.849813 | 443.829212 | 5.944440e+06 |
| min | 458.500000 | 463.000000 | 458.475006 | 461.450012 | 406.673004 | 4.834070e+05 |
| 25% | 690.325012 | 699.000000 | 682.662476 | 691.174988 | 627.743866 | 5.362184e+06 |
| 50% | 788.000000 | 797.000000 | 782.049988 | 789.549988 | 728.917236 | 7.016424e+06 |
| 75% | 1453.500000 | 1467.799988 | 1437.725036 | 1451.824951 | 1430.209045 | 9.723727e+06 |
| max | 1938.550049 | 1953.900024 | 1930.500000 | 1939.500000 | 1898.427979 | 9.043211e+07 |
.
tcs['Return'] = (tcs.Close - tcs.Open)/tcs.Open
tcs.head()
| Open | High | Low | Close | Adj Close | Volume | Return | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2017-11-02 | 1304.250000 | 1337.000000 | 1303.375000 | 1313.074951 | 1199.349854 | 1468422 | 0.006766 |
| 2017-11-03 | 1316.199951 | 1318.349976 | 1301.400024 | 1310.050049 | 1196.586670 | 666886 | -0.004672 |
| 2017-11-06 | 1310.000000 | 1335.900024 | 1305.875000 | 1332.775024 | 1217.343750 | 1377000 | 0.017386 |
| 2017-11-07 | 1335.000000 | 1384.000000 | 1333.025024 | 1355.025024 | 1237.666748 | 2696892 | 0.015000 |
| 2017-11-08 | 1361.500000 | 1379.500000 | 1359.125000 | 1367.500000 | 1249.061035 | 1402252 | 0.004407 |
infy['Return'] = (infy.Close - infy.Open)/infy.Open
infy.head()
| Open | High | Low | Close | Adj Close | Volume | Return | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2017-11-02 | 461.524994 | 463.000000 | 460.000000 | 461.450012 | 406.673004 | 4686138 | -0.000162 |
| 2017-11-03 | 458.500000 | 464.950012 | 458.475006 | 463.325012 | 408.325439 | 3699086 | 0.010523 |
| 2017-11-06 | 461.575012 | 467.500000 | 461.325012 | 464.350006 | 409.228668 | 5066534 | 0.006012 |
| 2017-11-07 | 463.125000 | 480.000000 | 462.924988 | 477.750000 | 421.038086 | 13442820 | 0.031579 |
| 2017-11-08 | 480.125000 | 480.924988 | 473.750000 | 476.149994 | 419.627991 | 5369994 | -0.008279 |
.
plt.figure(figsize=(16,5), dpi=400)
plt.title('Price Comparison', size=20)
sns.lineplot(data=tcs, x=tcs.index, y='Close', label='TCS')
sns.lineplot(data=infy, x=infy.index, y='Close', label='INFOSYS')
plt.show()
.
tcs_return = []
x = 0
for i in tcs.Return:
x += i
tcs_return.append(x)
infy_return = []
x = 0
for i in infy.Return:
x += i
infy_return.append(x)
plt.figure(figsize=(16,5), dpi=400)
plt.title('Return Comparison(in %)', size=20)
sns.lineplot(data=tcs, x=tcs.index, y=tcs_return, label='TCS')
sns.lineplot(data=infy, x=infy.index, y=infy_return, label='INFOSYS')
plt.show()
.
plt.figure(figsize=(16,5), dpi=400)
plt.title('Volume Comparsion', size=20)
sns.lineplot(data=tcs, x=tcs.index, y='Volume', label='TCS')
sns.lineplot(data=infy, x=infy.index, y='Volume', label='INFOSYS')
plt.show()
.
tcs['Fluctuation'] = tcs.High - tcs.Low
tcs.head()
| Open | High | Low | Close | Adj Close | Volume | Return | Fluctuation | |
|---|---|---|---|---|---|---|---|---|
| Date | ||||||||
| 2017-11-02 | 1304.250000 | 1337.000000 | 1303.375000 | 1313.074951 | 1199.349854 | 1468422 | 0.006766 | 33.625000 |
| 2017-11-03 | 1316.199951 | 1318.349976 | 1301.400024 | 1310.050049 | 1196.586670 | 666886 | -0.004672 | 16.949952 |
| 2017-11-06 | 1310.000000 | 1335.900024 | 1305.875000 | 1332.775024 | 1217.343750 | 1377000 | 0.017386 | 30.025024 |
| 2017-11-07 | 1335.000000 | 1384.000000 | 1333.025024 | 1355.025024 | 1237.666748 | 2696892 | 0.015000 | 50.974976 |
| 2017-11-08 | 1361.500000 | 1379.500000 | 1359.125000 | 1367.500000 | 1249.061035 | 1402252 | 0.004407 | 20.375000 |
infy['Fluctuation'] = infy.High - infy.Low
infy.head()
| Open | High | Low | Close | Adj Close | Volume | Return | Fluctuation | |
|---|---|---|---|---|---|---|---|---|
| Date | ||||||||
| 2017-11-02 | 461.524994 | 463.000000 | 460.000000 | 461.450012 | 406.673004 | 4686138 | -0.000162 | 3.000000 |
| 2017-11-03 | 458.500000 | 464.950012 | 458.475006 | 463.325012 | 408.325439 | 3699086 | 0.010523 | 6.475006 |
| 2017-11-06 | 461.575012 | 467.500000 | 461.325012 | 464.350006 | 409.228668 | 5066534 | 0.006012 | 6.174988 |
| 2017-11-07 | 463.125000 | 480.000000 | 462.924988 | 477.750000 | 421.038086 | 13442820 | 0.031579 | 17.075012 |
| 2017-11-08 | 480.125000 | 480.924988 | 473.750000 | 476.149994 | 419.627991 | 5369994 | -0.008279 | 7.174988 |
plt.figure(figsize=(16,5), dpi=400)
plt.title('Fluctuation Comparsion', size=20)
sns.lineplot(data=tcs, x=tcs.index, y='Fluctuation', label='TCS')
sns.lineplot(data=infy, x=infy.index, y='Fluctuation', label='INFOSYS', alpha=0.5)
plt.show()
.
data_tcs = tcs[['Open','High','Low','Close']].tail(50)
data_infy = infy[['Open','High','Low','Close']].tail(50)

mpl.plot(data_tcs, type='candle', mav=(3,6,9), style='yahoo', title='TCS')
mpl.plot(data_infy, type='candle', mav=(3,6,9), style='yahoo', title='INFOSYS')