Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

python - How to get data from DataFrame/Matplot

I've been trying to learn Python as of late and have began a personal project that essentially is a simple stock trader bot. I'm trying to implement a simple Bollinger Band want to get the Bollinger Bands' values when it is the current date. I currently have run into an issue of trying to pull the stock price (y-value) from the two plots (orange and green) when I have the x-value (current date). I was looking for ways to do this but most implement numpy as the points are already in an x-value list and a y-value list.

Any help is appreciated! (Attached below is the graph and the code for printing the graphs)

[Graphs] enter image description here

[Code]

enter image description here

question from:https://stackoverflow.com/questions/65713508/how-to-get-data-from-dataframe-matplot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Python has a library that provides a rich set of features as technical indicators. The official reference can be found here The library also calculates Bollinger Bands, and returns three arrays of results that can be plotted on a graph to get what you want.

# !pip install yfinance
# !wget https://launchpad.net/~mario-mariomedina/+archive/ubuntu/talib/+files/libta-lib0_0.4.0-oneiric1_amd64.deb -qO libta.deb
# !wget https://launchpad.net/~mario-mariomedina/+archive/ubuntu/talib/+files/ta-lib0-dev_0.4.0-oneiric1_amd64.deb -qO ta.deb
# !dpkg -i libta.deb ta.deb
# !pip install ta-lib
import talib
import yfinance as yf
import datetime
import pandas as pd
import numpy as np

begin = datetime.datetime.today() - datetime.timedelta(days=100)
finish = datetime.datetime.today()
stockinfo = yf.download("AAPL", start=begin, end=finish)

stockinfo['20 Day MA'] = stockinfo['Adj Close'].rolling(window=20).mean()
stockinfo['20 Day STD'] = stockinfo['Adj Close'].rolling(window=20).std()
stockinfo['Upper_bband'] = stockinfo['20 Day MA'] + (stockinfo['20 Day STD'] * 1.5)
stockinfo['Lower_bband'] = stockinfo['20 Day MA'] - (stockinfo['20 Day STD'] * 1.5)

bbands = talib.BBANDS(np.array(stockinfo['Adj Close']))

import matplotlib.pyplot as plt

plt.figure(figsize=(12.5, 5))
plt.plot(stockinfo.index, stockinfo['20 Day MA'], label='20 Day MA')
plt.plot(stockinfo.index, stockinfo['Upper_bband'], label='upper_bband')

plt.plot(stockinfo.index, stockinfo['Lower_bband'], label='lower_bband')
plt.plot(stockinfo.index, bbands[0], label='bband1')
plt.plot(stockinfo.index, bbands[1], label='bband2')
plt.plot(stockinfo.index, bbands[2], label='bband3')

plt.title('AAPL: 20 Day Bollinger Band')
plt.xlabel(begin.strftime('%Y-%m-%d') + '-' + finish.strftime('%Y-%m-%d'))
plt.ylabel('Price (in USD)')
plt.legend(loc='upper left')

plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...