
    'iq                         d dl mZmZmZmZ d dlZd dlmZ ddgZ	 G d dej                        Z G d dej                        Zy)    )absolute_importdivisionprint_functionunicode_literalsN)AutoOrderedDictDrawDownTimeDrawDownc                   >     e Zd ZdZdZ fdZd Zd Zd Zd Z	 xZ
S )r   a3  This analyzer calculates trading system drawdowns stats such as drawdown
    values in %s and in dollars, max drawdown in %s and in dollars, drawdown
    length and drawdown max length

    Params:

      - ``fund`` (default: ``None``)

        If ``None`` the actual mode of the broker (fundmode - True/False) will
        be autodetected to decide if the returns are based on the total net
        asset value or on the fund value. See ``set_fundmode`` in the broker
        documentation

        Set it to ``True`` or ``False`` for a specific behavior

    Methods:

      - ``get_analysis``

        Returns a dictionary (with . notation support and subdctionaries) with
        drawdown stats as values, the following keys/attributes are available:

        - ``drawdown`` - drawdown value in 0.xx %
        - ``moneydown`` - drawdown value in monetary units
        - ``len`` - drawdown length

        - ``max.drawdown`` - max drawdown value in 0.xx %
        - ``max.moneydown`` - max drawdown value in monetary units
        - ``max.len`` - max drawdown length
    )fundNc                     t         t        |           | j                  j                  &| j
                  j                  j                  | _        y | j                  j                  | _        y N)	superr   startpr   strategybrokerfundmode	_fundmodeself	__class__s    [/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/analyzers/drawdown.pyr   zDrawDown.startC   sD    h#%66;;!]]11::DN!VV[[DN    c                 J   t               | _        d| j                  _        d| j                  _        d| j                  _        d| j                  j
                  _        d| j                  j
                  _        d| j                  j
                  _        t        d      | _        y )Nr           -inf)r   retslendrawdown	moneydownmaxfloat	_maxvaluer   s    r   create_analysiszDrawDown.create_analysisJ   sj    #%			 		!				!$		"%		vr   c                 8    | j                   j                          y r   )r   _closer%   s    r   stopzDrawDown.stopW   s    		r   c                     | j                   s#|| _        t        | j                  |      | _        y || _        t        | j                  |      | _        y r   )r   _valuer"   r$   )r   cashvalue	fundvaluesharess        r   notify_fundzDrawDown.notify_fundZ   s;    ~~DK 7DN#DK ;DNr   c                    | j                   }| j                  | j                  z
  x|_        }d|z  | j                  z  x|_        }t        |j
                  j                  |      |j
                  _        t        |j
                  j                  |      x|j
                  _        }|r|j                  dz   nd|_        t        |j
                  j                  |j                        |j
                  _        y )N      Y@   r   )r   r$   r+   r!   r    r"   r   )r   rr!   r    maxdrawdowns        r   nextzDrawDown.nextb   s    II #'..4;;">>i %	 1DNN BB
X aeeooy9'*155>>8'DD%	1		155)	r   )__name__
__module____qualname____doc__paramsr   r&   r)   r0   r6   __classcell__r   s   @r   r   r      s(    >F)'<*r   c                   2     e Zd ZdZdZ fdZd Zd Z xZS )r	   a  This analyzer calculates trading system drawdowns on the chosen
    timeframe which can be different from the one used in the underlying data
    Params:

      - ``timeframe`` (default: ``None``)
        If ``None`` the ``timeframe`` of the 1st data in the system will be
        used

        Pass ``TimeFrame.NoTimeFrame`` to consider the entire dataset with no
        time constraints

      - ``compression`` (default: ``None``)

        Only used for sub-day timeframes to for example work on an hourly
        timeframe by specifying "TimeFrame.Minutes" and 60 as compression

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

      - ``fund`` (default: ``None``)

        If ``None`` the actual mode of the broker (fundmode - True/False) will
        be autodetected to decide if the returns are based on the total net
        asset value or on the fund value. See ``set_fundmode`` in the broker
        documentation

        Set it to ``True`` or ``False`` for a specific behavior

    Methods:

      - ``get_analysis``

        Returns a dictionary (with . notation support and subdctionaries) with
        drawdown stats as values, the following keys/attributes are available:

        - ``drawdown`` - drawdown value in 0.xx %
        - ``maxdrawdown`` - drawdown value in monetary units
        - ``maxdrawdownperiod`` - drawdown length

      - Those are available during runs as attributes
        - ``dd``
        - ``maxdd``
        - ``maxddlen``
    r   c                 0   t         t        |           | j                  j                  &| j
                  j                  j                  | _        n| j                  j                  | _        d| _	        d| _
        d| _        t        d      | _        d| _        y )Nr   r   r   )r   r	   r   r   r   r   r   r   r   ddmaxddmaxddlenr#   peakddlenr   s    r   r   zTimeDrawDown.start   sk    lD')66;;!]]11::DN!VV[[DN
&M	
r   c                    | j                   s%| j                  j                  j                         }n | j                  j                  j                  }|| j
                  kD  r|| _        d| _        d| j
                  |z
  z  | j
                  z  x| _        }| xj                  t        |      z  c_        t        | j                  |      | _
        t        | j                  | j                        | _        y )Nr   r2   )r   r   r   getvaluer.   rC   rD   r@   boolr"   rA   rB   )r   r-   r@   s      r   
on_dt_overzTimeDrawDown.on_dt_over   s    ~~MM((113EMM((22E 499DIDJ 		E 12TYY>>"

d2h
 R(
DMM4::6r   c                 h    | j                   | j                  d<   | j                  | j                  d<   y )Nr5   maxdrawdownperiod)rA   r   rB   r%   s    r   r)   zTimeDrawDown.stop   s'    #'::		- )-		%&r   )	r7   r8   r9   r:   r;   r   rH   r)   r<   r=   s   @r   r	   r	   q   s    ,\F
7&7r   )
__future__r   r   r   r   
backtraderbtbacktrader.utilsr   __all__Analyzerr   TimeFrameAnalyzerBaser	    r   r   <module>rS      sJ   ** *  , ~
&O*r{{ O*dT72++ T7r   