B
     9-eL                 @   s   d Z ddlmZ ddlmZ ddlT ddlT ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ eeZG d
d deZdS )z
Copyright (C) 2019 Interactive Brokers LLC. All rights reserved. This code is subject to the terms
 and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable.
    )order_condition)Object)*)OrderComboLeg)ComboLeg)TagValue)DeltaNeutralContract)SoftDollarTierc               @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1d_d` Z2dadb Z3dcdd Z4dedf Z5dgdh Z6didj Z7dkdl Z8dmdn Z9dodp Z:dqdr Z;dsdt Z<dudv Z=dwdx Z>dydz Z?d{d| Z@d}d~ ZAdd ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHeIfddZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdS )OrderDecoderc             C   s"   || _ || _|| _|| _|| _d S )N)contractorder
orderStateversionserverVersion)selfr   r   r   r   r    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\ibapi\orderdecoder.py__init__   s
    zOrderDecoder.__init__c             C   s   t t|| j_d S )N)decodeintr   orderId)r   fieldsr   r   r   decodeOrderId   s    zOrderDecoder.decodeOrderIdc             C   s   t t|| j_t t|| j_t t|| j_t t|| j_t t|| j_	t t|| j_
| jdkrlt t|| j_t t|| j_t t|| j_t t|| j_| jdkrt t|| j_d S )N    )r   r   r   conIdstrsymbolsecTypelastTradeDateOrContractMonthfloatstrikerightr   
multiplierexchangecurrencylocalSymboltradingClass)r   r   r   r   r   decodeContractFields   s    

z!OrderDecoder.decodeContractFieldsc             C   s   t t|| j_d S )N)r   r   r   action)r   r   r   r   r   decodeAction-   s    zOrderDecoder.decodeActionc             C   s   t t|| j_d S )N)r   Decimalr   totalQuantity)r   r   r   r   r   decodeTotalQuantity0   s    z OrderDecoder.decodeTotalQuantityc             C   s   t t|| j_d S )N)r   r   r   	orderType)r   r   r   r   r   decodeOrderType3   s    zOrderDecoder.decodeOrderTypec             C   s.   | j dk rtt|| j_ntt|t| j_d S )N   )r   r   r   r   lmtPrice
SHOW_UNSET)r   r   r   r   r   decodeLmtPrice6   s    
zOrderDecoder.decodeLmtPricec             C   s.   | j dk rtt|| j_ntt|t| j_d S )N   )r   r   r   r   auxPricer1   )r   r   r   r   r   decodeAuxPrice<   s    
zOrderDecoder.decodeAuxPricec             C   s   t t|| j_d S )N)r   r   r   tif)r   r   r   r   r   	decodeTIFB   s    zOrderDecoder.decodeTIFc             C   s   t t|| j_d S )N)r   r   r   ocaGroup)r   r   r   r   r   decodeOcaGroupE   s    zOrderDecoder.decodeOcaGroupc             C   s   t t|| j_d S )N)r   r   r   account)r   r   r   r   r   decodeAccountH   s    zOrderDecoder.decodeAccountc             C   s   t t|| j_d S )N)r   r   r   	openClose)r   r   r   r   r   decodeOpenCloseK   s    zOrderDecoder.decodeOpenClosec             C   s   t t|| j_d S )N)r   r   r   origin)r   r   r   r   r   decodeOriginN   s    zOrderDecoder.decodeOriginc             C   s   t t|| j_d S )N)r   r   r   orderRef)r   r   r   r   r   decodeOrderRefQ   s    zOrderDecoder.decodeOrderRefc             C   s   t t|| j_d S )N)r   r   r   clientId)r   r   r   r   r   decodeClientIdT   s    zOrderDecoder.decodeClientIdc             C   s   t t|| j_d S )N)r   r   r   permId)r   r   r   r   r   decodePermIdW   s    zOrderDecoder.decodePermIdc             C   s   t t|| j_d S )N)r   boolr   
outsideRth)r   r   r   r   r   decodeOutsideRthZ   s    zOrderDecoder.decodeOutsideRthc             C   s   t t|| j_d S )N)r   rF   r   hidden)r   r   r   r   r   decodeHidden]   s    zOrderDecoder.decodeHiddenc             C   s   t t|| j_d S )N)r   r   r   discretionaryAmt)r   r   r   r   r   decodeDiscretionaryAmt`   s    z#OrderDecoder.decodeDiscretionaryAmtc             C   s   t t|| j_d S )N)r   r   r   goodAfterTime)r   r   r   r   r   decodeGoodAfterTimec   s    z OrderDecoder.decodeGoodAfterTimec             C   s   t t|}d S )N)r   r   )r   r   Z_sharesAllocationr   r   r   skipSharesAllocationf   s    z!OrderDecoder.skipSharesAllocationc             C   s<   t t|| j_t t|| j_t t|| j_t t|| j_d S )N)r   r   r   faGroupfaMethodfaPercentage	faProfile)r   r   r   r   r   decodeFAParamsi   s    zOrderDecoder.decodeFAParamsc             C   s   | j tkrtt|| j_d S )N)r   MIN_SERVER_VER_MODELS_SUPPORTr   r   r   	modelCode)r   r   r   r   r   decodeModelCodeo   s    
zOrderDecoder.decodeModelCodec             C   s   t t|| j_d S )N)r   r   r   goodTillDate)r   r   r   r   r   decodeGoodTillDates   s    zOrderDecoder.decodeGoodTillDatec             C   s   t t|| j_d S )N)r   r   r   rule80A)r   r   r   r   r   decodeRule80Av   s    zOrderDecoder.decodeRule80Ac             C   s   t t|t| j_d S )N)r   r   r1   r   percentOffset)r   r   r   r   r   decodePercentOffsety   s    z OrderDecoder.decodePercentOffsetc             C   s   t t|| j_d S )N)r   r   r   settlingFirm)r   r   r   r   r   decodeSettlingFirm|   s    zOrderDecoder.decodeSettlingFirmc             C   sN   t t|| j_t t|| j_| jtkr2t t| n| jdkrJt t|| j_	d S )N   )
r   r   r   shortSaleSlotr   designatedLocationr   MIN_SERVER_VER_SSHORTX_OLDr   
exemptCode)r   r   r   r   r   decodeShortSaleParams   s    

