def Arbitrage(ctx):

    liqVol = math.random.expovariate(.1) * 2.

    ctx.volumeStep = 70

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                orderFactory=order.side_price.WithExpiry(
                    constant(50.), order.side_price.Limit(volume=liqVol)),
                initialValue=50.), "LiquidityProvider_A"),
        ctx.makeTrader_B(
            strategy.LiquidityProvider(
                orderFactory=order.side_price.WithExpiry(
                    constant(50.), order.side_price.Limit(volume=liqVol)),
                initialValue=150.), "LiquidityProvider_B"),
        ctx.makeTrader_C(
            strategy.LiquidityProvider(
                orderFactory=order.side_price.WithExpiry(
                    constant(50), order.side_price.Limit(volume=liqVol)),
                initialValue=100.), "LiquidityProvider_C"),
        ctx.makeMultiAssetTrader([ctx.remote_A, ctx.remote_B, ctx.remote_C],
                                 strategy.Arbitrage(), "Arbitrager")
    ]
Example #2
0
def Dependency(ctx):

    liqVol = constant(3)

    ctx.volumeStep = 70

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(event.Every(constant(1.)),
                                       order.side_price.Limit(volume=liqVol),
                                       initialValue=50.),
            "LiquidityProvider_A"),
        ctx.makeTrader_B(
            strategy.LiquidityProvider(event.Every(constant(1.)),
                                       order.side_price.Limit(volume=liqVol),
                                       initialValue=150.),
            "LiquidityProvider_B"),
        ctx.makeTrader_A(
            strategy.PairTrading(event.Every(constant(1.)),
                                 order.side.Market(),
                                 ctx.book_B,
                                 factor=2.), "A dependent on B ex"),
        ctx.makeTrader_B(
            strategy.PairTrading(event.Every(constant(1.)),
                                 order.side.Market(),
                                 ctx.book_A,
                                 factor=.5), "B dependent on A ex"),
    ]
def MeanReversion(ctx):

    ctx.volumeStep = 40

    alpha = 0.015
    V = 1
    linear_signal = math.RandomWalk(initialValue=200,
                                    deltaDistr=constant(-1),
                                    name="200-t")

    demo = ctx.addGraph('demo')
    myVolume = lambda: [(trader.Position(), demo)]

    return [
        ctx.makeTrader_A(strategy.LiquidityProvider(
            orderFactory=order.side_price.WithExpiry(
                constant(10), order.side_price.Limit(volume=constant(V *
                                                                     20)))),
                         label="liquidity"),
        ctx.makeTrader_A(
            strategy.Signal(event.Every(constant(1.)),
                            order.side.Market(volume=constant(V * 3)),
                            linear_signal), "signal",
            [(linear_signal, ctx.amount_graph)]),
        ctx.makeTrader_A(
            strategy.MeanReversion(event.Every(constant(1.)),
                                   order.side.Market(volume=constant(V)),
                                   alpha), "meanreversion_ex", myVolume()),
    ]
def TwoAverages(ctx):

    ctx.volumeStep = 30

    alpha_slow = 0.015
    alpha_fast = 0.15

    linear_signal = math.RandomWalk(initialValue=200,
                                    deltaDistr=const(-1),
                                    name="200-t")

    demo = ctx.addGraph('demo')
    myVolume = lambda: [(trader.Position(), demo)]

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                event.Every(constant(1.)),
                order.side_price.Limit(volume=const(10))), "liquidity"),
        ctx.makeTrader_A(
            strategy.Signal(event.Every(constant(1.)),
                            order.side.Market(volume=const(3)), linear_signal),
            "signal", [(linear_signal, ctx.amount_graph)]),
        ctx.makeTrader_A(
            strategy.CrossingAverages(event.Every(constant(1.)),
                                      order.side.Market(volume=const(1.)),
                                      alpha_slow, alpha_fast), 'avg_ex+',
            myVolume()),
        ctx.makeTrader_A(
            strategy.CrossingAverages(event.Every(constant(1.)),
                                      order.side.Market(volume=const(1.)),
                                      alpha_fast, alpha_slow), 'avg_ex-',
            myVolume()),
    ]
def Canceller(ctx):

    ctx.volumeStep = 15

    return [
        ctx.makeTrader_A(strategy.LiquidityProvider(), "LiquidityProviderEx-"),
        ctx.makeTrader_A(strategy.Canceller(), "canceller"),
        ctx.makeTrader_A(
            strategy.FundamentalValue(fundamentalValue=const(1000)), "fv_1000")
    ]
