掘金社区

策略研究-跨品种套利-历史价差数据研究Pinned highlighted

田野 发表在策略研究 2021-01-24 20:14:52

策略研究
1508
9
0

策略研究-跨品种套利-历史价差数据研究

测试环境
  • 掘金3终端
  • python3.8.5 64-bit
# coding=utf-8
from __future__ import print_function, absolute_import
from gm.api import *
import matplotlib.pyplot as plt
import datetime
import time


# 设置token, 查看已有token ID,在用户-秘钥管理里获取
set_token('token')


'''
# 画图表需要安装matplotlib库: python -m pip install matplotlib
# 图表Demo
x_data = ['2011','2012','2013','2014','2015','2016','2017']
y_data = [58000,60200,63000,71000,84000,90500,107000]

plt.plot(x_data,y_data)
plt.show()
'''

#i = datetime.datetime.now()
#print ("当前的日期和时间是 %s" % i)
today = time.strftime("%Y-%m-%d") + ' 16:00:00'
print (today)

#连续合约代码查阅: https://www.myquant.cn/docs/data/data_continue
symbolA = 'CFFEX.IF'
symbolB = 'CFFEX.IC'


# 查询历史行情
data = history(symbol=symbolA+','+symbolB, frequency='1d', start_time='2000-01-01 09:00:00', end_time=today, fields='symbol,bob,close', df=False)

print(len(data))

data1 = {}
data2 = {}

for row in data:
    rowMap = {str(row['bob']): row}
    if row['symbol'] == symbolA:
        data1.update(rowMap)
    elif row['symbol'] == symbolB:
        data2.update(rowMap)

#print(data1)
#print(data2)

date = []
diff = []
isInit = False
dataTotal = 0
dataMin = 0
dataMax = 0

for key, row in data1.items():
    if data2.get(key, False) != False:
        date.append((key.split(' ',1)[0]))
        row2 = data2[key]
        diffVal = float(row['close']) - float(row2['close'])
        diff.append(diffVal)

        dataTotal += diffVal
        if isInit == False:
                isInit = True
                dataMin = diffVal
                dataMax = diffVal
        else:
                dataMin = min(dataMin, diffVal)
                dataMax = max(dataMax, diffVal)

print(date)
print(diff)
print("---------------掘金量化-跨品种套利:" + symbolA + "&" + symbolB + "----------------")
print("日期:" + date[0] + "~" + date[-1])
print("min = %s, max = %s, avg = %s, last-date = %s, last-value = %s" %(dataMin, dataMax, (dataTotal/len(diff)), date[-1], diff[-1]))

plt.plot(date,diff)
plt.show()


