def __call__(self, side = None): from marketsim.gen._out.side._sell import Sell_ as _side_Sell_ from marketsim.gen._out.order._limit import Limit side = side if side is not None else _side_Sell_() price = self.price volume = self.volume return Limit(side, price, volume)
def __call__(self, price=None): from marketsim.gen._out._constant import constant_Float as _constant_Float from marketsim.gen._out.order._limit import Limit price = price if price is not None else _constant_Float(100.0) side = self.side volume = self.volume return Limit(side, price, volume)
def __call__(self, side=None, price=None): from marketsim.gen._out.side._sell import Sell_ as _side_Sell_ from marketsim.gen._out._constant import constant_Float as _constant_Float from marketsim.gen._out.order._limit import Limit side = side if side is not None else _side_Sell_() price = price if price is not None else _constant_Float(100.0) volume = self.volume return Limit(side, price, volume)
def Limit(self, price=None, volume=None): from marketsim.gen._out.order._limit import Limit return Limit(self, price, volume)
def inner(self, myQueue, side): """Called when in some queue a new best order appeared""" # ordered set of queues on my side myQueues = self._bests[side.id] oppositeSide = side.opposite # ordered set of queues on the opposite side oppositeQueues = self._bests[oppositeSide.id] bestOrder = myQueue.best if not myQueue.empty else None # since the price of the best order changed, # we remove its queue from the set of all queues if myQueue in self._oldBests: try: p = self._oldBests[myQueue] myQueues.pop(p) except Exception: pass # very strange things... # if the queue becomes empty if bestOrder == None: # just remove it from the set of all queues if myQueue in self._oldBests: self._oldBests.pop(myQueue) else: # otherwise, update correspondance queue -> signedPrice -> queue self._oldBests[myQueue] = bestOrder.signedPrice myQueues[bestOrder.signedPrice] = myQueue # if there are opposite queues if len(oppositeQueues) > 0: # take the best price of the best one bestOppositeSignedPrice = oppositeQueues.viewkeys()[0] # and the queue itself oppositeQueue = oppositeQueues[bestOppositeSignedPrice] if oppositeQueue.empty or oppositeQueue.best.price != abs(bestOppositeSignedPrice): # it means that we haven't yet received event that another queue has changed return oppositePrice = abs(bestOppositeSignedPrice) myPrice = bestOrder.price # is there some sense to trade if not side.better(oppositePrice, myPrice): volumeToTrade = min(bestOrder.volumeUnmatched, oppositeQueue.best.volumeUnmatched) # make two complimentary trades # for these trades we create limit orders # since price may change before orders will be processed # but cancel them immediately in order to avoid storing these limit orders in the book # this logic is implemented by ImmediateOrCancelOrder def send(o): self._send(myQueue.book, o) from marketsim.gen._out.side._buy import Buy from marketsim.gen._out.side._sell import Sell my_side = Buy() if side == Side.Buy else Sell() opp_side = Buy() if side == Side.Sell else Sell() from marketsim.gen._out._constant import constant send(ImmediateOrCancel( Limit( opp_side, constant(myPrice), constant(volumeToTrade)))()) send(ImmediateOrCancel( Limit( my_side, constant(oppositePrice), constant(volumeToTrade)))())