
    /j5b                    j   d Z ddlmZ ddlZddlmc mZ ddl	Z	ddl
mZ e	j                  j                  d e ee      j!                         j"                  j"                               ddlmZ ddlmZ ddlmZmZmZmZmZmZmZmZm Z  d6dZ!d	d
ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d7d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/d Z0d Z1d  Z2d! Z3d" Z4d# Z5d$ Z6d%d&	 	 	 d8d'Z7d( Z8d) Z9d* Z:d+ Z;d, Z<d- Z=d. Z>d/ Z?d0 Z@d1 ZAd2 ZBd3 ZCd9d4ZDeEd5k(  r e	j                   eD              yy):ub  
Smoke tests for trading/sizing.py.

Three tiers:
  1. Primitives: budget_pre_check, compute_contracts (Day 1 code,
     never had a test file — covered here).
  2. Conversion helpers: points_to_ticks, points_to_dollars.
  3. Public entry point: size_for_entry (EntryDecision -> SizingDecision).

Run:
    cd ~/apex_v16
    python -m tests.test_sizing
    )annotationsN)Path)config_futuresEntryDecision)	BudgetCheckResultSizingAuditSizingDecisionSizingResultbudget_pre_checkcompute_contractspoints_to_dollarspoints_to_tickssize_for_entryc                     t        d|         y )Nz  ok  )print)labels    (/home/work/apex_v16/tests/test_sizing.py_okr   %   s    	F5'
    BUY     @    @g     @K         ?	directionentry_pricesl_pricetp_price
confidencerisk_multiplierc                ,    t        | |||d|dd|i      S )N      ?testr"   )r   r   r   r    rr_multiplierr!   	rationalemetadatar   r   s         r   
make_entryr)   )   s.     #_5	 	r   c            	        t        ddddddd      } t        | t              }|sd	d
t        j                         v st        j                  t              rt        j                  t              nd
dt        j                         v st        j                  |       rt        j                  |       nddt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      dz  }t        t        j                  |            d}| j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)z(Headroom >> typical risk -> not skipped.MES      ?   (                pQ?symbol
tick_valuemin_sl_ticksmax_sl_ticks	daily_pnldaily_loss_hard_stopmax_risk_vs_daily_budget5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}
isinstanceresr   py0py1py2py4NFisz,%(py2)s
{%(py2)s = %(py0)s.skip
} is %(py5)sr>   r@   py5assert %(py7)spy7	budget_ok==)z.%(py2)s
{%(py2)s = %(py0)s.reason
} == %(py5)sz/budget_pre_check: ample headroom -> not skipped)r   r;   r   @py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprAssertionError_format_explanationskip_call_reprcomparereasonr   )r<   @py_assert3@py_format5@py_assert1@py_assert4@py_format6@py_format8s          r   test_budget_pre_check_okr\   B   s   
abG!%	C c,--------:---:------c---c------,---,----------88u8u8u338u::$$:$$$$:$$$$$$3$$$3$$$:$$$$$$$$$$9:r   c            	     Z   t        ddddddd      } | j                  }d	}||u }|st        j                  d
|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}d}| j                  }||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)zCDaily budget already drained -> min trade exceeds cap -> hard skip.r+   r,   r-   r.   g     Hr0   r1   r2   TrB   rD   r<   rE   rG   rH   Nbudget_hardinz.%(py1)s in %(py5)s
{%(py5)s = %(py3)s.reason
}r?   py3rF   z-budget_pre_check: budget drained -> hard skip)r   rS   rN   rT   rL   rM   rO   rP   rQ   rR   rU   r   r<   rX   rY   rV   rZ   r[   @py_assert0@py_assert2s           r   test_budget_pre_check_hard_skiprg   P   s   
ab$!%C 88t8t8t338t&CJJ&=J&&&&=J&&&=&&&&&&C&&&C&&&J&&&&&&&78r   c            	        	 t        ddddddd       t        d      # t        $ r$} d	}t        |       }||v }|st        j                  d
|fd||f      t        j
                  |      dt        j                         v st        j                  t              rt        j
                  t              nddt        j                         v st        j                  |       rt        j
                  |       ndt        j
                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        d       Y d} ~ yd} ~ ww xY w)z5tick_value <= 0 raises (no silent default tick math).Xr   r-   r.   r/   r0   r1   r2   r4   r_   z0%(py1)s in %(py6)s
{%(py6)s = %(py3)s(%(py4)s)
}strer?   rc   rA   py6assert %(py8)spy8Nz,budget_pre_check: tick_value=0 -> ValueErrorexpected ValueError)r   
ValueErrorrk   rN   rT   rP   rL   rM   rO   rQ   rR   r   rl   re   @py_assert5rf   @py_format7@py_format9s         r   $test_budget_pre_check_invalid_inputsrw   ^   s    
1%)		
 .
