如何在每天的Pandas DataFrame列中获得滚动盈亏?

时间:2017-02-21 00:43:17

标签: python pandas dataframe

概述
我正在重新测试交易策略,我希望能够根据策略表现逐日看到盈亏。

方法
我存储的股票数据的交易信号是“NoTrade”,“Win”和“Loss”。 NoTrade意味着没有采取交易,Win意味着交易,它是有利可图的,损失意味着交易,但是无利可图。我写了一个循环来检查是否有信号。如果有赢或输,那么我的账户余额会更新以反映收益或损失。

DataFrame

            WinLoss  ProfitAndLossPofChg  AccountBalance
D
2010-12-08  NoTrade             0.000000          2000.0
2010-12-09  NoTrade             0.000000          2000.0
2010-12-10  NoTrade             0.000000          2000.0
2010-12-13  NoTrade             0.000000          2000.0
2010-12-14  NoTrade             0.000000          2000.0
2010-12-15     Loss            -0.030842          2000.0
2010-12-16      Win             0.000539          2000.0
2010-12-17  NoTrade             0.000000          2000.0
2010-12-20  NoTrade             0.000000          2000.0
2010-12-21  NoTrade             0.000000          2000.0
2010-12-22      Win             0.014686          2000.0
2010-12-23  NoTrade             0.000000          2000.0
2010-12-27  NoTrade             0.000000          2000.0
2010-12-28     Loss            -0.006190          2000.0
2010-12-29  NoTrade             0.000000          2000.0
2010-12-30  NoTrade             0.000000          2000.0
2010-12-31  NoTrade             0.000000          2000.0
2011-01-03     Loss            -0.055686          2000.0
2011-01-04     Loss            -0.025471          2000.0
2011-01-05     Loss            -0.051420          2000.0
2011-01-06     Loss            -0.000299          2000.0
2011-01-07  NoTrade             0.000000          2000.0
2011-01-10  NoTrade             0.000000          2000.0
2011-01-11      Win             0.003719          2000.0
2011-01-12  NoTrade             0.000000          2000.0
2011-01-13     Loss            -0.041218          2000.0
2011-01-14      Win             0.033365          2000.0
2011-01-18      Win             0.018628          2000.0
2011-01-19  NoTrade             0.000000          2000.0
2011-01-20     Loss            -0.020820          2000.0

代码

def ProfitAndLoss(DataFrame):

    df = DataFrame

    for i in df.WinLoss:

        if i == "NoTrade":
            df.AccountBalance = df.AccountBalance.shift(-1)

        elif i == "Win":
            df.AccountBalance = df.AccountBalance.shift(-1) *  df.ProfitAndLossPofChg

        elif i == "Loss":
            df.AccountBalance = df.AccountBalance.shift(-1) *  df.ProfitAndLossPofChg

输出

            WinLoss  ProfitAndLossPofChg  AccountBalance
D
2010-12-08  NoTrade             0.000000             0.0
2010-12-09  NoTrade             0.000000             0.0
2010-12-10  NoTrade             0.000000             0.0
2010-12-13  NoTrade             0.000000             0.0
2010-12-14  NoTrade             0.000000             0.0
2010-12-15     Loss            -0.030842            -0.0
2010-12-16      Win             0.000539             0.0
2010-12-17  NoTrade             0.000000             0.0
2010-12-20  NoTrade             0.000000             0.0
2010-12-21  NoTrade             0.000000             0.0
2010-12-22      Win             0.014686             0.0
2010-12-23  NoTrade             0.000000             0.0
2010-12-27  NoTrade             0.000000             0.0
2010-12-28     Loss            -0.006190            -0.0
2010-12-29  NoTrade             0.000000             0.0
2010-12-30  NoTrade             0.000000             0.0
2010-12-31  NoTrade             0.000000             0.0
2011-01-03     Loss            -0.055686            -0.0
2011-01-04     Loss            -0.025471            -0.0
2011-01-05     Loss            -0.051420            -0.0
2011-01-06     Loss            -0.000299            -0.0
2011-01-07  NoTrade             0.000000             0.0
2011-01-10  NoTrade             0.000000             0.0
2011-01-11      Win             0.003719             0.0
2011-01-12  NoTrade             0.000000             0.0
2011-01-13     Loss            -0.041218            -0.0
2011-01-14      Win             0.033365             0.0
2011-01-18      Win             0.018628             0.0
2011-01-19  NoTrade             0.000000             0.0
2011-01-20     Loss            -0.020820            -0.0

问题
正如您所看到的,我的帐户余额全是零;未显示滚动损益,起始账户余额为$ 2000.00。在我的职能中,我认为如果没有交易,那么只需取得前一天的余额并使其达到今天的余额,或者如果是赢或输交易,则按照前一天的余额计算当前的收益或损失。我认为这会显示出盈利和亏损,但我想我错了。这是一个很长的说法,我不知道。

2 个答案:

答案 0 :(得分:1)

无需遍历数据帧。将shift应用于pd系列' df.AccountBalance'创造另一个系列。试试这个:

df['AccountBalance'] += df['AccountBalance'].shift(-1) * df['ProfitAndLossPofChg']

答案 1 :(得分:1)

def ProfitAndLoss(df):

    df = df.copy()
    df = df.reset_index()
    for index,row in df.iterrows():
        if index == 0:
            continue
        if row['WinLoss'] == "NoTrade":
            df['AccountBalance'][index] = df['AccountBalance'][index-1]

        elif row['WinLoss'] in ["Win", "Loss"]:
            df['AccountBalance'][index] = df['AccountBalance'][index-1] *  (1 + df['ProfitAndLossPofChg'][index])

    return df

print(ProfitAndLoss(df).set_index('D'))

输出:

            WinLoss  ProfitAndLossPofChg  AccountBalance
D
2010-12-08  NoTrade             0.000000     2000.000000
2010-12-09  NoTrade             0.000000     2000.000000
2010-12-10  NoTrade             0.000000     2000.000000
2010-12-13  NoTrade             0.000000     2000.000000
2010-12-14  NoTrade             0.000000     2000.000000
2010-12-15     Loss            -0.030842     1938.316000
2010-12-16      Win             0.000539     1939.360752
2010-12-17  NoTrade             0.000000     1939.360752
2010-12-20  NoTrade             0.000000     1939.360752
2010-12-21  NoTrade             0.000000     1939.360752
2010-12-22      Win             0.014686     1967.842204
2010-12-23  NoTrade             0.000000     1967.842204
2010-12-27  NoTrade             0.000000     1967.842204
2010-12-28     Loss            -0.006190     1955.661261
2010-12-29  NoTrade             0.000000     1955.661261
2010-12-30  NoTrade             0.000000     1955.661261
2010-12-31  NoTrade             0.000000     1955.661261
2011-01-03     Loss            -0.055686     1846.758308
2011-01-04     Loss            -0.025471     1799.719527
2011-01-05     Loss            -0.051420     1707.177949
2011-01-06     Loss            -0.000299     1706.667503
2011-01-07  NoTrade             0.000000     1706.667503
2011-01-10  NoTrade             0.000000     1706.667503
2011-01-11      Win             0.003719     1713.014599
2011-01-12  NoTrade             0.000000     1713.014599
2011-01-13     Loss            -0.041218     1642.407564
2011-01-14      Win             0.033365     1697.206492
2011-01-18      Win             0.018628     1728.822055
2011-01-19  NoTrade             0.000000     1728.822055
2011-01-20     Loss            -0.020820     1692.827979