
    pjV                    J   d Z ddlmZ ddlmZmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZ  G d d	ee	      Z ed
       G d d             Z ed
       G d d             Ze G d d             Ze G d d             Ze G d d             Ze G d de             Zy)zNContracts between the live orchestrator, broker session, and strategy plugins.    )annotations)	dataclassfield)datedatetime)Enum)Path)AnyProtocolruntime_checkablec                      e Zd ZdZdZdZy)
RiskActionallowblock_new_entrieshalt_allN)__name__
__module____qualname__ALLOWBLOCK_NEW_ENTRIESHALT_ALL     2/opt/rentech/trading_bot/RenTech/live/protocols.pyr   r      s    E+Hr   r   T)frozenc                  v    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<   ded<   ded<   ded<   y)PositionLegz0Normalized open leg from IB (or strategy state).intcon_idstrsymbolsec_typeexpiryfloatstrikerightpositionavg_costmarket_valueunrealized_pnlNr   r   r   __doc____annotations__r   r   r   r   r      s9    :KKMKMJOOr   r   c                      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
<   ded<   ded<    ee      Zded<    ee      Zded<   ddZ	ddZ
y)PortfolioSnapshotz9Account + positions as seen by the broker at cycle start.r   as_ofr$   net_liquidation_usdavailable_funds_usdexcess_liquidity_usdmaintenance_margin_usdunrealized_pnl_usdrealized_pnl_today_usdmargin_utilizationtuple[PositionLeg, ...]	positionsdefault_factoryz"dict[str, tuple[PositionLeg, ...]]positions_by_symbolzdict[str, float]raw_account_tagsc                :    t        d | j                  D              S )Nc              3  @   K   | ]  }|j                   d k(  s|  yw)OPTN)r"   .0ps     r   	<genexpr>z5PortfolioSnapshot.open_option_legs.<locals>.<genexpr>3   s     F1!**2EQFs   )tupler9   selfs    r   open_option_legsz"PortfolioSnapshot.open_option_legs2   s    FFFFr   c                :    t        d | j                  D              S )Nc              3  F   K   | ]  }t        |j                          y w)N)absr)   rA   s     r   rD   z7PortfolioSnapshot.gross_notional_usd.<locals>.<genexpr>6   s     ?13q~~&?s   !)sumr9   rF   s    r   gross_notional_usdz$PortfolioSnapshot.gross_notional_usd5   s    ????r   N)returnr8   )rN   r$   )r   r   r   r,   r-   r   dictr<   r=   rH   rM   r   r   r   r/   r/   "   si    CO!!!!&&>CTX>Y;Y).t)D&DG@r   r/   c                  X    e Zd ZU ded<    ee      Zded<   dZded<   dZded	<   dd
Z	y)RiskVerdictr   actionr:   	list[str]reasonsTboolallow_exitsallow_new_entriesc                r    t         j                  | _        d| _        | j                  j                  |       y )NF)r   r   rR   rW   rT   append)rG   reasons     r   merge_block_entrieszRiskVerdict.merge_block_entries@   s)     22!&F#r   N)rZ   r    rN   None)
r   r   r   r-   r   listrT   rV   rW   r[   r   r   r   rQ   rQ   9   s1    t4GY4K"t"$r   rQ   c                  v    e Zd ZU ded<   ded<   dZded<   dZded<    ee	      Zd
ed<    ee		      Z
ded<   y)StrategyCycleReportr    strategy_idrU   okr   r   entries_attemptedexits_attemptedr:   rS   messagesdict[str, Any]metadataN)r   r   r   r-   rb   rc   r   r]   rd   rO   rf   r   r   r   r_   r_   F   s@    HsOS5Hi5$T:Hn:r   r_   c                      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<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded <   ded!<   y")#StrategyContextz0Per-cycle context passed to every live strategy.r    run_idr   todayr   now_etr
   ibr/   	portfoliorQ   platform_riskstrategy_riskrU   recommend_onlyforce_entry_nowentry_after_etz
str | Noneentry_before_etr`   re   strategy_configr$   capital_budget_usdr	   	repo_rootlog_dir	state_dirr   max_new_entries_per_daymax_open_positionsenabled_for_entriesNr+   r   r   r   rh   rh   P   sw    :KKG  ##OMO  r   rh   c                  ,    e Zd ZU dZded<   ddZddZy)LiveStrategyz3Plugin interface: one implementation per live book.r    r`   c                   K   yw)z7Manage exits first, then entries if risk + gates allow.Nr   rG   ctxs     r   	run_cyclezLiveStrategy.run_cyclep   	        c                   K   yw)z3Optional reconciliation vs broker (default: no-op).Nr   r   s     r   auditzLiveStrategy.auditt   r   r   N)r   rh   rN   r_   )r   r   r   r,   r-   r   r   r   r   r   r}   r}   j   s    =r   r}   N)r,   
__future__r   dataclassesr   r   r   r   enumr   pathlibr	   typingr
   r   r   r    r   r   r/   rQ   r_   rh   r}   r   r   r   <module>r      s    T " ( #   3 3d  $   $@ @ @, 	$ 	$ 	$ ; ; ;   2 8  r   