//	  %s1v%|v%%%%|v%%%|%%%%%%s%%%s%%%%%%1%%%1%%%v%%%%%%%:;s    
EDEEc                 D   t        ddddddddd	d

      } | j                  }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        d| j                   d| j                          y)uE  
    MES, 100k balance, risk 0.003 = $300. sl_ticks=30 viene "floored" al
    worst-case MAX_SL_TICKS[MES]=40 (MES è in WORST_CASE_SIZING_ASSETS),
    quindi sl_usd_per_contract = 40 * $1.25 = $50/ct.
    target = 300 / 50 = 6.0 → 6 contratti (entro MAX 8).
    real_risk = 6 * 50 = $300. Daily cap $495 → 300 fits.
    r+           j@r,   ~jth?r-   ffffff?r/   r0   r1   
r3   sl_ticksaccount_balancer4   risk_per_trademax_contractsmin_contract_fractionr7   r8   r9   FrB   rD   r<   rE   rG   rH   N   rJ   z1%(py2)s
{%(py2)s = %(py0)s.contracts
} == %(py5)s     r@z5%(py2)s
{%(py2)s = %(py0)s.real_risk_usd
} == %(py5)sz9%(py2)s
{%(py2)s = %(py0)s.sl_ticks_original
} == %(py5)sTz8%(py2)s
{%(py2)s = %(py0)s.sl_ticks_floored
} is %(py5)sz compute_contracts: base case -> zct $)r   rS   rN   rT   rL   rM   rO   rP   rQ   rR   	contractsreal_risk_usdsl_ticks_originalsl_ticks_flooredr   r<   rX   rY   rV   rZ   r[   s         r   test_compute_contracts_baser   r   s{    r!dA!G!%C 88u8u8u338u==A=A=A33=A%%%%%%%%%%%%3%%%3%%%%%%%%%%%%%  &B& B&&&& B&&&&&&3&&&3&&& &&&B&&&&&&&'4'4''''4''''''3'''3''''''4'''''''
*3==/c>O>O=PQRr   c                 ~   t        ddddddddd	d

      } | j                  }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}d}| j                  }||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)zHuge SL -> target fraction below floor -> skip size_too_small.
    Use 'ES' (no entry in MIN/MAX_SL_TICKS, no worst-case floor) so sl_ticks
    non viene "floored" e il caso di studio resta valido.ESi  rz   r,   r{   r-   r|   r/   r0   r1   r}   TrB   rD   r<   rE   rG   rH   Nsize_too_smallr_   ra   rb   Fr   rJ   r   z,compute_contracts: huge SL -> size_too_small)r   rS   rN   rT   rL   rM   rO   rP   rQ   rR   rU   r   r   r   rd   s           r   %test_compute_contracts_size_too_smallr      s    c!dA!G!%C 88t8t8t338t)szz)z))))z)))))))))s)))s)))z)))))))(5(5((((5((((((3(((3((((((5(((((((  'C' C'''' C''''''3'''3''' '''C'''''''67r   c                    	 t        ddddddddd	d

       t        d      # t        $ r$} d}t        |       }||v }|st        j                  d|fd||f      t        j
                  |      dt        j                         v st        j                  t              rt        j
                  t              nddt        j                         v st        j                  |       rt        j
                  |       ndt        j
                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        d       Y d} ~ yd} ~ ww xY w)z!sl_ticks=0 raises (no silent /0).r+   r   rz   r,   r{   r-   r|   r/   r0   r1   r}   r~   r_   rj   rk   rl   rm   ro   rp   Nz+compute_contracts: sl_ticks=0 -> ValueErrorrq   )r   rr   rk   rN   rT   rP   rL   rM   rO   rQ   rR   r   rs   s         r   &test_compute_contracts_invalid_sl_zeror      s    1%$ "%%)	
 .
//	  #SV#zV####zV###z######S###S############V#######9:s   " 
EDEEc                    d} d}t        | |      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d x} x}x}x}}t        d
       y )Nr+         @
   rJ   z9%(py6)s
{%(py6)s = %(py0)s(%(py2)s, %(py4)s)
} == %(py9)sr   r>   r@   rA   rn   py9assert %(py11)spy11z%points_to_ticks: MES 2.5p -> 10 ticks)
r   rN   rT   rL   rM   rO   rP   rQ   rR   r   rX   rV   rt   @py_assert8@py_assert7@py_format10@py_format12s          r   test_points_to_ticks_mesr      s     ,#,?5#&,",&",,,,&",,,,,,?,,,?,,,5,,,#,,,&,,,",,,,,,,/0r   c                    d} d}t        | |      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d x} x}x}x}}t        d
       y )Nr+   r         )@rJ   r   r   r   r   r   z(points_to_dollars: MES 2.5p -> $12.50/ct)
r   rN   rT   rL   rM   rO   rP   rQ   rR   r   r   s          r   test_points_to_dollars_mesr      s    "1C1UC(1E1(E1111(E111111111111U111C111(111E111111123r   c                    	 t        dd       t        d      # t        $ r$} d}t        |       }||v }|st        j                  d|fd||f      t        j
                  |      dt        j                         v st        j                  t              rt        j
                  t              nddt        j                         v st        j                  |       rt        j
                  |       ndt        j
                  |      dz  }dd	|iz  }t        t        j                  |            d x}x}}t        d
       Y d } ~ y d } ~ ww xY w)NBOGUSr   r_   rj   rk   rl   rm   ro   rp   z/points_to_dollars: unknown symbol -> ValueErrorrq   )r   rr   rk   rN   rT   rP   rL   rM   rO   rQ   rR   r   rs   s         r   ,test_points_to_dollars_unknown_symbol_raisesr      s    '3'
 .
//	   #a& w&    w&   w      #   #      a   a   &       =>    
EDEEc                    	 t        dd       t        d      # t        $ r$} d}t        |       }||v }|st        j                  d|fd||f      t        j
                  |      dt        j                         v st        j                  t              rt        j
                  t              nddt        j                         v st        j                  |       rt        j
                  |       ndt        j
                  |      dz  }d	d
|iz  }t        t        j                  |            d x}x}}t        d       Y d } ~ y d } ~ ww xY w)Nr+   r   distance_pointsr_   rj   rk   rl   rm   ro   rp   z)points_to_ticks: distance=0 -> ValueErrorrq   )r   rr   rk   rN   rT   rP   rL   rM   rO   rQ   rR   r   rs   s         r    test_points_to_ticks_zero_raisesr      s    q!
 .
