Пример #1
0
def TrendFollower(ctx):

    V = 1
    alpha = 0.015
    ctx.volumeStep = 30

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

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

    return [
        ctx.makeTrader_A(strategy.price.LiquidityProvider().Strategy(
            orderFactory=order.side_price.Limit(
                volume=constant(V * 8)).sideprice_WithExpiry(constant(100))),
                         label="liquidity"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(constant(1.)), order.side.Market(const(V * 3))),
            "signal", [(linear_signal, ctx.amount_graph)]),
        ctx.makeTrader_A(
            strategy.side.TrendFollower(alpha).Strategy(
                event.Every(constant(1.)), order.side.Market(volume=const(V))),
            "trendfollower_ex", myVolume()),
    ]
Пример #2
0
def Ladder(ctx):

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

    return [
        ctx.makeTrader_A(
            strategy.price.LiquidityProvider().Strategy(
                orderFactory=order.side_price.Limit(
                    volume=const(10.)).sideprice_WithExpiry(const(50.))),
            "liquidity"),
        ctx.makeTrader_A(
            order.side_price.Limit(volume=const(1.))
            #                .sideprice_Iceberg(lotSize=const(1))
            .LadderMM(initialSize=10).LadderBalancer(maximalSize=10).StopLoss(
                lossFactor=constant(0.03)).Suspend(CurrentTime() < 30).Suspend(
                    (300 < CurrentTime()).And(CurrentTime() < 350)),
            "ladder mm"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(constant(1.)), order.side.Market(const(4))),
            "signal"),
    ]
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()),
    ]
Пример #4
0
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() / 3, demo),
                        (orderbook.OfTrader().MidPrice.EW(alpha).Avg.OnEveryDt(
                            1), demo),
                        (orderbook.OfTrader().Asks.BestPrice, demo),
                        (orderbook.OfTrader().Bids.BestPrice, demo)]

    return [
        ctx.makeTrader_A(strategy.price.LiquidityProvider(
            initialValue=30.).Strategy(orderFactory=order.side_price.Limit(
                volume=constant(V * 20)).sideprice_WithExpiry(constant(10))),
                         label="liquidity"),
        ctx.makeTrader_A(
            strategy.side.Noise().Strategy(
                event.Every(constant(1.)),
                order.side.Market(volume=constant(V * 3))), "signal",
            [(linear_signal, ctx.amount_graph)]),
        ctx.makeTrader_A(
            strategy.side.MeanReversion(alpha).Strategy(
                event.Every(constant(1.)),
                order.side.Market(volume=constant(V))), "meanreversion_ex",
            myVolume()),
    ]
Пример #5
0
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()),
    ]
Пример #6
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()),
    ]
Пример #7
0
def Signal(ctx):

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

    return [
        ctx.makeTrader_A(
            strategy.price.LiquidityProvider().Strategy(
                event.Every(constant(1.)),
                order.side_price.Limit(volume=const(5))), "liquidity"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(constant(1.)), order.side.Market(const(1))),
            "signal"),
    ]
Пример #8
0
def ChooseTheBest(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(alpha).Avg.
                                OnEveryDt(1), demo)]

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

    def strategies():
        return [cross(slow_alpha, fast_alpha), cross(fast_alpha, slow_alpha)]

    return [
        ctx.makeTrader_A(
            strategy.price.LiquidityProvider().Strategy(
                orderFactory=order.side_price.Limit(volume=constant(45))),
            "liquidity"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(constant(1.)),
                order.side.Market(volume=constant(20))), "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(
            strategy.ChooseTheBest(strategies(),
                                   strategy.account.virtualMarket()),
            'best virt', myVolume()),
        ctx.makeTrader_A(
            strategy.ChooseTheBest(strategies(), strategy.account.real()),
            'best real', myVolume()),
    ]
