Eka
Eka

Reputation: 15000

Cannot initialize python function

I have (re)wrote a back-test function in python using pandas

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

where positions and price are both dataframe of 1 column and 5 column respectively .

Inorder for checking error I run this function alone in my python but it is returning this error

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

SyntaxError: invalid syntax

Upvotes: 0

Views: 54

Answers (1)

EdChum
EdChum

Reputation: 393893

missing trailing parenthesis on line before:

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

should be:

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

Whenever you get a syntax error, look at the line before if the error and line in question look fine and doesn't make sense

Upvotes: 2

Related Questions