进入一个熊猫数据框,我通过API期权链检索股票的数据。在``到期''列中,您可以看到在此测试用例中,我有三个期权系列分别有到期时间:2019-08-15、2019-09-15和2019-10-15。
我想要实现的是:
这是一个接近我的实际环境的测试用例的代码:
import pandas as pd
undPrice = 202
df = pd.DataFrame(columns=['expiration', 'strike', 'undPrice', 'IV_model', 'desired_outcome'])
df['expiration'] = ['2019-08-15', '2019-08-15', '2019-08-15', '2019-08-15', '2019-08-15', '2019-08-15', '2019-08-15', '2019-08-15', '2019-09-15', '2019-09-15', '2019-09-15', '2019-09-15', '2019-09-15', '2019-09-15', '2019-09-15', '2019-09-15', '2019-09-15', '2019-09-15', '2019-10-15', '2019-10-15', '2019-10-15', '2019-10-15', '2019-10-15', '2019-10-15', '2019-10-15', '2019-10-15', '2019-10-15', '2019-10-15']
df['expiration'] = df['expiration'].apply(lambda x: pd.to_datetime(str(x), utc=True,format='%Y-%m-%d'))
df['strike'] = [170, 175, 180, 185, 190, 195, 200, 205, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205]
df['undPrice'] = [undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice, undPrice]
df['IV_model'] = [0.28, 0.27, 0.26, 0.25, 0.24, 0.23, 0.22, 0.21, 0.35, 0.34, 0.33, 0.32, 0.31, 0.30, 0.29, 0.28, 0.27, 0.26, 0.42, 0.41, 0.40, 0.39, 0.38, 0.37, 0.36, 0.35, 0.34, 0.33]
df['IV_model'] = df['IV_model'].map('{:.2%}'.format)
df['desired_outcome'] = [0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.28, 0.28, 0.28, 0.28, 0.28, 0.28, 0.28, 0.28, 0.28, 0.28, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34]
df['desired_outcome'] = df['desired_outcome'].map('{:.2%}'.format)
print(df)
这将是(期望的)结果(显然是手动填写的“ desired_outcome”):
expiration strike undPrice IV_model desired_outcome
0 2019-08-15 00:00:00+00:00 170 202 28.00% 22.00%
1 2019-08-15 00:00:00+00:00 175 202 27.00% 22.00%
2 2019-08-15 00:00:00+00:00 180 202 26.00% 22.00%
3 2019-08-15 00:00:00+00:00 185 202 25.00% 22.00%
4 2019-08-15 00:00:00+00:00 190 202 24.00% 22.00%
5 2019-08-15 00:00:00+00:00 195 202 23.00% 22.00%
6 2019-08-15 00:00:00+00:00 200 202 22.00% 22.00%
7 2019-08-15 00:00:00+00:00 205 202 21.00% 22.00%
8 2019-09-15 00:00:00+00:00 165 202 35.00% 28.00%
9 2019-09-15 00:00:00+00:00 170 202 34.00% 28.00%
10 2019-09-15 00:00:00+00:00 175 202 33.00% 28.00%
11 2019-09-15 00:00:00+00:00 180 202 32.00% 28.00%
12 2019-09-15 00:00:00+00:00 185 202 31.00% 28.00%
13 2019-09-15 00:00:00+00:00 190 202 30.00% 28.00%
14 2019-09-15 00:00:00+00:00 195 202 29.00% 28.00%
15 2019-09-15 00:00:00+00:00 200 202 28.00% 28.00%
16 2019-09-15 00:00:00+00:00 205 202 27.00% 28.00%
17 2019-09-15 00:00:00+00:00 210 202 26.00% 28.00%
18 2019-10-15 00:00:00+00:00 160 202 42.00% 34.00%
19 2019-10-15 00:00:00+00:00 165 202 41.00% 34.00%
20 2019-10-15 00:00:00+00:00 170 202 40.00% 34.00%
21 2019-10-15 00:00:00+00:00 175 202 39.00% 34.00%
22 2019-10-15 00:00:00+00:00 180 202 38.00% 34.00%
23 2019-10-15 00:00:00+00:00 185 202 37.00% 34.00%
24 2019-10-15 00:00:00+00:00 190 202 36.00% 34.00%
25 2019-10-15 00:00:00+00:00 195 202 35.00% 34.00%
26 2019-10-15 00:00:00+00:00 200 202 34.00% 34.00%
27 2019-10-15 00:00:00+00:00 205 202 33.00% 34.00%
我是Python编程的相对入门者,我已经走了很长一段路,但这超出了我的能力。我希望有人可以帮助我解决这个问题。
答案 0 :(得分:1)
这是一种方法:
通过找到undPrice和行使价之间的最小距离来创建IV_model到期的字典。
desiredOutcomeMap = df.groupby('expiration').apply(lambda x: df.loc[abs(x['undPrice']-x['strike']).idxmin(), 'IV_model']).to_dict()
然后将其映射到原始df。
df['desired_outcome'] = df['expiration'].map(desiredOutcomeMap)