def __init__(self, x = None, elsePart = None):
     from marketsim import float
     from marketsim.ops._all import Observable
     from marketsim.gen._out._constant import constant
     from marketsim.gen._out._constant import constant
     from marketsim import _
     from marketsim import event
     Observable[float].__init__(self)
     self.x = x if x is not None else constant()
     self.elsePart = elsePart if elsePart is not None else constant()
     self.impl = self.getImpl()
     event.subscribe(self.impl, _(self).fire, self)
Example #2
0
 def clearPosition(self):
     from marketsim._pub import order, side
     position = self._position()
     pending = self._pendingVolume()
     if position is not None and pending is not None:
         self._internalSuspended = True
         self.suspended = True
         # signed number of assets that we have
         p = position + pending
         if p > 0: # if positive, we sell
             self._send(order.Market(side.Sell(), constant(p))())
         if p < 0: # if negative, we buy
             self._send(order.Market(side.Buy(), constant(-p))())
Example #3
0
 def __init__(self, base = None, power = None):
     from marketsim.gen._out._constant import constant
     from marketsim import event
     from marketsim import types
     from marketsim.gen._out._constant import constant
     from marketsim import event
     from marketsim import types
     Observable[float].__init__(self)
     self.base = base if base is not None else constant(1.0)
     if isinstance(base, types.IEvent):
         event.subscribe(self.base, self.fire, self)
     self.power = power if power is not None else constant(1.0)
     if isinstance(power, types.IEvent):
         event.subscribe(self.power, self.fire, self)
 def __init__(self, source = None, epsilon = None):
     from marketsim.gen._out._constant import constant
     from marketsim.gen._out._constant import constant
     from marketsim import event
     from marketsim import types
     from marketsim import event
     from marketsim import types
     self.source = source if source is not None else constant()
     self.epsilon = epsilon if epsilon is not None else constant(0.01)
     MaxEpsilon_Impl.__init__(self)
     if isinstance(source, types.IEvent):
         event.subscribe(self.source, self.fire, self)
     if isinstance(epsilon, types.IEvent):
         event.subscribe(self.epsilon, self.fire, self)
Example #5
0
 def __init__(self):
     self._event = event.subscribe(self.source, _(self)._update, self)
     event.subscribe(event.Every(constant(self.timeframe)),
                     _(self)._flush, self)
     self.reset()
     self._mean = self.source.Cumulative.Avg
     self._stddev = self.source.Cumulative.StdDev
Example #6
0
 def __init__(self):
     self._event = event.subscribe(self.source, _(self)._update, self)
     event.subscribe(event.Every(constant(self.timeframe)),
                     _(self)._flush, self)
     self.reset()
     self._mean = CMA(self.source)
     self._stddev = StdDev(self.source)
Example #7
0
 def __init__(self, x = None):
     from marketsim.gen._out._constant import constant
     from marketsim import event
     from marketsim import types
     Observable[float].__init__(self)
     self.x = x if x is not None else constant(1.0)
     if isinstance(x, types.IEvent):
         event.subscribe(self.x, self.fire, self)
Example #8
0
 def _makeOrder(self, t):
     """ Creates an order with price equivalent to 't' ticks
     """
     p = self._orderQueue.ticksToPrice(t)
     order = self.orderFactory(self.side, constant(p))()
     order.ticks = t
     order.source = self
     return order
Example #9
0
 def __init__(self):
     Strategy.__init__(self)
     self._current = None
     self._estimators = []
     for s in self.strategies:
         event.subscribe(s.on_order_created, _(self).send, self)
         e = self.performance(self.account(s))
         e._origin = s
         self._estimators.append(e)
     event.subscribe(event.Every(constant(1.)), _(self)._wakeUp, self)
 def __init__(self):
     Strategy.__init__(self)
     self._current = None
     self._estimators = []
     for s in self.strategies:
         event.subscribe(s.on_order_created, _(self).send, self)
         e = self.normalizer(self.weight(self.account(s)))
         e._origin = s
         self._estimators.append(e)
     event.subscribe(event.Every(constant(1.)), _(self)._wakeUp, self)
Example #11
0
 def __init__(self, signal = None, threshold = None):
     from marketsim import Side
     from marketsim.ops._all import Observable
     from marketsim.gen._out._constant import constant
     from marketsim import _
     from marketsim import event
     Observable[Side].__init__(self)
     self.signal = signal if signal is not None else constant()
     self.threshold = threshold if threshold is not None else 0.7
     self.impl = self.getImpl()
     event.subscribe(self.impl, _(self).fire, self)
 def __init__(self, fv = None, book = None):
     from marketsim import Side
     from marketsim.ops._all import Observable
     from marketsim.gen._out._constant import constant
     from marketsim.gen._out.observable.orderbook._OfTrader import OfTrader
     from marketsim import _
     from marketsim import event
     Observable[Side].__init__(self)
     self.fv = fv if fv is not None else constant(200.0)
     self.book = book if book is not None else OfTrader()
     self.impl = self.getImpl()
     event.subscribe(self.impl, _(self).fire, self)
 def __init__(self, queue = None, defaultValue = None):
     from marketsim import float
     from marketsim.ops._all import Observable
     from marketsim.gen._out.observable.orderbook._Asks import Asks
     from marketsim.gen._out._constant import constant
     from marketsim import _
     from marketsim import event
     Observable[float].__init__(self)
     self.queue = queue if queue is not None else Asks()
     self.defaultValue = defaultValue if defaultValue is not None else constant(100.0)
     self.impl = self.getImpl()
     event.subscribe(self.impl, _(self).fire, self)
 def __init__(self, dt = None, x = None):
     from marketsim.gen._out._constant import constant
     from marketsim import event
     from marketsim import types
     from marketsim import event
     from marketsim import types
     self.dt = dt if dt is not None else 1.0
     self.x = x if x is not None else constant()
     _OnEveryDt_Impl.__init__(self)
     if isinstance(dt, types.IEvent):
         event.subscribe(self.dt, self.fire, self)
     if isinstance(x, types.IEvent):
         event.subscribe(self.x, self.fire, self)
