我正在尝试绘制一些财务图表,并且使用了图库。
我对此很陌生,因此不得不寻找一种方法来做,并且想离线进行,所以我找到了这种方法:
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
from pandas_datareader import data as web
import datetime
import fix_yahoo_finance as yf
import plotly
from plotly import figure_factory as ff
import os
yf.pdr_override()
class Stock:
#We need a constructor to get all the data when we create the class
def __init__(self, name): #This needs a string as argument to work
self.name = name
#If we already have the sotcks data we don't download it again
if os.path.exists('stock_prices_yahoo/' + name + '.csv'):
self.df = pd.read_csv('stock_prices_yahoo/' + name + '.csv')
else:
start_date = '1950-01-01'
end_date = datetime.date.today()
df = web.get_data_yahoo(name, start_date, end_date)
df.to_csv('stock_prices_yahoo/' + name + '.csv')
def plot_stock(self): #This will print a plot of the stock prices
fig = ff.create_candlestick(self.df.Open, self.df.High, self.df.Low, self.df.Close, dates = self.df.Date)
fig['layout'].update({
'title': self.name + ' prices',
'yaxis': {'title': 'Price'},
'xaxis': {'title': 'Date'},
})
plotly.offline.plot(fig, filename = self.name + '-candlestick.html', validate = True)
AAPL = Stock('AAPL')
AAPL.plot_stock()
关键是我想在上面叠加一些移动平均线,并且在整个互联网上寻找它之后,peaple总是通过matplotlib进行操作。 Figure_factory有什么办法吗?