Bitcoin Prise Using LSTM.ipynb - Colab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 49

1. What is LSTM ?

Long short-term memory is an arti cial recurrent neural network architecture used in the eld of deep
learning. Unlike standard feedforward neural networks, LSTM has feedback connections. It can process
not only single data points, but also entire sequences of data.
Long Short-Term Memory (LSTM) networks are a type of recurrent neural network capable of learning
order dependence in sequence prediction problems. This is a behavior required in complex problem
domains like machine translation, speech recognition, and more. LSTMs are a complex area of deep
learning.
LSTMs are often referred to as fancy RNNs. Vanilla RNNs do not have a cell state. They only have
hidden states and those hidden states serve as the memory for RNNs. Meanwhile, LSTM has both cell
states and a hidden states.

 2. Importing Library

# First we will import the necessary Library

import os import pandas


as pd import numpy as
np import math import
datetime as dt import
matplotlib.pyplot as plt

# For Evalution we will use these library

from sklearn.metrics import mean_squared_error, mean_absolute_error,


explained_variance_score, r2_score from sklearn.metrics import
mean_poisson_deviance, mean_gamma_deviance, accuracy_score from
sklearn.preprocessing import MinMaxScaler # For model building we will use
these library

import tensorflow as tf from


tensorflow.keras.models import
Sequential from tensorflow.keras.layers
import Dense, Dropout from
tensorflow.keras.layers import LSTM

# For PLotting we will use these library

import matplotlib.pyplot
as plt from itertools
import cycle import
plotly.graph_objects as go
import plotly.express as
px
from plotly.subplots import make_subplots
 3. Loading Dataset

maindf=pd.read_csv('/content/BTC-USD.csv')

