python - 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
解决方案
推荐阅读
- python - 为什么导入平台上的 Python3 platform.processor 输出为空?
- html - 图像和标题之间不需要的空间
- google-cloud-monitoring - 监控 PubSub 的推送请求?
- c - 为什么我的 C 程序在迭代变量达到 2100 时停止?
- flutter - 如何在颤动中制作这样的自定义应用栏?
- python-3.x - 用 django 模型对象中的默认值替换所有等于 Null 的字段的更优雅的方法
- ios - 如何在 HStack 中始终位于中心的 Spacers 之间创建视图?
- apache-spark - Glue Crawler 可以爬取 deltalake 文件以在 aws 胶水目录中创建表吗?
- c# - Azure Active Directory: Bearer error="invalid_token", error_description="The signature is invalid"
- scala - IntelliJ 在创建 Scala 项目后在 build.sbt 中给出“无法解析符号”的错误