大家好!今天给你们带来了简单Python股市预测工具,仅供学习,请勿实用!
安装:
pip install baostock``pip install numpy``pip install statsmodels
完整实例:
import baostock as bs``import numpy as np``from statsmodels.tsa.statespace.sarimax import SARIMAX``from statsmodels.tsa.seasonal import seasonal_decompose``from datetime import datetime, timedelta`` ``def stock_price_prediction(stock_code, predict_days):` `#登录baostock` `lg = bs.login()`` ` `#设置日期范围` `end_date = datetime.now().strftime("%Y-%m-%d")` `start_date = (datetime.now() - timedelta(days=60)).strftime("%Y-%m-%d")#分析天数`` ` `#查询股票数据` `rs = bs.query_history_k_data(stock_code,` `"date,open,close",` `start_date=start_date,` `end_date=end_date,` `frequency="d",` `adjustflag="3")` `data_list = []` `while rs.next():` `data_list.append(rs.get_row_data())` `data = np.array(data_list)`` ` `#数据处理` `close_prices = data[:, 2].astype(float) #仅使用收盘价` `close_prices_diff = np.diff(close_prices) #计算价格差分`` ` `#季节性分解` `result = seasonal_decompose(close_prices_diff, model='additive', period=7)` `trend = result.trend` `seasonal = result.seasonal` `residual = result.resid`` ` `#根据季节性分解结果设置SARIMAX参数` `#这里假设季节性周期为7天,根据实际情况调整` `seasonal_order = (1, 1, 1, 7)` `#根据残差选择ARIMA参数` `#这里假设ARIMA参数为(1, 1, 1),根据实际情况调整` `order = (1, 1, 1)`` ` `# 建立SARIMAX模型` `model = SARIMAX(trend, order=order, seasonal_order=seasonal_order)` `model_fit = model.fit(disp=False)`` ` `#预测未来价格变动` `forecast = model_fit.forecast(steps=predict_days)`` ` `#将预测的价格变动转换为未来的收盘价` `future_close_prices = close_prices[-1] + forecast.cumsum()`` ` `#打印预测结果` `print("\n未来%d天的股票价格预测:" % predict_days)` `print("==============================")` `print("|{:^14s}|{:^14s}|".format("日期", "收盘价"))` `print("|" + "-" * 14 + "|" + "-" * 14 + "|")` `for i in range(predict_days):` `date_str = (np.datetime64(end_date) + np.timedelta64(i+1, 'D')).astype(str)` `close_price_str = "%.2f" % future_close_prices[i]` `print("|{:^14s}|{:^14s}|".format(date_str, close_price_str))` `print("==============================")`` ` `#显示历史数据` `display_data(data)`` ` `#退出baostock` `bs.logout()`` ``def display_data(data):` `print("\n获取到的历史股票价格数据:")` `print("=" * 50)` `print("|{:^14s}|{:^14s}|{:^14s}|".format("日期", "开盘价", "收盘价"))` `print("|" + "-" * 14 + "|" + "-" * 14 + "|" + "-" * 14 + "|")` `for row in data:` `print("|{:^14s}|{:^14s}|{:^14s}|".format(row[0], row[1], row[2]))` `print("=" * 50)`` ``#预测的股票代码和天数``stock_code = "sh.600519" # 贵州茅台``predict_days = 5 # 预测天数``stock_price_prediction(stock_code, predict_days)
输出:
login success!`` ``未来5天的股票价格预测:``==============================``| 日期 | 收盘价 |``|--------------|--------------|``| 2024-04-15 | 1624.04 |``| 2024-04-16 | 1614.54 |``| 2024-04-17 | 1604.57 |``| 2024-04-18 | 1595.40 |``| 2024-04-19 | 1586.41 |``==============================`` ``获取到的历史股票价格数据:``==================================================``| 日期 | 开盘价 | 收盘价 |``|--------------|--------------|--------------|``| 2024-02-19 | 1737.8700 | 1695.4300 |``| 2024-02-20 | 1697.3600 | 1670.0000 |``| 2024-02-21 | 1666.0200 | 1717.6600 |``| 2024-02-22 | 1707.5000 | 1718.3800 |``| 2024-02-23 | 1711.1100 | 1714.0900 |``| 2024-02-26 | 1708.0000 | 1694.9800 |``| 2024-02-27 | 1683.0000 | 1689.5000 |``| 2024-02-28 | 1688.9200 | 1681.5500 |``| 2024-02-29 | 1681.5500 | 1695.0000 |``| 2024-03-01 | 1690.0000 | 1685.0600 |``| 2024-03-04 | 1679.0000 | 1676.3000 |``| 2024-03-05 | 1670.8100 | 1695.1000 |``| 2024-03-06 | 1691.0000 | 1680.5500 |``| 2024-03-07 | 1680.0000 | 1683.6300 |``| 2024-03-08 | 1680.5600 | 1671.4300 |``| 2024-03-11 | 1675.0000 | 1694.4900 |``| 2024-03-12 | 1693.9400 | 1745.0000 |``| 2024-03-13 | 1739.9700 | 1727.4500 |``| 2024-03-14 | 1719.0000 | 1716.6300 |``| 2024-03-15 | 1712.5000 | 1717.4900 |``| 2024-03-18 | 1715.0000 | 1708.2600 |``| 2024-03-19 | 1703.0000 | 1708.0200 |``| 2024-03-20 | 1703.3600 | 1709.6000 |``| 2024-03-21 | 1708.1000 | 1709.0000 |``| 2024-03-22 | 1705.0300 | 1701.3500 |``| 2024-03-25 | 1698.5400 | 1689.0100 |``| 2024-03-26 | 1687.4600 | 1709.2900 |``| 2024-03-27 | 1705.0000 | 1701.0000 |``| 2024-03-28 | 1695.0000 | 1701.6400 |``| 2024-03-29 | 1701.6400 | 1702.9000 |``| 2024-04-01 | 1709.9900 | 1721.3300 |``| 2024-04-02 | 1722.0000 | 1713.9900 |``| 2024-04-03 | 1734.0000 | 1715.1100 |``| 2024-04-08 | 1695.0000 | 1666.6600 |``| 2024-04-09 | 1660.6700 | 1662.2200 |``| 2024-04-10 | 1662.2700 | 1647.9800 |``| 2024-04-11 | 1640.1100 | 1648.0000 |``| 2024-04-12 | 1651.0000 | 1634.0300 |``==================================================``logout success!
为了帮助大家更好的学习网络安全,我给大家准备了一份网络安全入门/进阶学习资料,里面的内容都是适合零基础小白的笔记和资料,不懂编程也能听懂、看懂这些资料!
因篇幅有限,仅展示部分资料,需要点击下方链接即可前往获取
CSDN大礼包:《黑客&网络安全入门&进阶学习资源包》免费分享
因篇幅有限,仅展示部分资料,需要点击下方链接即可前往获取