You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Pandas计算多股票NetMF累计值(自定义规则)

基于Pandas实现多股票NetMF计算

示例原始数据

stockdatetimeAVG_PriceMFNetMF(预期结果)
ABCD2022-12-06 09:15:00234.50237864237864
ABCD2022-12-06 09:16:00236.55357784595648
ABCD2022-12-06 09:17:00233.23334959260689
ABCD2022-12-06 09:18:00233.23498756759445
ABCD2022-12-06 09:19:00225.23456789302656
ABCD2022-12-06 09:20:00222.23678546-375890
ABCD2022-12-06 09:21:00222.23567483-943373
ABCD2022-12-06 09:22:00220.2367483-1010856

NetMF计算规则

  1. 09:15时间点,NetMF等于对应MF值;
  2. 09:16及之后的时间点,按以下逻辑计算:
    • 当前AVG_Price > 前一刻AVG_Price:NetMF = 前一刻NetMF + 当前MF
    • 当前AVG_Price < 前一刻AVG_Price:NetMF = 前一刻NetMF - 当前MF
    • 当前AVG_Price = 前一刻AVG_Price且前一刻NetMF < 0:NetMF = 前一刻NetMF - 当前MF
    • 当前AVG_Price = 前一刻AVG_Price且前一刻NetMF >= 0:NetMF = 前一刻NetMF + 当前MF

Pandas实现方案

由于NetMF的计算依赖前一行结果,需按股票分组后逐行迭代计算,代码如下:

import pandas as pd

# 1. 构造或读取数据(此处含多股票示例)
data = {
    'stock': ['ABCD']*8 + ['EFGH']*3,
    'datetime': ['2022-12-06 09:15:00', '2022-12-06 09:16:00', '2022-12-06 09:17:00',
                '2022-12-06 09:18:00', '2022-12-06 09:19:00', '2022-12-06 09:20:00',
                '2022-12-06 09:21:00', '2022-12-06 09:22:00',
                '2022-12-06 09:15:00', '2022-12-06 09:16:00', '2022-12-06 09:17:00'],
    'AVG_Price': [234.50, 236.55, 233.23, 233.23, 225.23, 222.23, 222.23, 220.23,
                  150.00, 152.00, 152.00],
    'MF': [237864, 357784, 334959, 498756, 456789, 678546, 567483, 67483,
           100000, 200000, 150000]
}
df = pd.DataFrame(data)

# 2. 预处理:转换时间类型,按股票和时间排序
df['datetime'] = pd.to_datetime(df['datetime'])
df = df.sort_values(by=['stock', 'datetime']).reset_index(drop=True)

# 3. 定义分组计算NetMF的函数
def compute_netmf(group):
    group['NetMF'] = 0.0
    # 初始化第一个时间点的NetMF
    first_idx = group.index[0]
    group.loc[first_idx, 'NetMF'] = group.loc[first_idx, 'MF']
    
    # 从第二个数据点开始循环计算
    for i in range(1, len(group)):
        current_idx = group.index[i]
        prev_idx = group.index[i-1]
        
        prev_netmf = group.loc[prev_idx, 'NetMF']
        curr_price = group.loc[current_idx, 'AVG_Price']
        prev_price = group.loc[prev_idx, 'AVG_Price']
        curr_mf = group.loc[current_idx, 'MF']
        
        if curr_price > prev_price:
            group.loc[current_idx, 'NetMF'] = prev_netmf + curr_mf
        elif curr_price < prev_price:
            group.loc[current_idx, 'NetMF'] = prev_netmf - curr_mf
        else:
            # 价格相等时根据前一刻NetMF的正负判断
            if prev_netmf < 0:
                group.loc[current_idx, 'NetMF'] = prev_netmf - curr_mf
            else:
                group.loc[current_idx, 'NetMF'] = prev_netmf + curr_mf
    return group

# 4. 按股票分组执行计算
df = df.groupby('stock', group_keys=False).apply(compute_netmf)

# 5. 查看结果
print(df)

代码说明

  • 预处理步骤确保数据按股票和时间顺序排列,保证计算逻辑的正确性;
  • 通过groupby('stock')实现多股票的独立计算,不同股票的时间序列互不干扰;
  • 逐行迭代计算时严格遵循给定规则,覆盖所有价格对比场景;
  • 示例中添加了第二只股票EFGH的测试数据,验证多股票场景的支持能力。

内容的提问来源于stack exchange,提问作者Naveen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 14:45:57