不用matplotlib库
时间: 2023-07-22 20:03:12 浏览: 90
Matplotlib中文字体配置文件(解决中文乱码)
好的,如果不使用matplotlib库,你可以尝试使用其他库来进行绘图,比如Plotly。以下是一个使用Plotly绘制K线图、M日移动平均线和BOLL指标线的示例代码:
```python
import pandas as pd
import plotly.graph_objects as go
# 获取股票数据,假设您有一个名为stock_data的数据框
stock_data = pd.read_csv('stock_data.csv') # 根据您的实际情况修改文件路径和读取方式
# 设置起始日期和结束日期
start_date = '2022-01-01'
end_date = '2022-12-31'
# 根据日期筛选数据
stock_data = stock_data[(stock_data['Date'] >= start_date) & (stock_data['Date'] <= end_date)]
# 计算M日移动平均线
M = 10 # 假设M为10
stock_data['MA'] = stock_data['Close'].rolling(window=M).mean()
# 计算N日标准差
N = 20 # 假设N为20
stock_data['std'] = stock_data['Close'].rolling(window=N).std()
# 计算BOLL指标线
stock_data['mid'] = stock_data['MA']
stock_data['upper'] = stock_data['mid'] + 2 * stock_data['std']
stock_data['lower'] = stock_data['mid'] - 2 * stock_data['std']
# 绘制K线图和指标线
fig = go.Figure(data=[go.Candlestick(x=stock_data['Date'],
open=stock_data['Open'],
high=stock_data['High'],
low=stock_data['Low'],
close=stock_data['Close'])])
fig.add_trace(go.Scatter(x=stock_data['Date'], y=stock_data['MA'],
name='MA', line=dict(color='orange')))
fig.add_trace(go.Scatter(x=stock_data['Date'], y=stock_data['upper'],
name='Upper BOLL', line=dict(color='red')))
fig.add_trace(go.Scatter(x=stock_data['Date'], y=stock_data['lower'],
name='Lower BOLL', line=dict(color='green')))
fig.update_layout(title='Stock K-line and BOLL Indicator',
xaxis_title='Date',
yaxis_title='Price')
fig.show()
```
请注意,上述代码中的数据获取、文件路径和数据处理部分需要根据您的实际情况进行修改。此外,您需要安装plotly库来运行此示例代码。
阅读全文