Example #15
0
 def __init__(self, source = None, timeframe = None):
     from marketsim.gen._out._constant import constant
     from marketsim import event
     from marketsim import types
     from marketsim import event
     from marketsim import types
     self.source = source if source is not None else constant()
     self.timeframe = timeframe if timeframe is not None else 100.0
     Max_Impl.__init__(self)
     if isinstance(source, types.IEvent):
         event.subscribe(self.source, self.fire, self)
     if isinstance(timeframe, types.IEvent):
         event.subscribe(self.timeframe, self.fire, self)
 def __init__(self, dependee = None, factor = None, book = None):
     from marketsim import Side
     from marketsim.ops._all import Observable
     from marketsim.gen._out.observable.orderbook._OfTrader import OfTrader
     from marketsim.gen._out._constant import constant
     from marketsim.gen._out.observable.orderbook._OfTrader import OfTrader
     from marketsim import _
     from marketsim import event
     Observable[Side].__init__(self)
     self.dependee = dependee if dependee is not None else OfTrader()
     self.factor = factor if factor is not None else constant(1.0)
     self.book = book if book is not None else OfTrader()
     self.impl = self.getImpl()
     event.subscribe(self.impl, _(self).fire, self)
 def __init__(self, book = None, depth = None):
     from marketsim.gen._out.observable.orderbook._OfTrader import OfTrader
     from marketsim.gen._out._constant import constant
     from marketsim import event
     from marketsim import types
     from marketsim import event
     from marketsim import types
     self.book = book if book is not None else OfTrader()
     self.depth = depth if depth is not None else constant()
     CumulativePrice_Impl.__init__(self)
     if isinstance(book, types.IEvent):
         event.subscribe(self.book, self.fire, self)
     if isinstance(depth, types.IEvent):
         event.subscribe(self.depth, self.fire, self)
Example #18
0
 def __init__(self):
     self._quotes = None
     self._current = None
     event.subscribe(event.Every(constant(1)), _(self)._wakeUp, self)
 def __init__(self):
     self.attributes = {'smooth':True}
     self._dataSource = self.x
     IndicatorBase.__init__(self)
     self._subscription = event.subscribe(event.Every(constant(self.dt)), self.fire, self)
Example #20
0
 def __call__(self):
     proto = self.proto(constant(0))()
     return Order_Impl(proto,
                       self.floatingPrice) if proto is not None else None
Example #21
0
    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)))())
Example #22
0
 def __init__(self):
     self._quotes = None
     self._current = None
     event.subscribe(event.Every(constant(1)), _(self)._wakeUp, self)
Example #23
0
 def constant(self):
     from marketsim.gen._out._constant import constant
     return constant(self)
Example #24
0
 def __call__(self):
     from marketsim.gen._out._constant import constant
     proto = self.proto(constant(0))()
     return Peg(proto) if proto is not None else None
Example #25
0
 def __init__(self):
     event.subscribe(event.Every(constant(1)), self.fire, self)
Example #26
0
 def __init__(self):
     self._event = event.subscribe(self.source, _(self)._update, self)
     event.subscribe(event.Every(constant(self.timeframe)), _(self)._flush, self)
     self.reset()
     self._mean = self.source.Cumulative.Avg
     self._stddev = self.source.Cumulative.StdDev
 def getImpl(self):
     from marketsim.gen._out.observable.orderbook._SafeSidePrice import SafeSidePrice
     from marketsim.gen._out.observable.orderbook._Queue import Queue
     from marketsim.gen._out._constant import constant
     return SafeSidePrice(Queue(self.book,self.side),constant(self.initialValue))*self.priceDistr
Example #28
0
 def __call__(self):
     from marketsim.gen._out._constant import constant
     proto = self.proto(constant(0))()
     return Peg(proto) if proto is not None else None
Example #29
0
 def __init__(self, source = None, alpha = None):
     from marketsim.gen._out._constant import constant
     self.source = source if source is not None else constant()
     self.alpha = alpha if alpha is not None else 0.015
     EWMA_Impl.__init__(self)
 def __call__(self):
     proto = self.proto(constant(0))()
     return Order_Impl(proto, self.floatingPrice) if proto is not None else None
Example #31
0
 def constant(self):
     from marketsim.gen._out._constant import constant
     return constant(self)
Example #32
0
 def __init__(self):
     self.attributes = {'smooth': True}
     self._dataSource = self.x
     IndicatorBase.__init__(self)
     self._subscription = event.subscribe(event.Every(constant(self.dt)),
                                          self.fire, self)
 def __init__(self):
     event.subscribe(event.Every(constant(1)), self.fire, self)
 def __init__(self, lhs = None, rhs = None):
     from marketsim.gen._out._constant import constant
     BinaryOp[float].__init__(self,
                              lhs if lhs is not None else constant(1.),
                              rhs if rhs is not None else constant(1.))