Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TradingBot/bollinger_bands.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
33 lines (24 sloc)
1.15 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Functionality for Bollinger Bands Technical Indicator """ | |
import yfinance as yf | |
# Function returns bollinger band values. Takes security and trading interval as parameters | |
def bollinger_bands(ticker, interval): | |
# 1m interval max 7d period on API call | |
if (interval in ('1m', '2m', '5m', '10m', '15m')): | |
data_period = '7d' | |
else: | |
data_period = '30d' | |
# security to be used | |
symbol = ticker | |
# Use yfinance to retrieve the stock data and load it into a pandas DataFrame | |
df = yf.download(symbol, interval=interval, period=data_period, progress=False) | |
# Calculate the 20-day moving average and standard deviation using the rolling() method | |
rolling_mean = df['Close'].rolling(window=20).mean() | |
rolling_std = df['Close'].rolling(window=20).std() | |
# Calculate the upper and lower bands using the extracted data | |
upper_band = rolling_mean + (2 * rolling_std) | |
lower_band = rolling_mean - (2 * rolling_std) | |
# Get the latest upper and lower band values | |
current_lb = lower_band.iloc[-1] | |
current_ub = upper_band.iloc[-1] | |
# return the current lower and upper band values | |
return current_ub, current_lb |