0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

The line before of course! I kept looking for it in the error message ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.