//	   *CF* F**** F*** ******C***C************F*******78r   c            	        t        t        dd      dddddd	
      } t        | t              }|sddt	        j
                         v st        j                  t              rt        j                  t              nddt	        j
                         v st        j                  |       rt        j                  |       nddt	        j
                         v st        j                  t              rt        j                  t              ndt        j                  |      dz  }t        t        j                  |            d}| j                  }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                   }|j"                  }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j$                  }d"}||k(  }|st        j                  d|fd#||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j&                  }d$}||k(  }|st        j                  d|fd%||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j(                  }d&}||k(  }|st        j                  d|fd'||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j*                  }d}||k(  }|st        j                  d|fd(||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j,                  }d)}||k(  }|st        j                  d|fd*||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j.                  }d}||k(  }|st        j                  d|fd+||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j0                  }d}||u }|st        j                  d|fd,||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j2                  }d}||u }|st        j                  d|fd-||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j4                  }d.}||u }|st        j                  d|fd/||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}| j                   }|j6                  }d}||k(  }|st        j                  d|fd0||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }	t        t        j                  |	            dx}x}x}}t9        d1| j                   d2       y)3u  
    MES, balance $100k, risk 0.003 = $300 target.
    SL distance = 5800 - 5797.50 = 2.50 points = 10 ticks (Brain-proposed).
    Worst-case sizing (MES ∈ WORST_CASE_SIZING_ASSETS): sl_ticks_for_sizing =
    MAX_SL_TICKS[MES] = 40 → sl_usd_per_contract = 40 * $1.25 = $50/ct.
    target_contracts = 300 / 50 = 6.0 → 6 ct (= MAX MES). real_risk = $300.
    decision.sl_ticks resta 10 (SL inviato al broker = quello del Brain).
    r   r   r   r   r+   rz   r{   r/   r0   r1   entryr3   r   r   r7   r8   r9   r:   r;   decisionr
   r=   NFrB   rD   rE   rG   rH   r   rJ   r   r   z0%(py2)s
{%(py2)s = %(py0)s.sl_ticks
} == %(py5)sr   r   )zI%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.symbol
} == %(py7)sr>   r@   rA   rH   assert %(py9)sr   g      ?zL%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.tick_size
} == %(py7)sr,   zM%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.tick_value
} == %(py7)sg      I@zV%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.sl_usd_per_contract
} == %(py7)szR%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.risk_usd_target
} == %(py7)sg      @)zO%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.target_float
} == %(py7)s)zU%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.max_contracts_used
} == %(py7)szO%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.clamp_active
} is %(py7)szQ%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.inverted_quote
} is %(py7)sTzS%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.sl_ticks_floored
} is %(py7)s)zT%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.sl_ticks_original
} == %(py7)sz!size_for_entry: MES base -> 6ct $z worst-case sizing)r   r)   r;   r
   rL   rM   rN   rO   rP   rQ   rR   rS   rT   r   r~   r   auditr3   	tick_sizer4   sl_usd_per_contractrisk_usd_targettarget_floatmax_contracts_usedclamp_activeinverted_quoter   r   r   )
