pandas DataFrame 的矢量化回测创建 [英] vectorized backtest creation of pandas DataFrame

查看:120
本文介绍了pandas DataFrame 的矢量化回测创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在发布几个可能没有得到妥善解决/描述的问题后的最后一次尝试.我想实现这个 DataFrame 作为结果:

This is my final try after posting several questions that might have been badly addressed/described. I want to achive this DataFrame as result:

        Signal   price  buy_units   sell_units   cashflow  balance
   Index
      0     0       40          0         0           0     100000
      1     1       50       2000         0      -100000         0
      2    -1      100          0     -2000      +200000    200000  
      3     1       50       4000         0      -200000         0
      4    -1       70          0     -4000      +280000    280000

这是一个股票交易策略回测.当 Signal 为 ==1 buy_units 等于当前 balance(前一行的值)除以 price.balance 然后减少 cashflow(即单位 x 价格*-1).其余部分应该是不言自明的.问题:计算 buy_units 无需迭代但以矢量化方式计算.谁知道如何解决这个问题?

It's a stock trading strategy backtest. When Signal is ==1 buy_units is equal to current balance(value from prior row) divided by price. balance then becomes reduced by cashflow (that is units x price*-1). The remainder should be self-explanatory. The problem: calculating buy_units without iteration but in vectorized fashion. Who has an idea how to solve this?

最终我想在一个带有计算单位"和计算余额"方法的类中设计它,但这在第一步中不是必需的.

ultimately I would like to design this in a class with a "Calculate units" and "calculate balance" method but this is not necessary in first step.

推荐答案

如果 -1+1 信号总是像示例中那样交替,则平衡步n+2 等于 n 步的余额乘以 n+1n+2.

If -1 and +1 signals always alternate as in the example, then the balance in step n+2 is equal to the balance in step n multiplied by the price return between n+1 and n+2.

我使用累积乘积将其转换为 Pandas 中的矢量化操作:

I use the cumulated product to translate this into vectorized operations in pandas:

# initialize balance
df['balance'] = 0.0
df.balance.iloc[0] = 10000.0

# calculate returns
df['return'] = df.price / df.price.shift()

# calculate balance where signal is -1
df.loc[df.Signal == -1, 'balance'] = \
    df.balance.iloc[0] * df.loc[df.Signal == -1, 'return'].cumprod()

现金流和单位数量可以很容易地从余额中计算出来.

The cashflows and number of units can easily be computed from the balance.

这篇关于pandas DataFrame 的矢量化回测创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