[1]:
## 因子计算分析

import datetime

from tqsdk import TqApi, TqAuth
from tqsdk.tafunc import get_t, get_bs_price

import numpy as np
import pandas as pd
import plotly.express as px

pd.options.display.max_rows= None
pd.options.display.max_columns = None
pd.options.display.width = 0
pd.options.display.float_format = '{:.6f}'.format
在使用天勤量化之前,默认您已经知晓并同意以下免责条款,如果不同意请立即停止使用:https://www.shinnytech.com/blog/disclaimer/
[2]:
# 标准 BS 定价模型假设波动律为常量, 但是波动率实际和时间相关, 这里扩展 BS 定价模型引入时间相关波动率并与标准模型进行对比
api = TqApi(auth=TqAuth('快期账户', '快期密码'))
quote = api.get_quote('CFFEX.IO2405-C-3550')
underlying = quote.underlying_symbol
exp = datetime.datetime.fromtimestamp(quote.expire_datetime)
2024-06-26 16:40:45 -     INFO - 通知 : 与 wss://api.shinnytech.com/t/nfmd/front/mobile 的网络连接已建立
[3]:
# 获取沪深300指数过去一年的5分钟现数据,用来分析波动率
csi = api.get_kline_data_series(symbol=underlying, duration_seconds=300, start_dt=exp-datetime.timedelta(days=365), end_dt=exp)
# 将 unix time 转换成 pandas.Datetime 并获得周几及时间信息
csi["dt"] = pd.to_datetime(csi["datetime"], utc=True).dt.tz_convert('Asia/Shanghai')
csi["time"] = csi["dt"].dt.time
csi["week"] = csi["dt"].dt.weekday+1
# p 为每五分钟的波动率, 不计算跨交易时段的波动, 用来分析波动率的周期性
csi["p"] = np.log(csi["close"]/csi["open"])
[4]:
# 可以看到周一到周五的波动率有显著性的差异
px.bar(csi.groupby("week").std(numeric_only=True), y="p")

