def __init__(self): Strategy.__init__(self) from marketsim._pub import strategy, side self._seller = strategy.price.Ladder(self.orderFactory, self.initialSize, side.Sell()) self._buyer = strategy.price.Ladder(self.orderFactory, self.initialSize, side.Buy()) event.subscribe(self._seller.on_order_created, _(self)._send, self) event.subscribe(self._buyer.on_order_created, _(self)._send, self)
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))())
def __call__(self): return side.Buy() if self._impl() > 0 else side.Sell()
def orderBooksToRender(ctx, traders): books = list(set(itertools.chain(*[t.orderBooks for t in traders]))) books = filter(lambda b: type(b) is Local_StringFloatIntListITimeSerie, books) graphs = ctx.graphs def orderbook_ts(): from marketsim.gen._out.math._max import Max thisBook = orderbook.Proxy() assetPrice = thisBook.MidPrice scaled = (assetPrice - 100) / 10 def bollinger(avg, stddev): return [ assetPrice, avg, (avg + stddev * 2).OnEveryDt(1), (avg - stddev * 2).OnEveryDt(1) ] ts = { ctx.price_graph: [assetPrice, thisBook.Asks.BestPrice, thisBook.Bids.BestPrice], ctx.askbid_graph: [ thisBook.Asks.LastTradePrice, thisBook.Asks.WeightedPrice(), thisBook.Bids.LastTradePrice, thisBook.Bids.WeightedPrice() ], ctx.avgs_graph: [ assetPrice.Cumulative.Avg, (assetPrice.Moving(20).Avg, config.collectMoving), (assetPrice.Moving(100).Avg, config.collectMoving), assetPrice.EW(0.15).Avg, assetPrice.EW(0.65).Avg, assetPrice.EW(0.015).Avg, ], ctx.macd_graph: [ scaled, scaled.EW(2. / 27).Avg, scaled.EW(2. / 13).Avg, assetPrice.macd().Value, assetPrice.macd().Signal(), assetPrice.macd().Histogram(), ((assetPrice.LogReturns() * 100).OnEveryDt(1), config.collectMoving) ], ctx.minmax_graph: [ assetPrice, assetPrice.Cumulative.MaxEpsilon(), assetPrice.Cumulative.MinEpsilon(), (assetPrice.Moving(100.).Maximum, config.collectMoving), (assetPrice.Moving(100.).Minimum, config.collectMoving) ], ctx.bollinger_100_graph: bollinger( assetPrice.Moving(100).Avg, assetPrice.Moving(100).StdDev) if config.collectMoving else [], ctx.bollinger_20_graph: bollinger(assetPrice.Moving(20).Avg, assetPrice.Moving(20).StdDev) if config.collectMoving else [], ctx.bollinger_a015_graph: bollinger(assetPrice.EW(0.015).Avg, assetPrice.EW(0.015).StdDev), } out = [] for (graph, timeserie_list) in ts.iteritems(): for timeserie in timeserie_list: if type(timeserie) is tuple: if timeserie[1]: timeserie = timeserie[0] else: continue out.append(makeTimeSerie(timeserie, graph)) return out for b in books: thisBook = orderbook.Proxy() ts = orderbook_ts() b.volumes_graph = ctx.addGraph("Volume levels " + b.label) ts.append( volumeLevels( thisBook.Queue(side.Sell()).VolumeLevels(30, 10), b.volumes_graph)) ts.append( volumeLevels( thisBook.Queue(side.Buy()).VolumeLevels(30, 10), b.volumes_graph)) b.timeseries = ts if config.collectRSI: b.rsi_graph = ctx.addGraph("RSI " + b.label) ts.append(TimeSerie(thisBook.MidPrice, b.rsi_graph)) for timeframe in [ #0., #0.001, #0.01, 0.1, #0.3, 0.5, 1., #1.5, 2, #3, #4, 5 ]: ts.append( TimeSerie( thisBook.MidPrice.RSI(timeframe, 1. / 14).Value.OnEveryDt(1), b.rsi_graph)) return books