+
    0.j[                     p    R t ^ RIHt ^ RIHt ^ RIt^ RIt^ RI	H
t
 ]
! ]4      t ! R R4      t]! 4       tR# )z2Statistical and quantitative finance calculations.)Decimal)AnyN)
get_loggerc                     a  ] tR t^t o RtV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR	 ltV 3R
 lR lt	V 3R lR lt
V 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltR,V 3R lR lltR,V 3R lR lltV 3R lR ltV 3R lR ltV 3R  lR! ltR-V 3R" lR# lltV 3R$ lR% ltR.V 3R& lR' lltV 3R( lR) ltR*tV tR+# )/StatisticsServicez<Mathematical and statistical formulas for trading decisions.c                N   < V ^8  d   QhRS[ P                  RS[ P                  /#    pricesreturnpdSeries)format__classdict__s   "IC:\xampp\htdocs\tradingbinance\backend\app\services\statistics_service.py__annotate__StatisticsService.__annotate__   s#     , ,		 ,bii ,    c                >    VP                  4       P                  4       # N)
pct_changedropnaselfr
   s   &&r   calculate_returns#StatisticsService.calculate_returns   s      "))++r   c                N   < V ^8  d   QhRS[ P                  RS[ P                  /# r   r   )r   r   s   "r   r   r      s#     9 9")) 9		 9r   c                t    \         P                  ! WP                  ^4      ,          4      P                  4       # )   )nplogshiftr   r   s   &&r   log_returnsStatisticsService.log_returns   s%    vvf||A./6688r   c                :   < V ^8  d   QhRS[ P                  RS[/# r	   datar   r   r   float)r   r   s   "r   r   r      s     " " "u "r   c                4    \        VP                  4       4      # r   )r)   meanr   r'   s   &&r   r+   StatisticsService.mean   s    TYY[!!r   c                :   < V ^8  d   QhRS[ P                  RS[/# r&   r(   )r   r   s   "r   r   r      s     $ $299 $ $r   c                4    \        VP                  4       4      # r   )r)   medianr,   s   &&r   r0   StatisticsService.median   s    T[[]##r   c                :   < V ^8  d   QhRS[ P                  RS[/# r&   r(   )r   r   s   "r   r   r      s     ! !BII !% !r   c                4    \        VP                  4       4      # r   )r)   stdr,   s   &&r   std_devStatisticsService.std_dev       TXXZ  r   c                :   < V ^8  d   QhRS[ P                  RS[/# r&   r(   )r   r   s   "r   r   r       s     ! !RYY !5 !r   c                4    \        VP                  4       4      # r   )r)   varr,   s   &&r   varianceStatisticsService.variance    r7   r   c                ,   < V ^8  d   QhRS[ RS[ RS[ /# )r	   startendr   r)   )r   r   s   "r   r   r   #   s"     + + +E +e +r   c                @    V^ 8X  d   R# W!,
          V,          ^d,          #              )r   r>   r?   s   &&&r   
pct_returnStatisticsService.pct_return#   s    A:u$s**r   c                P   < V ^8  d   QhRS[ P                  RS[S[S[3,          /# r	   returnsr   r   r   dictstrr)   )r   r   s   "r   r   r   (   s(     8 8299 8c5j9I 8r   c                    VP                   '       d   R RRR/# V^ 8  P                  4       \        V4      ,          pR \        V4      R\        ^V,
          4      /# )upg      ?down)emptysumlenr)   )r   rJ   rO   s   && r   up_down_probability%StatisticsService.up_down_probability(   sP    ===#vs++k 3w</eBiq2v77r   c                T   < V ^8  d   QhRS[ P                  RS[ P                  RS[/# )r	   series_aseries_br   r(   )r   r   s   "r   r   r   .   s/     B BBII B Bu Br   c                    \         P                  ! W.^R7      P                  4       p\        V4      ^8  d   R# \	        VP
                  R,          P                  VP
                  R,          4      4      # )r   )axisrD   )NNNrC   )r[   r   )r   concatr   rS   r)   iloccorr)r   rW   rX   aligneds   &&& r   correlationStatisticsService.correlation.   sU    ))X0q9@@Bw<!W\\$',,W\\$-?@AAr   c                P   < V ^8  d   QhRS[ P                  RS[S[S[3,          /# rI   rK   )r   r   s   "r   r   r   4   s(     
 