z"OrderDecoder.decodeShortSaleParamsc             C   s   t t|| j_d S )N)r   r   r   auctionStrategy)r   r   r   r   r   decodeAuctionStrategy   s    z"OrderDecoder.decodeAuctionStrategyc             C   s4   t t|t| j_t t|t| j_t t|t| j_d S )N)r   r   r1   r   startingPricestockRefPricedelta)r   r   r   r   r   decodeBoxOrderParams   s    z!OrderDecoder.decodeBoxOrderParamsc             C   s$   t t|t| j_t t|t| j_d S )N)r   r   r1   r   stockRangeLowerstockRangeUpper)r   r   r   r   r   decodePegToStkOrVolOrderParams   s    z+OrderDecoder.decodePegToStkOrVolOrderParamsc             C   s   t t|t| j_d S )N)r   r   r1   r   displaySize)r   r   r   r   r   decodeDisplaySize   s    zOrderDecoder.decodeDisplaySizec             C   s   t t|| j_d S )N)r   rF   r   
blockOrder)r   r   r   r   r   decodeBlockOrder   s    zOrderDecoder.decodeBlockOrderc             C   s   t t|| j_d S )N)r   rF   r   sweepToFill)r   r   r   r   r   decodeSweepToFill   s    zOrderDecoder.decodeSweepToFillc             C   s   t t|| j_d S )N)r   rF   r   	allOrNone)r   r   r   r   r   decodeAllOrNone   s    zOrderDecoder.decodeAllOrNonec             C   s   t t|t| j_d S )N)r   r   r1   r   minQty)r   r   r   r   r   decodeMinQty   s    zOrderDecoder.decodeMinQtyc             C   s   t t|| j_d S )N)r   r   r   ocaType)r   r   r   r   r   decodeOcaType   s    zOrderDecoder.decodeOcaTypec             C   s   t t|}d S )N)r   rF   )r   r   Z_eTradeOnlyr   r   r   skipETradeOnly   s    zOrderDecoder.skipETradeOnlyc             C   s   t t|}d S )N)r   rF   )r   r   Z_firmQuoteOnlyr   r   r   skipFirmQuoteOnly   s    zOrderDecoder.skipFirmQuoteOnlyc             C   s   t t|t}d S )N)r   r   r1   )r   r   Z_nbboPriceCapr   r   r   skipNbboPriceCap   s    zOrderDecoder.skipNbboPriceCapc             C   s   t t|| j_d S )N)r   r   r   parentId)r   r   r   r   r   decodeParentId   s    zOrderDecoder.decodeParentIdc             C   s   t t|| j_d S )N)r   r   r   triggerMethod)r   r   r   r   r   decodeTriggerMethod   s    z OrderDecoder.decodeTriggerMethodc             C   s   t t|t| j_t t|| j_t t|| j_t t|t| j_	| j
dkr| jjrt t|| j_|rt t|| j_t t|| j_t t|| j_| j
dkr| jjr|rt t|| j_t t|| j_t t|| j_t t|| j_t t|| j_t t|| j_d S )N      )r   r   r1   r   
volatilityr   volatilityTyper   deltaNeutralOrderTypedeltaNeutralAuxPricer   deltaNeutralConIddeltaNeutralSettlingFirmdeltaNeutralClearingAccountdeltaNeutralClearingIntentdeltaNeutralOpenCloserF   deltaNeutralShortSaledeltaNeutralShortSaleSlotdeltaNeutralDesignatedLocationcontinuousUpdatereferencePriceType)r   r   ZreadOpenOrderAttribsr   r   r   decodeVolOrderParams   s$    z!OrderDecoder.decodeVolOrderParamsc             C   s.   t t|t| j_| jdkr*t t|t| j_d S )Nr3   )r   r   r1   r   trailStopPricer   trailingPercent)r   r   r   r   r   decodeTrailParams   s    
zOrderDecoder.decodeTrailParamsc             C   s$   t t|t| j_t t|t| j_d S )N)r   r   r1   r   basisPointsr   basisPointsType)r   r   r   r   r   decodeBasisPoints   s    zOrderDecoder.decodeBasisPointsc             C   s  t t|| j_| jdkr
t t|}|dkrg | j_xt|D ]x}t }t t||_	t t||_
t t||_t t||_t t||_t t||_t t||_t t||_| jj| q>W t t|}|dkr
g | j_x2t|D ]&}t }t t|t|_| jj| qW d S )Nr/   r   )r   r   r   comboLegsDescripr   r   	comboLegsranger   r   ratior(   r#   r<   ra   rb   rd   appendr   orderComboLegsr   r   r1   price)r   r   comboLegsCount_comboLegorderComboLegsCountorderComboLegr   r   r   decodeComboLegs   s.    


zOrderDecoder.decodeComboLegsc             C   sf   | j dkrbtt|}|dkrbg | j_x<t|D ]0}t }tt||_tt||_	| jj
| q.W d S )N   r   )r   r   r   r   smartComboRoutingParamsr   r   r   tagvaluer   )r   r   smartComboRoutingParamsCountr   tagValuer   r   r   decodeSmartComboRoutingParams   s    

