Skip to content

code fix : estimate_intraday 'date' is both an index level and a column label, which is ambiguous. #711

Open
@cateyelow

Description

@cateyelow

Problem Description

pyfoliozer = firstStrat.analyzers.getbyname('pyfolio')
returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()

import pyfolio
pyfolio.create_full_tear_sheet(
    returns,
    transactions=transactions,
    positions=positions,
    live_start_date='2022-03-01',
    round_trips=True)

Please provide the full traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[/tmp/ipykernel_1028383/1898883094.py](https://file+.vscode-resource.vscode-cdn.net/tmp/ipykernel_1028383/1898883094.py) in ?()
      1 import pyfolio
----> 2 pyfolio.create_full_tear_sheet(
      3     returns,
      4     transactions=transactions,
      5     positions=positions,

[~/.local/lib/python3.11/site-packages/pyfolio/tears.py](https://file+.vscode-resource.vscode-cdn.net/hdd1/Whisper/Binance_Boosting/Binance_Boosting/30min_V2/~/.local/lib/python3.11/site-packages/pyfolio/tears.py) in ?(returns, positions, transactions, market_data, benchmark_rets, slippage, live_start_date, sector_mappings, bayesian, round_trips, estimate_intraday, hide_positions, cone_std, bootstrap, unadjusted_returns, style_factor_panel, sectors, caps, shares_held, volumes, percentile, turnover_denom, set_context, factor_returns, factor_loadings, pos_in_dollars, header_rows, factor_partitions)
    194         unadjusted_returns = returns.copy()
    195         returns = txn.adjust_returns_for_slippage(returns, positions,
    196                                                   transactions, slippage)
    197 
--> 198     positions = utils.check_intraday(estimate_intraday, returns,
    199                                      positions, transactions)
    200 
    201     create_returns_tear_sheet(

[~/.local/lib/python3.11/site-packages/pyfolio/utils.py](https://file+.vscode-resource.vscode-cdn.net/hdd1/Whisper/Binance_Boosting/Binance_Boosting/30min_V2/~/.local/lib/python3.11/site-packages/pyfolio/utils.py) in ?(estimate, returns, positions, transactions)
    296             if detect_intraday(positions, transactions):
    297                 warnings.warn('Detected intraday strategy; inferring positi' +
    298                               'ons from transactions. Set estimate_intraday' +
    299                               '=False to disable.')
--> 300                 return estimate_intraday(returns, positions, transactions)
...
   1795                 f"{label_article} {label_type} label, which is ambiguous."
   1796             )
-> 1797             raise ValueError(msg)

ValueError: 'date' is both an index level and a column label, which is ambiguous.
Output is truncated. View as a [scrollable element](command:cellOutput.enableScrolling?aab8a8ca-bc5a-4c37-b919-c6db578d14de) or open in a [text editor](command:workbench.action.openLargeOutput?aab8a8ca-bc5a-4c37-b919-c6db578d14de). Adjust cell output [settings](command:workbench.action.openSettings?%5B%22%40tag%3AnotebookOutputLayout%22%5D)...

Please provide any additional information below:
here is modified estimate_intraday

def estimate_intraday(returns, positions, transactions, EOD_hour=23):
    """
    Intraday strategies will often not hold positions at the day end.
    This attempts to find the point in the day that best represents
    the activity of the strategy on that day, and effectively resamples
    the end-of-day positions with the positions at this point of day.
    The point of day is found by detecting when our exposure in the
    market is at its maximum point. Note that this is an estimate.

    Parameters
    ----------
    returns : pd.Series
        Daily returns of the strategy, noncumulative.
         - See full explanation in create_full_tear_sheet.
    positions : pd.DataFrame
        Daily net position values.
         - See full explanation in create_full_tear_sheet.
    transactions : pd.DataFrame
        Prices and amounts of executed trades. One row per trade.
         - See full explanation in create_full_tear_sheet.

    Returns
    -------
    pd.DataFrame
        Daily net position values, resampled for intraday behavior.
    """
    # Construct DataFrame of transaction amounts
    txn_val = transactions.copy()
    txn_val.index.names = ['date']
    txn_val['value'] = txn_val.amount * txn_val.price
    txn_val = txn_val.reset_index().pivot_table(
        index='date', values='value',
        columns='symbol').replace(np.nan, 0)

    # index convert to datetime index
    txn_val = txn_val.reset_index()
    txn_val['date'] = pd.to_datetime(txn_val['date'])
    txn_val.set_index('date', inplace=True)

    # Cumulate transaction amounts each day
    txn_val = txn_val.groupby(pd.Grouper(freq='24H')).cumsum()
    print(txn_val)

    # Calculate exposure, then take peak of exposure every day
    txn_val['exposure'] = txn_val.abs().sum(axis=1)
    condition = (txn_val['exposure'] == txn_val.groupby(
        pd.Grouper(freq='24H'))['exposure'].transform(max))
    txn_val = txn_val[condition].drop('exposure', axis=1)

    # Compute cash delta
    txn_val['cash'] = -txn_val.sum(axis=1)

    # Shift EOD positions to positions at start of next trading day
    positions_shifted = positions.copy().shift(1).fillna(0)
    starting_capital = positions.iloc[0].sum() / (1 + returns[0])
    positions_shifted.cash[0] = starting_capital

    # Format and add start positions to intraday position changes
    txn_val.index = txn_val.index.normalize()
    corrected_positions = positions_shifted.add(txn_val, fill_value=0)
    corrected_positions.index.name = 'period_close'
    corrected_positions.columns.name = 'sid'

    return corrected_positions

Versions

  • Pyfolio version:
  • Python version:
  • Pandas version:
  • Matplotlib version:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions