
    'i-                     t    d dl mZmZmZmZ d dlZddlmZ ddlm	Z	  G d d ee	            Z
 G d d	e
      Zy)
    )absolute_importdivisionprint_functionunicode_literalsN   )with_metaclass)
MetaParamsc                        e Zd ZdZ ed      \  ZZdZ fdZe	d        Z
e	d        Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )CommInfoBaseaY  Base Class for the Commission Schemes.

    Params:

      - ``commission`` (def: ``0.0``): base commission value in percentage or
        monetary units

      - ``mult`` (def ``1.0``): multiplier applied to the asset for
        value/profit

      - ``margin`` (def: ``None``): amount of monetary units needed to
        open/hold an operation. It only applies if the final ``_stocklike``
        attribute in the class is set to ``False``

      - ``automargin`` (def: ``False``): Used by the method ``get_margin``
        to automatically calculate the margin/guarantees needed with the
        following policy

          - Use param ``margin`` if param ``automargin`` evaluates to ``False``

          - Use param ``mult`` * ``price`` if ``automargin < 0``

          - Use param ``automargin`` * ``price`` if ``automargin > 0``

      - ``commtype`` (def: ``None``): Supported values are
        ``CommInfoBase.COMM_PERC`` (commission to be understood as %) and
        ``CommInfoBase.COMM_FIXED`` (commission to be understood as monetary
        units)

        The default value of ``None`` is a supported value to retain
        compatibility with the legacy ``CommissionInfo`` object. If
        ``commtype`` is set to None, then the following applies:

          - ``margin`` is ``None``: Internal ``_commtype`` is set to
            ``COMM_PERC`` and ``_stocklike`` is set to ``True`` (Operating
            %-wise with Stocks)

          - ``margin`` is not ``None``: ``_commtype`` set to ``COMM_FIXED`` and
            ``_stocklike`` set to ``False`` (Operating with fixed rount-trip
            commission with Futures)

        If this param is set to something else than ``None``, then it will be
        passed to the internal ``_commtype`` attribute and the same will be
        done with the param ``stocklike`` and the internal attribute
        ``_stocklike``

      - ``stocklike`` (def: ``False``): Indicates if the instrument is
        Stock-like or Futures-like (see the ``commtype`` discussion above)

      - ``percabs`` (def: ``False``): when ``commtype`` is set to COMM_PERC,
        whether the parameter ``commission`` has to be understood as XX% or
        0.XX

        If this param is ``True``: 0.XX
        If this param is ``False``: XX%

      - ``interest`` (def: ``0.0``)

        If this is non-zero, this is the yearly interest charged for holding a
        short selling position. This is mostly meant for stock short-selling

        The formula: ``days * price * abs(size) * (interest / 365)``

        It must be specified in absolute terms: 0.05 -> 5%

        .. note:: the behavior can be changed by overriding the method:
                 ``_get_credit_interest``

      - ``interest_long`` (def: ``False``)

        Some products like ETFs get charged on interest for short and long
        positions. If ths is ``True`` and ``interest`` is non-zero the interest
        will be charged on both directions

      - ``leverage`` (def: ``1.0``)

        Amount of leverage for the asset with regards to the needed cash

    Attributes:

      - ``_stocklike``: Final value to use for Stock-like/Futures-like behavior
      - ``_commtype``: Final value to use for PERC vs FIXED commissions

      This two are used internally instead of the declared params to enable the
      compatibility check described above for the legacy ``CommissionInfo``
      object

       )
)
commission        )mult      ?)marginN)commtypeN)	stocklikeF)percabsF)interestr   )interest_longF)leverager   )
automarginFc                 z   t         t        |           | j                  j                  | _        | j                  j                  | _        | j                  G| j                  j                  rd| _        | j                  | _        nd| _        | j                  | _        | j
                  s'| j                  j                  sd| j                  _        | j                  | j                  k(  r5| j                  j                  s| j                  xj                  dz  c_        | j                  j                  dz  | _        y )NFTr   g      Y@g     v@)superr   __init__pr   
_stockliker   	_commtyper   
COMM_FIXED	COMM_PERCr   r   r   _creditrate)self	__class__s    Q/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/comminfo.pyr   zCommInfoBase.__init__   s    lD*,&&** >>!vv}}"'!%"&!%tvv}}DFFM>>T^^+DFFNNFF&66??U2    c                 .    | j                   j                  S N)r   r   r"   s    r$   r   zCommInfoBase.margin   s    vv}}r%   c                     | j                   S r'   )r   r(   s    r$   r   zCommInfoBase.stocklike   s    r%   c                     | j                   j                  s| j                   j                  S | j                   j                  dk  r|| j                   j                  z  S || j                   j                  z  S )av  Returns the actual margin/guarantees needed for a single item of the
        asset at the given price. The default implementation has this policy:

          - Use param ``margin`` if param ``automargin`` evaluates to ``False``

          - Use param ``mult`` * ``price`` if ``automargin < 0``

          - Use param ``automargin`` * ``price`` if ``automargin > 0``
        r   )r   r   r   r   )r"   prices     r$   
