
    'i                     >    d dl mZmZmZmZ d dlmZ  G d de      Zy)    )absolute_importdivisionprint_functionunicode_literals)copyc                   N    e Zd ZdZd ZddZd Zd Zd Zd Z	e	Z
d Zd	 ZddZy
)Positionag  
    Keeps and updates the size and price of a position. The object has no
    relationship to any asset. It only keeps size and price.

    Member Attributes:
      - size (int): current size of the position
      - price (float): current price of the position

    The Position instances can be tested using len(position) to see if size
    is not null
    c                 t   t               }|j                  d       |j                  dj                  | j                               |j                  dj                  | j                               |j                  dj                  | j
                               |j                  dj                  | j                               |j                  dj                  | j                               |j                  dj                  | j                               |j                  d       d	j                  |      S )
Nz--- Position Beginz
- Size: {}z- Price: {}z- Price orig: {}z- Closed: {}z- Opened: {}z- Adjbase: {}z--- Position End
)
listappendformatsizeprice
price_origupclosedupopenedadjbasejoin)selfitemss     Q/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/position.py__str__zPosition.__str__)   s    )*\((34]))$**56'..t?@^**4==9:^**4==9:_++DLL9:'(yy    c                     || _         |r|x| _        | _        nd| _        d | _        || _        d| _        | j                  ||       d | _        y )N        r   )r   r   r   r   r   r   setupdtr   r   r   s      r   __init__zPosition.__init__5   sK    	+00DJDJu	r   c                 T    | j                   }|| _         || _        | j                   |k(  S Nr   r   )r   r   r   oldsizes       r   fixzPosition.fixD   s(    ))	
yyG##r   c                    | j                   dkD  re|| j                   kD  r|| j                   z
  | _        d| _        nt        d|      | _        t        | j                   | j                   |z
        | _        n| j                   dk  re|| j                   k  r|| j                   z
  | _        d| _        nRt	        d|      | _        t	        | j                   | j                   |z
        | _        n| j                   | _        d| _        || _         | j
                  | _        |r|| _        nd| _        | j                   | j
                  | j                  | j                  fS )Nr   r   )r   r   r   minmaxr   r   r   s      r   r   zPosition.setJ   s   99q=dii $tyy 0 ! !$At !$DIItyy4/? @YY]dii $tyy 0 ! !$At !$DIItyy4/? @ !IIDMDM	**DJDJyy$**dmmT]]BBr   c                 ,    t        | j                        S r"   )absr   r   s    r   __len__zPosition.__len__n   s    499~r   c                 2    t        | j                  dk7        S )Nr   )boolr   r+   s    r   __bool__zPosition.__bool__q   s    DIIN##r   c                 D    t        | j                  | j                        S )Nr#   )r	   r   r   r+   s    r   clonezPosition.clonev   s    TYYdjj99r   c                 b    t        | j                  | j                        j                  ||      S r"   )r	   r   r   updater   s      r   pseudoupdatezPosition.pseudoupdatey   s$    		4::.55dEBBr   Nc                    || _         | j                  | _        | j                  }| xj                  |z  c_        | j                  sd|}}d| _        n|s|d}}|| _        n|dkD  r\|dkD  r,|d}}| j                  |z  ||z  z   | j                  z  | _        n| j                  dkD  rd|}}nr| j                  | }}|| _        n[|dk  r,|d}}| j                  |z  ||z  z   | j                  z  | _        n*| j                  dk  rd|}}n| j                  | }}|| _        || _        || _        | j                  | j                  ||fS )a  
        Updates the current position and returns the updated size, price and
        units used to open/close a position

        Args:
            size (int): amount to update the position size
                size < 0: A sell operation has taken place
                size > 0: A buy operation has taken place

            price (float):
                Must always be positive to ensure consistency

        Returns:
            A tuple (non-named) contaning
               size - new position size
                   Simply the sum of the existing size plus the "size" argument
               price - new position price
                   If a position is increased the new average price will be
                   returned
                   If a position is reduced the price of the remaining size
                   does not change
                   If a position is closed the price is nullified
                   If a position is reversed the price is the price given as
                   argument
               opened - amount of contracts from argument "size" that were used
                   to open/increase a position.
                   A position can be opened from 0 or can be a reversal.
                   If a reversal is performed then opened is less than "size",
                   because part of "size" will have been used to close the
                   existing position
               closed - amount of units from arguments "size" that were used to
                   close/reduce a position

            Both opened and closed carry the same sign as the "size" argument
            because they refer to a part of the "size" argument
        r   r   )datetimer   r   r   r   r   )r   r   r   dtr$   openedcloseds          r   r3   zPosition.update|   sB   J **))		T	yyFFDJ!1FFDJq[ax!%q"jj72TE\ATYYN
Q!"D "&WH"
 ax!%q"jj72TE\ATYYN
Q!"D "&WH"
yy$**ff44r   )r   r   r"   )__name__
__module____qualname____doc__r   r    r%   r   r,   r/   __nonzero__r1   r4   r3    r   r   r	   r	      s?    

 $"CH$ K:CR5r   r	   N)
__future__r   r   r   r   r   objectr	   r?   r   r   <module>rB      s"   ** * r5v r5r   