
    T>qj)                        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	 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mZmZmZmZ ddlmZmZ  ed	      Ze G d
 d             Z G d de      Z G d de      Z  G d de       Z!e G d d             Z" G d dee"         Z# G d de      Z$ G d de      Z% G d de      Z&y)z&Access to realtime market information.    )	dataclassfield)datetime)ClassVarListOptionalUnion)EventOp)Contract)	DOMLevel	DividendsFundamentalRatiosMktDepthDataOptionComputationTickByTickAllLastTickByTickBidAskTickByTickMidPointTickData)dataclassReprisNannanc                      e Zd ZU dZdZeed<   dZee	   ed<   dZ
ee   ed<   dZeed<   eZeed	<   eZeed
<   eZeed<   dZeed<   eZeed<   eZeed<   dZeed<   eZeed<   eZeed<   dZeed<   eZeed<   eZeed<   eZeed<   eZeed<   eZeed<   eZ eed<   eZ!eed<   eZ"eed<   eZ#eed<   eZ$eed<   eZ%eed<   eZ&eed<   eZ'eed <   eZ(eed!<   eZ)eed"<   eZ*eed#<   eZ+eed$<   eZ,eed%<   eZ-eed&<   eZ.eed'<   eZ/eed(<   eZ0eed)<   eZ1eed*<   eZ2eed+<   eZ3eed,<   eZ4eed-<   dZ5ee   ed.<   eZ6eed/<   eZ7eed0<   eZ8eed1<   eZ9eed2<   eZ:eed3<   eZ;eed4<   eZ<eed5<   eZ=eed6<   eZ>eed7<   eZ?eed8<   eZ@eed9<   eZAeed:<   eZBeed;<   eZCeed<<   dZDeeE   ed=<   dZFeeG   ed><    eHeI?      ZJeKeL   ed@<    eHeI?      ZMeKeNeOePeQf      edA<    eHeI?      ZReKeS   edB<    eHeI?      ZTeKeS   edC<    eHeI?      ZUeKeV   edD<   dZWeeX   edE<   dZYeeX   edF<   dZZeeX   edG<   dZ[eeX   edH<   eZ\eedI<   eZ]eedJ<   eZ^eedK<   eZ_eedL<   dZ`eedM<   dNZaeedO<   dP ZbdQ ZcdR ZdeeZfeeZgdSehfdTZidSefdUZjdSefdVZky)WTickera  
    Current market data such as bid, ask, last price, etc. for a contract.

    Streaming level-1 ticks of type :class:`.TickData` are stored in
    the ``ticks`` list.

    Streaming level-2 ticks of type :class:`.MktDepthData` are stored in the
    ``domTicks`` list. The order book (DOM) is available as lists of
    :class:`.DOMLevel` in ``domBids`` and ``domAsks``.

    Streaming tick-by-tick ticks are stored in ``tickByTicks``.

    For options the :class:`.OptionComputation` values for the bid, ask, resp.
    last price are stored in the ``bidGreeks``, ``askGreeks`` resp.
    ``lastGreeks`` attributes. There is also ``modelGreeks`` that conveys
    the greeks as calculated by Interactive Brokers' option model.

    Events:
        * ``updateEvent`` (ticker: :class:`.Ticker`)
    )updateEventeventsNcontracttime   marketDataTypeminTickbidbidSize bidExchangeaskaskSizeaskExchangelastlastSizelastExchangeprevBidprevBidSizeprevAskprevAskSizeprevLastprevLastSizevolumeopenhighlowclosevwap	low13week