z*OrderDecoder.decodeSmartComboRoutingParamsc             C   s   | j dkr,tt|t| j_tt|t| j_n tt|t| j_tt|t| j_tt|t| j_	| j dkr| jj	t
kr| jj	dkrtt|t| j_tt|t| j_tt|t| j_tt|| j_tt|t| j_tt|t| j_tt|| j_d S )N      g        )r   r   r   r1   r   scaleInitLevelSizescaleSubsLevelSizeZnotSuppScaleNumComponentsr   scalePriceIncrementUNSET_DOUBLEscalePriceAdjustValuescalePriceAdjustIntervalscaleProfitOffsetrF   scaleAutoResetscaleInitPositionscaleInitFillQtyscaleRandomPercent)r   r   r   r   r   decodeScaleOrderParams   s    
z#OrderDecoder.decodeScaleOrderParamsc             C   s2   | j dkr.tt|| j_| jjr.tt|| j_d S )N   )r   r   r   r   	hedgeType
hedgeParam)r   r   r   r   r   decodeHedgeParams  s    
zOrderDecoder.decodeHedgeParamsc             C   s   | j dkrtt|| j_d S )N   )r   r   rF   r   optOutSmartRouting)r   r   r   r   r   decodeOptOutSmartRouting  s    
z%OrderDecoder.decodeOptOutSmartRoutingc             C   s    t t|| j_t t|| j_d S )N)r   r   r   clearingAccountclearingIntent)r   r   r   r   r   decodeClearingParams  s    z!OrderDecoder.decodeClearingParamsc             C   s   | j dkrtt|| j_d S )N   )r   r   rF   r   notHeld)r   r   r   r   r   decodeNotHeld   s    
zOrderDecoder.decodeNotHeldc             C   sV   | j dkrRtt|}|rRt | j_tt|| jj_tt|| jj_	tt|| jj_
d S )Nr   )r   r   rF   r   r   deltaNeutralContractr   r   r   rj   r   )r   r   ZdeltaNeutralContractPresentr   r   r   decodeDeltaNeutral$  s    


