使用tushare库,用同花顺对股价进行获取·,然后进行小波变换的去噪处理,将生成两条曲线进行对比,直接给出代码
时间: 2023-12-10 16:40:20 浏览: 39
以下是使用tushare库获取股票数据并进行小波变换去噪处理的示例代码:
```python
import tushare as ts
import pywt
import numpy as np
import matplotlib.pyplot as plt
# 获取股票数据
pro = ts.pro_api()
df = pro.daily(ts_code='000001.SZ', start_date='20200101', end_date='20210101').sort_values('trade_date')
# 将收盘价取出来
close_price = df['close'].values
# 进行小波变换去噪处理
coeffs = pywt.wavedec(close_price, 'db4', level=6)
threshold = np.std(coeffs[-1]) * np.sqrt(2*np.log(len(close_price)))
coeffs[1:] = (pywt.threshold(i, value=threshold, mode="soft" ) for i in coeffs[1:])
reconstructed_close_price = pywt.waverec(coeffs, 'db4')
# 绘制原始收盘价与去噪后的收盘价曲线对比
plt.figure(figsize=(10, 5))
plt.plot(close_price, label='Original')
plt.plot(reconstructed_close_price, label='Denoised')
plt.legend()
plt.show()
```
该示例代码中,我们使用了tushare库获取了上证指数(股票代码为000001.SZ)在2020年1月1日至2021年1月1日期间的每日收盘价数据,并将其存储在一个pandas的DataFrame对象中。然后,我们将收盘价取出来,使用pywt库进行小波变换去噪处理,最后将原始收盘价与去噪后的收盘价曲线进行对比。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)