
    'i                     `    d dl mZmZmZmZ d dlZddlmZ dgZ	 G d dej                        Zy)    )absolute_importdivisionprint_functionunicode_literalsN   )TimeDrawDownCalmarc                   b    e Zd ZdZdZdej                  j                  fddfZd Z	d Z
d Zd	 Zy
)r	   a)  This analyzer calculates the CalmarRatio
    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

    See also:

      - https://en.wikipedia.org/wiki/Calmar_ratio

    Methods:
      - ``get_analysis``

        Returns a OrderedDict with a key for the time period and the
        corresponding rolling Calmar ratio

    Attributes:
      - ``calmar`` the latest calculated calmar ratio
    )collectionsmath	timeframe)period$   )fundNc                 x    t        | j                  j                  | j                  j                        | _        y )N)r   compression)r   pr   r   _maxddselfs    Y/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/analyzers/calmar.py__init__zCalmar.__init__S   s(    "TVV-=-=/3vv/A/AC    c                 z   t        d      | _        t        j                  t        d      g| j                  j
                  z  | j                  j
                        | _        | j                  j                  &| j                  j                  j                  | _        n| j                  j                  | _        | j                  s>| j                  j                  | j                  j                  j                                y | j                  j                  | j                  j                  j                         y )Nz-infNan)maxlen)float_mddr   dequer   r   _valuesr   strategybrokerfundmode	_fundmodeappendgetvalue	fundvaluer   s    r   startzCalmar.startW   s    &M	"((%,$&&--)G04 ) ?66;;!]]11::DN!VV[[DN~~LL 4 4 = = ?@LL 4 4 > >?r   c                 p   t        | j                  | j                  j                        | _        | j                  s>| j
                  j                  | j                  j                  j                                n9| j
                  j                  | j                  j                  j                         t        j                  | j
                  d   | j
                  d   z        t        | j
                        z  }|| j                  xs t        d      z  x| _        }|| j                   | j"                  <   y )Nr   Inf)maxr   r   maxddr$   r    r%   r!   r"   r&   r'   r   loglenr   calmarretsdtkey)r   rannr0   s      r   
on_dt_overzCalmar.on_dt_overe   s    		4;;#4#45	~~LL 4 4 = = ?@LL 4 4 > >?xxR(4<<?:;c$,,>OO#tyy'@E%LAAf &		$**r   c                 $    | j                          y )N)r4   r   s    r   stopzCalmar.stopp   s    r   N)__name__
__module____qualname____doc__packagesbt	TimeFrameMonthsparamsr   r(   r4   r6    r   r   r	   r	      sF    )V (H 
bll))*FC@	'r   )
__future__r   r   r   r   
backtraderr<    r   __all__TimeFrameAnalyzerBaser	   r@   r   r   <module>rF      s4   ** *   *RR%% Rr   