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|ƒ}xÌy|j|d� P W qt tk
rú } zV|jdddd	tjjgkrè|rèt	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|�rŒt6j7 8| j9j:d|¡}6t;|6ƒ�}7|7 <¡ }8W dQ R X t=|8|6dƒ}9t>|9ƒ t?ƒ  @tAƒ ¡ |�r¬tAƒ  @|¡ |  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