
    'iT                     j    d dl mZmZmZmZ d dlZd dlZd dlmZ ddl	m
Z
 ddlmZ  G d d	e      ZeZy)
    )absolute_importdivisionprint_functionunicode_literalsN)TimeFrameAnalyzerBase   )Returns   )standarddevc                        e Zd ZdZdZej                  j                  dej                  j                  dej                  j                  dej                  j                  diZd Z fdZ fd	Zd
 Zd Z xZS )VWRaJ  Variability-Weighted Return: Better SharpeRatio with Log Returns

    Alias:

      - VariabilityWeightedReturn

    See:

      - https://www.crystalbull.com/sharpe-ratio-better-with-log-returns/

    Params:

      - ``timeframe`` (default: ``None``)
        If ``None`` then the complete return over the entire backtested period
        will be reported

        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

      - ``tann`` (default: ``None``)

        Number of periods to use for the annualization (normalization) of the
        average returns. If ``None``, then standard ``t`` values will be used,
        namely:

          - ``days: 252``
          - ``weeks: 52``
          - ``months: 12``
          - ``years: 1``

      - ``tau`` (default: ``2.0``)

        factor for the calculation (see the literature)

      - ``sdev_max`` (default: ``0.20``)

        max standard deviation (see the literature)

      - ``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 returns as values and the datetime points for
        each return as keys

        The returned dict contains the following keys:

          - ``vwr``: Variability-Weighted Return
    ))tannN)taug?)sdev_maxg       @)fundNg     o@g      J@g      (@      ?c                     t        | j                  j                  | j                  j                  | j                  j                        | _        y )N)	timeframecompressionr   )r	   pr   r   r   _returnsselfs    V/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/analyzers/vwr.py__init__zVWR.__init__r   s2    $&&*:*:,0FF,>,>%)VV[[2    c                    t         t        |           | j                  j                  &| j
                  j                  j                  | _        n| j                  j                  | _        | j                  s+| j
                  j                  j                         g| _
        n&| j
                  j                  j                  g| _
        d g| _        y )N)superr   startr   r   strategybrokerfundmode	_fundmodegetvalue_pis	fundvalue_pns)r   	__class__s    r   r   z	VWR.startx   s    c4 66;;!]]11::DN!VV[[DN~~--6689DI--778DIF	r   c                    t         t        |           | j                  d   4| j                  j                          | j                  j                          | j                  j                         }|d   }|d   }g }t        t        | j                  | j                        d      D ]<  \  }}|\  }}||t        j                  ||z        z  z  dz
  }	|j                  |	       > t        |d      }
|dt        |
| j                  j                   z  | j                  j"                        z
  z  }|| j$                  d<   y )	Nravgrnorm100r   r   T)besselvwr)r   r   stopr'   r%   popr   get_analysis	enumeratezipmathexpappendr   powr   r   r   rets)r   rsr+   r,   dtsnpipnpipndtsdev_pr.   r(   s               r   r/   zVWR.stop   s   c4 99R= IIMMOIIMMO ]]'')&zj>  TYY		!:A> 	GAtFBrDHHTAX../#5BJJrN		 S.#FTVV__$<dffjj IIJ		%r   c                 Z    | j                   s|| j                  d<   y || j                  d<   y Nr*   )r#   r'   )r   cashvaluer&   sharess        r   notify_fundzVWR.notify_fund   s#    ~~!DIIbM%DIIbMr   c                     | j                   j                  | j                  d          | j                  j                  d        y rB   )r%   r6   r'   r   s    r   _on_dt_overzVWR._on_dt_over   s.    		2'		r   )__name__
__module____qualname____doc__paramsbt	TimeFrameDaysWeeksMonthsYears_TANNr   r   r/   rF   rH   __classcell__)r(   s   @r   r   r       so    AFF 	5
D
T
C	E26&r   r   )
__future__r   r   r   r   r4   
backtraderrN   r    r	   mathsupportr   r   VariabilityWeightedReturn r   r   <module>r\      s8   ** *   ,  %J
 JZ   r   