299 
c5j9I 
r   c                   VP                   '       d   / # R \        VP                  4       4      R\        VP                  4       4      R\        VP	                  4       4      R\        VP                  4       4      R\        VP                  4       4      R\        VP                  4       4      R\        VP                  R4      4      R\        VP                  R	4      4      /# )
r+   r4   skewkurtosisminmaxpercentile_5g?percentile_95gffffff?)	rQ   r)   r+   r4   rd   re   rf   rg   quantile)r   rJ   s   &&r   return_distribution%StatisticsService.return_distribution4   s    ===IE',,.)5'E',,.)g..015'5'E'"2"24"89U7#3#3D#9:	
 		
r   c                :   < V ^8  d   QhRS[ P                  RS[/# )r	   equity_curver   r(   )r   r   s   "r   r   r   B   s     0 0 0u 0r   c                    VP                   '       d   R # VP                  4       pW,
          V,          p\        \        VP	                  4       4      ^d,          4      # rD   )rQ   cummaxr)   absrf   )r   rn   rolling_maxdrawdowns   &&  r   max_drawdownStatisticsService.max_drawdownB   sG    "))+ .+=S(3.//r   c                F   < V ^8  d   QhRS[ P                  RS[RS[RS[/# r	   rJ   risk_free_rateperiodsr   r   r   r)   int)r   r   s   "r   r   r   I   s5     F FBII Fu FUX Fch Fr   c                   VP                   '       g   VP                  4       ^ 8X  d   R# WV,          ,
          p\        VP                  4       VP                  4       ,          \        P
                  ! V4      ,          4      # rB   rQ   r4   r)   r+   r    sqrt)r   rJ   ry   rz   excesss   &&&& r   sharpe_ratioStatisticsService.sharpe_ratioI   sR    ===GKKMQ.G33V[[]VZZ\1BGGG4DDEEr   c                F   < V ^8  d   QhRS[ P                  RS[RS[RS[/# rx   r{   )r   r   s   "r   r   r   O   s5     H HRYY H HVY Hdi Hr   c                B   VP                   '       d   R # WV,          ,
          pWD^ 8  ,          pVP                   '       g   VP                  4       ^ 8X  d   R # \        VP                  4       VP                  4       ,          \        P
                  ! V4      ,          4      # rp   r~   )r   rJ   ry   rz   r   downsides   &&&&  r   sortino_ratioStatisticsService.sortino_ratioO   sj    ===G331*%>>>X\\^q0V[[]X\\^3bggg6FFGGr   c                6   < V ^8  d   QhRS[ S[,          RS[/# r	   pnlsr   listr)   )r   r   s   "r   r   r   X   s     & &T%[ &U &r   c                l    V'       g   R # \        R V 4       4      pV\        V4      ,          ^d,          # )rD   c              3   6   "   T F  q^ 8  g   K  ^x  K  	  R# 5irC   NrE   .0ps   & r   	<genexpr>-StatisticsService.win_rate.<locals>.<genexpr>[   s     ,d!e11d   
rR   rS   )r   r   winss   && r   win_rateStatisticsService.win_rateX   s,    ,d,,c$i#%%r   c                6   < V ^8  d   QhRS[ S[,          RS[/# r   r   )r   r   s   "r   r   r   ^   s     ) )$u+ )% )r   c                    \        R  V 4       4      p\        \        R V 4       4      4      pV^ 8X  d   V^ 8  d   \        R4      # R# W#,          # )c              3   6   "   T F  q^ 8  g   K  Vx  K  	  R# 5ir   rE   r   s   & r   r   2StatisticsService.profit_factor.<locals>.<genexpr>_   s     4d!e11dr   c              3   6   "   T F  q^ 8  g   K  Vx  K  	  R# 5ir   rE   r   s   & r   r   r   `   s     61AQQr   infrD   )rR   rr   r)   )r   r   gross_profit
gross_losss   &&  r   profit_factorStatisticsService.profit_factor^   sM    4d446667
?#/!#35<<<((r   c                6   < V ^8  d   QhRS[ S[,          RS[/# r   r   )r   r   s   "r   r   r   e   s     % %4; %5 %r   c                L    V'       g   R # \        V4      \        V4      ,          # rp   r   )r   r   s   &&r   expected_value StatisticsService.expected_valuee   s    4y3t9$$r   c          
      8   < V ^8  d   QhRS[ RS[ RS[ RS[ RS[ /# )r	   win_rate_pctavg_winavg_losscapr   r@   )r   r   s   "r   r   r   j   s2     	 	E 	E 	U 	Y^ 	jo 	r   c                    V^ 8X  g   V^ 8:  d   R# V^d,          p^V,
          p\        W#,          4      pWVV,          ,
          p\        R\        W4      4      pV# )z+Adjusted Kelly fraction, capped for safety.rD   )rr   rg   rf   )	r   r   r   r   r   win_prob	loss_probwin_loss_ratiokellys	   &&&&&    r   kelly_criterion!StatisticsService.kelly_criterionj   sU    q=LA-#%L	W/067CU)r   c                @   < V ^8  d   QhRS[ RS[P                  RS[ /# )r	   valueseriesr   )r)   r   r   )r   r   s   "r   r   r   u   s&     = =U =BII =% =r   c                    VP                  4       ^ 8X  d   R# \        WP                  4       ,
          VP                  4       ,          4      # rB   )r4   r)   r+   )r   r   r   s   &&&r   z_scoreStatisticsService.z_scoreu   s2    ::<1ekkm+vzz|;<<r   c                V   < V ^8  d   QhRS[ P                  RS[RS[S[S[3,          /# )r	   r   	thresholdr   )r   r   r)   rL   rM   r   )r   r   s   "r   r   r   z   s1     
 
ryy 
U 
TRUWZRZ^ 
r   c                    V P                  VP                  R,          V4      p\        V4      V8  pRTRTRW28  d   RRV/# W2) 8  d   RRV/# RRV/# )	r   r   
is_anomaly	directionhighlownormalr   )r   r]   rr   )r   r   r   zr   s   &&&  r   detect_anomalies"StatisticsService.detect_anomaliesz   s|    LLR&1Vi'
q*1=	
 	
 @A:~e	
 	
 T\	
 	
r   c                n   < V ^8  d   QhRS[ S[,          RS[ S[,          RS[RS[RS[S[S[3,          /# )r	   rn   
trade_pnlsinitial_capitalbuy_hold_returnr   )r   r)   rL   rM   r   )r   r   s   "r   r   r      sM     
 
5k
 K
 	

 
 
c3h
r   c                    \         P                  ! V4      pVP                  4       P                  4       p\	        V4      ^ 8  d   VP
                  R,          MTpV P                  W74      pV U	u. uF  q^ 8  g   K  V	NK  	  p
p	V U	u. uF  q^ 8  g   K  V	NK  	  pp	RTRV P                  V4      RV P                  V4      RV P                  V4      RV P                  V4      RV P                  V4      RV P                  V4      RT P                  V P                  V4      V
'       d   \        P                  ! V
4      M^ V'       d   \        P                  ! V4      M^4      R	VR
W8  R\	        V4      /# u up	i u up	i )rC   total_return_pctru   r   r   r   r   r   kelly_fractionr   beats_buy_holdtotal_tradesr   )r   r   r   r   rS   r]   rF   ru   r   r   r   r   r   r   r    r+   )r   rn   r   r   r   equityrJ   finaltotal_returnr   r   lossess   &&&&&       r   compute_backtest_metrics*StatisticsService.compute_backtest_metrics   sM    <(##%,,.#&v;?B>%/:aQ:/'1Zq5!!Z1 D--f5D--g6T//8j1T//
;d11*=d22j)!%1#)q
 l<C
O
 	
 01s   /E6<E6E;E;rE   N)rD   im  )g      ?)g       @)__name__
__module____qualname____firstlineno____doc__r   r#   r+   r0   r5   r;   rF   rT   r`   rk   ru   r   r   r   r   r   r   r   r   r   __static_attributes____classdictcell__)r   s   @r   r   r      s     F, ,9 9" "$ $! !! !+ +
8 8B B
 
0 0F FH H& &) )% %
	 	= =

 

 
r   r   )r   decimalr   typingr   numpyr    pandasr   app.core.loggingr   r   loggerr   statistics_servicerE   r   r   <module>r      s9    8     '	H	U
 U
p '( r   