get_marginzCommInfoBase.get_margin   sY     vv  66== VV"466;;&&tvv((((r%   c                 .    | j                   j                  S )z?Returns the level of leverage allowed for this comission scheme)r   r   r(   s    r$   get_leveragezCommInfoBase.get_leverage   s     vvr%   c                     | j                   s4t        | j                  j                  || j	                  |      z  z        S t        | j                  j                  ||z  z        S )zAReturns the needed size to meet a cash operation at a given price)r   intr   r   r,   )r"   r+   cashs      r$   getsizezCommInfoBase.getsize   sK    tvv$$//%2H*HIJJ466??dem455r%   c                 p    | j                   st        |      | j                  |      z  S t        |      |z  S )z9Returns the needed amount of cash an operation would costr   absr,   r"   sizer+   s      r$   getoperationcostzCommInfoBase.getoperationcost   s0    t9tu5554y5  r%   c                 ^    | j                   st        |      | j                  |      z  S ||z  S )ziReturns the value of size for given a price. For future-like
        objects it is fixed at size * marginr4   r6   s      r$   getvaluesizezCommInfoBase.getvaluesize   s-     t9tu555e|r%   c                     | j                   s't        |j                        | j                  |      z  S |j                  }|dk\  r||z  S |j                  |z  }||j                  |z
  |z  z  }|S )zkReturns the value of a position given a price. For future-like
        objects it is fixed at size * marginr   )r   r5   r7   r,   r+   )r"   positionr+   r7   values        r$   getvaluezCommInfoBase.getvalue   so     x}}%(>>>}}19%< %(..5(D00r%   c                     | j                   | j                  k(  r%t        |      | j                  j                  z  |z  S t        |      | j                  j                  z  S )zCalculates the commission of an operation at a given price

        pseudoexec: if True the operation has not yet been executed
        )r   r    r5   r   r   )r"   r7   r+   
pseudoexecs       r$   _getcommissionzCommInfoBase._getcommission   sK    
 >>T^^+t9tvv0005884y466,,,,r%   c                 *    | j                  ||d      S )zCCalculates the commission of an operation at a given price
        Tr@   rA   r6   s      r$   getcommissionzCommInfoBase.getcommission   s     ""44"@@r%   c                 *    | j                  ||d      S )NFrC   rD   r6   s      r$   confirmexeczCommInfoBase.confirmexec   s    ""45"AAr%   c                 @    |||z
  z  | j                   j                  z  S )z,Return actual profit and loss a position has)r   r   r"   r7   r+   newprices       r$   profitandlosszCommInfoBase.profitandloss   s    x%'(466;;66r%   c                 Z    | j                   s|||z
  z  | j                  j                  z  S y)z7Calculates cash adjustment for a given price differencer   )r   r   r   rI   s       r$   
cashadjustzCommInfoBase.cashadjust   s)    8e+,tvv{{::r%   c                    |j                   |j                  }}|dkD  r| j                  j                  sy|j	                         }|j
                  j	                         }||k  ry| j                  |||||z
  j                  ||      S )z?Calculates the credit due for short selling or product specificr   r   )r7   r+   r   r   datedatetime_get_credit_interestdays)r"   dataposdtr7   r+   dt0dt1s           r$   get_credit_interestz CommInfoBase.get_credit_interest  s|    hh		e!8DFF00ggill!#:((tU*-))9)93E 	Er%   c                 >    || j                   z  t        |      z  |z  S )a  
        This method returns  the cost in terms of credit interest charged by
        the broker.

        In the case of ``size > 0`` this method will only be called if the
        parameter to the class ``interest_long`` is ``True``

        The formulat for the calculation of the credit interest rate is:

          The formula: ``days * price * abs(size) * (interest / 365)``


        Params:
          - ``data``: data feed for which interest is charged

          - ``size``: current position size. > 0 for long positions and < 0 for
            short positions (this parameter will not be ``0``)

          - ``price``: current position price

          - ``days``: number of days elapsed since last credit calculation
            (this is (dt0 - dt1).days)

          - ``dt0``: (datetime.datetime) current datetime

          - ``dt1``: (datetime.datetime) datetime of previous calculation

        ``dt0`` and ``dt1`` are not used in the default implementation and are
        provided as extra input for overridden methods
        )r!   r5   )r"   rS   r7   r+   rR   rV   rW   s          r$   rQ   z!CommInfoBase._get_credit_interest  s#    > d&&&T2U::r%   )__name__
__module____qualname____doc__ranger    r   paramsr   propertyr   r   r,   r.   r2   r8   r:   r>   rA   rE   rG   rK   rM   rX   rQ   __classcell__)r#   s   @r$   r   r      s    Wr "!HIz	F38    )$
6!-A
B7E ;r%   r   c                       e Zd ZdZdZy)CommissionInfoa+  Base Class for the actual Commission Schemes.

    CommInfoBase was created to keep suppor for the original, incomplete,
    support provided by *backtrader*. New commission schemes derive from this
    class which subclasses ``CommInfoBase``.

    The default value of ``percabs`` is also changed to ``True``

    Params:

      - ``percabs`` (def: True): when ``commtype`` is set to COMM_PERC, whether
        the parameter ``commission`` has to be understood as XX% or 0.XX

        If this param is True: 0.XX
        If this param is False: XX%

    ))r   TN)rZ   r[   r\   r]   r_    r%   r$   rc   rc   4  s    "Fr%   rc   )
__future__r   r   r   r   rP   	utils.py3r   metabaser	   r   rc   rd   r%   r$   <module>rh      s:   ** *  %  S;>*- S;l\ r%   