B
     9-ek                 @   s   d dl Z d dlZd dlmZ d dlmZ d dlZd dl	m
Z
 d dlZd dlmZ dd Zdd	d
Zdd ZG dd deZG dd deZG dd deZdS )    N)PrintableII)TimeGeneratorType)exit)epoch_to_dtc             c   s^   xX| D ]P}t |tjs.ttd|f   t  |jd krPttd|f   t  |V  qW d S )Nz<::make_custom_time_generator: spotTime=%s must be a datetimez<::make_custom_time_generator: spotTime=%s must have timezone)
isinstancedtdatetimeprint__name__r   tzinfo)customSpotTimeListspotTime 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\IBridgePy\TimeGenerator.pymake_custom_time_generator   s    

r   1Tc             c   s   d}d}|dkr*| j dd}|j dd}nt|dkrP| j ddd}|j ddd}nN|dkrz| j dddd}|j dddd}n$|d	kr| }|}nttd
|f   tj|||tdd}|d gt| }x|D ]}| V  qW dS )a  

    :param startingTime:
    :param endingTime:
    :param freq:
    # 1S = 1 second; 1T = 1 minute; 1H = 1 hour; 1D = 1 day
    # https://pandas.pydata.org/pandas-docs/stable/timeseries.html#timeseries-offset-aliases
    :return: a datetime with timezone
    N)r   r   )second)Z1H)r   minute)1D)r   r   hour)Z1Sz7::make_auto_time_generator: Exit, cannot handle freq=%sz
US/Eastern)freqtz)	replacer   r
   pd
date_rangepytztimezonelistto_pydatetime)startingTime
endingTimer   ZadjustedStartTimeZadjustedEndTimetmpctr   r   r   make_auto_time_generator   s&    

r#   c               c   s   xt tt V  qW dS )z,

    :return: a datetime with timezone
    N)r   inttimer   r   r   r   make_local_time_generator@   s    r&   c               @   s   e Zd Zdd Zdd ZdS )Iterc             C   s
   || _ d S )N)	generator)selfr(   r   r   r   __init__K   s    zIter.__init__c             C   s
   t | jS )N)nextr(   )r)   r   r   r   get_nextN   s    zIter.get_nextN)r
   
__module____qualname__r*   r,   r   r   r   r   r'   J   s   r'   c               @   s   e Zd Zdd Zdd ZdS )TimeGeneratorFactoryc             C   s
   d | _ d S )N)
_singleton)r)   r   r   r   r*   T   s    zTimeGeneratorFactory.__init__c             C   s   | j r| j S t|| _ | j S )N)r0   TimeGenerator)r)   timeGeneratorConfigr   r   r   get_timeGeneratorW   s    
z&TimeGeneratorFactory.get_timeGeneratorN)r
   r-   r.   r*   r3   r   r   r   r   r/   S   s   r/   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r1   c             C   s   |j tjkr,tt|j|j|j| _d| _	nB|j tj
krLtt | _d| _	n"|j tjkrntt|j| _d| _	d | _td| _d S )NTFr   )timeGeneratorTyper   AUTOr'   r#   r   r    r   iter_isBacktestLIVEr&   CUSTOMr   custom_timeNowr   	timedelta_diffBetweenLocalAndServer)r)   r2   r   r   r   r*   _   s    zTimeGenerator.__init__c             C   sL   | j s| j S | jdkr&| j | _t| jtjr@| j | _| j| j S )ze
        This should be used most of the time, except repeater.
        :return: dt.datetime
        N)	r7   r6   r,   r;   r   r   	Timestampr   r=   )r)   r   r   r   get_current_timeo   s    

zTimeGenerator.get_current_timec             C   s   | j  | _| j| j S )z}
        This should ONLY be used by repeater to advance time, either live or backtest.
        :return: dt.datetime
        )r6   r,   r;   r=   )r)   r   r   r   get_next_time   s    zTimeGenerator.get_next_timec             C   s   | j r
d S || j  | _d S )N)r7   r6   r,   r=   )r)   Zdt_serverTimer   r   r   set_diffBetweenLocalAndServer   s    z+TimeGenerator.set_diffBetweenLocalAndServerN)r
   r-   r.   r*   r?   r@   rA   r   r   r   r   r1   ^   s   	r1   )r   )r   r   r   BasicPyLib.Printabler   IBridgePy.constantsr   pandasr   sysr   r%   BasicPyLib.BasicToolsr   r   r#   r&   r'   objectr/   r1   r   r   r   r   <module>   s   
&
	