评论: 9
  • ---------------掘金量化-跨品种套利:CZCE.FG&CZCE.SA----------------
    日期:2019-12-06~2021-01-22
    min = -253.0, max = 653.0, avg = 55.11231884057971, last-date = 2021-01-22, last-value = 225.0

    2021-01-24 20:15:56
  • ---------------掘金量化-跨品种套利:CFFEX.IF&CFFEX.IC----------------
    日期:2015-04-16~2021-01-22
    min = -6066.19970703125, max = -903.0, avg = -2204.1781572057844, last-date = 2021-01-22, last-value = -1021.80029296875

    2021-01-24 20:16:06
  • IF&IC价差:

    0_1611491069793_历史价差-IF-IC-20210124.png

    2021-01-24 20:24:56
  • ---------------掘金量化-跨品种套利:A&M----------------
    DATE = 2005-01-04 ~ 2021-02-02
    AREA, TOTAL, RATIO
    (LT)100, 2, 0.05109862%
    (LT)200, 76, 1.9417475%
    (LT)300, 258, 6.5917225%
    (LT)400, 83, 2.1205928%
    (LT)500, 168, 4.292284%
    (LT)600, 260, 6.642821%
    (LT)700, 335, 8.559019%
    (LT)800, 414, 10.5774145%
    (LT)900, 275, 7.02606%
    (LT)1000, 447, 11.420542%
    (LT)1100, 315, 8.048033%
    (LT)1200, 244, 6.2340317%
    (LT)1300, 287, 7.332652%
    (LT)1400, 294, 7.5114975%
    (LT)1500, 129, 3.2958612%
    (LT)1600, 109, 2.784875%
    (LT)1700, 60, 1.5329586%
    (LT)1800, 50, 1.2774655%
    (LT)1900, 21, 0.5365355%
    (LT)2000, 15, 0.38323966%
    (LT)2100, 19, 0.4854369%
    (LT)2200, 15, 0.38323966%
    (LT)2300, 13, 0.33214104%
    (LT)2400, 5, 0.12774655%
    (LT)2500, 0, 0%
    (GT)2500, 2, 0.05109862%
    AVG = 1022.03906

    2021-02-02 16:50:21
  • ---------------掘金量化-跨品种套利:L&V----------------
    DATA-LEN=2838
    DATE = 2009-05-25 ~ 2021-02-02
    AREA, TOTAL, RATIO
    (LT)100, 9, 0.31712475%
    (LT)400, 185, 6.5186753%
    (LT)700, 153, 5.3911204%
    (LT)1000, 39, 1.3742071%
    (LT)1300, 9, 0.31712475%
    (LT)1600, 23, 0.81042993%
    (LT)1900, 69, 2.4312897%
    (LT)2200, 173, 6.0958424%
    (LT)2500, 250, 8.80902%
    (LT)2800, 356, 12.5440445%
    (LT)3100, 343, 12.085976%
    (LT)3400, 385, 13.565892%
    (LT)3700, 268, 9.44327%
    (LT)4000, 205, 7.223397%
    (LT)4300, 116, 4.0873857%
    (LT)4600, 111, 3.911205%
    (LT)4900, 53, 1.8675122%
    (LT)5200, 16, 0.56377727%
    (LT)5500, 0, 0%
    (GT)5500, 31, 1.0923185%
    AVG = 3009.283

    2021-02-03 10:13:27
  • ---------------掘金量化-跨品种套利:SF&SM----------------
    DATA-LEN=1517
    DATE = 2014-08-12 ~ 2021-02-02
    AREA, TOTAL, RATIO
    (LT)-2500, 27, 1.7798285%
    (LT)-2300, 19, 1.252472%
    (LT)-2100, 51, 3.3618987%
    (LT)-1900, 71, 4.68029%
    (LT)-1700, 137, 9.030982%
    (LT)-1500, 124, 8.174027%
    (LT)-1300, 118, 7.7785106%
    (LT)-1100, 109, 7.185234%
    (LT)-900, 157, 10.349374%
    (LT)-700, 260, 17.139091%
    (LT)-500, 161, 10.613051%
    (LT)-300, 145, 9.558339%
    (LT)-100, 72, 4.7462096%
    (LT)100, 34, 2.2412655%
    (LT)300, 19, 1.252472%
    (LT)500, 0, 0%
    (GT)500, 6, 0.39551747%
    AVG = -899.851

    2021-02-03 11:07:38
  • ----RB0&HC0----
    DATA-LEN = 1680
    DATE = 2014-03-21 ~ 2021-02-03
    AREA, TOTAL, RATIO
    (LT)-350, 87, 5.1785717%
    (LT)-300, 92, 5.4761906%
    (LT)-250, 142, 8.452381%
    (LT)-200, 169, 10.059524%
    (LT)-150, 240, 14.285715%
    (LT)-100, 192, 11.428572%
    (LT)-50, 282, 16.785713%
    (LT)0, 126, 7.5000005%
    (LT)50, 125, 7.440476%
    (LT)100, 91, 5.4166665%
    (LT)150, 38, 2.2619047%
    (LT)200, 6, 0.35714287%
    (LT)250, 10, 0.5952381%
    (LT)300, 6, 0.35714287%
    (LT)350, 0, 0%
    (GT)350, 1, 0.059523813%
    MIN = -548, MAX = 390, AVG = -94.315475

    2021-02-03 18:05:24
  • ----C0&CS0----
    DATA-LEN = 1492
    DATE = 2014-12-22 ~ 2021-02-03
    AREA, TOTAL, RATIO
    (LT)-600, 33, 2.2117963%
    (LT)-550, 64, 4.2895446%
    (LT)-500, 216, 14.477211%
    (LT)-450, 217, 14.544237%
    (LT)-400, 253, 16.957104%
    (LT)-350, 339, 22.72118%
    (LT)-300, 200, 13.404825%
    (LT)-250, 91, 6.099196%
    (LT)-200, 45, 3.0160856%
    (LT)-150, 14, 0.93833774%
    (LT)-100, 0, 0%
    (GT)-100, 1, 0.067024134%
    MIN = -801, MAX = 1612, AVG = -367.70242

    2021-02-03 18:48:06
  • 直接返回df格式进行运算,速度应该会快很多

    2021-02-07 13:57:55

Looks like your connection to 掘金量化社区 - 量化交易者的交流社区 was lost, please wait while we try to reconnect.