high13week	low26week
high26week	low52week
high52weekbidYieldaskYield	lastYield	markPricehaltedrtHistVolatilityrtVolumertTradeVolumertTimeavVolume
tradeCount	tradeRate
volumeRateshortableSharesindexFuturePremiumfuturesOpenInterestputOpenInterestcallOpenInterest	putVolume
callVolumeavOptionVolumehistVolatilityimpliedVolatility	dividendsfundamentalRatios)default_factorytickstickByTicksdomBidsdomAsksdomTicks	bidGreeks	askGreeks
lastGreeksmodelGreeksauctionVolumeauctionPriceauctionImbalanceregulatoryImbalancebboExchanger   snapshotPermissionsc                 $    t        d      | _        y Nr   )TickerUpdateEventr   selfs    O/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/ib_insync/ticker.py__post_init__zTicker.__post_init__v   s    ,];    c                 
    | |u S N rk   others     rl   __eq__zTicker.__eq__y       u}rn   c                     t        |       S rp   idrj   s    rl   __hash__zTicker.__hash__|       $xrn   returnc                     | j                   dk7  xra t        | j                          xrI | j                  dkD  xr8 | j                  dk7  xr' t        | j                         xr | j                  dkD  S )z+See if this ticker has a valid bid and ask.r   )r"   r   r#   r&   r'   rj   s    rl   	hasBidAskzTicker.hasBidAsk   sp     HHN L5?2 Lt||a7G LBL',TXX#6L;?<<!;K	Mrn   c                 f    | j                         r| j                  | j                  z   dz  S t        S )zf
        Return average of bid and ask, or NaN if no valid bid and ask
        are available.
        g      ?)r~   r"   r&   r   rj   s    rl   midpointzTicker.midpoint   s*    
 /3nn.>488#s*GCGrn   c                     | j                         rL| j                  | j                  cxk  r| j                  k  rn n| j                  }|S | j	                         }|S | j                  }|S )z
        Return the first available one of

        * last price if within current bid/ask or no bid/ask available;
        * average of bid and ask (midpoint).
        )r~   r"   r)   r&   r   )rk   prices     rl   marketPricezTicker.marketPrice   s[     >>xx49900		
    IIErn   )l__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r   r   r    intr   r!   floatr"   r#   r%   strr&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   r   rV   r   r   listrX   r   r   rY   r	   r   r   r   rZ   r   r[   r\   r   r]   r   r^   r_   r`   ra   rb   rc   rd   re   rf   rm   rt   ry   r   __repr____str__boolr~   r   r   rq   rn   rl   r   r      s   * (FH'#'Hhx '#D(8
#NCGUCGUKCGUKD%HeL#GUKGUKHeL%FED%D%CE5D%IuJIuJIuJHeHeIuIuFE!e!HeM5!%FHX%HeJIuJ OU  ##!$$ OU !e!IuJNENE"u"%)Ix	")59x 129!$7E4>7 	d# e+-??A B $ $D9GT(^9#D9GT(^9#(#>Hd< >-1Ix)*1-1Ix)*1.2J*+2/3K+,3M5L%!e!!$$K  < HGM4 MH% HU rn   r   c                   8    e Zd ZdZddZddZddZddZddZy)	ri   rq   c                     t        d|       S )zEmit trade ticks.)      0   D   G   
Tickfilterrj   s    rl   tradeszTickerUpdateEvent.trades   s    ,d33rn   c                     t        d|       S )zEmit bid ticks.)r   r   B   E   r   rj   s    rl   bidszTickerUpdateEvent.bids       .$//rn   c                     t        d|       S )zEmit ask ticks.)      C   F   r   rj   s    rl   askszTickerUpdateEvent.asks   r   rn   c                     t        d|       S )zEmit bid and ask ticks.)r   r   r   r   r   r   r   r   r   rj   s    rl   bidaskszTickerUpdateEvent.bidasks   s    6==rn   c                     t        d|       S )zEmit midpoint ticks.rq   )	Midpointsrj   s    rl   	midpointszTickerUpdateEvent.midpoints   s    T""rn   N)r{   r   )	r   r   r   	__slots__r   r   r   r   r   rq   rn   rl   ri   ri      s     I400>#rn   ri   c                   R    e Zd ZdZdZddZd Zdeddfd	Zd
e	ddfdZ
de	ddfdZy)r   z@Tick filtering event operators that ``emit(time, price, size)``.)
_tickTypesNc                 P    t        j                  | |       t        |      | _        y rp   )r   __init__setr   )rk   	tickTypessources      rl   r   zTickfilter.__init__   s    
D&!i.rn   c                     |j                   D ]L  }|j                  | j                  v s| j                  |j                  |j
                  |j                         N y rp   )rX   tickTyper   emitr   r   size)rk   tickerts      rl   	on_sourcezTickfilter.on_source   sA     	3AzzT__,		!&&!''1662	3rn   timerr{   TimeBarsc                     t        ||       S )aK  
        Aggregate ticks into time bars, where the timing of new bars
        is derived from a timer event.
        Emits a completed :class:`Bar`.

        This event stores a :class:`BarList` of all created bars in the
        ``bars`` property.

        Args:
            timer: Event for timing when a new bar starts.
        )r   rk   r   s     rl   timebarszTickfilter.timebars   s     t$$rn   countTickBarsc                     t        ||       S )a%  
        Aggregate ticks into bars that have the same number of ticks.
        Emits a completed :class:`Bar`.

        This event stores a :class:`BarList` of all created bars in the
        ``bars`` property.

        Args:
            count: Number of ticks to use to form one bar.
        )r   )rk   r   s     rl   tickbarszTickfilter.tickbars   s     t$$rn   r2   