Example #6
0
def Noise(ctx):

    ctx.volumeStep = 10

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                orderFactory=order.side_price.WithExpiry(
                    constant(10), order.side_price.Limit(volume=constant(2)))),
            "liquidity"),
        ctx.makeTrader_A(strategy.Noise(), "noise_ex"),
    ]
Example #7
0
def TradeIfProfitable(ctx):

    ctx.volumeStep = 30

    slow_alpha = 0.015
    fast_alpha = 0.15

    linear_signal = math.RandomWalk(initialValue=200,
                                    deltaDistr=constant(-1),
                                    name="200-t")

    demo = ctx.addGraph('demo')
    myVolume = lambda: [(trader.Position(), demo)]
    myAverage = lambda alpha: [(orderbook.OfTrader().MidPrice.EW_Avg(alpha).
                                OnEveryDt(1), demo)]

    def cross(alpha1, alpha2):
        return strategy.CrossingAverages(
            event.Every(constant(1.)), order.side.Market(volume=constant(1.)),
            alpha1, alpha2)

    avg_plus_virt = strategy.TradeIfProfitable(
        cross(slow_alpha, fast_alpha), strategy.account.virtualMarket())
    avg_minus_virt = strategy.TradeIfProfitable(
        cross(fast_alpha, slow_alpha), strategy.account.virtualMarket())

    avg_plus_real = strategy.TradeIfProfitable(cross(slow_alpha, fast_alpha),
                                               strategy.account.real())
    avg_minus_real = strategy.TradeIfProfitable(cross(fast_alpha, slow_alpha),
                                                strategy.account.real())

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(orderFactory=order.side_price.Limit(
                volume=constant(45))), "liquidity"),
        ctx.makeTrader_A(
            strategy.Signal(event.Every(constant(1.)),
                            order.side.Market(volume=constant(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()),
    ]
Example #8
0
def Signal(ctx):

    const = constant
    linear_signal = math.RandomWalk(initialValue=20,
                                    deltaDistr=const(-.1),
                                    name="20-0.1t")

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                event.Every(constant(1.)),
                order.side_price.Limit(volume=const(5))), "liquidity"),
        ctx.makeTrader_A(
            strategy.Signal(event.Every(constant(1.)),
                            order.side.Market(volume=const(1)), linear_signal),
            "signal_ex"),
    ]
Example #9
0
def RSI(ctx):

    const = constant
    linear_signal = math.RandomWalk(initialValue=20,
                                    deltaDistr=const(-.1),
                                    name="20-0.1t")

    one = const(1)

    threshold = 30

    demo = ctx.addGraph('demo')
    myVolume = lambda: [(trader.Position(), demo)]

    alpha = 1. / 14

    myRsiBis = lambda: [(orderbook.OfTrader().RSI(1, alpha).OnEveryDt(1), demo)
                        ]

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                event.Every(constant(1.)),
                order.side_price.Limit(volume=const(4))), "liquidity"),
        ctx.makeTrader_A(
            strategy.Signal(event.Every(constant(1.)), order.side.Market(),
                            linear_signal), "signal",
            [(linear_signal, ctx.amount_graph)]),
        ctx.makeTrader_A(
            strategy.RSI_linear(order.signedVolume.MarketSigned(),
                                alpha=alpha,
                                timeframe=1), "rsi_linear", myVolume()),
        ctx.makeTrader_A(
            strategy.RSIbis(event.Every(constant(1.)),
                            order.side.Market(one),
                            alpha=alpha,
                            timeframe=1,
                            threshold=threshold), "rsi_bis",
            myVolume() + myRsiBis()),
    ]
Example #10
0
def FundamentalValue(ctx):
    
    ctx.volumeStep = 30
    fv = 200

    demo = ctx.addGraph('demo')
    myVolume = lambda: [(trader.Position(), demo)]
    myPrice = lambda: [(orderbook.MidPrice(), demo)]

    return [
        ctx.makeTrader_A( 
            strategy.LiquidityProvider(
                        orderFactory = order.side_price.WithExpiry(const(10),
                            order.side_price.Limit(volume=const(6)))),
            "liquidity"),
    
        ctx.makeTrader_A(
             strategy.FundamentalValue(
                event.Every(const(1.)),
                order.side.Market(volume = const(1.)),
                const(fv)),
            "fv_200",
            myVolume()),
    ]