zOrderDecoder.decodeDeltaNeutralc             C   s|   | j dkrxtt|| j_| jjrxtt|}|dkrxg | j_x<t|D ]0}t }tt||_	tt||_
| jj| qDW d S )N   r   )r   r   r   r   algoStrategyr   
algoParamsr   r   r   r   r   )r   r   algoParamsCountr   r   r   r   r   decodeAlgoParams-  s    

zOrderDecoder.decodeAlgoParamsc             C   s   | j dkrtt|| j_d S )N!   )r   r   rF   r   	solicited)r   r   r   r   r   decodeSolicited:  s    
zOrderDecoder.decodeSolicitedc             C   s   t t|| j_d S )N)r   r   r   status)r   r   r   r   r   decodeOrderStatus>  s    zOrderDecoder.decodeOrderStatusc             C   s   t t|| j_t| | | jtkrxt t|| j	_
t t|| j	_t t|| j	_t t|| j	_t t|| j	_t t|| j	_t t|| j	_t t|| j	_t t|| j	_t t|t| j	_t t|t| j	_t t|t| j	_t t|| j	_t t|| j	_d S )N)r   rF   r   whatIfr
   r   r   !MIN_SERVER_VER_WHAT_IF_EXT_FIELDSr   r   initMarginBeforemaintMarginBeforeequityWithLoanBeforeinitMarginChangemaintMarginChangeequityWithLoanChangeinitMarginAftermaintMarginAfterequityWithLoanAfterr   r1   