VolumeBarsc                     t        ||       S )a  
        Aggregate ticks into bars that have the same volume.
        Emits a completed :class:`Bar`.

        This event stores a :class:`BarList` of all created bars in the
        ``bars`` property.

        Args:
            count: Number of ticks to use to form one bar.
        )r   )rk   r2   s     rl   
volumebarszTickfilter.volumebars   s     &$''rn   rp   )r   r   r   r   r   r   r   r
   r   r   r   r   rq   rn   rl   r   r      sM    JI)3
%e %
 %%c %j %( ( (rn   r   c                       e Zd ZdZd Zy)r   rq   c                 t    |j                   r,| j                  |j                  |j                         d       y y )Nr   )rX   r   r   r   )rk   r   s     rl   r   zMidpoints.on_source   s)    <<IIfkk6??#4a8 rn   N)r   r   r   r   r   rq   rn   rl   r   r      s    I9rn   r   c                   r    e Zd ZU ee   ed<   eZeed<   eZ	eed<   eZ
eed<   eZeed<   dZeed<   dZeed<   y	)
Barr   r3   r4   r5   r6   r   r2   r   N)r   r   r   r   r   r   r   r3   r   r4   r5   r6   r2   r   r   rq   rn   rl   r   r      sG    
8
D%D%CE5FCOE3Nrn   r   c                   *     e Zd Z fdZd Zd Z xZS )BarListc                 >    t        |   |  t        d      | _        y rh   )superr   r
   r   )rk   args	__class__s     rl   r   zBarList.__init__  s    $ /rn   c                 
    | |u S rp   rq   rr   s     rl   rt   zBarList.__eq__  ru   rn   c                     t        |       S rp   rw   rj   s    rl   ry   zBarList.__hash__  rz   rn   )r   r   r   r   rt   ry   __classcell__)r   s   @rl   r   r     s    0rn   r   c                   b    e Zd ZU dZej
                  j                  Zeed<   ddZ	d Z
d Zd Zy)	r   )_timerbarsr   Nc                     t        j                  | |       || _        | j                  j                  | j                  d | j
                         t               | _        y rp   )r   r   r   connect	_on_timer_on_timer_doner   r   )rk   r   r   s      rl   r   zTimeBars.__init__  s@    
