B
     9-e##                 @   sX   d dl Zd dlZd dlmZ d dlmZ d dlmZ G dd de	Z
G dd de	ZdS )	    N)PrintableII)TimeConcept)exitc               @   sN   e Zd Zdd dfddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	dS )Repeaterc             C   s   dS )NF )xr   r   G:\My Drive\STUDY\EPAT\09 TBP - Trading & Back-testing Platforms\TBP04 - Backtesting & Live Trading\IB IBridgePy API\02 Python 3.7 IBridgePy_Win_Anaconda37_64\BasicPyLib\repeater.py<lambda>       zRepeater.<lambda>Nc             C   sJ   i | _ i | _i | _g | _|| _|| _tdddddd| _|| _	|| _
dS )av  
        To schedule events
        :param repeaterBaseFreq: repeater will run based on this base frequency, unit=second, fraction of seconds are OK. In backtest mode, it should be set to zero because the simulated time will come in by TimeGenerator and no delay is appropriate.
        :param getTimeNowFuncGlobal: a function can return time. timezone is not used in repeater. The user of the repeater should consider timezone when supplying a time generator because spot-time event is related to timezone.
        :param stopFuncGlobal: a function can return a boolean. True=stop repeater
        :param log: system log
        i           ;   N)
spotEventsrepeatedEventsconceptEvents	hftEventsgetTimeNowFuncGlobalstopFuncGlobaldtdatetimetimePreviousrepeaterBaseFreq_log)selfr   r   r   logr   r   r   __init__   s    zRepeater.__init__c             C   s   t |tjr| | npt |tjr:|jdkr| | nNt |tjrR| | n6t |tj	rl| j
| n| jtd|f   t  d S )Nr   z.::schedule_event: Exit, cannot handle event=%s)
isinstanceEventSpotTimeEvent_spot_time_schedulerRepeatedEventfreq_repeated_schedulerConceptEvent_concept_schedulerHftEventr   appendr   error__name__r   )r   eventr   r   r   schedule_event   s    
zRepeater.schedule_eventc             C   s.   |j | jkrg | j|j < | j|j  | dS )z
        Change repeated events to spot-time events, Then, add it to repeaterEngine which only process spot-time events
        :param repeatedEvent:
        :return:
        N)r"   r   r'   )r   ZrepeatedEventr   r   r   r#   +   s    zRepeater._repeated_schedulerc             C   s.   |j | jkrg | j|j < | j|j  | dS )z
        Change repeated events to spot-time events, Then, add it to repeaterEngine which only process spot-time events
        :param conceptEvent:
        :return:
        N)conceptr   r'   )r   ZconceptEventr   r   r   r%   5   s    zRepeater._concept_schedulerc             C   sF   |j d d |jd  |j }|| jkr2g | j|< | j| | d S )N<   )onHouronMinuteonSecondr   r'   )r   ZsingleEventspotTimer   r   r   r    ?   s    

zRepeater._spot_time_schedulerc             C   s.   x(| j D ]}x| j | D ]
}d|_qW qW d S )NF)r   hadExecutedToday)r   r1   r*   r   r   r   1_reset_hasExecutedToday_flag_for_spot_time_eventsE   s    z:Repeater._reset_hasExecutedToday_flag_for_spot_time_eventsc             C   s  | j td| jf   | j td| jf   | j td| jf   | j td| jf   xby2| j td  |  }| j td|f   W n t	k
r   P Y nX | 
|r| j td  P |j| jjkr|   tj| jkrx(| jtj D ]}||r|| qW |j| jjkrftj| jkrfx,| jtj D ]}||rF|| qFW |jd d |jd  |j }|j| jjks|j| jjks|j| jjks|j| jjkrxJ| jD ]@}|| d	krx*| j| D ]}||r|| qW qW |jd d |jd  |j }|| jkrtx8| j| D ]*}|jsF||rF|| d
|_qFW | jd	krt| j | jrx&| jD ]}||r|| qW || _qdW d S )Nz::repeat: spotEvents=%sz::repeat: repeatedEvents=%sz::repeat: conceptEvents=%sz::repeat: hftEvents=%sz.::repeat: ####    START a new datetime    ####z::repeat: timeNow=%sz1::repeat: timeNow=%s not go through repeat enginer-   r   T)r   debugr)   r   r   r   r   notsetr   StopIterationr   dayr   r3   r   NEW_DAYpassFuncdo_somethinghourNEW_HOURminutesecondr2   r   timesleep)r   timeNowr*   ZcurrentHourMinuteSecondsr"   r   r   r   repeatJ   s^    

  
zRepeater.repeat)
r)   
__module____qualname__r   r+   r#   r%   r    r3   rB   r   r   r   r   r   
   s   

r   c               @   sT   e Zd Zdd ZG dd deZG dd deZG dd deZG d	d
 d
eZdS )r   c             C   s   d S )Nr   )r   r   r   r   r      s    zEvent.__init__c               @   s   e Zd Zdd fddZdS )zEvent.HftEventc             C   s   dS )NTr   )r   r   r   r   r	      r
   zEvent.HftEvent.<lambda>c             C   s   d| _ || _|| _d S )Nr&   )typer9   r:   )r   r:   r9   r   r   r   r      s    zEvent.HftEvent.__init__N)r)   rC   rD   r   r   r   r   r   r&      s   r&   c               @   s   e Zd Zdd fddZdS )zEvent.SpotTimeEventc             C   s   dS )NTr   )r   r   r   r   r	      r
   zEvent.SpotTimeEvent.<lambda>c             C   s.   d| _ || _|| _|| _|| _|| _d| _d S )Nr   F)rE   r9   r:   r.   r/   r0   r2   )r   r.   r/   r0   r:   r9   r   r   r   r      s    zEvent.SpotTimeEvent.__init__N)r)   rC   rD   r   r   r   r   r   r      s   r   c               @   s   e Zd Zdd fddZdS )zEvent.RepeatedEventc             C   s   dS )NTr   )r   r   r   r   r	      r
   zEvent.RepeatedEvent.<lambda>c             C   s   d| _ || _|| _|| _dS )z

            :param freq: number in seconds !!!
            :param do_something: !!!!!! do_something must have one input -- datetime
            :param passFunc: True = stop repeater
            r!   N)rE   r"   r:   r9   )r   r"   r:   r9   r   r   r   r      s    zEvent.RepeatedEvent.__init__N)r)   rC   rD   r   r   r   r   r   r!      s   r!   c               @   s   e Zd Zdd fddZdS )zEvent.ConceptEventc             C   s   dS )NTr   )r   r   r   r   r	      r
   zEvent.ConceptEvent.<lambda>c             C   s   d| _ || _|| _|| _dS )z

            :param concept: NEW_DAY, NEW_HOUR
            :param do_something: !!!!!! do_something must have one input -- datetime
            :param passFunc: True = stop repeater
            r$   N)rE   r,   r:   r9   )r   r,   r:   r9   r   r   r   r      s    zEvent.ConceptEvent.__init__N)r)   rC   rD   r   r   r   r   r   r$      s   r$   N)	r)   rC   rD   r   r   r&   r   r!   r$   r   r   r   r   r      s
   
r   )r   r   r?   BasicPyLib.Printabler   IBridgePy.constantsr   sysr   objectr   r   r   r   r   r   <module>   s    