使用python的pandas為你的股票繪制趨勢(shì)圖
前言
手里有一點(diǎn)點(diǎn)公司的股票, 拿不準(zhǔn)在什么時(shí)機(jī)拋售, 程序員也沒(méi)時(shí)間天天盯著看,不如動(dòng)手寫(xiě)個(gè)小程序, 把股票趨勢(shì)每天早上發(fā)到郵箱里,用 python 的 pandas, matplotlib 寫(xiě)起來(lái)很容易, 幾十行代碼搞定。
準(zhǔn)備環(huán)境
python3 -m venv venv source ./venv/bin/activate pip install pandas pip install pandas_datareader pip install matplotlib
代碼如下
繪制 2019 年到今天2019-02-15 的我司 ( Cisco ) 的股票趨勢(shì) ( open:開(kāi)盤(pán)價(jià), close: 收盤(pán)價(jià), high 最高價(jià):, low: 最低價(jià),單位為美元)
$ vi stock.py
import matplotlib.pyplot as plt
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
import pandas_datareader.data as web
import matplotlib
import time
import matplotlib.pyplot as plt
import argparse
def drawStockTrend(inc, startDate, endDate, pngFile):
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(18.5, 10.5)
df = web.DataReader(name=inc, data_source='iex', start=startDate, end=endDate)
print(df)
plt.style.use('seaborn-whitegrid')
plt.xticks(rotation=30)
plt.plot(df.index, df['open'], label='open', marker='o', linestyle=':', linewidth=1, markersize=3, color='gray')
plt.plot(df.index, df['high'], label='high', marker='o', linestyle=':', linewidth=1, markersize=3, color='green')
plt.plot(df.index, df['low'], label='low', marker='o', linestyle=':', linewidth=1, markersize=3, color='blue')
plt.plot(df.index, df['close'], label='close', marker='o', linestyle='-', linewidth=2, markersize=6, color='red')
for x, y in zip(df.index, df['close']):
plt.text(x, y + 0.3, '%.2f' % y, ha='center', va='bottom', color='red')
plt.legend()
plt.title("%s' stock trend" % company)
plt.show(block=True)
time.sleep(1)
if(not pngFile):
fig.savefig(pngFile)
plt.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-c', action='store', dest='company', help='specify company')
parser.add_argument('-s', action='store', dest='start', help='specify start date')
parser.add_argument('-e', action='store', dest='end', help='specify end date')
parser.add_argument('-f', action='store', dest='file', help='specify the filename')
args = parser.parse_args()
company = 'CSCO'
startDate = '2019-01-01'
endDate = '2019-02-19'
pngFile = None
if(args.company):
company = args.company
if (args.start):
startDate = args.start
if (args.end):
endDate = args.end
if (args.file):
pngFile = args.file
drawStockTrend(company, startDate, endDate, pngFile)
#example
# python stock.py -c GOOGL -s 2019-01-01 -e 2019-02-19 -f google_stock_trend.png
# python stock.py -c CSCO -s 2019-01-01 -e 2019-02-19 -f cisco_stock_trend.png
# python stock.py -c SINA -s 2019-01-01 -e 2019-02-19 -f sina_stock_trend.png
# python stock.py -c BIDU -s 2019-01-01 -e 2019-02-19 -f baidu_stock_trend.png
# python stock.py -c NTES -s 2019-01-01 -e 2019-02-19 -f netease_stock_trend.png
運(yùn)行命令如下
python stock.py -c CSCO -s 2019-01-01 -e 2019-02-19 -f cisco_stock_trend.png
圖表如下
cisco

cisco
看來(lái)最近股價(jià)漲勢(shì)不錯(cuò)。
再看看其他公司

Baidu

baidu
Netease

netease
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python用線性回歸預(yù)測(cè)股票價(jià)格的實(shí)現(xiàn)代碼
- Python繪制股票移動(dòng)均線的實(shí)例
- python多線程+代理池爬取天天基金網(wǎng)、股票數(shù)據(jù)過(guò)程解析
- 用Python徒手?jǐn)]一個(gè)股票回測(cè)框架搭建【推薦】
- python利用re,bs4,requests模塊獲取股票數(shù)據(jù)
- python買(mǎi)賣(mài)股票的最佳時(shí)機(jī)(基于貪心/蠻力算法)
- 使用Python畫(huà)股票的K線圖的方法步驟
- 利用python numpy+matplotlib繪制股票k線圖的方法
- python3使用pandas獲取股票數(shù)據(jù)的方法
- 使用Python寫(xiě)一個(gè)量化股票提醒系統(tǒng)
- 使用python爬蟲(chóng)實(shí)現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo
- Python爬取股票信息,并可視化數(shù)據(jù)的示例
相關(guān)文章
python實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建類(lèi)的方法分析
這篇文章主要介紹了python實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建類(lèi)的方法,結(jié)合實(shí)例形式分析了Python動(dòng)態(tài)創(chuàng)建類(lèi)的原理、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
使用Python+Matplotlib制作時(shí)序動(dòng)態(tài)圖
時(shí)序圖是一個(gè)二維圖,橫軸表示對(duì)象,縱軸表示時(shí)間,消息在各對(duì)象之間橫向傳遞,依照時(shí)間順序縱向排列,可以直觀的描述并發(fā)進(jìn)程,所以本文就使用Python和Matplotlib制作一個(gè)簡(jiǎn)單的時(shí)許動(dòng)態(tài)圖,感興趣的跟著小編一起來(lái)看看吧2023-07-07
一文詳解PyQt5中實(shí)現(xiàn)不規(guī)則窗口的顯示
這篇文章主要為大家詳細(xì)介紹了Python?PyQt5中實(shí)現(xiàn)不規(guī)則窗口的顯示的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-12-12
Python+flask編寫(xiě)一個(gè)簡(jiǎn)單實(shí)用的自動(dòng)排班系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何基于Python+flask編寫(xiě)一個(gè)簡(jiǎn)單實(shí)用的自動(dòng)排班系統(tǒng),文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2025-03-03
如何解決cmd運(yùn)行python提示不是內(nèi)部命令
在本篇文章里小編給大家整理了關(guān)于如何解決cmd運(yùn)行python提示不是內(nèi)部命令的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2020-07-07
python正則表達(dá)式抓取成語(yǔ)網(wǎng)站
做NLPproject時(shí)需要一個(gè)成語(yǔ)庫(kù),我需要的是純成語(yǔ),網(wǎng)上找的都是有詳細(xì)解釋的。于是自己寫(xiě)了一個(gè)爬成語(yǔ)的python程序2013-11-11

