-
Notifications
You must be signed in to change notification settings - Fork 2
/
bollinger_bands_&_keltner_channels.py
99 lines (76 loc) · 3.31 KB
/
bollinger_bands_&_keltner_channels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""Bollinger Bands & Keltner Channels
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1n2Ts70zGUvLZXjRiRYgzHKHh0-JJaDVl
"""
!pip install yfinance
!pip install ta
#Installing yahoo finance libraries for data collection
import yfinance as yf
import ta
import pandas as pd
from datetime import date, timedelta, datetime
from IPython.display import clear_output
!pip3 install numpy==1.20.0
!pip3 install pandas==1.1.4
!pip3 install pandas-datareader==0.9.0
!pip3 install matplotlib==3.3.3
!pip3 install --upgrade pandas
!pip3 install --upgrade pandas-datareader
#https://www.learnpythonwithrune.org/pandas-calculate-and-plot-the-bollinger-bands-for-a-stock/
import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt
ticker = pdr.get_data_yahoo("HDFCBANK.NS", dt.datetime(2018, 4, 1), dt.datetime(2019,3,31))[['Close', 'High', 'Low']]
# Boillinger band calculations
ticker['TP'] = (ticker['Close'] + ticker['Low'] + ticker['High'])/3
ticker['std'] = ticker['TP'].rolling(20).std(ddof=0)
ticker['MA-TP'] = ticker['TP'].rolling(20).mean()
#Using Simple moving average filter->20
ticker['BOLU'] = ticker['MA-TP'] + 2*ticker['std']
#Upper Bollinger Band
ticker['BOLD'] = ticker['MA-TP'] - 2*ticker['std']
#Lower Bollinger Band
ticker = ticker.dropna()
ax = ticker[['Close', 'BOLU', 'BOLD']].plot(color=['blue', 'orange', 'yellow'])
ax.fill_between(ticker.index, ticker['BOLD'], ticker['BOLU'], facecolor='orange', alpha=0.1)
plt.show()
#https://www.learnpythonwithrune.org/pandas-calculate-and-plot-the-bollinger-bands-for-a-stock/
import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt
ticker = pdr.get_data_yahoo("INFY.NS", dt.datetime(2018, 4, 1), dt.datetime(2019,3,31))[['Close', 'High', 'Low']]
# Boillinger band calculations
ticker['TP'] = (ticker['Close'] + ticker['Low'] + ticker['High'])/3
ticker['std'] = ticker['TP'].rolling(20).std(ddof=0)
ticker['MA-TP'] = ticker['TP'].rolling(20).mean()
#Using Simple moving average filter->20
ticker['BOLU'] = ticker['MA-TP'] + 2*ticker['std']
#Upper Bollinger Band
ticker['BOLD'] = ticker['MA-TP'] - 2*ticker['std']
#Lower Bollinger Band
ticker = ticker.dropna()
print(ticker)
ax = ticker[['Close', 'BOLU', 'BOLD']].plot(color=['blue', 'orange', 'yellow'])
ax.fill_between(ticker.index, ticker['BOLD'], ticker['BOLU'], facecolor='orange', alpha=0.1)
plt.show()
#https://www.learnpythonwithrune.org/pandas-calculate-and-plot-the-bollinger-bands-for-a-stock/
import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt
ticker = pdr.get_data_yahoo("TATAMOTORS.NS", dt.datetime(2018, 4, 1), dt.datetime(2019,3,31))[['Close', 'High', 'Low']]
# Boillinger band calculations
ticker['TP'] = (ticker['Close'] + ticker['Low'] + ticker['High'])/3
ticker['std'] = ticker['TP'].rolling(20).std(ddof=0)
ticker['MA-TP'] = ticker['TP'].rolling(20).mean()
#Using Simple moving average filter->20
ticker['BOLU'] = ticker['MA-TP'] + 2*ticker['std']
#Upper Bollinger Band
ticker['BOLD'] = ticker['MA-TP'] - 2*ticker['std']
#Lower Bollinger Band
ticker = ticker.dropna()
print(ticker)
ax = ticker[['Close', 'BOLU', 'BOLD']].plot(color=['blue', 'orange', 'yellow'])
ax.fill_between(ticker.index, ticker['BOLD'], ticker['BOLU'], facecolor='orange', alpha=0.1)
plt.show()