在列中查找值以查找其他两列中的大多数匹配值

2024-04-20 10:47:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过API选项链检索股票的数据。在“expiration”列中,您可以看到在这个测试用例中,我有三个选项系列,其expiries分别为:2019-08-15、2019-09-15和2019-10-15。你知道吗

我想要实现的是:

  • 对于每个选项系列(本测试用例中为3个)
  • 查找最接近“undPrice”的“行权”价格(=标的股票价格)
  • 对于最接近“undPrice”的“行权”价格,请在“IV\ U模型”中查找相应的值(=隐含波动率)
  • 将该值填入该过期序列的所有选项组合的“期望的\u结果”列中(因此,在这个测试用例中,使用相同的值阻塞三次)
  • 因此,在一系列数据中基本上有三个查找。你知道吗

这是一个接近我的实时环境的测试用例的代码:

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) 

这将是(期望的)结果(显然是手动填写的“期望的结果”):

                  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编程的相对初学者,我已经走了很长的路,但这不是我的能力范围。我希望有人能帮我解决这个问题。你知道吗


Tags: 数据formatdfmodel选项测试用例价格pd
1条回答
网友
1楼 · 发布于 2024-04-20 10:47:04

有一种方法:

通过找到undPrice和strike之间的最小距离,创建一个到IV_模型的到期日dict。你知道吗

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)

相关问题 更多 >