o
    ¿$¸jø  ã                   @   sv   d Z ddlmZmZ ddlmZ ddlmZmZmZm	Z	m
Z
 eG dd„ dƒƒZeG dd„ dƒƒZeG d	d
„ d
ƒƒZdS )aŠ  
rules_api.py

The contract between the bot CORE (monitor.py) and the RULE MODULES
(breakout_rules.py, reversal_rules.py, exit_rules.py, or any new file
listed under config.json "rules"). Core file -- rule work never needs to
change it.

What monitor.py hands a rule module every poll (every
schedule.poll_interval_seconds) is a MarketView: a read-only picture of
one symbol right now. What it expects back is an EntryDecision (entry
modules) or an ExitDecision (the exit module).

ENTRY MODULE -- must define:
    evaluate(view: MarketView, state: dict) -> EntryDecision
        Called for every watched symbol with no open position while a
        slot is free and before schedule.no_new_entries_after.
        `state` is this module's own per-symbol dict; monitor.py keeps it
        between polls (put timers, confirmations, anything in it). It is
        cleared when the symbol leaves the watchlist or after a buy fills.
        To buy: return EntryDecision(should_enter=True, stop=<price>, ...).
        The stop is required (position size = 1% account risk to that
        stop, capped by trading.max_position_notional_pct_of_equity).

EXIT MODULE -- must define:
    evaluate(view: MarketView, position: dict, state: dict) -> ExitDecision
        Called every poll for every open position. `position` is the
        position record (entry_price, stop_price, qty, entry_time, setup =
        which entry module bought it, plan = whatever that module put in
        EntryDecision.plan). `state` is per-position (fresh dict for each
        new position).

OPTIONAL in any rule module:
    on_trade(symbol, ts, price, size)          raw ticks, straight from the
    on_quote(symbol, ts, bid, ask, bid_size, ask_size)    stream thread --
    on_bar(symbol, bar)                        keep these FAST, they run
    seed_bars(symbol, bars)                    for every tick
    on_entry(symbol, position)                 after a buy is placed
    on_exit(symbol, position, reason)          after a sell is placed
    on_session_end()                           at EOD (flush files etc.)

Anything a rule module raises is caught and logged by monitor.py; it can
never crash the core.
é    )Ú	dataclassÚfield)Údatetime)ÚCallableÚDictÚListÚOptionalÚTuplec                   @   sò   e Zd ZU eed< eed< eed< ee ed< ee ed< e	e
eef  ed< eee	e f ed< e	e ed< eed	< eed
< eed< e	e ed< eed< eed�Zeeee f ed< dZe	eege	e f  ed< ddede	e fdd„ZdS )Ú
MarketViewÚsymbolÚnowÚpriceÚbarsÚbars_subÚquoteÚlevelsÚvolume_baselineÚscanÚminutes_since_openÚ
slots_freeÚ
last_tradeÚcfg©Údefault_factoryÚ
benchmarksNÚ
_imbalanceç      4@Úwindow_secondsÚreturnc                 C   s   | j r|   |¡S dS )ziBuy-minus-sell trade imbalance over the last window, -1..+1
        (None when there are too few trades).N)r   )Úselfr   © r    ú%/var/www/screener/trade1/rules_api.pyÚ	imbalanceE   s   zMarketView.imbalance)r   )Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__r   Úfloatr   Údictr   r	   r   Úboolr   r   r   r   r"   r    r    r    r!   r
   2   s"   
 r
   c                   @   s|   e Zd ZU dZeed< dZee ed< dZ	e
ed< dZe
ed< eed	�Zee
 ed
< eed	�Zeed< eed	�Zeed< dS )ÚEntryDecisionFÚshould_enterNÚstopÚWAITÚstateÚ Úreasonr   ÚreasonsÚplanÚmetrics)r#   r$   r%   r,   r*   r'   r-   r   r(   r/   r&   r1   r   Úlistr2   r   r)   r3   r4   r    r    r    r!   r+   K   s   
 r+   c                   @   sD   e Zd ZU dZeed< dZeed< dZeed< e	e
d�Ze
ed< d	S )
ÚExitDecisionFÚshould_exitÚHOLDr/   r0   r1   r   r4   N)r#   r$   r%   r7   r*   r'   r/   r&   r1   r   r)   r4   r    r    r    r!   r6   V   s
   
 r6   N)Ú__doc__Údataclassesr   r   r   Útypingr   r   r   r   r	   r
   r+   r6   r    r    r    r!   Ú<module>   s    ,
