总览
我正在重新测试交易策略,并希望能够根据策略的性能逐日查看损益。

方法
我存储的股票数据带有表示“ NoTrade”,“ Win”和“ Loss”的交易信号。 NoTrade表示未进行任何交易,Win表示已进行了交易且有利可图,而亏损则表示已进行了交易但无利可图。我编写了一个循环来检查是否有信号。如果有赢或亏,那么我的帐户余额会更新以反映盈亏。

数据框

            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的滚动损益。在我的职能中,我认为,如果没有交易,则仅获取前几天的余额并使其成为今天的余额,或者如果是成功或失败的交易,则将前几天的余额乘以当前损益。我认为这将显示连续的盈亏总额,但我想我觉得错了。这是我不知道的长话。

最佳答案

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

09-27 11:02