
    'i                     b    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	m
Z
 d dlmZ  G d de      Zy)    )absolute_importdivisionprint_functionunicode_literalsN)Analyzer)AutoOrderedDictAutoDict)MAXINTc                   .     e Zd ZdZd Z fdZd Z xZS )TradeAnalyzera  
    Provides statistics on closed trades (keeps also the count of open ones)

      - Total Open/Closed Trades

      - Streak Won/Lost Current/Longest

      - ProfitAndLoss Total/Average

      - Won/Lost Count/ Total PNL/ Average PNL / Max PNL

      - Long/Short Count/ Total PNL / Average PNL / Max PNL

          - Won/Lost Count/ Total PNL/ Average PNL / Max PNL

      - Length (bars in the market)

        - Total/Average/Max/Min

        - Won/Lost Total/Average/Max/Min

        - Long/Short Total/Average/Max/Min

          - Won/Lost Total/Average/Max/Min

    Note:

      The analyzer uses an "auto"dict for the fields, which means that if no
      trades are executed, no statistics will be generated.

      In that case there will be a single field/subfield in the dictionary
      returned by ``get_analysis``, namely:

        - dictname['total']['total'] which will have a value of 0 (the field is
          also reachable with dot notation dictname.total.total
    c                 X    t               | _        d| j                  j                  _        y )Nr   )r   retstotal)selfs    `/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/analyzers/tradeanalyzer.pycreate_analysiszTradeAnalyzer.create_analysisD   s    #%	 !		    c                 ^    t         t        |           | j                  j	                          y )N)superr   stopr   _close)r   	__class__s    r   r   zTradeAnalyzer.stopH   s     mT')		r   c                    |j                   rS| j                  j                  xj                  dz  c_        | j                  j                  xj                  dz  c_        y |j                  |j
                  k(  rJ| j                  }t               }t        |j                  dk\        x}|_	        t        |       x}|_
        |j                  x}|_        |j                   x}|_        |j                  xj                  dz  c_        |j                  xj                  dz  c_        dD ]  }||   }	|j                  |   xj                   |	z  c_        |j                  |   xj                   |	z  c_        |j                  |   j"                  xs d}
t%        |
|j                  |   j                         |j                  |   _         |j&                  }|j(                  xj                  |j&                  z  c_        |j&                  j(                  j                  |j                  j                  z  |j(                  _        |j,                  xj                  |j                  z  c_        |j&                  j,                  j                  |j                  j                  z  |j,                  _        dD ]  }||   }	||   }|xj                  |	z  c_        |j&                  }|j                  |	z  }|xj                  |z  c_        |j                  |j                  xs dz  |_        |j$                  xs d}|dk(  rt$        nt.        } |||      |_         dD ]U  }||   }|d|z      }
|xj                  |
z  c_        |j&                  xj                  |j                  |
z  z  c_        |j&                  j                  |j                  xs dz  |j&                  _        dD ]  }||   }	|j                  |	z  |
z  }||xx   |	|
z  z  cc<   |j&                  |   xj                  |z  c_        |j&                  |   j                  ||   xs dz  |j&                  |   _        |j&                  |   j$                  xs d}|dk(  rt$        nt.        } |||      |j&                  |   _         X |j0                  xj                  |j2                  z  c_        |j0                  j                  |j                  j                  z  |j0                  _        |j0                  j$                  xs d}t%        ||j2                        |j0                  _        |j0                  j.                  xs t4        }t/        ||j2                        |j0                  _        dD ]  }|j0                  |   }||   }	|xj                  |j2                  |	z  z  c_        |j                  ||   j                  xs dz  |_        |j$                  xs d}t%        ||j2                  |	z        |_        |j2                  |	z  s|j.                  xs t4        }t/        ||j2                  |	z        |_         dD ]O  }|j0                  |   }|d|z      }
|j2                  |
z  }|xj                  |z  c_        ||   j                  }|j                  |xs dz  |_        |j$                  xs d}t%        ||      |_        |j.                  xs t4        }t/        ||xs |      |_        dD ]  }||   }	|j2                  |
z  |	z  }||   }|xj                  |z  c_        |j                  ||   |   xs dz  |_        |j$                  xs d}t%        ||      |_        |j.                  xs t4        }t/        ||xs |      |_         R y y )	N   g        )wonlostr   g      ?r   )longshortt)
justopenedr   r   openstatusClosedr	   intpnlcommr   r   r   tlongtshortclosedstreakcurrentlongestmaxpnlgrossaveragenetminlenbarlenr
   )r   tradetradesresr   r   r&   r'   wlnamewllstrpnltrwltrwlpnlr%   wmfunctnametrlsmlmlsnamer3   total_lsbarlen2trls_wls                             r   notify_tradezTradeAnalyzer.notify_tradeL   s\   IIOO!!Q&!IIOO  A% \\U\\)YYF*C   455C#'!c'l*D38 %

*ECI&+jj.0FSZLL"LL1$ * ;[f%--3-f%--3-]]6*227aFMM&199: f%-; JJEKK*"(**"2"2"8"86<<;N;N"NEKKIIOOu}},O &

 4 4v||7J7J JEII * 0[f~

b 
((--",(")--4::3D"E[['C$os3"2w/0" + =e}u%

b 
%--""44#'88>>TZZ5F3#G - =FVB#mmb025GLBG+LHHV$**g5*(..$v,2E#F HHV$, &)--4B"(E/3sD+/G+<DHHV$(==. JJ,!'!1!1FLL4G4G!GFJJ$1B U\\2FJJN)6B U\\2FJJN * 9zz&)[

ellR//
#zzVF^-A-A-HSIHHMq%,,"34<<"$*FA"1ellR&78DH9 , 7zz&)v&*

f$
!&>//#zzX_= HHMq&>HH&q&+A.- 7FVB#llR/"4G"6lGMMW,M  )?)F3G O  (qA"%a/GK-vA"%aA"6GK7!7 *r   )__name__
__module____qualname____doc__r   r   rG   __classcell__)r   s   @r   r   r      s    #H"D7r   r   )
__future__r   r   r   r   sys
backtraderr   backtrader.utilsr   r	   backtrader.utils.py3r
   r    r   r   <module>rS      s+   ** *   6 'q7H q7r   