commissionminCommissionmaxCommissioncommissionCurrencywarningText)r   r   r   r   r   decodeWhatIfInfoAndCommissionA  s"    
z*OrderDecoder.decodeWhatIfInfoAndCommissionc             C   s*   | j dkr&tt|| j_tt|| j_d S )N"   )r   r   rF   r   randomizeSizerandomizePrice)r   r   r   r   r   decodeVolRandomizeFlagsV  s    
z$OrderDecoder.decodeVolRandomizeFlagsc             C   s`   | j tkr\| jjdkr\tt|| j_tt|| j_tt	|| j_
tt	|| j_tt|| j_d S )Nz	PEG BENCH)r   "MIN_SERVER_VER_PEGGED_TO_BENCHMARKr   r-   r   r   referenceContractIdrF   isPeggedChangeAmountDecreaser   peggedChangeAmountreferenceChangeAmountr   referenceExchangeId)r   r   r   r   r   decodePegToBenchParams[  s    
z#OrderDecoder.decodePegToBenchParamsc             C   s   | j tkr~tt|}|dkr~g | j_x<t|D ]0}tt|}t|}|| | jj	| q.W tt
|| j_tt
|| j_d S )Nr   )r   r   r   r   r   
conditionsr   r   Creater   rF   conditionsIgnoreRthconditionsCancelOrder)r   r   ZconditionsSizer   ZconditionType	conditionr   r   r   decodeConditionsd  s    




zOrderDecoder.decodeConditionsc             C   sn   | j tkrjtt|| j_tt|| j_t	| | tt|| j_
tt|| j_tt|| j_tt|| j_d S )N)r   r   r   r   r   adjustedOrderTyper   triggerPricer
    decodeStopPriceAndLmtPriceOffsetadjustedStopPriceadjustedStopLimitPriceadjustedTrailingAmountr   adjustableTrailingUnit)r   r   r   r   r   decodeAdjustedOrderParamss  s    
z&OrderDecoder.decodeAdjustedOrderParamsc             C   s    t t|| j_t t|| j_d S )N)r   r   r   r   lmtPriceOffset)r   r   r   r   r   r   }  s    z-OrderDecoder.decodeStopPriceAndLmtPriceOffsetc             C   s<   | j tkr8tt|}tt|}tt|}t|||| j_d S )N)r   MIN_SERVER_VER_SOFT_DOLLAR_TIERr   r   r	   r   softDollarTier)r   r   namer   displayNamer   r   r   decodeSoftDollarTier  s
    



z!OrderDecoder.decodeSoftDollarTierc             C   s   | j tkrtt|| j_d S )N)r   MIN_SERVER_VER_CASH_QTYr   r   r   cashQty)r   r   r   r   r   decodeCashQty  s    
zOrderDecoder.decodeCashQtyc             C   s   | j tkrtt|| j_d S )N)r   #MIN_SERVER_VER_AUTO_PRICE_FOR_HEDGEr   rF   r   dontUseAutoPriceForHedge)r   r   r   r   r   decodeDontUseAutoPriceForHedge  s    
z+OrderDecoder.decodeDontUseAutoPriceForHedgec             C   s   | j tkrtt|| j_d S )N)r   MIN_SERVER_VER_ORDER_CONTAINERr   rF   r   isOmsContainer)r   r   r   r   r   decodeIsOmsContainers  s    
z"OrderDecoder.decodeIsOmsContainersc             C   s   | j tkrtt|| j_d S )N)r   MIN_SERVER_VER_D_PEG_ORDERSr   rF   r   discretionaryUpToLimitPrice)r   r   r   r   r   !decodeDiscretionaryUpToLimitPrice  s    
z.OrderDecoder.decodeDiscretionaryUpToLimitPricec             C   s   t t|| j_d S )N)r   r   r   autoCancelDate)r   r   r   r   r   decodeAutoCancelDate  s    z!OrderDecoder.decodeAutoCancelDatec             C   s   t t|| j_d S )N)r   r*   r   filledQuantity)r   r   r   r   r   decodeFilledQuantity  s    z!OrderDecoder.decodeFilledQuantityc             C   s   t t|| j_d S )N)r   r   r   refFuturesConId)r   r   r   r   r   decodeRefFuturesConId  s    z"OrderDecoder.decodeRefFuturesConIdc             C   s   | j |krtt|| j_d S )N)r   r   rF   r   autoCancelParent)r   r   ZminVersionAutoCancelParentr   r   r   decodeAutoCancelParent  s    
z#OrderDecoder.decodeAutoCancelParentc             C   s   t t|| j_d S )N)r   r   r   shareholder)r   r   r   r   r   decodeShareholder  s    zOrderDecoder.decodeShareholderc             C   s   t t|| j_d S )N)r   rF   r   imbalanceOnly)r   r   r   r   r   decodeImbalanceOnly  s    z OrderDecoder.decodeImbalanceOnlyc             C   s   t t|| j_d S )N)r   rF   r   routeMarketableToBbo)r   r   r   r   r   decodeRouteMarketableToBbo  s    z'OrderDecoder.decodeRouteMarketableToBboc             C   s   t t|| j_d S )N)r   r   r   parentPermId)r   r   r   r   r   decodeParentPermId  s    zOrderDecoder.decodeParentPermIdc             C   s   t t|| j_d S )N)r   r   r   completedTime)r   r   r   r   r   decodeCompletedTime  s    z OrderDecoder.decodeCompletedTimec             C   s   t t|| j_d S )N)r   r   r   completedStatus)r   r   r   r   r   decodeCompletedStatus  s    z"OrderDecoder.decodeCompletedStatusc             C   s   | j tkrtt|| j_d S )N)r   MIN_SERVER_VER_PRICE_MGMT_ALGOr   rF   r   usePriceMgmtAlgo)r   r   r   r   r   decodeUsePriceMgmtAlgo  s    
z#OrderDecoder.decodeUsePriceMgmtAlgoc             C   s   | j tkrtt|t| j_d S )N)r   MIN_SERVER_VER_DURATIONr   r   r1   r   duration)r   r   r   r   r   decodeDuration  s    
zOrderDecoder.decodeDurationc             C   s   | j tkrtt|t| j_d S )N)r   MIN_SERVER_VER_POST_TO_ATSr   r   r1   r   	postToAts)r   r   r   r   r   decodePostToAts  s    
zOrderDecoder.decodePostToAtsc             C   s^   | j tkrZtt|t| j_tt|t| j_tt|t| j_	tt|t| j_
tt|t| j_d S )N)r   %MIN_SERVER_VER_PEGBEST_PEGMID_OFFSETSr   r   r1   r   minTradeQtyminCompeteSizer   competeAgainstBestOffsetmidOffsetAtWholemidOffsetAtHalf)r   r   r   r   r   "decodePegBestPegMidOrderAttributes  s    
z/OrderDecoder.decodePegBestPegMidOrderAttributesN)U__name__
__module____qualname__r   r   r'   r)   r,   r.   r2   r5   r7   r9   r;   r=   r?   rA   rC   rE   rH   rJ   rL   rN   rO   rT   rW   rY   r[   r]   r_   re   rg   rk   rn   rp   rr   rt   rv   rx   rz   r{   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r	  r  r  r  r  r  MIN_CLIENT_VERr  r  r  r  r  r!  r#  r&  r)  r,  r3  r   r   r   r   r
      s   		
r
   N)__doc__ibapir   ibapi.object_implemr   ibapi.utilsibapi.server_versionsibapi.orderr   ibapi.contractr   ibapi.tag_valuer   ibapi.wrapperr   ibapi.softdollartierr	   logging	getLoggerr4  loggerr
   r   r   r   r   <module>   s   