D&!DNND$2E2EFI	rn   c                    | j                   sy | j                   d   }t        |j                        r|x|_        x|_        |_        t        |j                  |      |_        t        |j                  |      |_        ||_        |xj                  |z  c_        |xj                  dz  c_	        | j                   j                  j                  | j                   d       y )Nr}   r   F)r   r   r3   r4   r5   maxminr6   r2   r   r   r   rk   r   r   r   bars        rl   r   zTimeBars.on_source  s    yyiim?,11CH1sx#'sxx'cggu%	

d
		Q			""499e4rn   c                    | j                   r| j                   d   }t        |j                        rKt        | j                         dkD  r3| j                   d   j                  x|_        x|_        x|_        |_        | j                   j                  j                  | j                   d       | j                  |       | j                   j                  t        |             y )Nr}   r   T)r   r   r6   lenr3   r4   r5   r   r   appendr   )rk   r   r   s      rl   r   zTimeBars._on_timer(  s    99))B-CSYYC		NQ$6IIbM''( (38 (cg	II!!&&tyy$7IIcN		T#rn   c                 2    d | _         | j                          y rp   )r   set_doner   s     rl   r   zTimeBars._on_timer_done2  s    rn   rp   )r   r   r   r   r   r   r   r   r   r   r   r   r   rq   rn   rl   r   r     s1    #I!!))G
M5$rn   r   c                   V    e Zd ZU dZej
                  j                  Zeed<   ddZ	d Z
y)r   )_countr   r   Nc                 \    t        j                  | |       || _        t               | _        y rp   )r   r   r   r   r   )rk   r   r   s      rl   r   zTickBars.__init__=  s     
D&!I	rn   c           	      x   | j                   r&| j                   d   j                  | j                  k(  r-t        ||||||d      }| j                   j	                  |       nv| j                   d   }t        |j                  |      |_        t        |j                  |      |_        ||_	        |xj                  |z  c_
        |xj                  dz  c_        |j                  | j                  k(  rL| j                   j                  j                  | j                   d       | j                  | j                          y y Nr}   r   T)r   r   r   r   r   r   r4   r   r5   r6   r2   r   r   r   s        rl   r   zTickBars.on_sourceB  s    yyDIIbM//4;;>dE5%a@CIIS!))B-C388U+CH#''5)CGCIJJ$JIINI99#II!!&&tyy$7IIdii  $rn   rp   )r   r   r   r   r   r   r   r   r   r   r   rq   rn   rl   r   r   7  s'    "I!!))G
M
!rn   r   c                   V    e Zd ZU dZej
                  j                  Zeed<   ddZ	d Z
y)r   )_volumer   r   Nc                 \    t        j                  | |       || _        t               | _        y rp   )r   r   r   r   r   )rk   r2   r   s      rl   r   zVolumeBars.__init__X  s     
D&!I	rn   c           	      x   | j                   r&| j                   d   j                  | j                  k\  r-t        ||||||d      }| j                   j	                  |       nv| j                   d   }t        |j                  |      |_        t        |j                  |      |_        ||_	        |xj                  |z  c_        |xj                  dz  c_
        |j                  | j                  k\  rL| j                   j                  j                  | j                   d       | j                  | j                          y y r   )r   r2   r   r   r   r   r4   r   r5   r6   r   r   r   r   s        rl   r   zVolumeBars.on_source]  s    yyDIIbM00DLL@dE5%a@CIIS!))B-C388U+CH#''5)CGCIJJ$JIINI::%II!!&&tyy$7IIdii  &rn   rp   )r   r   r   r   r   r   r   r   r   r   r   rq   rn   rl   r   r   R  s'    #I##++G
M
!rn   r   N)'r   dataclassesr   r   r   typingr   r   r   r	   eventkitr
   r   ib_insync.contractr   ib_insync.objectsr   r   r   r   r   r   r   r   r   ib_insync.utilr   r   r   r   r   ri   r   r   r   r   r   r   r   rq   rn   rl   <module>r      s    , (  2 2  '   0El I I IX# #04( 4(n9
 9   
d3i 
%r %P!r !6! !rn   