前言
手里有一點(diǎn)點(diǎn)公司的股票, 拿不準(zhǔn)在什么時(shí)機(jī)拋售, 程序員也沒時(shí)間天天盯著看,不如動(dòng)手寫個(gè)小程序, 把股票趨勢(shì)每天早上發(fā)到郵箱里,用 python 的 pandas, matplotlib 寫起來很容易, 幾十行代碼搞定。
準(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:開盤價(jià), close: 收盤價(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
看來最近股價(jià)漲勢(shì)不錯(cuò)。
再看看其他公司
Baidu
baidu
Netease
netease
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
