py画一个随机生成的股票三线图
时间: 2024-11-24 20:43:43 浏览: 16
Python 的 Matplotlib 库是一个强大的绘图工具,你可以使用它来绘制股票三线图,通常包括均线、开盘价线和收盘价线。以下是一个简单的例子:
```python
import matplotlib.pyplot as plt
import numpy as np
# 模拟一些数据
np.random.seed(0) # 设置随机种子保证结果可复现
days = 100
open_prices = np.random.rand(days)
close_prices = open_prices + np.random.randn(days) * 0.5
high_prices = close_prices + np.random.randn(days) * 0.2
low_prices = close_prices - np.random.randn(days) * 0.2
# 计算简单移动平均线
short_window = 5
medium_window = 20
long_window = 60
short_ma = close_prices.rolling(window=short_window).mean()
medium_ma = close_prices.rolling(window=medium_window).mean()
long_ma = close_prices.rolling(window=long_window).mean()
# 绘制图形
plt.figure(figsize=(10,6))
plt.plot(close_prices, label='Close Price')
plt.plot(high_prices, label='High Price')
plt.plot(low_prices, label='Low Price')
plt.plot(short_ma, label=f'Short MA ({short_window} days)', color='green')
plt.plot(medium_ma, label=f'Medium MA ({medium_window} days)', color='orange')
plt.plot(long_ma, label=f'Long MA ({long_window} days)', color='red')
plt.fill_between(range(len(open_prices)), high_prices, low_prices, alpha=0.3)
plt.title('Random Stock Chart with Moving Averages')
plt.xlabel('Days')
plt.ylabel('Price')
plt.legend()
plt.show()
阅读全文