r   rV   rW   rX   rY   rZ   r[   @py_assert6rt   r   s
             r   !test_size_for_entry_mes_base_caser      sQ    Ww?i$!%H h////////:///://////h///h///////////////////==!E!=E!!!!=E!!!!!!8!!!8!!!=!!!E!!!!!!!""""""""""""8"""8"""""""""""""""""""""""""8"""8"""""""""""""!!*U*!U****!U******8***8***!***U*******>>)>  )E) E)))) E))))))8)))8)))>))) )))E)))))))>>+>##+t+#t++++#t++++++8+++8+++>+++#+++t+++++++>>,>$$,,$,,,,$,,,,,,8,,,8,,,>,,,$,,,,,,,,,,>>5>--55-5555-55555585558555>555-5555555555>>2>))2U2)U2222)U22222282228222>222)222U2222222>>->&&-#-&#----&#------8---8--->---&---#------->>1>,,11,1111,11111181118111>111,1111111111>>/>&&/%/&%////&%//////8///8///>///&///%///////>>1>((1E1(E1111(E11111181118111>111(111E1111111>>2>**2d2*d2222*d22222282228222>222*222d2222222>>1>++1r1+r1111+r11111181118111>111+111r1111111
+H,B,B+CCUVWr   c            	     F   t        t        dd      dddddd	
      } | j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }|j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}x}}| j                  }|j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}x}}t        d| j                  j                    d       y)u}  
    Asset non-worst-case e fuori da MIN_CONTRACTS_PER_TRADE (MCL): il target
    atterra naturalmente sotto max_contracts e nessun floor entra in gioco.
    MCL: tick_size=0.01, tick_value=$1, MIN_SL_TICKS=20, MAX_SL_TICKS=80,
    MAX_CONTRACTS=4. SL distance=0.70 → 70 ticks (>MIN, no floor).
    sl_usd = 70 * 1 = $70/ct. risk=80k*0.003=$240 → target=3.43 → 3 ct < 4.
    g     Q@g33333SQ@r   MCLg     @r{   r/   r0   r1   r   FrB   rD   r   rE   rG   rH   N   rJ   r   r   r   r   r   r   zsize_for_entry: target z fits -> clamp_active=False)r   r)   rS   rN   rT   rL   rM   rO   rP   rQ   rR   r   r   r   r   r   r   	r   rX   rY   rV   rZ   r[   r   rt   r   s	            r   3test_size_for_entry_clamp_inactive_when_target_fitsr      s3    UU;h$!%H ==!E!=E!!!!=E!!!!!!8!!!8!!!=!!!E!!!!!!!""""""""""""8"""8""""""""""""">>/>&&/%/&%////&%//////8///8///>///&///%///////>>3>**3e3*e3333*e33333383338333>333*333e3333333
!(.."="=!>>YZ[r   c            	     0   t        t        dd      dddddd	
      } | j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}d}| j                  }||v }|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        d| j                  j                   d       y)z+SL so wide that target_float < 0.7 -> skip.g\(\?gHzG?r   6Erz   r{   r/   r0   r1   r   TrB   rD   r   rE   rG   rH   Nr   rJ   r   r   r_   ra   rb   z/size_for_entry: size_too_small -> skip (target ))r   r)   rS   rN   rT   rL   rM   rO   rP   rQ   rR   r   rU   r   r   r   r   rX   rY   rV   rZ   r[   re   rf   s           r   'test_size_for_entry_size_too_small_skipr     s   
 Vf=Y$!%H == D =D    =D      8   8   =   D       """"""""""""8"""8""""""""""""".x..............x...x..........
9(..:U:U9VVWXYr   c            	     p   t        t        dd      dddddd	
      } | j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}d}| j                  }||v }|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)zEDaily near hard stop -> any reasonable trade exceeds 33% cap -> skip.r       @r   r+   rz   r{   g     Pr0   r1   r   TrB   rD   r   rE   rG   rH   Nrisk_exceeds_daily_budgetr_   ra   rb   z-size_for_entry: real_risk > daily cap -> skip)r   r)   rS   rN   rT   rL   rM   rO   rP   rQ   rR   rU   r   r   s           r   'test_size_for_entry_risk_exceeds_budgetr   /  s   Ww?i$!%H == D =D    =D      8   8   =   D       &9(//9&/9999&/999&999999(999(999/999999978r   c            	     @   t        t        dd      dddddd	
      } | j                  }|j                  }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}x}}| j                  }|j                  }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}x}}| j                  }|j                  }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}x}}| j                  }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}}| j                  }|j                  }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}x}}| j                  }|j                   }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}x}}| j                  }|j"                  }d }||u }|st	        j
                  d!|fd"||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}x}}| j$                  }d }||u }|st	        j
                  d!|fd#||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}}d$}	| j&                  }|	|v }
