使用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)文章
68行Python代碼實(shí)現(xiàn)帶難度升級(jí)的貪吃蛇
本文主要介紹了Python代碼實(shí)現(xiàn)帶難度升級(jí)的貪吃蛇,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01基于python和flask實(shí)現(xiàn)http接口過(guò)程解析
這篇文章主要介紹了基于python和flask實(shí)現(xiàn)http接口過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python 實(shí)現(xiàn)"神經(jīng)衰弱"翻牌游戲
這篇文章主要介紹了python 實(shí)現(xiàn)"神經(jīng)衰弱"游戲,幫助大家更好的理解和使用python的pygame庫(kù),感興趣的朋友可以了解下2020-11-11python如何求100以?xún)?nèi)的素?cái)?shù)
在本篇文章里小編給大家分享的是關(guān)于python如何求100以?xún)?nèi)的素?cái)?shù)的方法實(shí)例,需要的朋友們可以學(xué)習(xí)下。2020-05-05python?tornado協(xié)程調(diào)度原理示例解析
這篇文章主要為大家介紹了python?tornado協(xié)程調(diào)度原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09