Example #11
0
def Orders(ctx):

    linear_signal = math.RandomWalk(initialValue=20,
                                    deltaDistr=const(-.1),
                                    name="20-0.1t")

    midPrice = orderbook.MidPrice(ctx.book_A)

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                event.Every(const(1.)),
                order.side_price.Limit(volume=const(35))), "liquidity"),
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                event.Every(const(100.)),
                order.side_price.StopLoss(
                    const(0.1), order.side_price.Limit(volume=const(5)))),
            "liquidity stoploss"),
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                event.Every(const(10.)),
                order.side_price.Iceberg(
                    order.side_price.Limit(volume=const(5)), const(1))),
            "liquidity iceberg"),
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                event.Every(const(10.)),
                order.side_price.WithExpiry(
                    const(5), order.side_price.Limit(volume=const(5)))),
            "liquidity expiry"),
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                event.Every(const(10.)),
                order.side_price.WithExpiry(
                    const(5),
                    order.side_price.Iceberg(
                        order.side_price.Limit(volume=const(15)), const(1)))),
            "liquidity iceberg expiry"),
        ctx.makeTrader_A(
            strategy.Signal(event.Every(const(1.)),
                            order.side.Market(volume=const(1)), linear_signal),
            "signal market"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(const(1.)),
                order.side.FloatingPrice(const(100.),
                                         order.side.price.Limit(const(1))),
                linear_signal), "signal floating"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(const(1.)),
                order.side.Iceberg(order.side.Limit(const(110), const(3)),
                                   const(1)), linear_signal),
            "signal iceberg"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(const(1.)),
                order.side.ImmediateOrCancel(
                    order.side.Limit(const(120), const(1))), linear_signal),
            "signal ioc"),
        ctx.makeTrader_A(
            strategy.Signal(event.Every(const(10.)),
                            order.side.Peg(order.side.price.Limit(const(1))),
                            linear_signal), "signal peg"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(const(1.)),
                order.side.StopLoss(const(0.1),
                                    order.side.Market(volume=const(1))),
                linear_signal), "signal stoploss"),
        ctx.makeTrader_A(
            strategy.Signal(event.Every(const(10.)),
                            order.side.Limit(price=midPrice, volume=const(1)),
                            linear_signal), "signal limit"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(const(10)),
                order.side.Limit(price=const(120), volume=const(1)),
                Interlacing()), "noise limit"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(const(10)),
                order.side.WithExpiry(
                    const(10),
                    order.side.Limit(price=const(120), volume=const(1))),
                Interlacing()), "noise expiry"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(const(10.)),
                order.side.Iceberg(
                    order.side.Peg(order.side.price.Limit(const(1))),
                    const(1)), Interlacing()), "iceberg peg"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(const(10.)),
                order.side.Peg(
                    order.side.price.Iceberg(order.side.price.Limit(const(3)),
                                             const(1))), Interlacing()),
            "peg iceberg"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(constant(3.)),
                order.side.WithExpiry(
                    constant(3.),
                    order.side.Peg(
                        order.side.price.Iceberg(
                            order.side.price.Limit(const(3)), const(1)))),
                Interlacing()), "peg iceberg expiry"),
        ctx.makeTrader_A(
            strategy.Signal(
                event.Every(constant(10.)),
                order.side.WithExpiry(
                    constant(10.),
                    order.side.Iceberg(
                        order.side.Peg(order.side.price.Limit(const(1))),
                        const(1))), Interlacing()), "iceberg peg expiry"),
    ]