Пример #9
0
def Dependency(ctx):

    V = 1
    alpha = 0.015
    ctx.volumeStep = 30
    const = constant

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

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

    return [
            ctx.makeTrader_A(
                strategy.price.LiquidityProvider(100.)
                              .Strategy(orderFactory =
                                            order.side_price.Limit(volume=constant(V*8)).sideprice_WithExpiry(constant(100))),
                             label="liquidity"),

            ctx.makeTrader_B(
                strategy.price.LiquidityProvider(200.)
                              .Strategy(orderFactory =
                                            order.side_price.Limit(volume=constant(V*8)).sideprice_WithExpiry(constant(100))),
                             label="liquidity B"),

            ctx.makeTrader_A(strategy.side.Signal(linear_signal)
                                          .Strategy(event.Every(constant(1.)),
                                                    order.side.Market(const(V*3))),
                            "signal",
                            [
                             (linear_signal, ctx.amount_graph),
                            ]),

            ctx.makeTrader_B(
                strategy.side.PairTrading(
                    ctx.book_A,
                    factor=2.
                ).Strategy(event.Every(constant(1.)),
                           order.side.Market(const(V*5))),
                "B dependent on A ex"),
    ]
Пример #10
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()),
    ]
Пример #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.price.LiquidityProvider().Strategy(
                event.Every(const(1.)),
                order.side_price.Limit(volume=const(35))), "liquidity"),
        ctx.makeTrader_A(
            strategy.price.LiquidityProvider().Strategy(
                event.Every(const(100.)),
                order.side_price.Limit(volume=const(5)).sideprice_StopLoss(
                    const(0.1))), "liquidity stoploss"),
        ctx.makeTrader_A(
            strategy.price.LiquidityProvider().Strategy(
                event.Every(const(10.)),
                order.side_price.Limit(volume=const(5)).sideprice_Iceberg(
                    const(1))), "liquidity iceberg"),
        ctx.makeTrader_A(
            strategy.price.LiquidityProvider().Strategy(
                event.Every(const(10.)),
                order.side_price.Limit(volume=const(5)).sideprice_WithExpiry(
                    const(5))), "liquidity expiry"),
        ctx.makeTrader_A(
            strategy.price.LiquidityProvider().Strategy(
                event.Every(const(10.)),
                order.side_price.Limit(volume=const(15)).sideprice_Iceberg(
                    const(1)).sideprice_WithExpiry(const(5))),
            "liquidity iceberg expiry"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(const(1.)), order.side.Market(volume=const(1))),
            "signal market"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(const(1.)),
                order.side.price.Limit(const(1)).side_FloatingPrice(
                    const(100))), "signal floating"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(const(1.)),
                order.side.Limit(const(110), const(3)).side_Iceberg(const(1))),
            "signal iceberg"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(const(1.)),
                order.side.Limit(const(120), const(1)).side_ImmediateOrCancel),
            "signal ioc"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(const(10.)),
                order.side.price.Limit(const(1)).side_Peg), "signal peg"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(const(1.)),
                order.side.Market(volume=const(1)).side_StopLoss(const(0.1))),
            "signal stoploss"),
        ctx.makeTrader_A(
            strategy.side.Signal(linear_signal).Strategy(
                event.Every(const(10.)),
                order.side.Limit(price=midPrice, volume=const(1))),
            "signal limit"),
        ctx.makeTrader_A(
            strategy.side.Signal(Interlacing()).Strategy(
                event.Every(const(10)),
                order.side.Limit(price=const(120), volume=const(1))),
            "noise limit"),
        ctx.makeTrader_A(
            strategy.side.Signal(Interlacing()).Strategy(
                event.Every(const(10)),
                order.side.Limit(price=const(120),
                                 volume=const(1)).side_WithExpiry(const(10))),
            "noise expiry"),
        ctx.makeTrader_A(
            strategy.side.Signal(Interlacing()).Strategy(
                event.Every(const(10.)),
                order.side.price.Limit(const(1)).side_Peg.side_Iceberg(
                    const(1))), "iceberg peg"),
        ctx.makeTrader_A(
            strategy.side.Signal(Interlacing()).Strategy(
                event.Every(const(10.)),
                order.side.price.Limit(const(3)).side_price_Iceberg(
                    const(1)).side_Peg), "peg iceberg"),
        ctx.makeTrader_A(
            strategy.side.Signal(Interlacing()).Strategy(
                event.Every(constant(3.)),
                order.side.price.Limit(const(3)).side_price_Iceberg(
                    const(1)).side_Peg.side_WithExpiry(const(3))),
            "peg iceberg expiry"),
        ctx.makeTrader_A(
            strategy.side.Signal(Interlacing()).Strategy(
                event.Every(constant(10.)),
                order.side.price.Limit(const(1)).side_Peg.side_Iceberg(
                    const(1)).side_WithExpiry(const(10.))),
            "iceberg peg expiry"),
    ]