|
st	        j
                  d%|
fd&|	|f      t	        j                  |	      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      d'z  }dd|iz  }t        t	        j                  |            dx}	x}
}t)        d(| j                   d)| j                  j                   d*       y)+a\  
    6J: tick_size = 0.0000005, tick_value = $6.25, inverted_quote = True.
    Manual math:
      sl_distance_points = 0.0001000 (typical 6J SL)
      sl_ticks = 0.0001 / 0.0000005 = 200
      sl_usd_per_contract = 200 * 6.25 = $1250
      risk_usd = 100k * 0.003 = $300
      target_contracts = 300 / 1250 = 0.24 < 0.70 -> skip size_too_small
    y&1|?glxz,C|?r   6Jrz   r{   r/   r0   r1   r   g-C6?rJ   )zU%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.sl_distance_points
} == %(py7)sr   r   r   r   NgƠ>r         @r      r   rE   rG   rH   g     @r   r   r   TrB   r   rD   r   r_   ra   rb   z*size_for_entry: 6J math correct (sl_ticks=z, $/ct=z), inverted_quote audit-only)r   r)   r   sl_distance_pointsrN   rT   rL   rM   rO   rP   rQ   rR   r   r4   r~   r   r   r   rS   rU   r   )r   rX   rV   r   rt   r[   r   rY   rZ   re   rf   s              r   )test_size_for_entry_6j_dollar_correctnessr   A  s    YCY$!%H >>6>,,66,6666,66666686668666>666,6666666666>>0>##0y0#y0000#y00000080008000>000#000y0000000>>,>$$,,$,,,,$,,,,,,8,,,8,,,>,,,$,,,,,,,,,,############8###8#############>>7>--77-7777-77777787778777>777-7777777777>>2>))2U2)U2222)U22222282228222>222)222U2222222>>0>((0D0(D0000(D00000080008000>000(000D0000000== D =D    =D      8   8   =   D       .x..............x...x..........
4X5F5F4G H2233O	Q Rr   c            	     2   t        t        dd      dddddd	
      } | j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }|j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}x}}| j                  }|j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}x}}t        d| j                   d       y)ub   6J with smaller SL produces contracts >= 2 (V18 MIN floor) — execution path with inverted_quote.r   gui|?r   r   g    Ar{   r/   g     pr$   r   FrB   rD   r   rE   rG   rH   N   rJ   r   Tr   r   r   r   r   zsize_for_entry: 6J executable zct, clamp on MAX_CONTRACTS)r   r)   rS   rN   rT   rL   rM   rO   rP   rQ   rR   r   r   r   r   r   r   s	            r   &test_size_for_entry_6j_executable_sizer   b  s-   YCY$!%H ==!E!=E!!!!=E!!!!!!8!!!8!!!=!!!E!!!!!!!""""""""""""8"""8""""""""""""">>.>&&.$.&$....&$......8...8...>...&...$.......>>0>((0D0(D0000(D00000080008000>000(000D0000000
(););(<<VWXr   c            	        	 t        t        dd      dddddd	       t        d      # t        $ r$} d
}t        |       }||v }|st	        j
                  d|fd||f      t	        j                  |      dt        j                         v st	        j                  t              rt	        j                  t              nddt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}}t        d       Y d} ~ yd} ~ ww xY w)zKentry_price == sl_price -> sl_distance = 0 -> ValueError (not silent skip).r   r   r+   rz   r{   r/   r0   r1   r   r   r_   rj   rk   rl   rm   ro   rp   Nz6size_for_entry: entry==sl -> ValueError (no silent /0)rq   r   r)   rr   rk   rN   rT   rP   rL   rM   rO   rQ   rR   r   rs   s         r   +test_size_for_entry_sl_distance_zero_raisesr   x  s    
&A) C!(4		
 .
//	  #-s1v-#v----#v---#------s---s------1---1---v-------DEs   * 
EDEEc            	     0   	 t        t               dddddd       t        d      # t        $ rd} g }d}t        |       }||v }|}|sd}t        |       }||v }|}|st	        j
                  d	|fd
||f      t	        j                  |      dt        j                         v st	        j                  t              rt	        j                  t              nddt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      dz  }	dd|	iz  }
|j                  |
       |st	        j
                  d	fdf      t	        j                  |      dt        j                         v st	        j                  t              rt	        j                  t              nddt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      dz  }dd|iz  }|j                  |       t	        j                  |d      i z  }dd|iz  }t        t	        j                  |            d x}x}x}x}x}x}x}}t        d       Y d } ~ y d } ~ ww xY w)Nr   rz   r{   r/   r0   r1   r   
ASSETS_MAPr_   )z0%(py3)s in %(py8)s
{%(py8)s = %(py5)s(%(py6)s)
}rk   rl   )rc   rF   rn   rp   z%(py10)spy10)z5%(py13)s in %(py18)s
{%(py18)s = %(py15)s(%(py16)s)
})py13py15py16py18z%(py20)spy20   zassert %(py23)spy23z,size_for_entry: unknown symbol -> ValueErrorrq   )r   r)   rr   rk   rN   rT   rP   rL   rM   rO   append_format_booloprQ   rR   r   )rl   rX   rf   r   rY   re   @py_assert12@py_assert17@py_assert14rv   @py_format11@py_format19@py_format21@py_format22@py_format24s                  r   )test_size_for_entry_unknown_symbol_raisesr     sz   
,I C!(4		
 .
//	  :w:#a&:w& :L:CF:LF$:::::w&:::w::::::#:::#::::::a:::a:::&:::::::LF:::L::::::C:::C::::::::::::F:::::::::::::::;s   ' 
JIJJc            	        	 t        t               dddddd       t        d      # t        $ r$} d}t        |       }||v }|st	        j
                  d|fd	||f      t	        j                  |      d
t        j                         v st	        j                  t              rt	        j                  t              nd
dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}}t        d       Y d} ~ yd} ~ ww xY w)zFaccount_balance <= 0 -> ValueError (delegated from compute_contracts).r+   r/   r{   r0   r1   r   r   r_   rj   rk   rl   rm   ro   rp   Nz'size_for_entry: balance=0 -> ValueErrorrq   r   rs   s         r   'test_size_for_entry_balance_zero_raisesr     s    
,# C!(4		
 .
//	   *CF* F**** F*** ******C***C************F*******56s   ' 
EDEEc            
        t        t        dd      dddddd	
      } t        t        ddd      dddddd	
      }|j                  }|j                  }| j                  }|j                  }d}||z  }||k(  }|s*t	        j
                  d|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      t	        j                  |      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }	dd|	iz  }
t        t	        j                  |
            dx}x}x}x}x}x}}|j                  }|j                  }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}x}}|j                  }| j                  }||k  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}}t        d| j                  j                   d |j                  j                   d!       y)"u   
    Brain provides risk_multiplier=0.5 via EntryDecision.metadata —
    real risk should halve compared to default 1.0.
    r   r   r   r+   rz   r{   r/   r0   r1   r   r$   )r   r   r"   r   rJ   )z%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.risk_usd_target
} == (%(py10)s
{%(py10)s = %(py8)s
{%(py8)s = %(py6)s.audit
}.risk_usd_target
} / %(py12)s)halvedbase)r>   r@   rA   rn   rp   r   py12zassert %(py15)sr   N)zR%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.audit
}.risk_multiplier
} == %(py7)sr   r   r   )<=)zP%(py2)s
{%(py2)s = %(py0)s.contracts
} <= %(py6)s
{%(py6)s = %(py4)s.contracts
})r>   r@   rA   rn   ro   rp   z<size_for_entry: risk_multiplier 0.5 halves risk_usd_target (z -> r   )r   r)   r   r   rN   rT   rL   rM   rO   rP   rQ   rR   r"   r   r   )r   r   rX   rV   r   @py_assert9@py_assert11@py_assert13rt   @py_format14@py_format16r   r[   r   ru   rv   s                   r   7test_size_for_entry_reads_risk_multiplier_from_metadatar    s   
 Ww?i$t	D Ww),.i$tF <<I<''I4::I:+E+EII+E+II'+IIIII'+IIIIIII6III6III<III'IIIIII4III4III:III+EIIIIIIIIIII<<.<''.3.'3....'3......6...6...<...'...3.......-t~~-~----~------6---6---------t---t---~------- JJ&&'tFLL,H,H+I	L Mr   g     j@)balancec               ,    t        | |||d|dddd
      S )Nr{   r|   r/   g     @r$   r}   )r   )r3   r~   r4   r   r  s        r   _computer    s)    JM!G!% r   c                 t   t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)uB   MNQ sl=6 (tight) → floor a MAX_SL_TICKS[MNQ]=48 → 6 contratti.MNQr   r$      r~   r4   r   FrB   rD   r<   rE   rG   rH   NrJ   r   Tr   r   u0   worst-case MNQ sl=6 → 6 ct (floored to MAX 48)r  rS   rN   rT   rL   rM   rO   rP   rQ   rR   r   r   r   r   r   s         r   #test_worst_case_mnq_tight_sl_to_6ctr       
51R
HC88u8u8u338u==A=A=A33=A'4'4''''4''''''3'''3''''''4'''''''  %A% A%%%% A%%%%%%3%%%3%%% %%%A%%%%%%%:;r   c                 t   t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)uy   MNQ sl=48 (= MAX) → forzato a MAX comunque → 6 contratti.
    Nota: sl_ticks resta 48, quindi sl_ticks_floored=False.r  0   r$   r  r	  FrB   rD   r<   rE   rG   rH   Nr   rJ   r   r   r   u)   worst-case MNQ sl=48 → 6 ct (no change)r
  r   s         r   "test_worst_case_mnq_already_max_slr    s    52$b
IC88u8u8u338u==A=A=A33=A(5(5((((5((((((3(((3((((((5(((((((  &B& B&&&& B&&&&&&3&&&3&&& &&&B&&&&&&&34r   c                 t   t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)u:   MES sl=4 → floor a MAX_SL_TICKS[MES]=40 → 3 contratti.r+      r,   r   r	  FrB   rD   r<   rE   rG   rH   Nr   rJ   r   Tr   r   u0   worst-case MES sl=4 → 3 ct (floored to MAX 40)r
  r   s         r   #test_worst_case_mes_tight_sl_to_3ctr       
51Q
GC88u8u8u338u==A=A=A33=A'4'4''''4''''''3'''3''''''4'''''''  %A% A%%%% A%%%%%%3%%%3%%% %%%A%%%%%%%:;r   c                 t   t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)u:   MYM sl=8 → floor a MAX_SL_TICKS[MYM]=50 → 6 contratti.MYMr-   r$   r  r	  FrB   rD   r<   rE   rG   rH   Nr   rJ   r   Tr   r   u0   worst-case MYM sl=8 → 6 ct (floored to MAX 50)r
  r   s         r   #test_worst_case_mym_tight_sl_to_6ctr    r  r   c                 t   t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)u:   MGC sl=5 → floor a MAX_SL_TICKS[MGC]=80 → 2 contratti.MGC   r   r  r	  FrB   rD   r<   rE   rG   rH   Nr   rJ   r   Tr   r   u0   worst-case MGC sl=5 → 2 ct (floored to MAX 80)r
  r   s         r   #test_worst_case_mgc_tight_sl_to_2ctr    r  r   c                    t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)u:   MGC sl=80 (= MAX) → 2 contratti, sl_ticks_floored=False.r  P   r   r  r	  FrB   rD   r<   rE   rG   rH   Nr   rJ   r   r   u)   worst-case MGC sl=80 → 2 ct (no change))r  rS   rN   rT   rL   rM   rO   rP   rQ   rR   r   r   r   r   s         r   "test_worst_case_mgc_already_max_slr    sg   
52$a
HC88u8u8u338u==A=A=A33=A(5(5((((5((((((3(((3((((((5(((((((34r   c                 t   t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)u0   6E sl=8 < MIN_SL_TICKS[6E]=10 → floor up a 10.r   r-   r   r   r	  FrB   rD   r<   rE   rG   rH   NTr   rJ   r   r   u+   non-worst-case 6E sl=8 → floored a MIN 10)r  rS   rN   rT   rL   rM   rO   rP   rQ   rR   r   r   r   r   r   s         r   #test_non_worst_case_6e_floor_min_slr    s   
4!A
FC88u8u8u338u'4'4''''4''''''3'''3''''''4'''''''  %A% A%%%% A%%%%%%3%%%3%%% %%%A%%%%%%%==A=A=A33=A56r   c                 V   t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)uC   6E sl=15 ≥ MIN=10 → sl_ticks invariato, sl_ticks_floored=False.r      r   r   r	  FrB   r   r<   rE   rG   rH   NrJ   r   u%   non-worst-case 6E sl=15 → invariator  r   rN   rT   rL   rM   rO   rP   rQ   rR   r   r   r   s         r   *test_non_worst_case_6e_above_min_unchangedr#  !  s    
4"Q
GC(5(5((((5((((((3(((3((((((5(((((((  &B& B&&&& B&&&&&&3&&&3&&& &&&B&&&&&&&/0r   c                 V   t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)zzAsset senza entry in MIN/MAX_SL_TICKS (e fuori dal worst-case set):
    sl_ticks resta invariato e sl_ticks_floored=False.r      r   r   r	  FrB   r   r<   rE   rG   rH   NrJ   r   u9   no-entry asset (ES) → invariato, sl_ticks_floored=Falser"  r   s         r   test_no_floor_entry_unchangedr&  )  s    4"a
HC(5(5((((5((((((3(((3((((((5(((((((  &B& B&&&& B&&&&&&3&&&3&&& &&&B&&&&&&&CDr   c                    t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)u7   6A sl=12 — sizing naturale 1ct → floor forza a 2ct.6Ar  g      $@r   r	  FrB   rD   r<   rE   rG   rH   NrJ   r   g      n@r   u.   MIN floor: 6A sl=12 → 2 ct (worst-case $240)r  rS   rN   rT   rL   rM   rO   rP   rQ   rR   r   r   r   r   s         r   test_min_contracts_floor_6ar*  :  g   
4"Q
GC88u8u8u338u==A=A=A33=A%%%%%%%%%%%%3%%%3%%%%%%%%%%%%%89r   c                    t        dddd      } | j                  }d}||u }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d	t	        j
                         v st        j                  |       rt        j                  |       nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}t        d       y)u7   6J sl=20 — sizing naturale 1ct → floor forza a 2ct.r   r%  r   r   r	  FrB   rD   r<   rE   rG   rH   NrJ   r   g     @o@r   u.   MIN floor: 6J sl=20 → 2 ct (worst-case $250)r)  r   s         r   test_min_contracts_floor_6jr-  E  r+  r   c                    t        ddddd      } | j                  }d}||u }|st        j                  d|fd	||f      d
t	        j
                         v st        j                  |       rt        j                  |       nd
t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}| j                  }d}||k(  }|st        j                  d|fd||f      d
t	        j
                         v st        j                  |       rt        j                  |       nd
t        j                  |      t        j                  |      dz  }t        j                  d      dz   d|iz  }t        t        j                  |            dx}x}}t        d       y)u   MNQ non è in MIN_CONTRACTS_PER_TRADE → nessun floor.

    Caso costruito: balance basso così sizing naturale produrrebbe 1 ct.
    Verifica che il floor floor NON intervenga.
    r  r   r$   r  g     @@)r~   r4   r   r  FrB   rD   r<   rE   rG   rH   Nr   rJ   r   z,MNQ non deve essere toccato dal MIN floor FXz
>assert %(py7)su3   MIN floor: MNQ low balance → 1 ct (non impattato))r  rS   rN   rT   rL   rM   rO   rP   rQ   rR   r   _format_assertmsgr   r   s         r   +test_min_contracts_floor_does_not_touch_mnqr0  P  s    d"C 88u8u8u338u==MAM=AMMM=AMMMMMM3MMM3MMM=MMMAMMMMMMMMMMM=>r   c                    t        d       t                t                t                t	                t                t                t                t                t                t                t                t                t                t                t                t!                t#                t%                t'                t)                t+                t-                t/                t1                t3                t5                t7                t9                t;                t=                t?                tA                t        d       y)Nztest_sizing.pyzALL 32 TESTS PASSEDr   )!r   r\   rg   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r#  r&  r*  r-  r0   r   r   mainr3  e  s    	
#%(*!)+*, 02$&%'79+-+--/*,/1-/+-;=')&(')')')&(').0!#!!/1	
 r   __main__)r   rk   returnNone)r   rk   r   floatr   r7  r    r7  r!   intr"   r7  r5  r   )
r3   rk   r~   r8  r4   r7  r   r8  r  r7  )r5  r8  )G__doc__
__future__r   builtinsrL   _pytest.assertion.rewrite	assertionrewriterN   syspathlibr   pathinsertrk   __file__resolveparentcorer   cfg_futcore.contractsr   trading.sizingr   r	   r
   r   r   r   r   r   r   r   r)   r\   rg   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r#  r&  r*  r-  r0  r3  __name__exitr2  r   r   <module>rL     s   #   
  3tH~--/66==> ? * (
 
 
     	
    2;90(S08&0,1400!XP\4Z,9$RBY,0 00(MH 3;		*/	<5<<<571E"::?*(V zCHHTV r   