print('Total number of days present in the dataset:


',maindf.shape[0]) print('Total number of fields present in the
dataset: ',maindf.shape[1])
Total number of days present in the dataset: 2713
Total number of fields present in the dataset: 7

maindf.shape
(2713, 7)

maindf.head()
Date Open High Low Close Adj CloseVolume
0 2014-09-17 465.864014 468.174011 452.421997 457.334015 457.334015 21056800
1 2014-09-18 456.859985 456.859985 413.104004 424.440002 424.440002 34483200
2 2014-09-19 424.102997 427.834991 384.532013 394.795990 394.795990 37919700
3 2014-09-20 394.673004 423.295990 389.882996 408.903992 408.903992 36863600
4 2014-09-21 408.084991 412.425995 393.181000 398.821014 398.821014 26580100

maindf.tail()
Date Open High Low Close Adj Close Volume

2708 2022-02-15 42586.464844 44667.218750 42491.035156 44575.203125 44575.203125 22721659051

270 202 44578.277 44578.277 43456.691 43961.859 43961.859 19792547


9 2- 344 344 406 375 375 657
02-
16
271 202 43937.070 44132.972 40249.371 40538.011 40538.011 26246662
0 2- 313 656 094 719 719 813
02-
17
271 202 40552.132 40929.152 39637.617 40030.976 40030.976 23310007
1 2- 813 344 188 563 563 704
02-
18
271 202 40022.132 40246.027 40010.867 40126.429 40126.429 22263900
2 2- 813 344 188 688 688 160
02-
19
maindf.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2713 entries, 0 to
2712 Data columns (total 7
columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 2713 non-null object
1 Open 2713 non-null float64
2 High 2713 non-null float64
3 Low 2713 non-null float64
4 Close 2713 non-null float64
5 Adj Close 2713 non-null float64 6 Volume 2713 non-null int64 dtypes: float64(5),
int64(1), object(1) memory usage: 148.5+ KB

maindf.describe()
Open High Low Close Adj Close Volume

count 2713.000000 2713.000000 2713.000000 2713.000000 2713.000000 2.713000e+03

mean11311.04106911614.29248210975.55505711323.91463711323.9146371.470462e+10
std 16106.42889116537.39064 15608.57256016110.36501016110.3650102.001627e+10
9
min 176.897003 211.731003 171.509995 178.102997 178.102997 5.914570e+06
25% 606.396973 609.260986 604.109985 606.718994 606.718994 7.991080e+07
50% 6301.569824 6434.617676 6214.220215 6317.609863 6317.609863 5.098183e+09
75% 10452.39941410762.64453 10202.38769510462.25976 10462.2597662.456992e+10
1 6
max 67549.73437568789.62500 66382.06250067566.82812 67566.8281253.509679e+11
0 5
 
 Checking for Null Values

print('Null Values:',maindf.isnull().values.sum())
Null Values: 0

print('NA values:',maindf.isnull().values.any())
NA values: False

maindf.shape
(2713, 7)
4. EDA(Exploratory Data Analysis)bold text

# Printing the start date and End date of the dataset

sd=maindf.iloc[0][0]
ed=maindf.iloc[-1][0]

print('Starting Date',sd)
print('Ending Date',ed)
Starting Date 2014-09-17
Ending Date 2022-02-19 <ipython-input-104-d7c77f12f3f6>:3: FutureWarning: Series.__getitem__

treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels

(con <ipython-input-104-d7c77f12f3f6>:4: FutureWarning:

Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be
treated as labels (con

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2014 = maindf.loc[(maindf['Date'] >=


'2014-09-17') &
(maindf['Date'] < '2014-12-31')]
y_2014.drop(y_2014[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

0 2014-09-17 465.864014 468.174011 452.421997 457.334015

1 2014-09-18 456.859985 456.85998 413.10400 424.44000


5 4 2
2 2014-09-19 424.102997 427.83499 384.53201 394.79599
1 3 0
3 2014-09-20 394.673004 423.29599 389.88299 408.90399
0 6 2
4 2014-09-21 408.084991 412.42599 393.18100 398.82101
5 0 4
... ... ... ... ... ...
100 2014-12-26 319.152008 331.42401 316.62701 327.92401
1 4 1
101 2014-12-27 327.583008 328.91101 312.63000 315.86300
1 5 7
102 2014-12-28 316.160004 320.02801 311.07800 317.23901
5 3 4
103 2014-12-29 317.700989 320.26699 312.30700 312.67001
8 7 3
104 2014-12-30 312.718994 314.80899 309.37298 310.73700
105 rows × 5 columns 0 6 0

monthvise= y_2014.groupby(y_2014['Date'].dt.strftime('%B'))
[['Open','Close']].mean()
new_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August',
'September', 'October', 'November', 'December']
monthvise = monthvise.reindex(new_order, axis=0)
monthvise

Open Close

Date

January NaN NaN

February NaN NaN

Marc Na NaN
h N
April Na NaN
N
May Na NaN
N
June Na NaN
N
July Na NaN
N
Augus Na NaN
t N
Septem 412.654 407.182
ber 003 428
Octob 365.748 364.148
er 000 873
Novem 364.850 366.099
ber 235 799
Decem 344.146 341.970
ber 864 366

fig = go.Figure()

fig.add_trace(go.Bar(

x=monthvise.index,

y=monthvise['Open'],

name='Stock Open Price',

marker_color='crimson'

))

fig.add_trace(go.Bar(

x=monthvise.index,

y=monthvise['Close'],

name='Stock Close Price',

marker_color='lightsalmon'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45,

title='Monthwise comparision between Stock open and close price')

fig.show()

Monthwise comparision between Stock open and close price

Stock Open Price


400
Stock Close Price

350

300

250

200

150

100

50

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov

Note that we only have few months in 2014 so the rest of the months are not plotted

since we do not have the data

y_2014.groupby(y_2014['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2014.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2014.groupby(y_2014['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)

fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()

Monthwise High and Low stock price

Stock high Price


Stock low Price

400

300

200

100

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

Note that we only have few months in 2014 so the rest of the months are not plotted

since we do not have the data

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2014, x=y_2014.Date, y=[y_2014['Open'], y_2014['Close'],


y_2014['High'], y_2014['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()
Stock analysis chart
Stock Parameters
460 Stock Open Price
Stock Close Price
440
Stock High Price
420 Stock Low Price
alue
400
Stock v
380

360

340

320

300

280
Oct 2014 Nov 2014 Dec 2014

Date

Note that we only have few months in 2014 so the rest of the months are not plotted

since we do not have the data

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2015 = maindf.loc[(maindf['Date'] >=


'2015-01-01') &
(maindf['Date'] < '2016-01-01')]
y_2015.drop(y_2015[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

106 2015-01-01 320.434998 320.434998 314.002991 314.248993

10 2015- 314.0790 315.8389 313.5650 315.0320


7 01-02 10 89 02 13
10 2015- 314.8460 315.1499 281.0820 281.0820
8 01-03 08 94 01 01
10 2015- 281.1459 287.2300 257.6120 264.1950
9 01-04 96 11 00 07
11 2015- 265.0840 278.3410 265.0840 274.4739
0 01-05 15 03 15 99
... . ... ... ... ...
.
.
46 2015- 416.5140 424.0069 408.8829 422.8229
6 12-27 08 89 96 98
46 2015- 423.3429 429.7690 418.4809 422.2789
7 12-28 87 12 88 92
46 2015- 422.0979 432.9830 420.6270 432.9830
8 12-29 92 02 14 02
46 2015- 433.2999 434.3869 422.0840 426.6199
9 12-30 88 93 15 95
47 2015- 425.8750 432.9209 418.7349 430.5669
0 12-31 00 90 85 86
365 rows × 5 columns

monthvise= y_2015.groupby(y_2015['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close

Date

January 251.799905 248.782547

February 232.821856 234.153645

Marc 269.278 269.042


h 419 259
April 235.708 235.491
667 534
May 237.161 236.997
806 001
June 236.941 238.081
433 766
July 278.857 279.563
679 740
Augus 252.986 250.733
t 774 805
Septem 233.486 233.595
ber 733 533
Octob 262.306 264.855
er 000 356
Novem 346.866 348.883
ber 833 332
Decem 422.618 424.464
ber 033 547
Next steps: Generate code with monthvise  View recommended plots New interactive sheet

fig = go.Figure()

fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()

Monthwise comparision between Stock open and close price

Stock Open Price


400 Stock Close Price

350

300

250

200

150

100

50

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov

y_2015.groupby(y_2015['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2015.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2015.groupby(y_2015['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()

Monthwise High and Low stock price

500 Stock high Price


Stock low Price

400

300

200

100

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2015, x=y_2015.Date, y=[y_2015['Open'], y_2015['Close'],


y_2015['High'], y_2015['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()

Stock analysis chart

500 Stock Parameters


Stock Open
Price
450Stock Close Price
Stock High Price
Stock Low
Price
400
alue
Stock v
350

300

250

200

Jan 2015 Apr 2015 Jul 2015 Oct 2015


Date

 Analysis of Year 2016

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2016 = maindf.loc[(maindf['Date'] >=


'2016-01-01') &
(maindf['Date'] < '2017-01-01')]
y_2016.drop(y_2016[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

471 2016-01-01 430.721008 436.246002 427.515015 434.334015

47 2016- 434.6220 436.0620 431.8699 433.4379


2 01-02 09 12 95 88
47 2016- 433.5780 433.7430 424.7059 430.0109
3 01-03 03 11 94 86
47 2016- 430.0610 434.5169 429.0840 433.0910
4 01-04 05 98 15 03
47 2016- 433.0690 434.1820 429.6759 431.9599
5 01-05 00 07 95 91
... . ... ... ... ...
.
.
83 2016- 908.3540 940.0479 904.2550 933.1979
2 12-27 04 74 05 98
83 2016- 934.8309 975.9210 934.8309 975.9210
3 12-28 94 21 94 21
83 2016- 975.1250 979.3969 954.5029 973.4970
4 12-29 00 73 91 09
83 2016- 972.5349 972.5349 934.8330 961.2379
5 12-30 73 73 08 76
83 2016- 960.6270 963.7429 947.2360 963.7429
6 12-31 14 81 23 81
366 rows × 5 columns

monthvise= y_2016.groupby(y_2016['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close

Date

January 412.805902 410.844485

February 402.304692 404.408274

Marc 417.262 416.525


h 033 774
April 433.487 434.339
433 398
May 459.237 461.954
547 415
June 638.544 642.869
834 061
July 662.977 661.356
779 103
Augus 581.238 579.585
t 966 197
Septem 604.614 605.848
ber 034 633
Octob 640.702 643.550
er 546 935
Novem 725.073 726.349
ber 804 101
Decem 821.108 828.060
ber 255 356

fig = go.Figure()

fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()

Monthwise comparision between Stock open and close price

Stock Open Price


800 Stock Close Price

700

600

500

400

300

200

100

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov

y_2016.groupby(y_2016['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2016.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2016.groupby(y_2016['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()

Monthwise High and Low stock price

1000 Stock high Price


Stock low Price

800

600

400

200

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2016, x=y_2016.Date, y=[y_2016['Open'], y_2016['Close'],


y_2016['High'], y_2016['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()

Stock analysis chart


1000
Stock Parameters

Stock Open
Price
900Stock Close Price
Stock High Price
Stock Low
Price
800
700

600

500

400

Jan 2016 Apr 2016 Jul 2016 Oct 2016


Date

 Analysis of Year 2017

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2017 = maindf.loc[(maindf['Date'] >=


'2017-01-01') &
(maindf['Date'] < '2018-01-01')]
y_2017.drop(y_2017[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

837 2017-01-01 963.658020 1003.080017 958.698975 998.325012

83 2017-998.61700 1031.390 996.70202 1021.750


8 01-02 4 015 6 000
83 2017-1021.599 1044.079 1021.599 1043.839
9 01-03976 956 976 966
84 2017-1044.400 1159.420 1044.400 1154.729
0 01-04024 044 024 980
84 2017-1156.729 1191.099 910.41699 1013.380
1 01-05980 976 2 005
... . ... ... ... ...
.
.
119 2017- 16163.500 16930.900 15114.299 15838.500
7 12-27 000 391 805 000
119 2017- 15864.099 15888.400 13937.299 14606.500
8 12-28 609 391 805 000
119 2017- 14695.799 15279.000 14307.000 14656.200
9 12-29 805 000 000 195
120 2017- 14681.900 14681.900 12350.099 12952.200
0 12-30 391 391 609 195
120 2017- 12897.700 14377.400 12755.599 14156.400
1 12-31 195 391 609 391
365 rows × 5 columns
monthvise= y_2017.groupby(y_2017['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close

Date

January 914.680971 914.916159

February 1055.620071 1062.533672

Marc 1133.212 1129.365


h 576 228
April 1197.646 1206.641
997 007
May 1865.748 1895.383
712 529
June 2630.573 2636.204
332 346
July 2509.213 2519.418
233 386
Augus 3819.812 3880.989
t 579 998
Septem 4077.400 4064.836
ber 993 312
Octob 5291.370 5360.071
er 007 604
Novem 7685.745 7813.132
ber 996 975
Decem 15175.21 15294.27
ber 9601 0980

fig = go.Figure()

fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()

Monthwise comparision between Stock open and close price


16k
Stock Open Price
Stock Close Price
14k

12k

10k

8k

6k

4k

2k

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov

y_2017.groupby(y_2017['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2017.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2017.groupby(y_2017['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
Monthwise High and Low stock price

20k Stock high Price


Stock low Price

15k

10k

5k

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2017, x=y_2017.Date, y=[y_2017['Open'], y_2017['Close'],


y_2017['High'], y_2017['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()
Stock analysis chart

20k Stock Parameters


Stock Open Price
Stock Close Price
Stock High Price
15k Stock Low Price
alue
Stock v
10k

5k

0
Jan 2017 Apr 2017 Jul 2017 Oct 2017

Date

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2018 = maindf.loc[(maindf['Date'] >=


'2018-01-01') &
(maindf['Date'] < '2019-01-01')]
y_2018.drop(y_2018[['Adj
Close','Volume']],axis=1)
Date
Open High Low Close

1202 2018-01-01 14112.200195 14112.200195 13154.700195 13657.200195

120 2018- 13625.000 15444.599 13163.599 14982.099


3 01-02 000 609 609 609
120 2018- 14978.200 15572.799 14844.500 15201.000
4 01-03 195 805 000 000
120 2018- 15270.700 15739.700 14522.200 15599.200
5 01-04 195 195 195 195
120 2018- 15477.200 17705.199 15202.799 17429.500
6 01-05 195 219 805 000
... . ... ... ... ...
.
.
156 2018- 3854.688 3874.416 3645.448 3654.833
2 12-27 477 992 486 496
156 2018- 3653.131 3956.135 3642.632 3923.918
3 12-28 836 986 080 701
156 2018- 3932.491 3963.758 3820.408 3820.408
4 12-29 699 789 691 691
156 2018- 3822.384 3901.908 3797.219 3865.952
5 12-30 766 936 238 637
156 2018- 3866.839 3868.742 3725.867 3742.700
6 12-31 111 920 432 439
365 rows × 5 columns

Analysis of Year 2018

monthvise= y_2018.groupby(y_2018['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close

Date

January 13212.074219 13085.558090

February 9462.242920 9472.001151

Marc 9156.591 9040.557


h 718 097
April 7963.618 8033.596
311 631
May 8505.240 8450.997
675 732
June 6829.257 6793.507
975 666
July 7101.466 7146.349
450 987
Augus 6723.800 6700.129
t 955 946
Septem 6622.821 6610.675
ber 338 033
Octob 6494.016 6485.118
er 491 747
Novem 5481.615 5404.250
ber 120 171
Decem 3726.475 3717.488
ber 106 344

fig = go.Figure()

fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()

Monthwise comparision between Stock open and close price

Stock Open Price


Stock Close Price
12k

10k

8k

6k

4k

2k

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2018.groupby(y_2018['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2018.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2018.groupby(y_2018['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()

Monthwise High and Low stock price

18k Stock high Price


Stock low Price
16k

14k

12k

10k

8k

6k

4k

2k

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2018, x=y_2018.Date, y=[y_2018['Open'], y_2018['Close'],


y_2018['High'], y_2018['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()
Stock analysis chart

18k Stock Parameters


Stock Open Price
16k Stock Close Price
Stock High Price
14k Stock Low Price
alue
12k
Stock v

10k

8k

6k

4k

Jan 2018 Apr 2018 Jul 2018 Oct 2018

Date

 Analysis of Year 2019

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2019 = maindf.loc[(maindf['Date'] >=


'2019-01-01') &
(maindf['Date'] < '2020-01-01')]
y_2019.drop(y_2019[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

1567 2019-01-01 3746.713379 3850.913818 3707.231201 3843.520020

156 2019- 3849.216 3947.981 3817.409 3943.409


8 01-02 309 201 424 424
156 2019- 3931.048 3935.685 3826.222 3836.741
9 01-03 584 059 900 211
157 2019- 3832.040 3865.934 3783.853 3857.717
0 01-04 039 570 760 529
157 2019- 3851.973 3904.903 3836.900 3845.194
1 01-05 877 076 146 580
... . ... ... ... ...
.
.
192 2019- 7238.141 7363.529 7189.934 7290.088
7 12-27 113 297 082 379
1922019- 7289.031 7399.041 7286.905 7317.990
8 12-28 250 016 273 234
192 2019- 7317.647 7513.948 7279.865 7422.652
9 12-29 461 242 234 832
193 2019- 7420.272 7454.824 7276.308 7292.995
0 12-30 949 219 105 117
193 2019- 7294.438 7335.290 7169.777 7193.599
1 12-31 965 039 832 121
365 rows × 5 columns

monthvise= y_2019.groupby(y_2019['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close

Date

January 3709.705645 3701.554963

February 3697.178327 3711.907261

Marc 3967.740 3976.069


h 400 100
April 5136.813 5178.469
314 434
May 7205.208 7309.694
024 131
June 9339.480 9415.900
322 179
July 10691.70 10669.33
6055 6158
Augus 10657.74 10643.24
t 5621 8362
Septem 9858.141 9814.067
ber 813 871
Octob 8382.432 8411.929
er 129 168
Novem 8427.103 8373.572
ber 516 412
Decem 7296.351 7284.013
ber 625 042
Next steps: Generate code with monthvise  View recommended plots New interactive sheet

fig = go.Figure()

fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()

Monthwise comparision between Stock open and close price

Stock Open Price


10k Stock Close Price

8k

6k

4k

2k

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov

y_2019.groupby(y_2019['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2019.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2019.groupby(y_2019['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()

Monthwise High and Low stock price

14k Stock high Price


Stock low Price

12k

10k

8k

6k

4k

2k

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2019, x=y_2019.Date, y=[y_2019['Open'], y_2019['Close'],


y_2019['High'], y_2019['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()
Stock analysis chart

14k Stock Parameters


Stock Open Price
Stock Close Price
12k Stock High Price
Stock Low Price
alue
10k
Stock v

8k

6k

4k

Jan 2019 Apr 2019 Jul 2019 Oct 2019

Date
 Analysis of Year 2020

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2020 = maindf.loc[(maindf['Date'] >=


'2020-01-01') &
(maindf['Date'] < '2021-01-01')]
y_2020.drop(y_2020[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

1932 2020-01-01 7194.892090 7254.330566 7174.944336 7200.174316

193 2020- 7202.551 7212.155 6935.270 6985.470


3 01-02 270 273 020 215
193 2020- 6984.428 7413.715 6914.996 7344.884
4 01-03 711 332 094 277
193 2020- 7345.375 7427.385 7309.514 7410.656
5 01-04 488 742 160 738
193 2020- 7410.451 7544.497 7400.535 7411.317
6 01-05 660 070 645 383
... . ... ... ... ...
.
.
229 2020- 26439.373 28288.839 25922.769 26272.294
3 12-27 047 844 531 922
229 2020- 26280.822 27389.111 26207.640 27084.808
4 12-28 266 328 625 594
229 2020- 27081.810 27370.720 25987.298 27362.437
5 12-29 547 703 828 500
229 2020- 27360.089 28937.740 27360.089 28840.953
6 12-30 844 234 844 125
229 2020- 28841.574 29244.876 28201.992 29001.720
7 12-31 219 953 188 703
366 rows × 5 columns

monthvise= y_2020.groupby(y_2020['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close

Date

January 8318.949597 8389.270476

February 9656.215113 9630.722185

Marc 6943.507 6871.016


h 009 113
April 7150.611 7224.477
328 328
May 9237.761 9263.151
530 745
June 9499.797 9489.227
005 214
July 9519.383 9589.899
852 729
Augus 11639.09 11652.39
t 7215 4185
Septem 10689.70 10660.27
ber 0163 6856
Octob 11791.30 11886.97
er 7491 8201
Novem 16450.12 16645.75
ber 1647 7422
Decem 21680.54 21983.13
ber 0827 7097
Next steps: Generate code with monthvise  View recommended plots New interactive sheet

fig = go.Figure()

fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
Monthwise comparision between Stock open and close price

Stock Open Price


Stock Close Price
20k

15k

10k

5k

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov

y_2020.groupby(y_2020['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2020.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2020.groupby(y_2020['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
Monthwise High and Low stock price
30k
Stock high Price
Stock low Price

25k

20k

15k

10k

5k

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2020, x=y_2020.Date, y=[y_2020['Open'], y_2020['Close'],


y_2020['High'], y_2020['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()
Stock analysis chart

30k
Stock Parameters
Stock Open Price
Stock Close Price
25k Stock High Price
Stock Low Price
alue
20k
Stock v

15k

10k

5k

Jan 2020 Apr 2020 Jul 2020 Oct 2020

Date

 Analysis of Year 2021

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2021 = maindf.loc[(maindf['Date'] >=


'2021-01-01') &
(maindf['Date'] < '2021-12-31')]
y_2021.drop(y_2021[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

2298 2021-01-01 28994.009766 29600.626953 28803.585938 29374.152344

229 2021- 29376.455 33155.117 29091.181 32127.267


9 01-02 078 188 641 578
230 2021- 32129.408 34608.558 32052.316 32782.023
0 01-03 203 594 406 438
230 2021- 32810.949 33440.218 28722.755 31971.914
1 01-04 219 750 859 063
230 2021- 31977.041 34437.589 30221.187 33992.429
2 01-05 016 844 500 688
... . ... ... ... ...
.
.
265 2021- 50428.691 51196.378 49623.105 50809.515
7 12-26 406 906 469 625
265 2021- 50802.609 51956.328 50499.468 50640.417
8 12-27 375 125 750 969
265 2021- 50679.859 50679.859 47414.210 47588.855
9 12-28 375 375 938 469
266 2021- 47623.871 48119.742 46201.496 46444.710
0 12-29 094 188 094 938
266 2021- 46490.605 47879.964 46060.312 47178.125
1 12-30 469 844 500 000
364 rows × 5 columns

monthvise= y_2021.groupby(y_2021['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close

Date

January 34652.961694 34761.649950

February 45874.967216 46306.798968

Marc 54544.67 54998.00


h 8176 8695
April 57251.25 57206.72
6250 0052
May 47105.82 46443.28
8503 6668
June 35920.54 35845.15
6940 4688
July 34234.21 34444.97
2450 3790
Augus 45516.11 45709.02
t 9834 2682
Septem 46041.85 45939.77
ber 9375 1484
Octob 57344.74 57911.97
er 3952 0514
Novem 60857.52 60621.48
ber 0313 8802
Decem 49753.77 49361.76
ber 9818 7969
Next steps: Generate code with monthvise  View recommended plots New interactive sheet

 Since we had data till 24-08-2021 in Months after August its showing NaN

fig = go.Figure()

fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
Monthwise comparision between Stock open and close price

Stock Open Price


60k
Stock Close Price

50k

40k

30k

20k

10k

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov

y_2021.groupby(y_2021['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2021.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2021.groupby(y_2021['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)

fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
Monthwise High and Low stock price

70k Stock high Price


Stock low Price

60k

50k

40k

30k

20k

10k

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2021, x=y_2021.Date, y=[y_2021['Open'], y_2021['Close'],


y_2021['High'], y_2021['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()
Stock analysis chart

70k
Stock Parameters
Stock Open Price
65k
Stock Close Price
Stock High Price
60k
Stock Low Price
alue55k
Stock v
50k

45k

40k

35k

30k

Jan 2021 Apr 2021 Jul 2021 Oct 2021

Date

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_2022 = maindf.loc[(maindf['Date'] >=


'2022-01-01') &
(maindf['Date'] < '2022-02-19')]
y_2022.drop(y_2015[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

2663 2022-01-01 46311.746094 47827.312500 46288.484375 47686.812500

2664 47680.9257 47881.4062 46856.9375 47345.2187


2022 81 50 00 50
-01-
02
2665 47343.5429 47510.7265 45835.9648 46458.1171
2022 69 63 44 88
-01-
03
2666 46458.8515 47406.5468 45752.4648 45897.5742
2022 63 75 44 19
-01-
04
2667 45899.3593 46929.0468 42798.2226 43569.0039
2022 75 75 56 06
-01-
05
2668 43565.5117 43748.7187 42645.5390 43160.9296
2022 19 50 63 88
-01-
06
2669 43153.5703 43153.5703 41077.4453 41557.9023
2022 13 13 13 44
-01-
07
2670 41561.4648 42228.9414 40672.2773 41733.9414
2022 44 06 44 06
-01-
08
2671 41734.7265 42663.9492 41338.1601 41911.6015
2022 63 19 56 63
-01-
09
2672 41910.2304 42199.4843 39796.5703 41821.2617
2022 69 75 13 19
-01-
10
2673 41819.5078 43001.1562 41407.7539 42735.8554
2022 13 50 06 69
-01-
11
2674 42742.1796 44135.3671 42528.9882 43949.1015
2022 88 88 81 63
-01-
12
2675 43946.7421 44278.4218 42447.0429 42591.5703
2022 88 75 69 13
-01-
13
267642598.8710 43346.6875 41982.6171 43099.6992
2022 94 00 88 19
-01-
14
2677 43101.8984 43724.6718 42669.0351 43177.3984
2022 38 75 56 38
-01-
15
2678 43172.0390 43436.8085 42691.0234 43113.8789
2022 63 94 38 06
-01-
16
2679 43118.1210 43179.3906 41680.3203 42250.5507
2022 94 25 13 81
-01-
17
2680 42250.0742 42534.4023 41392.2148 42375.6328
2022 19 44 44 13
-01-
18
2681 42374.0390 42478.3046 41242.9140 41744.3281
2022 63 88 63 25
-01-
19
2682 41744.0273 43413.0234 40672.8242 40680.4179
2022 44 38 19 69
-01-
20
2683 40699.6054 41060.5273 35791.4257 36457.3164
2022 69 44 81 06
-01-
21
2684 36471.5898 36688.8125 34349.2500 35030.2500
2022 44 00 00 00
-01-
22
2685 35047.3593 36433.3125 34784.9687 36276.8046
2022 75 00 50 88
-01-
23
2686 36275.7343 37247.5195 33184.0585 36654.3281
2022 75 31 94 25
-01-
24
2687 36654.8046 37444.5703 35779.4296 36954.0039
2022 88 13 88 06
-01-
25
2688 36950.5156 38825.4101 36374.9062 36852.1210
2022 25 56 50 94
-01-
26
2689 36841.8789 37148.3242 35629.2812 37138.2343
2022 06 19 50 75
-01-
27
2690 37128.4453 37952.8789 36211.1093 37784.3320
2022 13 06 75 31
-01-
28
2691 37780.7148 38576.2617 37406.4726 38138.1796
2022 44 19 56 88
-01-
29
2692 38151.9179 38266.3398 37437.7109 37917.6015
2022 69 44 38 63
-01-
30
2693 37920.2812 38647.2617 36733.5742 38483.1250
2022 50 19 19 00
-01-
31
2694 38481.7656 39115.1328 38113.6640 38743.2734
2022 25 13 63 38
-02-
01
2695 38743.7148 38834.6171 36832.7304 36952.9843
2022 44 88 69 75
-02-
02
2696 36944.8046 37154.6015 36375.5390 37154.6015
2022 88 63 63 63
-02-
03
2697 37149.2656 41527.7851 37093.6289 41500.8750
2022 25 56 06 00
-02-
04
2698 41501.4804 41847.1640 41038.0976 41441.1640
2022 69 63 56 63
-02-
05
2699 41441.1210 42500.7851 41244.9062 42412.4335
2022 94 56 50 94
-02-
06
2700 42406.7812 44401.8632 41748.1562 43840.2851
2022 50 81 50 56
-02-
07
2701 43854.6523 45293.8671 42807.8359 44118.4453
2022 44 88 38 13
-02-
08
2702 44096.7031 44727.8007 43232.9687 44338.7968
2022 25 81 50 75
-02-
09
2703 44347.8007 45661.1718 43402.8085 43565.1132
2022 81 75 94 81
-02-
10
2704 43571.1289 43810.8320 42114.5390 42407.9375
2022 06 31 63 00
-02-
11
2705 42412.3007 42992.5507 41852.5742 42244.4687
2022 81 81 19 50
-02-
12
2706 42236.5664 42693.0546 41950.9414 42197.5156
2022 06 88 06 25
-02-
13
2707 42157.3984 42775.7773 41681.9570 42586.9179
2022 38 44 31 69
-02-
14
2708 42586.4648 44667.2187 42491.0351 44575.2031
2022 44 50 56 25
-02-
15
2709 44578.2773 44578.2773 43456.6914 43961.8593
2022 44 44 06 75
-02-
16
2710 43937.0703 44132.9726 40249.3710 40538.0117
2022 13 56 94 19
-02-
17
2711 40552.1328 40929.1523 39637.6171 40030.9765
2022 13 44 88 63
-02-
18
monthvise= y_2022.groupby(y_2022['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise

Open Close

Date

January 41368.073463 41114.422379

February 41722.190538 41811.714627

March NaN NaN


April NaN NaN
May NaN NaN
June NaN NaN
July NaN NaN
August NaN NaN
September NaN NaN
October NaN NaN
November NaN NaN
December NaN NaN
Next steps: Generate code with monthvise  View recommended plots New interactive sheet

fig = go.Figure()

fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
Monthwise comparision between Stock open and close price

Stock Open Price


40k Stock Close Price

35k

30k

25k

20k

15k

10k

5k

January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov

y_2022.groupby(y_2022['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2022.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)

monthvise_low = y_2022.groupby(y_2022['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)

fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))

fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
Monthwise High and Low stock price
50k
Stock high Price
Stock low Price

40k

30k

20k

10k

0
January
February
MarchApril Ma June July August
September
October
No December
y vember

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_2022, x=y_2022.Date, y=[y_2022['Open'], y_2022['Close'],


y_2022['High'], y_2022['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()

Stock analysis chart


48k Stock Parameters
Stock Open Price
46k Stock Close Price
Stock High Price
44k Stock Low Price
alue
42kv
Stock

40k

38k

36k

34k

Jan 2 Jan 16 Jan 30 Feb 13


2022

Date 

Overall Analysis from 2014-2022

maindf['Date'] = pd.to_datetime(maindf['Date'], format='%Y-%m-%d')

y_overall = maindf.loc[(maindf['Date'] >=


'2014-09-17') & (maindf['Date']
<= '2022-02-19')]
y_overall.drop(y_overall[['Adj
Close','Volume']],axis=1)
Date Open High Low Close

0 2014-09-17 465.864014 468.174011 452.421997 457.334015

1 2014- 456.85998 456.85998 413.10400 424.44000


09-18 5 5 4 2
2 2014- 424.10299 427.83499 384.53201 394.79599
09-19 7 1 3 0
3 2014- 394.67300 423.29599 389.88299 408.90399
09-20 4 0 6 2
4 2014- 408.08499 412.42599 393.18100 398.82101
09-21 1 5 0 4
... . ... ... ... ...
.
.
270 2022- 42586.464 44667.218 42491.035 44575.203
8 02-15 844 750 156 125
270 2022- 44578.277 44578.277 43456.691 43961.859
9 02-16 344 344 406 375
271 2022- 43937.070 44132.972 40249.371 40538.011
0 02-17 313 656 094 719
271 2022- 40552.132 40929.152 39637.617 40030.976
1 02-18 813 344 188 563
271 2022- 40022.132 40246.027 40010.867 40126.429
2 02-19 813 344 188 688
2713 rows × 5 columns

monthvise= y_overall.groupby(y_overall['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close

Date

January 12855.131425 12828.374881

February 12773.077824 12837.802432

Marc 10918.89 10957.22


h 5761 6324
April 11338.44 11359.96
8900 2198
May 10659.45 10580.20
5257 9317
June 9299.305 9294.420
977 703
July 9285.402 9330.128
500 271
Augus 11312.97 11345.15
t 1706 7739
Septem 10489.36 10462.37
ber 5578 8150
Octob 11321.57 11416.07
er 8327 7925
Novem 12542.36 12537.44
ber 2183 1752
Decem 12391.97 12391.98
ber 5010 8926
Next steps: Generate code with monthvise  View recommended plots New interactive sheet

names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])

fig = px.line(y_overall, x=y_overall.Date, y=[y_overall['Open'], y_overall['Close'],


y_overall['High'], y_overall['Low']], labels={'Date': 'Date','value':'Stock value'})
fig.update_layout(title_text='Stock analysis chart', font_size=15,
font_color='black',legend_title_text='Stock Parameters') fig.for_each_trace(lambda t:
t.update(name = next(names))) fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()
Stock analysis chart

70k Stock Parameters


Stock Open Price
60k Stock Close Price
Stock High Price
50k Stock Low Price
alue
Stock
40kv

30k

20k

10k

0
2016 2018 2020 2022

Date

5. Building LSTM Model

 First Step is Preparing Data for Training and Testing

Here we are just considering 1 year data for training data


Since Bitcoin price has drastically ucated from 200 dollar in year 2014 to 15000 dollar in year 2018 to
3000 dollar in year 2019(theses values are apporx) so we will just consider 1 Year to avoid this type of
ucation in the data.
As we want to predict Close Price of the Bitcoin so we are just Considering Close aand Date
# Lets First Take all the Close Price
closedf = maindf[['Date','Close']]
print("Shape of close dataframe:", closedf.shape)
Shape of close dataframe: (2713, 2)

fig = px.line(closedf, x=closedf.Date, y=closedf.Close,labels={'date':'Date','close':'Close


Stock'}) fig.update_traces(marker_line_width=2, opacity=0.8,
marker_line_color='orange') fig.update_layout(title_text='Whole period of timeframe of
Bitcoin close price 2014-2022', plot_bgcolor='white', font_size=15,
font_color='black') fig.update_xaxes(showgrid=False) fig.update_yaxes(showgrid=False)
fig.show()
Whole period of timeframe of Bitcoin close price 2014-2022
70k

60k

50k

Close
40k

30k

20k

10k

0
2016 2018 2020 2022

Date

 Now we will Take data of just 1 Year

closedf = closedf[closedf['Date'] > '2021-02-


19'] close_stock = closedf.copy()
print("Total data for prediction: ",closedf.shape[0])
Total data for prediction: 365
closedf

Date Close

2348 2021-02-20 56099.519531

2349 2021-02-21 57539.945313

2350 2021-02-22 54207.320313

235 2021- 48824.4257


1 02-23 81
235 2021- 49705.3320
2 02-24 31
... . ...
.
.
270 2022- 44575.2031
8 02-15 25
270 2022- 43961.8593
9 02-16 75
271 2022- 40538.0117
0 02-17 19
271 2022- 40030.9765
1 02-18 63
271 2022- 40126.4296
2 02-19 88
365 rows × 2 columns
Next steps: Generate code with closedf  View recommended plots New interactive sheet

fig = px.line(closedf, x=closedf.Date,


y=closedf.Close,labels={'date':'Date','close':'Close Stock'})
fig.update_traces(marker_line_width=2, opacity=0.8,
marker_line_color='orange') fig.update_layout(title_text='Considered period to
predict Bitcoin close price', plot_bgcolor='white', font_size=15,
font_color='black') fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False) fig.show()

Considered period to predict Bitcoin close price

65k

60k

55k
Close
50k

45k

40k

35k

30k

Mar 2021 May 2021 Jul 2021 Sep 2021 Nov 2021 Jan 2022

Date

### Normalizing Data

Normalization is a technique often applied as part of data preparation for machine


learning. The goal of normalization is to change the values of numeric columns in the

dataset to use a common scale, without distorting differences in the ranges of values or losing
information.

MinMaxScaler. For each value in a feature, MinMaxScaler subtracts the minimum value in the feature
and then divides by the range. The range is the difference between the original maximum and original
minimum. MinMaxScaler preserves the shape of the original distribution.
# deleting date column and normalizing using MinMax Scaler

del closedf['Date']
scaler=MinMaxScaler(feature_range=(0,1))
closedf=scaler.fit_transform(np.array(closedf).re
shape(-1,1)) print(closedf.shape)
(365, 1)

# we keep the training set as 60% and 40% testing set

training_size=int(len(closedf)*0.60) test_size=len(closedf)-
training_size
train_data,test_data=closedf[0:training_size,:],closedf[training_size:l
en(closedf),:1] print("train_data: ", train_data.shape)
print("test_data: ", test_data.shape)
train_data: (219, 1)
test_data: (146, 1)

Now we Transform the Close price based on Time-series-analysis forecasting requirement , Here we
will take 15


# convert an array of values into a dataset matrix

def create_dataset(dataset, time_step=1):


dataX, dataY = [], [] for i in
range(len(dataset)-time_step-1):
a = dataset[i:(i+time_step), 0] ###i=0, 0,1,2,3-----
99 100 dataX.append(a)
dataY.append(dataset[i + time_step, 0]) return
np.array(dataX), np.array(dataY)

time_step = 15
X_train, y_train = create_dataset(train_data,
time_step) X_test, y_test =
create_dataset(test_data, time_step)

print("X_train: ", X_train.shape)


print("y_train: ", y_train.shape)
print("X_test: ", X_test.shape)
print("y_test", y_test.shape)
X_train: (203,
15) y_train:
(203,) X_test:
(130, 15) y_test
(130,)

# reshape input to be [samples, time steps, features] which is required for LSTM
X_train
=X_train.reshape(X_train.shape[0],X_train.shape[1] ,
1) X_test =
X_test.reshape(X_test.shape[0],X_test.shape[1] , 1)

print("X_train: ", X_train.shape)


print("X_test: ", X_test.shape)
X_train: (203, 15, 1)
X_test: (130, 15, 1)

 Actuall Model Building

model=Sequential()

model.add(LSTM(10,input_shape=(None,1),activation="rel

u")) model.add(Dense(1))

model.compile(loss="mean_squared_error",optimizer="ada

m")

/usr/local/lib/python3.10/dist-packages/keras/src/layers/rnn/rnn.py:204: UserWarning:
Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer
using an `Input(shape)` object as
 

history =
model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=200,batch_size=32,verbose=1)

You might also like