a
     9-eH                     @   s   d dl Z d dlZd dlmZ d dlZd dlZd dlZd dlZd dl	Z	G dd de
ZG dd de
Zddd	Zdd
dZdd ZdddZdd Zdd Zdd Zdd Zdd ZdS )    N)exitc                   @   s   e Zd Zdd ZdddZdS )	CONSTANTSc                 C   s   d S N selfr   r   G:\My Drive\STUDY\EPAT\09 TBP - Trading & Back-testing Platforms\TBP04 - Backtesting & Live Trading\IB IBridgePy API\IBridgePy_Win_Anaconda37_64\BasicPyLib\BasicTools.py__init__   s    zCONSTANTS.__init__Nc                 C   sJ   t | |rt| |S |r.ttd|| |f  nttd|| f  t  d S )Nz5::CONSTANTS: key=%s does not exist from %s. caller=%sz9::CONSTANTS: key=%s does not exist from %s. caller=None. )hasattrgetattrprint__name__r   )r   keycallerr   r   r   get   s    

zCONSTANTS.get)N)r   
__module____qualname__r	   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd Zdd ZdS )Timerc                 C   s   t j | _d S r   )dtdatetimenow	startTimer   r   r   r   r	      s    zTimer.__init__c                 C   s   t j | j  S r   )r   r   r   r   total_secondsr   r   r   r   elapsedInSecond   s    zTimer.elapsedInSecondN)r   r   r   r	   r   r   r   r   r   r      s   r   {Gz?c                 C   s&   | dk rt d|   t| | | S )z
    for US interactive Brokers, the minimum price change in US stocks is
    $0.01. So if the singleTrader made calculations on any price, the calculated
    price must be round using this function to the minTick, e.g., $0.01
    g        zEXIT, negative price=)r   int)ZpriceZminTickr   r   r   roundToMinTick#   s    r   c                 C   sT   | j du r&|r|| } ntj| } t| tjtjtddddd  S )z
    dt.datetime.fromtimestamp
    the return value depends on local machine timezone!!!!
    So, dt.datetime.fromtimestamp(0) will create different time at different machine
    So, this implementation does not use dt.datetime.fromtimestamp
    Ni     r   )	tzinfolocalizepytzutcr   
astimezoner   r   r   )Za_dtZshowTimeZoner   r   r   dt_to_epoch.   s
    
r#   c                 C   s   t t| S r   )timestamp_to_epochpd	Timestamp)Za_dater   r   r   date_to_epoch>   s    r'   UTCc                 C   s   t |tjt| S r   )r    timezoner   r   r   utcfromtimestampr   )ZutcInSecondsZstr_timezoner   r   r   epoch_to_dtB   s    r+   c                 C   sV   t | tju rt| S | jr:| tj} tj	dtjd}n
t	d}| | t
d S )Nr   )r   Z1s)typer   r   r#   r   r"   r    r!   r%   r&   	Timedelta)Z
aTimestampar   r   r   r$   H   s    
r$   c                 C   sB   d }t | tjr|  }n$t | tjr,| }nttd| f   |S )Nz0::isTradingDay: EXIT, cannot handle aDatetime=%s)
isinstancer   r   dater   r   )Z	aDatetimeZaDater   r   r   convert_datetime_to_dateT   s    
r1   c                 C   s$   | D ]}|  r| s dS qdS )NFT)isalphaisupper)wordletterr   r   r   isAllLettersCapital_   s
    r6   c              
   C   s   t  }t j| ||tdd}|D ]}| dkr$tdd}|tdd }t||tdd }	t||tdd }
t jt	||t	|	|t	|
|t	||t
d	d
dtt|gd}t ||g}q$|S )a  

    :param startTime: dt.datetime
    :param endTime: dt.datetime
    :param barSize: 1S = 1 second; 1T = 1 minute; 1H = 1 hour
    :param miniTick: float, 0.01, 0.05, 0.1, etc.
    :return: pd.DataFrame('open', 'high', 'low', 'close', 'volume'), index = datetime
    r(   )freqtz   2   d   gffffff?g?r   i'  iP  )openhighlowclosevolume)index)r%   	DataFrame
date_ranger    r)   weekdayrandomuniformmaxr   randintr   r#   concat)r   ZendTimeZbarSizeZminiTickansrA   dateTimeZ	openPriceZ
closePriceZ	highPriceZlowPriceZnewRowr   r   r   create_random_histg   s$    

rL   c                  C   s   t  } | dkrd} dtjv s(dtjv r.d}nRdtjtjv svdtjtjv svdtjtjv svdtjtjv r|d}nd}tt	tj
jt	tj
j }| ||fS )NDarwinZMacZAnacondaZanacondaZ	MinicondaZ	minicondaPython)platformsystemsysversionospathdirname
executabler   strversion_infomajorminor)pfZ
pythonNameZversionNamer   r   r   get_system_info   s    $r\   )r   )N)r(   )rO   rE   rQ   r   pandasr%   r    r   r   rS   objectr   r   r   r#   r'   r+   r$   r1   r6   rL   r\   r   r   r   r   <module>   s$   


$