这篇文章是使用akshare获取到的国债期货日线数据,计算近月合约和远月合约的价差,根据价差大小做的价差套利策略或者价差投机策略。
数据分析
分析一下近月合约和远月合约的价差大小。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv("./中金所期货合约数据.csv", index_col=0)
data = data[data['variety'] == "T"]
data['date'] = pd.to_datetime(data['date'], format="%Y%m%d")
data = data.dropna()# print(data.head())
result = []
for day, df in data.groupby("date"):# print(day)# print(df[['date', 'symbol', 'open_interest', 'close']])# 如果有3个合约以上存在,删除持仓量最小的一个if len(df) >= 3:df = df[df['open_interest']!=df['open_interest'].min()]if len(df) < 2:continuedf = df.sort_values(['symbol'])close_list = df['close'].to_list()