使用python写出符合以下要求的代码(1)导入沪深300指数2022.4.1日-2023.3.30日,1分钟高频数据,画出5分钟高频数据的k线图(不会画k线图的话,就直接resample,计算出5分钟数据的close); (2)计算5分钟数据的移动均线SMA(5)以及均线SMA(10),计算采用双移动均线策略的收益(直接用1分钟数据) (3)计算1分钟、5分钟、10分钟、15分钟数据的动量策略收益(直接用1分钟数据)
时间: 2024-02-17 18:01:29 浏览: 130
用Python做股市数据分析
以下是符合要求的 Python 代码:
```
import pandas as pd
import numpy as np
import tushare as ts
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ochl
import matplotlib.dates as mdates
# 获取沪深300指数2022.4.1日-2023.3.30日,1分钟高频数据
hs300 = ts.get_k_data('hs300', start='2022-04-01', end='2023-03-30', ktype='1')
# 将时间转换为datetime格式,设置为index
hs300.index = pd.to_datetime(hs300['date'])
hs300 = hs300.drop(['date'], axis=1)
# 计算5分钟数据的close
hs300_5min = hs300.resample('5T').last().dropna()
# 画出5分钟高频数据的k线图
fig, ax = plt.subplots(figsize=(16,8))
candlestick_ochl(ax, hs300_5min[['open', 'close', 'high', 'low']].values, width=0.01, colorup='r', colordown='g', alpha=0.7)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
plt.title('HS300 5分钟K线图')
plt.xlabel('日期')
plt.ylabel('价格')
plt.show()
# 计算5分钟数据的移动均线SMA(5)以及均线SMA(10)
hs300_5min['SMA5'] = hs300_5min['close'].rolling(5).mean()
hs300_5min['SMA10'] = hs300_5min['close'].rolling(10).mean()
# 计算采用双移动均线策略的收益
hs300_1min = hs300.copy()
hs300_1min['SMA5'] = hs300_1min['close'].rolling(5).mean()
hs300_1min['SMA10'] = hs300_1min['close'].rolling(10).mean()
hs300_1min['position'] = np.where(hs300_1min['SMA5'] > hs300_1min['SMA10'], 1, -1)
hs300_1min['returns'] = np.log(hs300_1min['close'] / hs300_1min['close'].shift(1))
hs300_1min['strategy_returns'] = hs300_1min['position'].shift(1) * hs300_1min['returns']
cumulative_returns = np.exp(hs300_1min[['returns', 'strategy_returns']].cumsum())
cumulative_returns.plot(figsize=(16,8))
plt.title('双移动均线策略收益')
plt.xlabel('日期')
plt.ylabel('累计收益')
plt.show()
# 计算1分钟、5分钟、10分钟、15分钟数据的动量策略收益
hs300['returns_1min'] = np.log(hs300['close'] / hs300['close'].shift(1))
hs300['returns_5min'] = np.log(hs300['close'] / hs300['close'].shift(5))
hs300['returns_10min'] = np.log(hs300['close'] / hs300['close'].shift(10))
hs300['returns_15min'] = np.log(hs300['close'] / hs300['close'].shift(15))
hs300['momentum_1min'] = np.where(hs300['returns_1min'] > 0, 1, -1)
hs300['momentum_5min'] = np.where(hs300['returns_5min'] > 0, 1, -1)
hs300['momentum_10min'] = np.where(hs300['returns_10min'] > 0, 1, -1)
hs300['momentum_15min'] = np.where(hs300['returns_15min'] > 0, 1, -1)
hs300['strategy_returns_1min'] = hs300['momentum_1min'].shift(1) * hs300['returns_1min']
hs300['strategy_returns_5min'] = hs300['momentum_5min'].shift(5) * hs300['returns_5min']
hs300['strategy_returns_10min'] = hs300['momentum_10min'].shift(10) * hs300['returns_10min']
hs300['strategy_returns_15min'] = hs300['momentum_15min'].shift(15) * hs300['returns_15min']
cumulative_returns = np.exp(hs300[['strategy_returns_1min', 'strategy_returns_5min', 'strategy_returns_10min', 'strategy_returns_15min']].cumsum())
cumulative_returns.plot(figsize=(16,8))
plt.title('动量策略收益')
plt.xlabel('日期')
plt.ylabel('累计收益')
plt.show()
```
阅读全文