Example #12
0
def Complete(ctx):

    ctx.volumeStep = 100

    c_200 = const(200.)

    fv_200_12 = strategy.FundamentalValue(
        fundamentalValue=c_200,
        orderFactory=order.side.Market(volume=const(12)))

    fv_200 = strategy.FundamentalValue(
        fundamentalValue=c_200,
        orderFactory=order.side.Market(volume=const(1)))

    def s_fv(fv):
        return strategy.TradeIfProfitable(
            strategy.FundamentalValue(
                fundamentalValue=const(fv),
                orderFactory=order.side.Market(volume=const(1))))

    def fv_virtual(fv):
        return ctx.makeTrader_A(s_fv(fv), "v" + str(fv))

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(
                orderFactory=order.side_price.WithExpiry(
                    constant(10), order.side_price.Limit(
                        volume=constant(170)))), "liquidity"),
        ctx.makeTrader_A(fv_200_12, "t200"),
        ctx.makeTrader_A(fv_200, "t200_1"),
        ctx.makeTrader_A(
            strategy.FundamentalValue(event.Every(constant(1.)),
                                      order.side.Market(const(1.)),
                                      fundamentalValue=const(150.)), "t150"),
        ctx.makeTrader_A(
            strategy.MeanReversion(event.Every(constant(1.)),
                                   order.side.Market(const(1.))), "mr_0_15"),
        ctx.makeTrader_A(strategy.CrossingAverages(event.Every(constant(1.)),
                                                   order.side.Market(
                                                       const(1.)),
                                                   ewma_alpha_1=0.15,
                                                   ewma_alpha_2=0.015),
                         label="avg+"),
        ctx.makeTrader_A(strategy.CrossingAverages(event.Every(constant(1.)),
                                                   order.side.Market(
                                                       const(1.)),
                                                   ewma_alpha_2=0.15,
                                                   ewma_alpha_1=0.015),
                         label="avg-"),
        ctx.makeTrader_A(strategy.TradeIfProfitable(fv_200), "v_fv200"),
        fv_virtual(160.),
        fv_virtual(170.),
        fv_virtual(180.),
        fv_virtual(190.),
        ctx.makeTrader_A(
            strategy.ChooseTheBest([
                s_fv(160.),
                s_fv(170.),
                s_fv(180.),
                s_fv(190.),
            ]), "best")
    ]
def MultiarmedBandit(ctx):

    ctx.volumeStep = 30

    demo = ctx.addGraph('demo')
    myVolume = lambda: [(trader.Position(), demo)]

    def fv(x):
        return strategy.FundamentalValue(
            event.Every(constant(1.)),
            order.side.Market(volume=constant(1.)),
            fundamentalValue=const(x))

    xs = range(100, 300, 50) + range(160, 190, 10)

    def strategies():
        return map(fv, xs)

    def fv_traders():
        return [ctx.makeTrader_A(fv(x), "fv" + str(x), myVolume()) for x in xs]

    return [
        ctx.makeTrader_A(
            strategy.LiquidityProvider(orderFactory=order.side_price.Limit(
                volume=constant(45))), "liquidity"),
        ctx.makeTrader_A(
            strategy.FundamentalValue(event.Every(constant(1.)),
                                      order.side.Market(volume=constant(12.)),
                                      fundamentalValue=const(200)),
            'fv 12-200'),
        ctx.makeTrader_A(
            strategy.MultiArmedBandit(
                strategies(), strategy.account.virtualMarket(),
                strategy.weight.efficiencyTrend()), 'virt trend', myVolume()),
        ctx.makeTrader_A(
            strategy.MultiArmedBandit(strategies(), strategy.account.real(),
                                      strategy.weight.efficiencyTrend()),
            'real trend', myVolume()),
        ctx.makeTrader_A(
            strategy.MultiArmedBandit(
                strategies(), strategy.account.virtualMarket(),
                strategy.weight.efficiency()), 'virt efficiency', myVolume()),
        ctx.makeTrader_A(
            strategy.MultiArmedBandit(strategies(), strategy.account.real(),
                                      strategy.weight.efficiency()),
            'real efficiency', myVolume()),
        ctx.makeTrader_A(
            strategy.MultiArmedBandit(
                strategies(), strategy.account.virtualMarket(),
                strategy.weight.score(), strategy.weight.atanPow()),
            'virt score', myVolume()),
        ctx.makeTrader_A(
            strategy.MultiArmedBandit(
                strategies(), strategy.account.real(), strategy.weight.score(),
                strategy.weight.clamp0()), 'real score', myVolume()),
        ctx.makeTrader_A(
            strategy.MultiArmedBandit(
                strategies(), strategy.account.virtualMarket(),
                strategy.weight.efficiencyTrend(), strategy.weight.identityF(),
                strategy.weight.chooseTheBest()), 'virt best', myVolume()),
        ctx.makeTrader_A(
            strategy.MultiArmedBandit(
                strategies(), strategy.account.real(), strategy.weight.unit(),
                strategy.weight.identityF()), 'uniform', myVolume()),
    ] + fv_traders()