
    'i-                     ~    d dl mZmZmZmZ d dlZd dlZd dlm	Z	m
Z
 ddlmZmZmZmZ  G d dej                         Zy)    )absolute_importdivisionprint_functionunicode_literalsN)items	iteritems   )
TimeReturnPositionsValueTransactionsGrossLeveragec                   b     e Zd ZdZdej
                  j                  fdfZd Z fdZ	d Z
 xZS )PyFolioa;  This analyzer uses 4 children analyzers to collect data and transforms it
    in to a data set compatible with ``pyfolio``

    Children Analyzer

      - ``TimeReturn``

        Used to calculate the returns of the global portfolio value

      - ``PositionsValue``

        Used to calculate the value of the positions per data. It sets the
        ``headers`` and ``cash`` parameters to ``True``

      - ``Transactions``

        Used to record each transaction on a data (size, price, value). Sets
        the ``headers`` parameter to ``True``

      - ``GrossLeverage``

        Keeps track of the gross leverage (how much the strategy is invested)

    Params:
      These are passed transparently to the children

      - timeframe (default: ``bt.TimeFrame.Days``)

        If ``None`` then the timeframe of the 1st data of the system will be
        used

      - compression (default: `1``)

        If ``None`` then the compression of the 1st data of the system will be
        used

    Both ``timeframe`` and ``compression`` are set following the default
    behavior of ``pyfolio`` which is working with *daily* data and upsample it
    to obtaine values like yearly returns.

    Methods:

      - get_analysis

        Returns a dictionary with returns as values and the datetime points for
        each return as keys
    	timeframe)compressionr	   c                     t        | j                  j                  | j                  j                        }t	        di || _        t        dd      | _        t        d      | _	        t               | _        y )N)r   r   T)headerscash)r    )dictpr   r   r
   _returnsr   
_positionsr   _transactionsr   
_gross_lev)selfdtfcomps     Z/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/analyzers/pyfolio.py__init__zPyFolio.__init__V   sZ    !1!1#'66#5#57 #-W-(DA)$7'/    c                 b   t         t        |           | j                  j	                         | j
                  d<   | j                  j	                         | j
                  d<   | j                  j	                         | j
                  d<   | j                  j	                         | j
                  d<   y )Nreturns	positionstransactions	gross_lev)	superr   stopr   get_analysisretsr   r   r   )r   	__class__s    r   r'   zPyFolio.stop_   s~    gt!##}}99;		)!%!=!=!?		+$($6$6$C$C$E		.!!%!=!=!?		+r    c                    ddl }ddl m} ddg}|j                  t        | j                  d         |d   |      }|j                  |j                        |_        |j                  j                  d      |_        |d   }| j                  d	   }t        |      D cg c]  \  }}|g|d
d z    }	}}|	j                  d      }|j                  |	|d   |      }
|j                  |
j                        |
_        |
j                  j                  d      |
_        | j                  d   }t               }t        |      D ]!  \  }}|D ]  }|j                  |g|z           # |j                  d      }|j                  ||d   |      }|j                  |j                        |_        |j                  j                  d      |_        ddg}|j                  t        | j                  d         |d   |      }|j                  |j                        |_        |j                  j                  d      |_        |d   }||
||fS c c}}w )a  Returns a tuple of 4 elements which can be used for further processing with
          ``pyfolio``

          returns, positions, transactions, gross_leverage

        Because the objects are meant to be used as direct input to ``pyfolio``
        this method makes a local import of ``pandas`` to convert the internal
        *backtrader* results to *pandas DataFrames* which is the expected input
        by, for example, ``pyfolio.create_full_tear_sheet``

        The method will break if ``pandas`` is not installed
        r   N)	DataFrameindexreturnr"   )r-   columnsUTCr#   r$   r%   )pandasr,   from_recordsr   r)   to_datetimer-   tz_localizepoplistappend)r   r2   DFcolsr"   r)   psskvpsr#   txsstxsv2r$   r%   glevs                    r   get_pf_itemszPyFolio.get_pf_itemsf   s2    	* "//)DIIi,@"A(,Q " ?**7==911%8x  ii$'0~6tq!qcAbcFl66vvayOOBd1gtOD	 ,,Y__=	#//55e<	 yy(f
 dO 	%DAq %

A38$%	% wwqzs$q'4H#//0B0BC)//;;EB %OOIdii.D$E*.q'4 $ A	 !,,Y__=	#//55e<	% Yd22C 7s   I)__name__
__module____qualname____doc__bt	TimeFrameDaysparamsr   r'   rC   __classcell__)r*   s   @r   r   r   !   s7    .` 
bll''(F
*@=3r    r   )
__future__r   r   r   r   collections
backtraderrH   backtrader.utils.py3r   r    r
   r   r   r   Analyzerr   r   r    r   <module>rS      s2   ** *   1 E EB3bkk B3r    