首页 > 解决方案 > pandas 中的条件 cumsum / floor 值

问题描述

我正在尝试在 cumsum 中使用下限和上限值。只要 uniqueEntry 有一个正值,Positions 就会增加,而只要 uniqueExit 是正数,Positions 就会减少。然而,仓位应该有 0 的底值和 3 的上限。

其次,对于每个 Symbol,如果在 Positions 处于上限时触发 uniqueEntry,则在 Positions 计算中将忽略 uniqueEntry 和 uniqueExit(每个 Symbol),直到 Positions 低于上限时触发 uniqueEntry。最终示例中的所需结果。

在实践中,我正在查看股票头寸,将头寸数量限制为一个值并排除其余部分。这可能没有循环吗?

测试数据:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta

np.random.seed(56)
symbols, symlen = 2, 3

times =             np.arange(datetime(2021, 1, 5), datetime(2021, 1, 12), timedelta(days=1)).astype(np.datetime64)
tlen =              len(times)
A, Z =              np.array(['A', 'Z']).view('int32')
symbol_names =      np.random.randint(low=A, high=Z, size=symbols * symlen, dtype='int32').view(f'U{symlen}')
limit =             np.random.randint(low=0, high=5, size=symbols*tlen, dtype='int32')
reset =             np.random.randint(low=-1, high=1, size=symbols*tlen, dtype='int32')
times =             np.concatenate([times] * symbols)
names =             np.array([y for x in [[s] * tlen for s in symbol_names] for y in x])
value_col =         np.random.randint(low=30, high=60, size=len(times), dtype='uint32')

df = pd.DataFrame({'Values': value_col}, index=[times, names])
df.index = df.index.set_names(['Date', 'Symbol'])

创建确定条件的列:

df['uniqueEntry'] = np.where(df.Values > 45, 1, 0)
df['uniqueExit'] = np.where(df.Values < 40, 1, 0)

排序和累加:

df = df.sort_index(level='Date')
df['Positions'] = (df.uniqueEntry - df.uniqueExit).cumsum()

当前结果:

df

                    Values  uniqueEntry     uniqueExit  Positions
Date        Symbol              
2021-01-05  ACL     42      0               0           0
            VEP     40      0               0           0
2021-01-06  ACL     30      0               1           -1
            VEP     31      0               1           -2
2021-01-07  ACL     38      0               1           -3
            VEP     46      1               0           -2
2021-01-08  ACL     59      1               0           -1
            VEP     43      0               0           -1
2021-01-09  ACL     52      1               0           0
            VEP     50      1               0           1
2021-01-10  ACL     34      0               1           0
            VEP     36      0               1           -1
2021-01-11  ACL     44      0               0           -1
            VEP     48      1               0           0

期望的结果:

                    Values  uniqueEntry     uniqueExit  Positions
Date        Symbol              
2021-01-05  ACL     42      0               0           0
            VEP     40      0               0           0
2021-01-06  ACL     30      0               1           0
            VEP     31      0               1           0
2021-01-07  ACL     38      0               1           0
            VEP     46      1               0           1
2021-01-08  ACL     59      1               0           2
            VEP     43      0               0           2
2021-01-09  ACL     52      1               0           3
            VEP     50      1               0           3 <- should be excluded
2021-01-10  ACL     34      0               1           2
            VEP     36      0               1           2 <- should be ignored as uniqueEntry triggered while Positions = 3
2021-01-11  ACL     44      0               0           1
            VEP     48      1               0           2

标签: pythonpandasmulti-index

解决方案


推荐阅读