a
     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\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 )NúL!!! 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$   Úrun_me_at_robinhood*   s    
r0   c                 C   s   |st  d¡}t||| ƒ d S )NÚTDr/   r   r#   r#   r$   Úrun_me_at_td_ameritrade0   s    
r2   c                 C   s$   t  d¡}tj|j_t||| ƒ d S )NÚHFT)r   r   r   r3   ZtraderConfigZrunModer-   r   r#   r#   r$   Úrun_me_at_HFT6   s    

r4   c              
   C   sj  t  t› d�¡ d}|d u r&t d¡}t||| ƒ}|jj|jjkrbt	d|jj|jjf ƒ t
|ƒ}|jj}t|ƒ}z|j|d� W �q^W qr t�y } zj|jdddd	tjjfv rê|rêt	d
ƒ d}t d¡ |j ¡  |jj|dd�|_nW Y d }~�q^W Y d }~qrd }~0  ttf�yZ } z2t	d|› dt ¡ › �ƒ W Y d }~�q^W Y d }~qrd }~0 0 qr| ¡  d S )Nz::run_meTÚIBr&   ©Ú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"   r7   r>   ÚcÚeÚerrr#   r#   r$   Úrun_me<   s2    

	

"&rM   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	fv rft d¡}nt
d|f ƒ‚tj|j _||j _| j|_| j|_t|d ƒ}|j ¡  |j ¡  ¡  |j| j_|j| _| S )Nr1   r5   r.   ZLOCALz:cannot handle userConfig.projectConfig.dataProviderName=%s)r'   r)   r   r1   r   r   r5   r.   ZRANDOMZ
LOCAL_FILErE   r   ÚERRORZlogLevelrD   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  ƒ n1 �s„0    Y  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)Cr;   r<   r=   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ÚcompilerP   ÚglobalsÚupdateÚlocalsZprepare_userConfig_with_trader):r"   r!   r    rO   rQ   rR   rS   rT   rU   rV   rW   rX   rY   ÚendrZ   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}   r~   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 )Nr6   )
r   r,   r   r?   rI   r   r	   Zingest_historical_datar'   ZhistIngestionPlan)r"   Z
liveOrTestr7   rJ   r#   r#   r$   r   á   s    


r   )N)N)N)N)NN)T)/r‚   rB   rG   r;   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-   r0   r2   r4   rM   r+   r   r   r#   r#   r#   r$   Ú<module>   s.   $



'
e