def TwoAveragesEx( average1=None, # mathutils.ewma(alpha = 0.15), average2=None, # mathutils.ewma(alpha = 0.015), threshold=0, orderFactory=order.MarketFactory, creationIntervalDistr=mathutils.rnd.expovariate(1.0), volumeDistr=mathutils.rnd.expovariate(1.0), ): if average1 is None: average1 = mathutils.ewma(alpha=0.15) if average2 is None: average2 = mathutils.ewma(alpha=0.015) return defs( Generic( orderFactory=orderFactory, volumeFunc=volumeDistr, eventGen=scheduler.Timer(creationIntervalDistr), sideFunc=SignalSide( mathutils.sub(observable.Fold(_.price, average1), observable.Fold(_.price, average2)), threshold ), ), {"price": observable.Price(orderbook.OfTrader())}, )
def LiquidityProviderEx (orderFactory = order.LimitFactory, defaultValue = 100., creationIntervalDistr = mathutils.rnd.expovariate(1.), priceDistr = mathutils.rnd.lognormvariate(0., .1), volumeDistr = mathutils.rnd.expovariate(1.)): orderBook = orderbook.OfTrader() def create(side): return LiquidityProviderSideEx(side, _.orderFactory, defaultValue, _.creationInterval, _.price, _.volume) return defs( Array([ create(Side.Sell), create(Side.Buy) ]), { 'creationInterval': creationIntervalDistr, 'volume' : volumeDistr, 'price' : priceDistr, 'orderFactory' : orderFactory })
def RSI(orderbook, timeframe, alpha): return defs(_rsi_sub( constant(100.), div( constant(100.), sum( constant(1.), _.rs)), orderbook, timeframe), { 'rs' : div(Fold(TwoPointFold(_.timer, _.price, upMovement), ewma(alpha)), Fold(TwoPointFold(_.timer, _.price, downMovement), ewma(alpha))), 'price' : Price(orderbook), 'timer' : scheduler.Timer(constant(timeframe)) \ if timeframe > 0 else\ _.price })
def SafeSidePrice(orderBook, side, defaultValue): return defs( NotNoneFloat( side_price(_.orderBook, side), NotNoneFloat( last_side_price(_.orderBook, side), mathutils.constant(defaultValue))), { 'orderBook': orderBook })
def SignalEx(signal = signal.RandomWalk(), threshold = 0., orderFactory = order.MarketFactory, volumeDistr = mathutils.rnd.expovariate(1.)): return defs( Generic(sideFunc = SignalSide(_.signal, threshold), volumeFunc = volumeDistr, orderFactory = orderFactory, eventGen = _.signal), {"signal" : signal })
def SignalSide(source, threshold = 0): return defs(ConditionSide( less_float( _.threshold, _.source), ConstantSide(Side.Buy), ConditionSide( less_float( _.source, mathutils.negate(_.threshold)), ConstantSide(Side.Sell), none_side())), { 'source' : source, 'threshold' : mathutils.constant(threshold) })
def RSIbis (timeframe = 0., threshold = 30, alpha = 1./14, orderFactory = order.MarketFactory, volumeDistr = mathutils.rnd.expovariate(1.), creationIntervalDistr = mathutils.rnd.expovariate(1.)): thisBook = orderbook.OfTrader() return defs(Generic(orderFactory = orderFactory, volumeFunc = volumeDistr, eventGen = scheduler.Timer(creationIntervalDistr), sideFunc = SignalSide(mathutils.sub(mathutils.constant(50), _.rsi), 50-threshold)), { 'rsi' : observable.RSI(thisBook, timeframe, alpha) })
def DependencyEx (bookToDependOn, factor = mathutils.constant(1.), orderFactory = order.MarketFactory, volumeDistr = mathutils.rnd.expovariate(1.)): orderBook = orderbook.OfTrader() r = defs( Generic(orderFactory= orderFactory, volumeFunc = volumeDistr, eventGen = _.dependee, sideFunc = FundamentalValueSide(orderBook, _.dependee)), { 'dependee' : observable.Price(bookToDependOn) }) r._alias = ["Generic", "Dependency"] return r
def FundamentalValueSide(orderBook, fundamentalValue): return defs( ConditionSide( less_float( _.fv, bid_price( _.orderBook)), ConstantSide(Side.Sell), ConditionSide( less_float( ask_price( _.orderBook), _.fv), ConstantSide(Side.Buy), none_side())), { 'fv' : fundamentalValue, 'orderBook' : orderBook })