def Signal(ctx): const = ops.constant linear_signal = signal.RandomWalk(initialValue=20, deltaDistr=const(-.1), label="20-0.1t") return [ ctx.makeTrader_A( strategy.LiquidityProvider( event.Every(ops.constant(1.)), order.factory.sideprice.Limit(volume=const(5))), "liquidity"), ctx.makeTrader_A(strategy.v0.Signal(linear_signal, volumeDistr=const(1)), "signal", [(linear_signal, ctx.amount_graph)]), ctx.makeTrader_A(strategy.Signal( event.Every(ops.constant(1.)), order.factory.side.Market(volume = const(1)), linear_signal), "signal_ex"), ]
def getImpl(self): return Generic(self.orderFactory( ops.constant(self.side), parts.price.LiquidityProvider(ops.constant(self.side), self.initialValue, self.priceDistr)), self.eventGen)
def Dependency(ctx): liqVol = ops.constant(2) ctx.volumeStep = 70 return [ ctx.makeTrader_A( strategy.LiquidityProvider(event.Every(ops.constant(1.)), order.factory.sideprice.Limit(volume = liqVol), defaultValue=50.), "LiquidityProvider_A"), ctx.makeTrader_B( strategy.LiquidityProvider(event.Every(ops.constant(1.)), order.factory.sideprice.Limit(volume = liqVol), defaultValue=150.), "LiquidityProvider_B"), ctx.makeTrader_A( strategy.Dependency( event.Every(ops.constant(1.)), order.factory.side.Market(), ctx.book_B, factor=2.), "A dependent on B ex"), ctx.makeTrader_B( strategy.Dependency( event.Every(ops.constant(1.)), order.factory.side.Market(), ctx.book_A, factor=.5), "B dependent on A ex"), ]
def __init__(self, side = ops.constant(Side.Sell), budget = ops.constant(200.)): ops.Observable[types.SideBudget].__init__(self) self.side = side self.budget = budget subscribe_if_observable(side, self) subscribe_if_observable(budget, self)
def RSI(ctx): const = ops.constant linear_signal = signal.RandomWalk(initialValue=20, deltaDistr=const(-.1), label="20-0.1t") one = const(1) rsi = observable.OnEveryDt(one.value, observable.Fold( observable.MidPrice( orderbook.OfTrader()), mathutils.rsi())) threshold = 30 demo = ctx.addGraph('demo') myVolume = lambda: [(observable.VolumeTraded(), demo)] myRsi = lambda: [(rsi, demo)] alpha = 1./14 myRsiBis = lambda: [(observable.OnEveryDt(1, observable.RSI(orderbook.OfTrader(), 1, alpha)), demo)] return [ ctx.makeTrader_A( strategy.LiquidityProvider( event.Every(ops.constant(1.)), order.factory.sideprice.Limit(volume=const(4))), "liquidity"), ctx.makeTrader_A(strategy.Signal(event.Every(ops.constant(1.)), order.factory.side.Market(), linear_signal), "signal", [(linear_signal, ctx.amount_graph)]), ctx.makeTrader_A(strategy.RSIbis(event.Every(ops.constant(1.)), order.factory.side.Market(one), alpha = alpha, timeframe = 1, threshold=threshold), "rsi_bis", myVolume() + myRsiBis()), ctx.makeTrader_A(strategy.v0.RSIEx(alpha = alpha, threshold=threshold, volumeDistr=one, creationIntervalDistr=one), "rsi_ex", (myVolume() + myRsi() + Constant(threshold, demo) + Constant(100-threshold, demo))) ]
def __init__(self, side = ops.constant(Side.Sell), price = ops.constant(100), volume = ops.constant(1)): ops.Observable[types.SidePriceVolume].__init__(self) self.side = side self.price = price self.volume = volume subscribe_if_observable(side, self) subscribe_if_observable(price, self) subscribe_if_observable(volume, self)
def TwoAverages(ctx): ctx.volumeStep = 30 alpha_slow = 0.015 alpha_fast = 0.15 linear_signal = signal.RandomWalk(initialValue=200, deltaDistr=const(-1), label="200-t") demo = ctx.addGraph("demo") myVolume = lambda: [(observable.VolumeTraded(), demo)] myAverage = lambda alpha: [(observable.avg(observable.MidPrice(orderbook.OfTrader()), alpha), demo)] return [ ctx.makeTrader_A( strategy.LiquidityProvider(event.Every(ops.constant(1.0)), order.factory.sideprice.Limit(volume=const(10))), "liquidity", ), ctx.makeTrader_A( strategy.Signal(event.Every(ops.constant(1.0)), order.factory.side.Market(volume=const(3)), linear_signal), "signal", [(linear_signal, ctx.amount_graph)], ), ctx.makeTrader_A( strategy.v0.TwoAverages( ewma_alpha1=alpha_slow, ewma_alpha2=alpha_fast, creationIntervalDistr=const(1.0), volumeDistr=const(1.0) ), "avg+", myAverage(alpha_slow) + myAverage(alpha_fast) + myVolume(), ), ctx.makeTrader_A( strategy.v0.TwoAverages( ewma_alpha2=alpha_slow, ewma_alpha1=alpha_fast, creationIntervalDistr=const(1.0), volumeDistr=const(1.0) ), "avg-", myVolume(), ), ctx.makeTrader_A( strategy.TwoAverages( event.Every(ops.constant(1.0)), order.factory.side.Market(volume=const(1.0)), alpha_slow, alpha_fast ), "avg_ex+", myVolume(), ), ctx.makeTrader_A( strategy.TwoAverages( event.Every(ops.constant(1.0)), order.factory.side.Market(volume=const(1.0)), alpha_fast, alpha_slow ), "avg_ex-", myVolume(), ), ]
def FundamentalValue(ctx): ctx.volumeStep = 30 fv = 200 demo = ctx.addGraph('demo') myVolume = lambda: [(observable.VolumeTraded(), demo)] myPrice = lambda: [(observable.MidPrice(orderbook.OfTrader()), demo)] return [ ctx.makeTrader_A( strategy.v0.LiquidityProvider( volumeDistr=ops.constant(6), orderFactoryT=order.WithExpiryFactory( expirationDistr=ops.constant(10))), "liquidity"), ctx.makeTrader_A( strategy.v0.FundamentalValue( fundamentalValue = ops.constant(fv), creationIntervalDistr = ops.constant(1.), volumeDistr = ops.constant(1)), "fv_200", myVolume() + myPrice() + Constant(fv, demo)), ctx.makeTrader_A( strategy.FundamentalValue( event.Every(ops.constant(1.)), order.factory.side.Market(volume = ops.constant(1.)), ops.constant(fv)), "fv_ex_200", myVolume()), ]
def getImpl(self): quotes = observable.Quote(self.ticker, self.start, self.end) # TODO: should be in definitions return Array([ Generic( order.factory.Iceberg( const(self.volume), order.factory.FloatingPrice( BreaksAtChanges(ops.constant(sign*self.delta) + quotes), order._limit.Price_Factory( side = const(side), volume = const(self.volume * 1000000)))), event.After(ops.constant(0)))\ for side, sign in {Side.Buy : -1, Side.Sell : 1}.iteritems() ])
def Noise(ctx): ctx.volumeStep = 10 return [ ctx.makeTrader_A(strategy.v0.LiquidityProvider( volumeDistr=ops.constant(2), orderFactoryT=order.WithExpiryFactory( expirationDistr=ops.constant(10))), "liquidity"), ctx.makeTrader_A(strategy.v0.Noise(), "noise"), ctx.makeTrader_A(strategy.Noise(), "noise_ex"), ]
def Arbitrage(ctx): liqVol = mathutils.rnd.expovariate(.1) * 2 ctx.volumeStep = 70 factory = order.WithExpiryFactory(ops.constant(50)) return [ ctx.makeTrader_A( strategy.v0.LiquidityProvider(defaultValue=50., orderFactoryT=factory, volumeDistr=liqVol), "LiquidityProvider_A"), ctx.makeTrader_B( strategy.v0.LiquidityProvider(defaultValue=150., orderFactoryT=factory, volumeDistr=liqVol), "LiquidityProvider_B"), ctx.makeMultiAssetTrader([ctx.remote_A, ctx.remote_B], strategy.Arbitrage(), "Arbitrager") ]
def __init__(self, source = ops.constant(1.)): ops.Observable[float].__init__(self) self._source = source self._event = event.subscribe(source, _(self)._wakeup, self) self._previous = None self._value = None
def __init__(self, ticker = "^GSPC", start = "2001-1-1", end = "2010-1-1"): ops.Observable[float].__init__(self) self.ticker = ticker self.start = start self.end = end self._quotes = None self._current = None event.subscribe(event.Every(ops.constant(1)), _(self)._wakeUp, self)
def MeanReversion(ctx): ctx.volumeStep = 40 alpha = 0.015 V = 1 linear_signal = signal.RandomWalk(initialValue=200, deltaDistr=const(-1), label="200-t") demo = ctx.addGraph('demo') myVolume = lambda: [(observable.VolumeTraded(), demo)] myAverage = lambda: [(observable.avg(observable.MidPrice(orderbook.OfTrader()), alpha), demo)] myPrice = lambda: [(observable.MidPrice(orderbook.OfTrader()), demo)] return [ ctx.makeTrader_A( strategy.v0.LiquidityProvider( volumeDistr=const(V*20), orderFactoryT=order.WithExpiryFactory( expirationDistr=const(10))), label="liquidity"), ctx.makeTrader_A(strategy.Signal( event.Every(ops.constant(1.)), order.factory.side.Market(volume = const(V*3)), linear_signal), "signal", [(linear_signal, ctx.amount_graph)]), ctx.makeTrader_A(strategy.v0.MeanReversion( ewma_alpha=(alpha), creationIntervalDistr = ops.constant(1.), volumeDistr = const(V)), "meanreversion", myVolume() + myAverage() + myPrice()), ctx.makeTrader_A( strategy.MeanReversion( event.Every(ops.constant(1.)), order.factory.side.Market(volume = const(V)), alpha), "meanreversion_ex", myVolume()), ]
def __init__(self, source, timeframe = 1.): ops.Observable[types.ICandleStick].__init__(self) self._source = source self._event = event.subscribe(source, _(self)._update, self) event.subscribe(event.Every(ops.constant(timeframe)), _(self)._flush, self) self.timeframe = timeframe self.reset() self._mean = CMA(source) self._stddev = StdDev(source)
def __init__(self): Strategy.__init__(self) self._eventGen = event.Every(ops.constant(0.9)) event.subscribe(self._eventGen, _(self)._wakeUp, self) self.book = orderbook.OfTrader() self.midprice = observable.MidPrice(self.book) self.log = TraderHistory(SingleProxy()) self.prev_mid = None
def __init__(self): Strategy.__init__(self) self._eventGen = event.Every(ops.constant(1)) event.subscribe(self._eventGen, _(self)._wakeUp, self) self.quotes = data.load(self.ticker, self.start, self.end)['Adj Close'] self.log = TraderHistory(SingleProxy()) self.waitingForCancel = False
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(ops.constant(1.)), _(self)._wakeUp, self)
def __init__(self, world, graph_renderer): self.world = world self.book_A = orderbook.Local(tickSize=0.01, label="A") self.book_B = orderbook.Local(tickSize=0.01, label="B") if config.showTiming: self.world.process(const(10), bind.Function(_print, '.')) self.world.process(const(100), bind.Function(_print, '\n')) delay = ops.constant(1.07) self.link_A = remote.TwoWayLink(remote.Link(delay), remote.Link(delay)) self.link_B = remote.TwoWayLink(remote.Link(delay), remote.Link(delay)) self.remote_A = orderbook.Remote(self.book_A, self.link_A) self.remote_B = orderbook.Remote(self.book_B, self.link_B) self.graph = graph_renderer self.price_graph = self.graph("Price") self.askbid_graph = self.graph("AskBid") self.candles_graph = self.graph("Candles") self.avgs_graph = self.graph("Averages") self.macd_graph = self.graph("MACD") self.eff_graph = self.graph("efficiency") self.amount_graph = self.graph("amount") self.balance_graph = self.graph('balance') self.bollinger_a015_graph = self.graph('bollinger alpha 0.15') self.bollinger_20_graph = self.graph('bollinger 20') self.bollinger_100_graph = self.graph('bollinger 100') self.minmax_graph = self.graph('minmax') self.minors_eff_graph = self.graph('minor traders efficiency') self.minors_amount_graph = self.graph('minor traders position') self.graphs = [ self.price_graph, self.askbid_graph, self.candles_graph, self.avgs_graph, self.macd_graph, self.eff_graph, self.amount_graph, self.balance_graph, self.bollinger_20_graph, self.bollinger_a015_graph, self.bollinger_100_graph, self.minmax_graph, self.minors_eff_graph, self.minors_amount_graph ] self.books = { 'Asset A' : self.book_A , 'Asset B' : self.book_B , 'Remote A': self.remote_A, 'Remote B': self.remote_B }
def getImpl(self): volumeTraded = observable.VolumeTraded(trader.SingleProxy()) return Array([ Generic( order.factory.Iceberg( ops.constant(10), order.factory.FloatingPrice( BreaksAtChanges( observable.OnEveryDt( 0.9, parts.price.SafeSidePrice(Queue(orderbook.OfTrader(), ops.constant(side)), ops.constant(100 + sign))\ / ops.Exp(ops.Atan(volumeTraded) / 1000) )), order._limit.Price_Factory( side = const(side), volume = const(self.volume * 1000000)))), event.After(ops.constant(0)))\ for side, sign in {Side.Buy : -1, Side.Sell : 1}.iteritems() ])
def Canceller(ctx): ctx.volumeStep = 15 return [ ctx.makeTrader_A(strategy.v0.LiquidityProviderSide(side = Side.Sell), "LiquidityProvider-"), ctx.makeTrader_A(strategy.LiquidityProvider(), "LiquidityProviderEx-"), ctx.makeTrader_A(strategy.v0.LiquidityProviderSide(side = Side.Buy), "LiquidityProviderBuy"), ctx.makeTrader_A( strategy.v0.LiquidityProviderSide( side = Side.Sell, orderFactoryT=order.WithExpiryFactory( expirationDistr=ops.constant(10))), "LiquidityProviderWithExpiry"), ctx.makeTrader_A( strategy.v0.FundamentalValue( fundamentalValue = ops.constant(1000)), "fv_1000") ]
def TradeIfProfitable(ctx): ctx.volumeStep = 30 slow_alpha = 0.015 fast_alpha = 0.15 linear_signal = signal.RandomWalk(initialValue=200, deltaDistr=const(-1), label="200-t") demo = ctx.addGraph("demo") myVolume = lambda: [(observable.VolumeTraded(), demo)] myAverage = lambda alpha: [(observable.avg(observable.MidPrice(orderbook.OfTrader()), alpha), demo)] def cross(alpha1, alpha2): return strategy.TwoAverages( event.Every(ops.constant(1.0)), order.factory.side.Market(volume=const(1.0)), alpha1, alpha2 ) avg_plus_virt = strategy.TradeIfProfitable(cross(slow_alpha, fast_alpha), strategy.adaptive.virtualMarket) avg_minus_virt = strategy.TradeIfProfitable(cross(fast_alpha, slow_alpha), strategy.adaptive.virtualMarket) avg_plus_real = strategy.TradeIfProfitable(cross(slow_alpha, fast_alpha), strategy.adaptive.account) avg_minus_real = strategy.TradeIfProfitable(cross(fast_alpha, slow_alpha), strategy.adaptive.account) return [ ctx.makeTrader_A(strategy.v0.LiquidityProvider(volumeDistr=const(45)), "liquidity"), ctx.makeTrader_A( strategy.Signal(event.Every(ops.constant(1.0)), order.factory.side.Market(volume=const(20)), linear_signal), "signal", [(linear_signal, ctx.amount_graph)], ), ctx.makeTrader_A( cross(slow_alpha, fast_alpha), "avg+", myAverage(slow_alpha) + myAverage(fast_alpha) + myVolume() ), ctx.makeTrader_A(cross(fast_alpha, slow_alpha), "avg-", myVolume()), ctx.makeTrader_A(avg_plus_virt, "avg+ virt", myVolume()), ctx.makeTrader_A(avg_minus_virt, "avg- virt", myVolume()), ctx.makeTrader_A(avg_plus_real, "avg+ real", myVolume()), ctx.makeTrader_A(avg_minus_real, "avg- real", myVolume()), ]
def __init__(self, source = ops.constant(1.)): """ Initializes folder with source of values and accumulator object """ self._source = source self._event = event.subscribe(self._source, _(self)._update, self)
def unit(trader): return ops.constant(1.)
def atanpow(f, base = 1.002): return ops.Atan(ops.Pow(ops.constant(base), f))
def clamp0(f): return ops.Max(f, ops.constant(0)) + 1
def __init__(self, price = ops.constant(100), factory = _limit.Side_Price_Factory()): self.price = price self.factory = factory
def __call__(self): proto = self.factory(ops.constant(0))() return FloatingPrice(proto, self.price) if proto is not None else None
def VolumeLevels(interval, orderbook, side, volumeDelta, volumeCount): return IndicatorBase(event.Every(ops.constant(interval)), volume_levels(orderbook, side, volumeDelta, volumeCount), {'smooth':True, 'volumeLevels' : True, 'fillBelow' : side == Side.Buy, 'fillAbove' : side == Side.Sell})
def PriceAtVolume(interval, orderbook, side, volume): return IndicatorBase(event.Every(ops.constant(interval)), price_at_volume(orderbook, side, volume), {'smooth':True, 'fillBelow' : side == Side.Buy, 'fillAbove' : side == Side.Sell})