Python作為一門高效且易用的編程語言,被廣泛應(yīng)用于人工智能、大數(shù)據(jù)分析和Web開發(fā)等領(lǐng)域。在投資領(lǐng)域中,Python也被用于實時監(jiān)控股票行情。
import yfinance as yf import tkinter as tk from tkinter import ttk # 創(chuàng)建股票監(jiān)控應(yīng)用窗口 window = tk.Tk() window.title("股票監(jiān)控器") window.geometry("600x400") window.resizable(False, False) # 創(chuàng)建列表控件并添加表頭 tree = ttk.Treeview(window, columns=("symbol", "price", "change", "volume")) tree.heading("symbol", text="股票代碼") tree.heading("price", text="當(dāng)前價") tree.heading("change", text="漲跌幅") tree.heading("volume", text="成交量") tree.pack(side=tk.TOP, fill=tk.BOTH) # 更新列表控件中的股票信息 def update_stock_info(): for child in tree.get_children(): tree.delete(child) symbols = ["AAPL", "MSFT", "AMZN", "GOOG", "FB"] for symbol in symbols: stock = yf.Ticker(symbol) curr_price = stock.info["regularMarketPrice"] prev_close = stock.info["regularMarketPreviousClose"] change = (curr_price - prev_close) / prev_close * 100 volume = stock.info["regularMarketVolume"] tree.insert("", tk.END, values=(symbol, curr_price, f"{change:.2f}%", volume)) window.after(5000, update_stock_info) # 啟動股票監(jiān)控應(yīng)用 update_stock_info() window.mainloop()
以上代碼使用yfinance庫獲取股票行情信息,并使用tkinter庫創(chuàng)建GUI界面展示股票的實時數(shù)據(jù)。每隔5秒鐘,程序會自動更新GUI界面中的股票行情信息。
通過Python實現(xiàn)的股票監(jiān)控工具方便了投資者及時掌握股票的實時情況,且方便程度高效快捷,非常適合股民、分析師等投資研究人員使用。