<s> """<STR_LIT>""" <EOL> import abc <EOL> from pyalgotrade import observer <EOL> from pyalgotrade import warninghelpers <EOL> from pyalgotrade import dispatchprio <EOL> class InstrumentTraits ( object ) : <EOL> __metaclass__ = abc . ABCMeta <EOL> @ abc . abstractmethod <EOL> def roundQuantity ( self , quantity ) : <EOL> raise NotImplementedError ( ) <EOL> class IntegerTraits ( InstrumentTraits ) : <EOL> def roundQuantity ( self , quantity ) : <EOL> return int ( quantity ) <EOL> class Order ( object ) : <EOL> """<STR_LIT>""" <EOL> class Action ( object ) : <EOL> BUY = <NUM_LIT:1> <EOL> BUY_TO_COVER = <NUM_LIT:2> <EOL> SELL = <NUM_LIT:3> <EOL> SELL_SHORT = <NUM_LIT:4> <EOL> class State ( object ) : <EOL> INITIAL = <NUM_LIT:1> <EOL> SUBMITTED = <NUM_LIT:2> <EOL> ACCEPTED = <NUM_LIT:3> <EOL> CANCELED = <NUM_LIT:4> <EOL> PARTIALLY_FILLED = <NUM_LIT:5> <EOL> FILLED = <NUM_LIT:6> <EOL> @ classmethod <EOL> def toString ( cls , state ) : <EOL> if state == cls . INITIAL : <EOL> return "<STR_LIT>" <EOL> elif state == cls . SUBMITTED : <EOL> return "<STR_LIT>" <EOL> elif state == cls . ACCEPTED : <EOL> return "<STR_LIT>" <EOL> elif state == cls . CANCELED : <EOL> return "<STR_LIT>" <EOL> elif state == cls . PARTIALLY_FILLED : <EOL> return "<STR_LIT>" <EOL> elif state == cls . FILLED : <EOL> return "<STR_LIT>" <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> class Type ( object ) : <EOL> MARKET = <NUM_LIT:1> <EOL> LIMIT = <NUM_LIT:2> <EOL> STOP = <NUM_LIT:3> <EOL> STOP_LIMIT = <NUM_LIT:4> <EOL> VALID_TRANSITIONS = { <EOL> State . INITIAL : [ State . SUBMITTED , State . CANCELED ] , <EOL> State . SUBMITTED : [ State . ACCEPTED , State . CANCELED ] , <EOL> State . ACCEPTED : [ State . PARTIALLY_FILLED , State . FILLED , State . CANCELED ] , <EOL> State . PARTIALLY_FILLED : [ State . PARTIALLY_FILLED , State . FILLED , State . CANCELED ] , <EOL> } <EOL> def __init__ ( self , type_ , action , instrument , quantity , instrumentTraits ) : <EOL> if quantity <= <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . __id = None <EOL> self . __type = type_ <EOL> self . __action = action <EOL> self . __instrument = instrument <EOL> self . __quantity = quantity <EOL> self . __instrumentTraits = instrumentTraits <EOL> self . __filled = <NUM_LIT:0> <EOL> self . __avgFillPrice = None <EOL> self . __executionInfo = None <EOL> self . __goodTillCanceled = False <EOL> self . __commissions = <NUM_LIT:0> <EOL> self . __allOrNone = False <EOL> self . __state = Order . State . INITIAL <EOL> self . __submitDateTime = None <EOL> def getInstrumentTraits ( self ) : <EOL> return self . __instrumentTraits <EOL> def getId ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __id <EOL> def getType ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __type <EOL> def getSubmitDateTime ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __submitDateTime <EOL> def setSubmitted ( self , orderId , dateTime ) : <EOL> assert ( self . __id is None or orderId == self . __id ) <EOL> self . __id = orderId <EOL> self . __submitDateTime = dateTime <EOL> def getAction ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __action <EOL> def getState ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __state <EOL> def isActive ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __state not in [ Order . State . CANCELED , Order . State . FILLED ] <EOL> def isInitial ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __state == Order . State . INITIAL <EOL> def isSubmitted ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __state == Order . State . SUBMITTED <EOL> def isAccepted ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __state == Order . State . ACCEPTED <EOL> def isCanceled ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __state == Order . State . CANCELED <EOL> def isPartiallyFilled ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __state == Order . State . PARTIALLY_FILLED <EOL> def isFilled ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __state == Order . State . FILLED <EOL> def getInstrument ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __instrument <EOL> def getQuantity ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __quantity <EOL> def getFilled ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __filled <EOL> def getRemaining ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __instrumentTraits . roundQuantity ( self . __quantity - self . __filled ) <EOL> def getAvgFillPrice ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __avgFillPrice <EOL> def getCommissions ( self ) : <EOL> return self . __commissions <EOL> def getGoodTillCanceled ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __goodTillCanceled <EOL> def setGoodTillCanceled ( self , goodTillCanceled ) : <EOL> """<STR_LIT>""" <EOL> if self . __state != Order . State . INITIAL : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . __goodTillCanceled = goodTillCanceled <EOL> def getAllOrNone ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __allOrNone <EOL> def setAllOrNone ( self , allOrNone ) : <EOL> """<STR_LIT>""" <EOL> if self . __state != Order . State . INITIAL : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . __allOrNone = allOrNone <EOL> def addExecutionInfo ( self , orderExecutionInfo ) : <EOL> if orderExecutionInfo . getQuantity ( ) > self . getRemaining ( ) : <EOL> raise Exception ( "<STR_LIT>" % ( self . getRemaining ( ) , orderExecutionInfo . getQuantity ( ) ) ) <EOL> if self . __avgFillPrice is None : <EOL> self . __avgFillPrice = orderExecutionInfo . getPrice ( ) <EOL> else : <EOL> self . __avgFillPrice = ( self . __avgFillPrice * self . __filled + orderExecutionInfo . getPrice ( ) * orderExecutionInfo . getQuantity ( ) ) / float ( self . __filled + orderExecutionInfo . getQuantity ( ) ) <EOL> self . __executionInfo = orderExecutionInfo <EOL> self . __filled = self . getInstrumentTraits ( ) . roundQuantity ( self . __filled + orderExecutionInfo . getQuantity ( ) ) <EOL> self . __commissions += orderExecutionInfo . getCommission ( ) <EOL> if self . getRemaining ( ) == <NUM_LIT:0> : <EOL> self . switchState ( Order . State . FILLED ) <EOL> else : <EOL> assert ( not self . __allOrNone ) <EOL> self . switchState ( Order . State . PARTIALLY_FILLED ) <EOL> def switchState ( self , newState ) : <EOL> validTransitions = Order . VALID_TRANSITIONS . get ( self . __state , [ ] ) <EOL> if newState not in validTransitions : <EOL> raise Exception ( "<STR_LIT>" % ( Order . State . toString ( self . __state ) , Order . State . toString ( newState ) ) ) <EOL> else : <EOL> self . __state = newState <EOL> def setState ( self , newState ) : <EOL> self . __state = newState <EOL> def getExecutionInfo ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __executionInfo <EOL> def isBuy ( self ) : <EOL> return self . __action in [ Order . Action . BUY , Order . Action . BUY_TO_COVER ] <EOL> def isSell ( self ) : <EOL> return self . __action in [ Order . Action . SELL , Order . Action . SELL_SHORT ] <EOL> class MarketOrder ( Order ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , action , instrument , quantity , onClose , instrumentTraits ) : <EOL> super ( MarketOrder , self ) . __init__ ( Order . Type . MARKET , action , instrument , quantity , instrumentTraits ) <EOL> self . __onClose = onClose <EOL> def getFillOnClose ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __onClose <EOL> class LimitOrder ( Order ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , action , instrument , limitPrice , quantity , instrumentTraits ) : <EOL> super ( LimitOrder , self ) . __init__ ( Order . Type . LIMIT , action , instrument , quantity , instrumentTraits ) <EOL> self . __limitPrice = limitPrice <EOL> def getLimitPrice ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __limitPrice <EOL> class StopOrder ( Order ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , action , instrument , stopPrice , quantity , instrumentTraits ) : <EOL> super ( StopOrder , self ) . __init__ ( Order . Type . STOP , action , instrument , quantity , instrumentTraits ) <EOL> self . __stopPrice = stopPrice <EOL> def getStopPrice ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __stopPrice <EOL> class StopLimitOrder ( Order ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , action , instrument , stopPrice , limitPrice , quantity , instrumentTraits ) : <EOL> super ( StopLimitOrder , self ) . __init__ ( Order . Type . STOP_LIMIT , action , instrument , quantity , instrumentTraits ) <EOL> self . __stopPrice = stopPrice <EOL> self . __limitPrice = limitPrice <EOL> def getStopPrice ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __stopPrice <EOL> def getLimitPrice ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __limitPrice <EOL> class OrderExecutionInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , price , quantity , commission , dateTime ) : <EOL> self . __price = price <EOL> self . __quantity = quantity <EOL> self . __commission = commission <EOL> self . __dateTime = dateTime <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __dateTime , self . __price , self . __quantity , self . __commission ) <EOL> def getPrice ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __price <EOL> def getQuantity ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __quantity <EOL> def getCommission ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __commission <EOL> def getDateTime ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __dateTime <EOL> class OrderEvent ( object ) : <EOL> class Type : <EOL> ACCEPTED = <NUM_LIT:1> <EOL> CANCELED = <NUM_LIT:2> <EOL> PARTIALLY_FILLED = <NUM_LIT:3> <EOL> FILLED = <NUM_LIT:4> <EOL> def __init__ ( self , order , eventyType , eventInfo ) : <EOL> self . __order = order <EOL> self . __eventType = eventyType <EOL> self . __eventInfo = eventInfo <EOL> def getOrder ( self ) : <EOL> return self . __order <EOL> def getEventType ( self ) : <EOL> return self . __eventType <EOL> def getEventInfo ( self ) : <EOL> return self . __eventInfo <EOL> class Broker ( observer . Subject ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = abc . ABCMeta <EOL> def __init__ ( self ) : <EOL> super ( Broker , self ) . __init__ ( ) <EOL> self . __orderEvent = observer . Event ( ) <EOL> def getDispatchPriority ( self ) : <EOL> return dispatchprio . BROKER <EOL> def notifyOrderEvent ( self , orderEvent ) : <EOL> self . __orderEvent . emit ( self , orderEvent ) <EOL> def getOrderUpdatedEvent ( self ) : <EOL> return self . __orderEvent <EOL> @ abc . abstractmethod <EOL> def getInstrumentTraits ( self , instrument ) : <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def getCash ( self , includeShort = True ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def getShares ( self , instrument ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def getPositions ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def getActiveOrders ( self , instrument = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def submitOrder ( self , order ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def createMarketOrder ( self , action , instrument , quantity , onClose = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def createLimitOrder ( self , action , instrument , limitPrice , quantity ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def createStopOrder ( self , action , instrument , stopPrice , quantity ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def createStopLimitOrder ( self , action , instrument , stopPrice , limitPrice , quantity ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def cancelOrder ( self , order ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) </s>
