B
     9-e/                 @   s2  d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
mZ d dlmZmZmZ d dlmZmZmZmZmZ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!m"Z" d d
l#m$Z$ d dl#m%Z% e  dddZ&dd Z'd ddZ(d!ddZ)dd Z*d"ddZ+dd Z,d#ddZ-d$ddZ.dS )%    N)roundToMinTickget_system_info)
UserConfig)superSymbolcheck_user_sys_compatibility)MarketManagersetup_services setup_backtest_account_init_info)MarketOrder	StopOrder
LimitOrderStopLimitOrderTrailStopLimitOrderTrailStopOrderMarketOnCloseOrder)Trader)LiveBacktestDataProviderNameLogLevelTraderRunMode)
date_rules
time_rules	calendars)CustomError)	ErrorCodec             C   s*   |st d}t||| }t|tj d S )NBACKTEST)r   
get_config_build_config_finally_runr   r   )fileNameuserManualInput
userConfig 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\configuration.pytest_me   s    
r$   c             C   sL   t | ||} | jj| jjkr<td| jj| jjf  t| } t| tj d S )NzL!!! Data provider is %s but the broker service is %s. They are NOT same. !!!)	r   projectConfigbrokerServiceNamedataProviderNameprint_setup_dataProviderServicer   r   LIVE)r!   r    r   r"   r"   r#   _helper"   s
    r+   c             C   s   |st d}t|||  d S )N	ROBINHOOD)r   r   r+   )r   r    r!   r"   r"   r#   run_me_at_robinhood*   s    
r-   c             C   s   |st d}t|||  d S )NTD)r   r   r+   )r   r    r!   r"   r"   r#   run_me_at_td_ameritrade0   s    
r/   c             C   s$   t d}tj|j_t|||  d S )NHFT)r   r   r   r0   ZtraderConfigZrunModer+   )r   r    r!   r"   r"   r#   run_me_at_HFT6   s    

r1   c          
   C   sL  t t d d}|d kr&td}t||| }|jj|jjkrbt	d|jj|jjf  t
|}|jj}t|}xy|j|d P W qt tk
r } zV|jdddd	tjjgkr|rt	d
 d}td |j  |jj|dd|_nP W d d }~X Y qt ttfk
r: } zt	d| dt   P W d d }~X Y qtX qtW |  d S )Nz::run_meTIBzL!!! Data provider is %s but the broker service is %s. They are NOT same. !!!)run_initi  i  i  i'  z4IBridgePy will sleep 10 seconds and reconnect again.F
   )check_cachez^If you need help, please send email to IBridgePy@gmail.com. Terminate IBridgePy because error= )loggingdebug__name__r   r   r   r%   r&   r'   r(   r)   autoReconnectPremiumr   runr   
error_coder   ZERR_LOST_HEART_BEATcodetimesleepZbrokerClientZdisconnectWrapperbrokerClientFactoryZget_brokerClient_by_userConfigRuntimeError	Exception	traceback	print_exc	terminate)r   r    r!   r3   r:   ceerrr"   r"   r#   run_me<   s4    
	

rI   c             C   s   | j j}|tjkrtd}nV|tjkr4td}n@|tjkrJtd}n*|tjtj	gkrftd}nt
d|f tj|j _||j _| j|_| j|_t|d }|j  |j   |j| j_|j| _| S )Nr.   r2   r,   ZLOCALz:cannot handle userConfig.projectConfig.dataProviderName=%s)r%   r'   r   r.   r   r   r2   r,   ZRANDOMZ
LOCAL_FILErA   r   ERRORZlogLevelr@   ZtimeGeneratorFactoryr   ZdataProviderServiceZsetAsDataProviderServiceZgetBrokerClientZsetAsDataProviderClienttrader)r!   r'   ZuserConfigDataProviderr"   r"   r#   r)   c   s(    






r)   c       :   	   C   s  t t d t }|j}|j}|j}|j}|j}|j	}	|j
}
|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j}|j } |j!}!|j"}"|j#}#|j$}$|j%}%|j&}&|j'}'|j(}(|j)})|j*}*|j+}+|j,},|j-}-|j.}.|j/}/|j0}0|j1}1|j2}2|j3}3|j4}4|j5}5|rt6j78| j9j:d|}6t;|6}7|7< }8W dQ R X t=|8|6d}9t>|9 t? @tA  |rtA @| | B|tA  | S )a  

    :param userConfig: Config::config_defs::UserConfig
    :param userManualInput: a dictionary, key=variable name, value= variable value, Should be provided by global() or local()
    :param fileName: the current strategy file name
    :return: modified userConfig
    z::_build_configZ
StrategiesNexec)Cr7   r8   r9   r   cancel_all_orderscancel_orderclose_all_positionsclose_all_positions_exceptcount_positionscreate_orderdisplay_alldisplay_orderStatusdisplay_positionsZsetWantToEndget_5_second_real_time_barget_datetimeget_all_open_ordersget_all_ordersget_all_positionsget_contract_detailsget_ibpy_expiry_in_daysget_open_ordersget_option_info	get_orderget_order_statusget_positionget_scanner_results"get_TD_access_token_expiry_in_dayshold_any_positionisEarlyCloseisTradingDaymodify_orderorderorder_status_monitororder_targetorder_target_percentorder_target_valueorder_percentorder_valueplace_combination_ordersplace_order_with_stoploss$place_order_with_stoploss_takeprofitplace_order_with_takeprofitrequest_historical_datarebalance_portfoliorecordschedule_function
send_emailshow_account_infoshow_real_time_priceshow_real_time_sizeshow_timestampsymbolsymbolsospathjoinr%   ZrootFolderPathopenreadcompilerL   globalsupdatelocalsZprepare_userConfig_with_trader):r!   r    r   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   endrV   rW   rX   rY   rZ   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   rv   rw   rx   ry   rz   r{   r|   r}   ZfullFileNamefscript
code_blockr"   r"   r#   r   |   s~    

r   Tc             C   sh   |t jkr(t| }|j|d |  n<|t jkrdt| }t|  || jj	 |j|d |  d S )N)r3   )
r   r*   r   r;   rE   r   r	   Zingest_historical_datar%   ZhistIngestionPlan)r!   Z
liveOrTestr3   rF   r"   r"   r#   r      s    


r   )N)N)N)N)NN)T)/r~   r>   rC   r7   ZBasicPyLib.BasicToolsr   r   ZConfig.config_defsr   ZIBridgePy.IbridgepyToolsr   r   ZIBridgePy.MarketManagerBaser   r   r	   ZIBridgePy.OrderTypesr
   r   r   r   r   r   r   ZIBridgePy.Traderr   ZIBridgePy.constantsr   r   r   r   ZIBridgePy.quantopianr   r   r   Z"broker_client_factory.CustomErrorsr   r   r$   r+   r-   r/   r1   rI   r)   r   r   r"   r"   r"   r#   <module>   s.   $



'
e