[5]:
# 每天不同时间段的波动率也有显著性的差异
# 早上及下午开盘的波动率显著偏高
# 注意这里的波动率是收盘价对比开盘价, 而不是收盘价对比上个收盘价, 因此 9:30 开盘的高波动率只包含集合竞价之后的价格波动, 不包含相对昨天收盘的波动
px.bar(csi.groupby("time").std(numeric_only=True), y="p")
[6]:
# 按周一~五绘制出不同时段的波动率, 可以观察到周一的 9:30 又显著高于周二~五
px.line(csi.groupby(["week","time"]).std(numeric_only=True).reset_index(), x="time", y="p", color="week")
[7]:
# 上述信息验证了波动率实际和时间相关的假设, 接下来基于该假设计算改进版的 BS 定价
# Tσ² = ∫σ²(dt)
# 其中 T 是距离到期时间, 按照波动率的周期性积分出改进版波动率
# 这里计算的 r 是使用当前周期的收盘价对比上一周期的收盘价, 使得积分的结果能够完整的覆盖 T
csi["r"] = np.log(csi["close"].shift(1)/csi["close"])
# 得到周一~五不同时间段的周期性收益率方差
csiv = csi.groupby(["week","time"]).var(numeric_only=True).reset_index()[["week", "time", "r"]]
[8]:
# 只关注期权最后一周的行情数据
serial = api.get_kline_serial(symbol=[underlying, quote.instrument_id], duration_seconds=300, data_length=48*5)
serial["dt"] = pd.to_datetime(serial["datetime"], utc=True).dt.tz_convert('Asia/Shanghai')
serial["time"] = serial["dt"].dt.time
serial["week"] = serial["dt"].dt.weekday+1
# 最后一个交易日使用最后两小时的算数平均数作为交割结算价
serial["close_avg"] = serial["close"]
serial.iloc[-24:, serial.columns.get_loc("close_avg")] = serial["close"].iloc[-24:].expanding().mean()
[9]:
# 将 csi 周期性收益率方差 join 期权价量信息, 计算出改进版波动率
serial = serial.merge(csiv, on=["week", "time"])
[10]:
# v 为年化后的改进版波动率
serial["v"] = np.sqrt(serial["r"].iloc[::-1].expanding().mean()*48*250)
[11]:
# 无风险利率 1.5%
rf = 0.015
# 距离到期的时间序列
t = get_t(serial, quote.expire_datetime)
# 行权价格的折现, 用于计算时间价值
serial["present_k"] = quote.strike_price * np.exp(-rf * t)
serial["time_value"] = (serial["close_avg"] - serial["present_k"]) - serial["close1"]
# 计算标准和改进版 BS 模型价格, 以及和实际期权价格的差
# 最后两小时使用算数平均作为交割结算价导致不能简单的使用波动率来估计交割结算价的变化, 因此跳过最后两小时
# v 为标准 BS 模型所用到的波动率
v = np.sqrt(np.var(csi["r"]) * 250*48)
serial["bs"] = get_bs_price(serial["close"].iloc[:-24], k=quote.strike_price, r=rf, v=v, t=t.iloc[:-24], option_class=quote.option_class)
serial["bs_diff"] = serial["bs"] - serial["close1"]
serial["tdbs"] = get_bs_price(serial["close"].iloc[:-24], k=quote.strike_price, r=rf, v=serial["v"].iloc[:-24], t=t.iloc[:-24], option_class=quote.option_class)
serial["tdbs_diff"] = serial["tdbs"] - serial["close1"]
[12]:
# 计算结果
serial[["dt", "close_avg", "time_value", "close1", "bs", "bs_diff", "tdbs", "tdbs_diff"]]
[12]:
dt close_avg time_value close1 bs bs_diff tdbs tdbs_diff
0 2024-05-13 09:30:00+08:00 3642.430000 1.654996 91.400000 94.055250 2.655250 94.053467 2.653467
1 2024-05-13 09:35:00+08:00 3642.540000 0.764482 92.400000 94.157842 1.757842 93.817840 1.417840
2 2024-05-13 09:40:00+08:00 3640.660000 0.083969 91.200000 92.360068 1.160068 91.990269 0.790269
3 2024-05-13 09:45:00+08:00 3642.880000 -0.096545 93.600000 94.477975 0.877975 94.127435 0.527435
4 2024-05-13 09:50:00+08:00 3644.590000 -0.387058 95.600000 96.114526 0.514526 95.779602 0.179602
5 2024-05-13 09:55:00+08:00 3644.780000 0.602428 94.800000 96.294486 1.494486 95.954176 1.154176
6 2024-05-13 10:00:00+08:00 3643.400000 0.021915 94.000000 94.968284 0.968284 94.607889 0.607889
7 2024-05-13 10:05:00+08:00 3649.690000 -1.288599 101.600000 101.022761 -0.577239 100.730440 -0.869560
8 2024-05-13 10:10:00+08:00 3653.490000 0.710888 103.400000 104.706017 1.306017 104.447285 1.047285
9 2024-05-13 10:15:00+08:00 3658.290000 -0.289626 109.200000 109.383127 0.183127 109.167791 -0.032209
10 2024-05-13 10:20:00+08:00 3659.020000 1.039861 108.600000 110.095092 1.495092 109.884757 1.284757
11 2024-05-13 10:25:00+08:00 3658.620000 2.239347 107.000000 109.702235 2.702235 109.490768 2.490768
12 2024-05-13 10:30:00+08:00 3660.600000 1.818833 109.400000 111.638249 2.238249 111.443192 2.043192
13 2024-05-13 10:35:00+08:00 3659.830000 1.248320 109.200000 110.882588 1.682588 110.683323 1.483323
14 2024-05-13 10:40:00+08:00 3660.470000 1.487806 109.600000 111.507632 1.907632 111.313157 1.713157
15 2024-05-13 10:45:00+08:00 3667.410000 -0.172707 118.200000 118.323862 0.123862 118.178861 -0.021139
16 2024-05-13 10:50:00+08:00 3669.310000 -1.873221 121.800000 120.195245 -1.604755 120.062026 -1.737974
17 2024-05-13 10:55:00+08:00 3667.370000 0.986266 117.000000 118.281704 1.281704 118.138850 1.138850
18 2024-05-13 11:00:00+08:00 3663.970000 0.585752 114.000000 114.935195 0.935195 114.772008 0.772008
19 2024-05-13 11:05:00+08:00 3663.260000 1.475239 112.400000 114.236226 1.836226 114.067249 1.667249
20 2024-05-13 11:10:00+08:00 3659.890000 1.104725 109.400000 110.929702 1.529702 110.736729 1.336729
21 2024-05-13 11:15:00+08:00 3661.100000 1.714212 110.000000 112.113643 2.113643 111.928860 1.928860
22 2024-05-13 11:20:00+08:00 3663.910000 1.123698 113.400000 114.870255 1.470255 114.705511 1.305511
23 2024-05-13 11:25:00+08:00 3665.520000 1.133185 115.000000 116.451949 1.451949 116.299429 1.299429
24 2024-05-13 13:00:00+08:00 3666.680000 0.883428 116.400000 117.567562 1.167562 117.430678 1.030678
25 2024-05-13 13:05:00+08:00 3669.440000 0.842915 119.200000 120.288497 1.088497 120.165952 0.965952
26 2024-05-13 13:10:00+08:00 3666.950000 0.952401 116.600000 117.830932 1.230932 117.694601 1.094601
27 2024-05-13 13:15:00+08:00 3664.050000 1.251888 113.400000 114.973967 1.573967 114.818286 1.418286
28 2024-05-13 13:20:00+08:00 3668.220000 0.821374 118.000000 119.080619 1.080619 118.950184 0.950184
29 2024-05-13 13:25:00+08:00 3671.950000 0.750861 121.800000 122.763406 0.963406 122.653171 0.853171
30 2024-05-13 13:30:00+08:00 3672.090000 0.490347 122.200000 122.900642 0.700642 122.792195 0.592195
31 2024-05-13 13:35:00+08:00 3670.360000 1.359834 119.600000 121.189497 1.589497 121.072959 1.472959
32 2024-05-13 13:40:00+08:00 3669.600000 1.399320 118.800000 120.437575 1.637575 120.318341 1.518341
33 2024-05-13 13:45:00+08:00 3669.570000 -0.231193 120.400000 120.406686 0.006686 120.289042 -0.110958
34 2024-05-13 13:50:00+08:00 3670.170000 0.568293 120.200000 120.998062 0.798062 120.885163 0.685163
35 2024-05-13 13:55:00+08:00 3670.580000 -1.622220 122.800000 121.401920 -1.398080 121.292969 -1.507031
36 2024-05-13 14:00:00+08:00 3673.590000 0.787266 123.400000 124.377621 0.977621 124.283658 0.883658
37 2024-05-13 14:05:00+08:00 3669.120000 0.316752 119.400000 119.957257 0.557257 119.842775 0.442775
38 2024-05-13 14:10:00+08:00 3665.720000 2.316239 114.000000 116.602395 2.602395 116.470472 2.470472
39 2024-05-13 14:15:00+08:00 3662.720000 1.715725 111.600000 113.648407 2.048407 113.500451 1.900451
40 2024-05-13 14:20:00+08:00 3660.120000 -0.284788 111.000000 111.093480 0.093480 110.929663 -0.070337
41 2024-05-13 14:25:00+08:00 3659.050000 0.244698 109.400000 110.042649 0.642649 109.871612 0.471612
42 2024-05-13 14:30:00+08:00 3658.900000 1.094185 108.400000 109.894002 1.494002 109.724688 1.324688
43 2024-05-13 14:35:00+08:00 3662.210000 0.803671 112.000000 113.141110 1.141110 112.993528 0.993528
44 2024-05-13 14:40:00+08:00 3664.110000 0.703158 114.000000 115.008462 1.008462 114.874169 0.874169
45 2024-05-13 14:45:00+08:00 3662.850000 -0.757356 114.200000 113.767335 -0.432665 113.626656 -0.573344
46 2024-05-13 14:50:00+08:00 3663.660000 0.452131 113.800000 114.562703 0.762703 114.426168 0.626168
47 2024-05-13 14:55:00+08:00 3664.690000 1.881617 113.400000 115.575260 2.175260 115.446259 2.046259
48 2024-05-14 09:30:00+08:00 3675.380000 -0.142898 126.000000 125.923052 -0.076948 125.888199 -0.111801
49 2024-05-14 09:35:00+08:00 3669.180000 0.856588 118.800000 119.754387 0.954387 119.693480 0.893480
50 2024-05-14 09:40:00+08:00 3668.870000 1.946075 117.400000 119.445348 2.045348 119.383071 1.983071
51 2024-05-14 09:45:00+08:00 3665.320000 1.995561 113.800000 115.919007 2.119007 115.843588 2.043588
52 2024-05-14 09:50:00+08:00 3660.910000 0.585048 110.800000 111.546159 0.746159 111.449895 0.649895
53 2024-05-14 09:55:00+08:00 3658.300000 0.774534 108.000000 108.962288 0.962288 108.851476 0.851476
54 2024-05-14 10:00:00+08:00 3651.790000 0.864021 101.400000 102.538132 1.138132 102.383731 0.983731
55 2024-05-14 10:05:00+08:00 3646.850000 -0.676493 98.000000 97.685160 -0.314840 97.488161 -0.511839
56 2024-05-14 10:10:00+08:00 3649.710000 0.582994 99.600000 100.489548 0.889548 100.318113 0.718113
57 2024-05-14 10:15:00+08:00 3651.900000 -0.627520 103.000000 102.641792 -0.358208 102.488000 -0.512000
58 2024-05-14 10:20:00+08:00 3652.970000 0.441967 103.000000 103.694095 0.694095 103.549219 0.549219
59 2024-05-14 10:25:00+08:00 3657.120000 3.391453 104.200000 107.788052 3.588052 107.671500 3.471500
60 2024-05-14 10:30:00+08:00 3652.390000 2.660940 100.200000 103.119731 2.919731 102.972943 2.772943
61 2024-05-14 10:35:00+08:00 3652.650000 1.920426 101.200000 103.374352 2.174352 103.229984 2.029984
62 2024-05-14 10:40:00+08:00 3652.450000 0.119912 102.800000 103.175829 0.375829 103.030603 0.230603
63 2024-05-14 10:45:00+08:00 3652.890000 2.159399 101.200000 103.607862 2.407862 103.467599 2.267599
64 2024-05-14 10:50:00+08:00 3654.460000 2.928885 102.000000 105.154556 3.154556 105.025329 3.025329
65 2024-05-14 10:55:00+08:00 3656.650000 1.518372 105.600000 107.315681 1.715681 107.201142 1.601142
66 2024-05-14 11:00:00+08:00 3656.350000 2.617858 104.200000 107.017923 2.817923 106.902754 2.702754
67 2024-05-14 11:05:00+08:00 3658.570000 1.837345 107.200000 109.211636 2.011636 109.110100 1.910100
68 2024-05-14 11:10:00+08:00 3657.860000 2.326831 106.000000 108.508019 2.508019 108.404232 2.404232
69 2024-05-14 11:15:00+08:00 3659.010000 -0.123682 109.600000 109.644577 0.044577 109.547984 -0.052016
70 2024-05-14 11:20:00+08:00 3654.440000 2.105804 102.800000 105.126397 2.326397 105.005914 2.205914
71 2024-05-14 11:25:00+08:00 3654.770000 0.835291 104.400000 105.450726 1.050726 105.333805 0.933805
72 2024-05-14 13:00:00+08:00 3652.710000 2.165534 101.000000 103.391106 2.391106 103.270910 2.270910
73 2024-05-14 13:05:00+08:00 3650.870000 1.525020 99.800000 101.575720 1.775720 101.439435 1.639435
74 2024-05-14 13:10:00+08:00 3652.930000 2.584506 100.800000 103.605310 2.805310 103.482591 2.682591
75 2024-05-14 13:15:00+08:00 3652.040000 1.293993 101.200000 102.725948 1.525948 102.599247 1.399247
76 2024-05-14 13:20:00+08:00 3651.300000 1.353479 100.400000 101.994935 1.594935 101.865638 1.465638
77 2024-05-14 13:25:00+08:00 3651.330000 1.182966 100.600000 102.023020 1.423020 101.896062 1.296062
78 2024-05-14 13:30:00+08:00 3653.810000 0.462452 103.800000 104.468396 0.668396 104.357940 0.557940
79 2024-05-14 13:35:00+08:00 3652.900000 1.751939 101.600000 103.568631 1.968631 103.454249 1.854249
80 2024-05-14 13:40:00+08:00 3652.170000 1.621425 101.000000 102.846928 1.846928 102.729466 1.729466
81 2024-05-14 13:45:00+08:00 3653.330000 1.180912 102.600000 103.990293 1.390293 103.880397 1.280397
82 2024-05-14 13:50:00+08:00 3655.620000 1.870398 104.200000 106.251688 2.051688 106.154887 1.954887
83 2024-05-14 13:55:00+08:00 3656.340000 1.389885 105.400000 106.962552 1.562552 106.871013 1.471013
84 2024-05-14 14:00:00+08:00 3658.040000 1.889371 106.600000 108.644041 2.044041 108.562048 1.962048
85 2024-05-14 14:05:00+08:00 3658.580000 3.628858 105.400000 109.177714 3.777714 109.097781 3.697781
86 2024-05-14 14:10:00+08:00 3654.920000 1.768344 103.600000 105.554367 1.954367 105.458036 1.858036
87 2024-05-14 14:15:00+08:00 3657.190000 2.637831 105.000000 107.798757 2.798757 107.713914 2.713914
88 2024-05-14 14:20:00+08:00 3657.730000 1.977317 106.200000 108.332210 2.132210 108.250726 2.050726
89 2024-05-14 14:25:00+08:00 3658.350000 3.796803 105.000000 108.945114 3.945114 108.867394 3.867394
90 2024-05-14 14:30:00+08:00 3658.450000 2.496290 106.400000 109.043001 2.643001 108.967213 2.567213
91 2024-05-14 14:35:00+08:00 3656.220000 2.665776 104.000000 106.833715 2.833715 106.749344 2.749344
92 2024-05-14 14:40:00+08:00 3656.810000 2.255263 105.000000 107.416414 2.416414 107.335882 2.335882
93 2024-05-14 14:45:00+08:00 3655.840000 1.884749 104.400000 106.455190 2.055190 106.372151 1.972151
94 2024-05-14 14:50:00+08:00 3656.660000 3.304236 103.800000 107.265454 3.465454 107.187130 3.387130
95 2024-05-14 14:55:00+08:00 3657.050000 2.693722 104.800000 107.650341 2.850341 107.575578 2.775578
96 2024-05-15 09:30:00+08:00 3646.770000 1.299202 95.800000 97.194105 1.394105 97.147714 1.347714
97 2024-05-15 09:35:00+08:00 3642.210000 1.738688 90.800000 92.671127 1.871127 92.596385 1.796385
98 2024-05-15 09:40:00+08:00 3638.550000 0.478175 88.400000 89.049836 0.649836 88.955822 0.555822
99 2024-05-15 09:45:00+08:00 3638.390000 -0.682339 89.400000 88.890298 -0.509702 88.795248 -0.604752
100 2024-05-15 09:50:00+08:00 3641.170000 -0.302852 91.800000 91.637479 -0.162521 91.557236 -0.242764
101 2024-05-15 09:55:00+08:00 3645.950000 -0.723366 97.000000 96.374352 -0.625648 96.315757 -0.684243
102 2024-05-15 10:00:00+08:00 3642.810000 -0.663879 93.800000 93.258960 -0.541040 93.187078 -0.612922
103 2024-05-15 10:05:00+08:00 3645.730000 0.055607 96.000000 96.153669 0.153669 96.094729 0.094729
104 2024-05-15 10:10:00+08:00 3644.640000 -1.034906 96.000000 95.070876 -0.929124 95.007886 -0.992114
105 2024-05-15 10:15:00+08:00 3640.620000 -2.055420 93.000000 91.086296 -1.913704 91.005444 -1.994556
106 2024-05-15 10:20:00+08:00 3641.790000 -1.085933 93.200000 92.243277 -0.956723 92.168346 -1.031654
107 2024-05-15 10:25:00+08:00 3639.600000 0.723553 89.200000 90.074408 0.874408 89.989087 0.789087
108 2024-05-15 10:30:00+08:00 3637.730000 -1.346961 89.400000 88.224799 -1.175201 88.129248 -1.270752
109 2024-05-15 10:35:00+08:00 3635.920000 0.042526 86.200000 86.436971 0.236971 86.331909 0.131909
110 2024-05-15 10:40:00+08:00 3640.400000 0.122012 90.600000 90.861687 0.261687 90.783249 0.183249
111 2024-05-15 10:45:00+08:00 3637.770000 -0.908501 89.000000 88.259755 -0.740245 88.168259 -0.831741
112 2024-05-15 10:50:00+08:00 3639.320000 -0.359015 90.000000 89.790361 -0.209639 89.709042 -0.290958
113 2024-05-15 10:55:00+08:00 3639.800000 -1.279528 91.400000 90.263787 -1.136213 90.186331 -1.213669
114 2024-05-15 11:00:00+08:00 3643.950000 -0.330042 94.600000 94.374352 -0.225648 94.316199 -0.283801
115 2024-05-15 11:05:00+08:00 3644.240000 -0.840555 95.400000 94.660888 -0.739112 94.604686 -0.795314
116 2024-05-15 11:10:00+08:00 3642.240000 1.158931 91.400000 92.676192 1.276192 92.613173 1.213173
117 2024-05-15 11:15:00+08:00 3642.710000 1.028417 92.000000 93.140862 1.140862 93.080868 1.080868
118 2024-05-15 11:20:00+08:00 3644.970000 0.087904 95.200000 95.381923 0.181923 95.331098 0.131098
119 2024-05-15 11:25:00+08:00 3647.190000 1.907390 95.600000 97.586002 1.986002 97.542763 1.942763
120 2024-05-15 13:00:00+08:00 3647.430000 2.137633 95.600000 97.804856 2.204856 97.767831 2.167831
121 2024-05-15 13:05:00+08:00 3646.080000 1.787119 94.600000 96.461464 1.861464 96.421280 1.821280
122 2024-05-15 13:10:00+08:00 3644.190000 1.496606 93.000000 94.582349 1.582349 94.537705 1.537705
123 2024-05-15 13:15:00+08:00 3644.680000 1.586092 93.400000 95.067990 1.667990 95.025518 1.625518
124 2024-05-15 13:20:00+08:00 3646.030000 0.535579 95.800000 96.408594 0.608594 96.370221 0.570221
125 2024-05-15 13:25:00+08:00 3645.340000 2.045065 93.600000 95.721655 2.121655 95.682533 2.082533
126 2024-05-15 13:30:00+08:00 3645.340000 0.644551 95.000000 95.720582 0.720582 95.681256 0.681256
127 2024-05-15 13:35:00+08:00 3642.780000 1.284038 91.800000 93.176505 1.376505 93.130829 1.330829
128 2024-05-15 13:40:00+08:00 3641.380000 -0.516476 92.200000 91.785984 -0.414016 91.737070 -0.462930
129 2024-05-15 13:45:00+08:00 3638.450000 0.753011 88.000000 88.880616 0.880616 88.821418 0.821418
130 2024-05-15 13:50:00+08:00 3639.240000 1.542497 88.000000 89.661794 1.661794 89.607325 1.607325
131 2024-05-15 13:55:00+08:00 3636.810000 0.511984 86.600000 87.254671 0.654671 87.191674 0.591674
132 2024-05-15 14:00:00+08:00 3635.040000 -0.658530 86.000000 85.503476 -0.496524 85.434319 -0.565681
133 2024-05-15 14:05:00+08:00 3633.420000 -1.279043 85.000000 83.902615 -1.097385 83.827834 -1.172166
134 2024-05-15 14:10:00+08:00 3632.060000 -1.839557 84.200000 82.560063 -1.639937 82.479929 -1.720071
135 2024-05-15 14:15:00+08:00 3631.760000 -3.540071 85.600000 82.262768 -3.337232 82.182892 -3.417108
136 2024-05-15 14:20:00+08:00 3637.500000 0.599416 87.200000 87.930403 0.730403 87.875313 0.675313
137 2024-05-15 14:25:00+08:00 3634.630000 0.128902 84.800000 85.090744 0.290744 85.026059 0.226059
138 2024-05-15 14:30:00+08:00 3633.040000 0.138389 83.200000 83.519539 0.319539 83.451494 0.251494
139 2024-05-15 14:35:00+08:00 3631.900000 -0.202125 82.400000 82.393800 -0.006200 82.322468 -0.077532
140 2024-05-15 14:40:00+08:00 3628.880000 -0.022638 79.200000 79.420272 0.220272 79.336179 0.136179
141 2024-05-15 14:45:00+08:00 3624.750000 -0.353152 75.400000 75.371095 -0.028905 75.269562 -0.130438
142 2024-05-15 14:50:00+08:00 3624.610000 -0.093665 75.000000 75.232007 0.232007 75.133333 0.133333
143 2024-05-15 14:55:00+08:00 3626.060000 -1.644179 78.000000 76.648150 -1.351850 76.561502 -1.438498
144 2024-05-16 09:30:00+08:00 3629.820000 -2.798704 82.800000 80.030639 -2.769361 80.019019 -2.780981
145 2024-05-16 09:35:00+08:00 3629.060000 -1.359218 80.600000 79.272244 -1.327756 79.253133 -1.346867
146 2024-05-16 09:40:00+08:00 3630.650000 -1.969731 82.800000 80.856281 -1.943719 80.839893 -1.960107
147 2024-05-16 09:45:00+08:00 3631.720000 -2.900245 84.800000 81.922489 -2.877511 81.907561 -2.892439
148 2024-05-16 09:50:00+08:00 3637.050000 -1.570758 88.800000 87.241325 -1.558675 87.232759 -1.567241
149 2024-05-16 09:55:00+08:00 3637.740000 -0.081272 88.000000 87.929678 -0.070322 87.921758 -0.078242
150 2024-05-16 10:00:00+08:00 3638.460000 0.838215 87.800000 88.648092 0.848092 88.640824 0.840824
151 2024-05-16 10:05:00+08:00 3639.940000 0.117701 90.000000 90.125815 0.125815 90.119747 0.119747
152 2024-05-16 10:10:00+08:00 3637.540000 -0.282813 88.000000 87.727853 -0.272147 87.719887 -0.280113
153 2024-05-16 10:15:00+08:00 3640.410000 0.386674 90.200000 90.594068 0.394068 90.588425 0.388425
154 2024-05-16 10:20:00+08:00 3638.140000 0.916160 87.400000 88.325749 0.925749 88.318639 0.918639
155 2024-05-16 10:25:00+08:00 3639.060000 1.435647 87.800000 89.244071 1.444071 89.237722 1.437722
156 2024-05-16 10:30:00+08:00 3642.220000 -0.404867 92.800000 92.400720 -0.399280 92.396434 -0.403566
157 2024-05-16 10:35:00+08:00 3642.200000 0.774620 91.600000 92.380117 0.780117 92.375872 0.775872
158 2024-05-16 10:40:00+08:00 3640.410000 1.384106 89.200000 90.590861 1.390861 90.585770 1.385770
159 2024-05-16 10:45:00+08:00 3639.900000 -0.126408 90.200000 90.080661 -0.119339 90.075368 -0.124632
160 2024-05-16 10:50:00+08:00 3640.220000 1.793079 88.600000 90.399747 1.799747 90.394782 1.794782
161 2024-05-16 10:55:00+08:00 3642.800000 0.772565 92.200000 92.977284 0.777284 92.973720 0.773720
162 2024-05-16 11:00:00+08:00 3641.620000 0.392052 91.400000 91.797433 0.397433 91.793466 0.393466
163 2024-05-16 11:05:00+08:00 3645.460000 1.231538 94.400000 95.634748 1.234748 95.632318 1.232318
164 2024-05-16 11:10:00+08:00 3646.390000 0.961025 95.600000 96.563804 0.963804 96.561696 0.961696
165 2024-05-16 11:15:00+08:00 3648.620000 0.990511 97.800000 98.792528 0.992528 98.790995 0.990995
166 2024-05-16 11:20:00+08:00 3652.170000 1.139997 101.200000 102.341208 1.141208 102.340249 1.140249
167 2024-05-16 11:25:00+08:00 3654.520000 2.889484 101.800000 104.690332 2.890332 104.689648 2.889648
168 2024-05-16 13:00:00+08:00 3656.080000 1.839726 104.400000 106.240137 1.840137 106.239800 1.839800
169 2024-05-16 13:05:00+08:00 3654.280000 0.839212 103.600000 104.439737 0.839737 104.439308 0.839308
170 2024-05-16 13:10:00+08:00 3651.510000 1.268699 100.400000 101.669471 1.269471 101.668847 1.268847
171 2024-05-16 13:15:00+08:00 3653.600000 1.758185 102.000000 103.758736 1.758736 103.758286 1.758286
172 2024-05-16 13:20:00+08:00 3652.570000 1.127672 101.600000 102.728297 1.128297 102.727793 1.127793
173 2024-05-16 13:25:00+08:00 3654.140000 1.697158 102.600000 104.297638 1.697638 104.297255 1.697255
174 2024-05-16 13:30:00+08:00 3652.530000 1.886644 100.800000 102.687240 1.887240 102.686771 1.886771
175 2024-05-16 13:35:00+08:00 3654.270000 1.826131 102.600000 104.426576 1.826576 104.426223 1.826223
176 2024-05-16 13:40:00+08:00 3656.340000 1.895617 104.600000 106.495931 1.895931 106.495684 1.895684
177 2024-05-16 13:45:00+08:00 3652.980000 2.735104 100.400000 103.135616 2.735616 103.135228 2.735228
178 2024-05-16 13:50:00+08:00 3652.960000 1.714590 101.400000 103.115090 1.715090 103.114721 1.714721
179 2024-05-16 13:55:00+08:00 3649.600000 2.354077 97.400000 99.754884 2.354884 99.754307 2.354307
180 2024-05-16 14:00:00+08:00 3649.650000 2.203563 97.600000 99.804344 2.204344 99.803801 2.203801
181 2024-05-16 14:05:00+08:00 3652.960000 2.313049 100.800000 103.113508 2.313508 103.113172 2.313172
182 2024-05-16 14:10:00+08:00 3648.120000 3.272536 95.000000 98.273466 3.273466 98.272807 3.272807
183 2024-05-16 14:15:00+08:00 3644.350000 2.502022 92.000000 94.503601 2.503601 94.502497 2.502497
184 2024-05-16 14:20:00+08:00 3644.000000 2.951509 91.200000 94.153129 2.953129 94.151956 2.951956
185 2024-05-16 14:25:00+08:00 3641.850000 2.000995 90.000000 92.003150 2.003150 92.001681 2.001681
186 2024-05-16 14:30:00+08:00 3635.850000 2.200481 83.800000 86.005358 2.205358 86.002381 2.202381
187 2024-05-16 14:35:00+08:00 3636.370000 3.519968 83.000000 86.524409 3.524409 86.521721 3.521721
188 2024-05-16 14:40:00+08:00 3636.190000 3.939454 82.400000 86.343906 3.943906 86.341310 3.941310
189 2024-05-16 14:45:00+08:00 3638.770000 4.318941 84.600000 88.921976 4.321976 88.920233 4.320233
190 2024-05-16 14:50:00+08:00 3641.200000 5.548427 85.800000 91.350520 5.550520 91.349336 5.549336
191 2024-05-16 14:55:00+08:00 3640.360000 4.107914 86.400000 90.510219 4.110219 90.509041 4.109041
192 2024-05-17 09:30:00+08:00 3636.920000 -0.246616 87.200000 86.953384 -0.246616 86.953384 -0.246616
193 2024-05-17 09:35:00+08:00 3644.450000 -0.317130 94.800000 94.482870 -0.317130 94.482870 -0.317130
194 2024-05-17 09:40:00+08:00 3647.620000 -1.347643 99.000000 97.652357 -1.347643 97.652357 -1.347643
195 2024-05-17 09:45:00+08:00 3645.080000 -2.288157 97.400000 95.111843 -2.288157 95.111843 -2.288157
196 2024-05-17 09:50:00+08:00 3644.230000 -1.138671 95.400000 94.261329 -1.138671 94.261329 -1.138671
197 2024-05-17 09:55:00+08:00 3648.570000 -1.799184 100.400000 98.600816 -1.799184 98.600816 -1.799184
198 2024-05-17 10:00:00+08:00 3640.520000 -1.249698 91.800000 90.550302 -1.249698 90.550302 -1.249698
199 2024-05-17 10:05:00+08:00 3638.290000 -0.680211 89.000000 88.319789 -0.680211 88.319789 -0.680211
200 2024-05-17 10:10:00+08:00 3639.280000 -1.690725 91.000000 89.309275 -1.690725 89.309275 -1.690725
201 2024-05-17 10:15:00+08:00 3635.060000 -0.511239 85.600000 85.088761 -0.511239 85.088761 -0.511239
202 2024-05-17 10:20:00+08:00 3638.010000 -0.161752 88.200000 88.038248 -0.161752 88.038248 -0.161752
203 2024-05-17 10:25:00+08:00 3632.990000 -1.182266 84.200000 83.017734 -1.182266 83.017734 -1.182266
204 2024-05-17 10:30:00+08:00 3628.510000 -0.462779 79.000000 78.537221 -0.462779 78.537221 -0.462779
205 2024-05-17 10:35:00+08:00 3631.370000 0.996707 80.400000 81.396707 0.996707 81.396707 0.996707
206 2024-05-17 10:40:00+08:00 3630.080000 -0.893807 81.000000 80.106193 -0.893807 80.106193 -0.893807
207 2024-05-17 10:45:00+08:00 3627.830000 -1.144320 79.000000 77.855680 -1.144320 77.855680 -1.144320
208 2024-05-17 10:50:00+08:00 3629.340000 -2.234834 81.600000 79.365166 -2.234834 79.365166 -2.234834
209 2024-05-17 10:55:00+08:00 3633.450000 -1.325347 84.800000 83.474653 -1.325347 83.474653 -1.325347
210 2024-05-17 11:00:00+08:00 3633.490000 -2.685861 86.200000 83.514139 -2.685861 83.514139 -2.685861
211 2024-05-17 11:05:00+08:00 3631.800000 -2.176374 84.000000 81.823626 -2.176374 81.823626 -2.176374
212 2024-05-17 11:10:00+08:00 3630.470000 -3.106888 83.600000 80.493112 -3.106888 80.493112 -3.106888
213 2024-05-17 11:15:00+08:00 3631.300000 -1.277402 82.600000 81.322598 -1.277402 81.322598 -1.277402
214 2024-05-17 11:20:00+08:00 3630.860000 -2.317915 83.200000 80.882085 -2.317915 80.882085 -2.317915
215 2024-05-17 11:25:00+08:00 3633.040000 1.461571 81.600000 83.061571 1.461571 83.061571 1.461571
216 2024-05-17 13:00:00+08:00 3634.530000 -0.458187 85.000000 NaN NaN NaN NaN
217 2024-05-17 13:05:00+08:00 3635.565000 0.576299 85.000000 NaN NaN NaN NaN
218 2024-05-17 13:10:00+08:00 3635.070000 -0.319214 85.400000 NaN NaN NaN NaN
219 2024-05-17 13:15:00+08:00 3634.500000 -0.089728 84.600000 NaN NaN NaN NaN
220 2024-05-17 13:20:00+08:00 3634.216000 0.025758 84.200000 NaN NaN NaN NaN
221 2024-05-17 13:25:00+08:00 3633.796667 -0.394089 84.200000 NaN NaN NaN NaN
222 2024-05-17 13:30:00+08:00 3633.867143 -0.324126 84.200000 NaN NaN NaN NaN
223 2024-05-17 13:35:00+08:00 3634.633750 -3.558032 88.200000 NaN NaN NaN NaN
224 2024-05-17 13:40:00+08:00 3635.740000 -5.652296 91.400000 NaN NaN NaN NaN
225 2024-05-17 13:45:00+08:00 3636.584000 -4.208810 90.800000 NaN NaN NaN NaN
226 2024-05-17 13:50:00+08:00 3637.460909 -3.532414 91.000000 NaN NaN NaN NaN
227 2024-05-17 13:55:00+08:00 3638.371667 -5.622170 94.000000 NaN NaN NaN NaN
228 2024-05-17 14:00:00+08:00 3639.120000 -2.674350 91.800000 NaN NaN NaN NaN
229 2024-05-17 14:05:00+08:00 3639.754286 -2.440578 92.200000 NaN NaN NaN NaN
230 2024-05-17 14:10:00+08:00 3640.271333 -1.524044 91.800000 NaN NaN NaN NaN
231 2024-05-17 14:15:00+08:00 3640.685000 -1.110891 91.800000 NaN NaN NaN NaN
232 2024-05-17 14:20:00+08:00 3641.273529 -1.922875 93.200000 NaN NaN NaN NaN
233 2024-05-17 14:25:00+08:00 3641.865556 -2.131363 94.000000 NaN NaN NaN NaN
234 2024-05-17 14:30:00+08:00 3642.788421 -3.209011 96.000000 NaN NaN NaN NaN
235 2024-05-17 14:35:00+08:00 3643.980500 -2.817446 96.800000 NaN NaN NaN NaN
236 2024-05-17 14:40:00+08:00 3645.153333 -1.045126 96.200000 NaN NaN NaN NaN
237 2024-05-17 14:45:00+08:00 3646.353636 -1.245336 97.600000 NaN NaN NaN NaN
238 2024-05-17 14:50:00+08:00 3647.678696 -0.320791 98.000000 NaN NaN NaN NaN
239 2024-05-17 14:55:00+08:00 3648.940833 1.540833 97.400000 NaN NaN NaN NaN
[13]:
# 绘制出时间价值(time_value), 标准 BS 定价与期权价格的差(bs_diff), 改进版 BS 定价与期权价格的差(tdbs_diff)
px.line(serial, x="dt", y=["time_value", "bs_diff", "tdbs_diff"])
[14]:
# 残差均值
serial[["bs_diff", "tdbs_diff"]].mean()
[14]:
bs_diff     0.873137
tdbs_diff   0.793846
dtype: float64
[15]:
# 标准模型的残差标准差
np.log(serial["bs"]/serial["close1"]).std()
[15]:
0.016289936725434342
[16]:
# 改进模型的残差标准差
np.log(serial["tdbs"]/serial["close1"]).std()
[16]:
0.016233962768335282
[17]:
api.close()
[17]: