pyfolio icon indicating copy to clipboard operation
pyfolio copied to clipboard

Exception has occurred: AttributeError 'DataFrame' object has no attribute 'amount'

Open cmperal opened this issue 5 years ago • 4 comments

Problem Description

Dear,

I'm trying to parse my generated backtest object into 'returns', 'positions' and 'transactions' by using function 'pf.utils.extract_rets_pos_txn_from_zipline' however, when I call such function I get this error "Exception has occurred: AttributeError 'DataFrame' object has no attribute 'amount'". I have the pyfolio logic within the function "analyze" and it is called from the function "run_algorithm" (I follow the quantopian pipeline)

This is the location where I get the error:

def analyze(context, perf):
    returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(perf)

This is how I call the above function:

run_algorithm(
    start=start_date, 
    end=end_date, 
    initialize=initialize, 
    handle_data=handle_data,
    analyze=analyze,
    capital_base=10000,
    data_frequency = 'daily',
    bundle='quantopian-quandl')

Thank you.

Versions

  • Pyfolio version: 0.9.2
  • Python version: 3.6.12
  • Pandas version: 0.22.0
  • Matplotlib version: 3.3.2

cmperal avatar Nov 29 '20 18:11 cmperal

@cmperal I'm having the same issue. Have you been able to solve it?

george-adams1 avatar Mar 05 '21 02:03 george-adams1

this probably is because your daily net positions are all zero, so during the function call the positions extract failed because there's no daily end positions.

ayxemma avatar Apr 19 '21 03:04 ayxemma

I have the same issue. How to address it?

bergen288 avatar May 15 '21 20:05 bergen288

Ok, the root cause is that no trade is triggered in handle_data function. Try to low the trigger condition from RSI 90/10 to 80/20 to avoid the issue.

def handle_data(context, data): 
    price_hist = data.history(context.stock, ["open", "high", "low","close"], context.rolling_window, "1d")             
    stock=sdf.retype(price_hist)   
    rsi = stock.get('rsi_12')
    if rsi[-1] > 90:
        order_target_percent(context.stock, 0.0)     
    elif rsi[-1] < 10:
        order_target_percent(context.stock, 1.0)   

bergen288 avatar May 15 '21 20:05 bergen288