
    iS                        d Z ddlmZ ddlmZ ddlmZ  ed       G d d             Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zdd
Z	y)u  
APEX V16 — P&L calculator for futures.

Pure functions. No I/O, no broker calls, no AI.
Inputs are explicit; outputs are typed.

Migrated from V15's _compute_paper_pnl() with the following
intentional differences:

  - V16 raises ValueError if asset specs are missing.
    V15 silently fell back to MES tick specs (0.25 / 1.25),
    which produced wrong P&L for any asset whose specs failed
    to load. Better to crash loud than to compute silently wrong.

  - V16 inputs are typed dataclass-friendly (entry_price, sl_price,
    tp_price as floats; direction as "BUY"/"SELL" string).

Inverted quote handling (6J, 6C, etc.) is NOT done here. It is
handled upstream in sizing / direction translation. The tick_value
from config_futures.py already encodes the dollar value of one tick
in the trader's account currency (USD), regardless of quote
direction. So this module is symmetric BUY/SELL.
    )annotations)	dataclass)OptionalT)frozenc                  X    e Zd ZU dZded<   ded<   ded<   ded<   ded<   d	ed
<   d	ed<   y)	PnLResultz<Result of a P&L calculation. All values rounded for display.floatnet_profit_usdnet_profit_usd_intintprofit_ticksprofit_ticks_floatprogress_pctboolhit_slhit_tpN)__name__
__module____qualname____doc____annotations__     -/home/work/apex_v16/trading/pnl_calculator.pyr   r      s-    FLLr   r   c           
     *   |dk  rt        d|       |dk  rt        d|       |dk  rt        d|       |j                         j                         }|dvrt        d|      |dk(  r|| z
  }||k  }	||k\  }
|| z
  }n| |z
  }||k\  }	||k  }
| |z
  }||z  }||z  |z  }t        t	        |            }||z  |z  }d}|rt        |      dkD  r||z  d	z  }t        t	        |d
      t	        |d
      |t	        |d
      t	        |d      |	|
      S )a  
    Compute P&L for an open futures trade.

    Args:
        entry_price: trade entry
        sl_price:    stop loss
        tp_price:    take profit
        current_price: current market price
        direction:   "BUY" or "SELL" (case-insensitive)
        contracts:   contract count (positive integer)
        tick_size:   minimum price increment for the asset
                     (e.g., 0.25 for MES, 0.0001 for 6E)
        tick_value:  dollar value of one tick for one contract
                     (e.g., 1.25 for MES, 6.25 for 6E)

    Returns:
        PnLResult.

    Raises:
        ValueError: if tick_size <= 0, contracts <= 0, or direction
                    not in BUY/SELL.
    r   ztick_size must be > 0, got ztick_value must be > 0, got zcontracts must be > 0, got )BUYSELLz#direction must be BUY or SELL, got r   g        g      Y@      )r
   r   r   r   r   r   r   )
ValueErrorupperstripr   roundabsr   )entry_pricesl_pricetp_pricecurrent_price	direction	contracts	tick_size
tick_value
price_diffr   r   tp_diffr   r
   r   r   r   s                    r   compute_pnlr/   +   s   D A~6ykBCCQ7
|DEEA~6ykBCC!'')I'>ymLMM E"[0
(*(*[( =0
(*(*( $i/'*4y@N u/01L%
2Y> L3w<!#"W,5^Q/ !3Q7! !3Q7<+ r   c                    | |vrt        d| d      ||    }d|vrt        d|       d|vrt        d|       t        |d         t        |d         fS )z
    Look up tick_size and tick_value for symbol from asset_specs dict
    (typically core.config_futures.ASSETS_MAP).

    Raises:
        KeyError if symbol not in asset_specs
        KeyError if tick_size or tick_value missing for that symbol
    zNo specs for symbol z?. Add it to config_futures.ASSETS_MAP or check the symbol name.r+   ztick_size missing in specs for r,   z tick_value missing in specs for )KeyErrorr	   )symbolasset_specsspecs      r   get_specs_or_raiser5      s     [ "6* -L M
 	
 vD$8
CDD49&DEEk"#U4+=%>>>r   N)r%   r	   r&   r	   r'   r	   r(   r	   r)   strr*   r   r+   r	   r,   r	   returnr   )r2   r6   r3   dictr7   ztuple[float, float])
r   
__future__r   dataclassesr   typingr   r   r/   r5   r   r   r   <module>r<      s   0 # !  $  NN N 	N
 N N N N N Nj?r   