Ë
    €D±j  ã                   óX   — d Z ddlmZ ddlZ ed¬«       G d„ d«      «       Zdddd	œd
„Zy)a?  Experimental state-aware action replay; disabled in the production strategy.

Actions are decisions at the close, not broker fill states. A deferred score exit
is latched until the minimum holding age, so a one-bar crossunder is not lost.
Trailing stops always override holding. Entry blocks never queue stale entries.
é    )Ú	dataclassNT)Úfrozenc                   ó>   — e Zd ZU dZeed<   dZeed<   dZeed<   d„ Zy)ÚStatePolicyr   Úmacro_memoryÚexit_cooldownÚmin_holdc                 ó´   — | j                   | j                  | j                  fD ]2  }t        |t        «      rt        |t
        «      s|dk  sŒ)t        d«      ‚ y )Nr   z1Policy durations must be nonnegative integer bars)r   r   r	   Ú
isinstanceÚintÚboolÚ
ValueError)ÚselfÚvalues     úF/Users/jameslopez/projects/TradingBot25/strategies/mlp_state_policy.pyÚ__post_init__zStatePolicy.__post_init__   sJ   € Ø×'Ñ'¨×);Ñ);¸T¿]¹]ÓKˆEÜ˜e¤SÔ)¬Z¸¼tÔ-DÈÐPQË	Ü Ð!TÓUÐUñ Ló    N)	Ú__name__Ú
__module__Ú__qualname__r   r   Ú__annotations__r   r	   r   © r   r   r   r      s&   … à€L�#ÓØ€M�3ÓØ€HˆcÓóVr   r   F)ÚriskÚtime_okÚ	dead_zonec          
      óò  ‡— t        | «      Š|	€t        j                  ‰t        ¬«      nt        j                  |	t        ¬«      }	|
€t        j
                  ‰t        ¬«      nt        j                  |
t        ¬«      }
t        ˆfd„||||||	|
fD «       «      rt        d«      ‚t        j                  ‰t        ¬«      }t        j                  ‰t        «      t        j                  ‰t        «      }}t        j                  ‰dd¬«      }t        j                  ‰t        «      t        j                  ‰t        «      }}d\  }}}d\  }}}d}t        ‰«      D �]  }|	|   r||j                  z   }||k  xs ||k  }|r¤||d	z   k7  rt        |||   «      }|d
kD  xr ||   |d	|z
  z  k  }|r||   n||   }|
|   r|xs t        |«      }||z
  |j                  k  }|xr |xr
 | xr |
|   ||<   |
|   r&|s|r"|s d\  }}}d||<   d||<   ||j                  z   }nMd||<   nGt        | |   «      xr	 |xr |
|   ||<   | |   r&|s$|
|   rd}d||<   d||<   |||   }}|xr ||   |k  }|||<   �Œ ||||||dœS )a/  Replay raw opportunities using position age, exit memory and macro risk.

    A risk bar blocks itself and N following bars, refreshed on every risk bar.
    An exit blocks the following N bars; entry becomes possible at exit+N+1.
    Risk is entry-only: it does not liquidate an existing position.
    )Údtypec              3   ód   •K  — | ]'  }t        j                  |«      j                  ‰fk7  –— Œ) y ­w)N)ÚnpÚasarrayÚshape)Ú.0ÚaÚns     €r   Ú	<genexpr>z&replay_state_policy.<locals>.<genexpr>$   s)   øè ø€ Ð
jÑ0i¨1Œ2�:‰:�a‹=×Ñ 1 $Õ&Ñ0iùs   ƒ-0z7Replay arrays must be one-dimensional and equally sizedÚ	stay_flatz<U10)FFF)éÿÿÿÿr'   r'   g        é   r   TÚ	exit_longÚ	hold_longÚ
enter_long)ÚpositionÚexecute_entryÚexecute_exitÚpolicy_actionÚpolicy_blocked_entryÚpolicy_deferred_exit)Úlenr   Úzerosr   r    ÚonesÚanyr   r   ÚfullÚranger   Úmaxr	   r   )ÚentryÚexitsÚfallbackÚ
score_exitÚexit_thresholdÚcloseÚhighÚtrailÚpolicyr   r   r   Ú	positionsÚenteredÚexitedÚactionsÚblockedÚdeferredr,   Úuse_rawÚpending_exitÚ	entry_barÚmacro_untilÚcooldown_untilÚ
trade_highÚtÚentry_blockedÚstopÚscore_signalÚholdr$   s                                 @r   Úreplay_state_policyrS      s–  ø€ ô 	ˆE‹
€AØ&* lŒ2�8‰8�AœTÕ"¼¿
¹
À4ÌtÔ8T€DØ(/¨Œb�g‰g�aœtÕ$¼R¿Z¹ZÈÔW[Ô=\€GÜ
Ó
j°¸À*ÈeÐUYÐ[_ÐahÑ0iÓ
jÔjÜÐRÓSÐSÜ—‘˜¤#Ô&€IÜ—h‘h˜q¤$Ó'¬¯©°!´TÓ):ˆV€GÜ�g‰g�a˜¨FÔ3€GÜŸ™ ¤DÓ)¬2¯8©8°A´tÓ+<ˆX€GØ&9Ñ#€Hˆg�|Ø-7Ñ*€Iˆ{˜NØ€JÜ�1�XˆØ�Š7Ø˜f×1Ñ1Ñ1ˆKØ˜[Ñ(Ò?¨A°Ñ,?ˆÙØ�I ‘MÒ!Ü  ¨T°!©WÓ5�
Ø˜1‘9ÒE  q¡¨Z¸1¸u¹9Ñ-EÑ!EˆDÙ*1˜8 Aš;°u¸Q±xˆLØ�qŠzØ+ÒA¬t°LÓ/A�Ø�y‘= 6§?¡?Ñ2ˆDØ&ÒK¨4ÒK¸°HÒKÀÈÁˆH�Q‰KØ�qŠz™t©¹TØ2EÑ/�˜,¨Ø ��q‘	Ø(�˜‘
Ø!" V×%9Ñ%9Ñ!9‘à(�˜’
ä˜e A™h›ÒH¨MÒH¸gÀa¹jˆG�A‰JØ�QŠx¡°'¸!²*Ø�Ø!�˜‘
Ø)�˜‘
Ø()¨5°©8˜:�	Ø#ÒF¨
°1©¸Ñ(F�Øˆ	�!‹ð9 ð: "°GÈVØ$¸gØ$,ñ.ð .r   )Ú__doc__Údataclassesr   Únumpyr   r   rS   r   r   r   Ú<module>rW      sC   ðñõ "ã ñ �$Ô÷Vð Vó ðVð =AÈ$Ø"'õ4.r   