无法初始化python函数

时间:2016-08-29 09:31:55

标签: python pandas

我已经(重新)使用pandas

在python中编写了一个反向测试函数
def backtest(positions,price,initial_capital=10000):
    #creating protfolio
    portfolio =positions*price['price']    
    pos_diff=positions.diff()

    #creating holidings
    portfolio['holidings']=(positions*price['price'].sum(axis=1)
    portfolio['cash']=initial_capital-(pos_diff*price['price']).sum(axis=1).cumsum()

    #full account equity
    portfolio['total']=portfolio['cash']+ portfolio['holidings']
    portfolio['return']=portfolio['total'].pct_change()
    return portfolio

其中位置价格分别是1列和5列的数据框。

为了检查错误,我在我的python中单独运行此函数,但它返回此错误

File "", line 8
    portfolio['cash']=initial_capital-(pos_diff*price['price']).sum(axis=1).cumsum()

SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:2)

之前缺少尾随括号:

portfolio['holidings']=(positions*price['price'].sum(axis=1)
                                               ^ need ) here

应该是:

portfolio['holidings']=(positions*price['price']).sum(axis=1)

每当出现语法错误时,请查看之前的行,如果错误和行有问题并且没有意义