Upgrade to Pro — share decks privately, control downloads, hide ads and more …

BBGO 2022 - Modern Crypto Trading Bot

Yo-An Lin
December 05, 2022

BBGO 2022 - Modern Crypto Trading Bot

Yo-An Lin

December 05, 2022
Tweet

More Decks by Yo-An Lin

Other Decks in Technology

Transcript

  1. Yo-an Lin 2022
    BBGO v2022
    Overview, Design and Strategy Development

    View full-size slide

  2. Design Concept
    • Simple, Simple and Simple.

    • Easy to Use and Develop.

    • Single Binary To Deliver, Install, Use.

    View full-size slide

  3. Requirement/Features
    • Multi-session support.

    • Easy to extend indicators.

    • Multi-session x Multi-strategy in one single process.

    • Ability to develop private strategy.

    • Back-testing with KLine Data (candlestick data)

    • Grid Search Optimizer

    View full-size slide

  4. Architecture

    View full-size slide

  5. Architecture
    Single BBGO Process
    Binance Session
    Strategy #1
    Strategy #2
    Cross Exchange Strategy #1
    Cross Exchange Strategy #2
    Binance Spot Session
    Binance Session
    FTX Session
    Binance Isolated Margin Session
    Binance Cross Margin Session
    Binance Isolated Margin Session
    Binance Session
    MAX Session
    Binance Session
    MAX Margin Session
    Stream
    MarketDataStream
    UserDataStream
    Stream
    MarketDataStream
    UserDataStream
    Stream
    MarketDataStream
    UserDataStream
    Stream
    MarketDataStream
    UserDataStream

    View full-size slide

  6. Exchange Session & Strategy
    Binance Spot Session
    MarketDataStream
    UserDataStream Market Info
    Standard Indicators
    Market Data Store
    Trade Store
    Account Information
    Margin Related Info
    Strategy #1
    Bind Callbacks Bind Callbacks Access
    Common
    Exchange
    API
    Send Request

    View full-size slide

  7. ExchangeSession & API Server
    Binance Spot Session
    MarketDataStream
    UserDataStream
    Common Exchange API
    Binance API Server
    WebSocket
    WebSocket
    RESTful API
    *bbgo.ExchangeSession

    View full-size slide

  8. How To Run BBGO?

    View full-size slide

  9. All you need are just 3
    fi
    les

    View full-size slide

  10. bbgo

    bbgo.yaml

    .env.local

    View full-size slide

  11. bbgo

    bbgo.yaml

    .env.local
    Binary
    Con
    fi
    g File
    Credential

    View full-size slide

  12. exchangeStrategies:


    - on: binance


    grid:


    symbol: BTCUSDT


    quantity: 0.001


    gridNumber: 20


    profitSpread: 1000.0


    upperPrice: 30_000.0


    lowerPrice: 28_000.0
    bbgo.yaml

    View full-size slide

  13. DB_DRIVER=mysql

    DB_DSN=root:123123@tcp(127.0.0.1:3306)/bbgo

    MAX_API_KEY=….

    MAX_API_SECRET=….

    BINANCE_API_KEY=….

    BINANCE_API_SECRET=….
    .env.local

    View full-size slide

  14. $ bbgo backtest

    View full-size slide

  15. Strategy Skeleton

    View full-size slide

  16. Strategy Skeleton
    Strategy Struct
    package pivotshort


    type Strategy struct {


    Symbol string `json:"symbol"`


    }
    pkg/strategy/pivotshort/strategy.go

    View full-size slide

  17. Strategy Skeleton
    Registering Strategy
    package pivotshort


    const ID = "pivotshort"


    func init() {


    bbgo.RegisterStrategy(ID, &Strategy{})


    }

    View full-size slide

  18. Strategy Skeleton
    Main Strategy Methods
    • ID() — returns the strategy ID.

    • InstanceID() — returns the instance ID of the strategy.

    • Subscribe(session) — for adding the market data subscriptions.

    • Run() — the core strategy logics.

    View full-size slide

  19. Strategy Skeleton
    ID methods
    const ID = "pivotshort"


    func (s *Strategy) ID() string {


    return ID


    }


    func (s *Strategy) InstanceID() string {


    return ID + ":" + s.Symbol


    }

    View full-size slide

  20. Strategy Skeleton
    Subscribe
    func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {


    session.Subscribe(types.KLineChannel,


    s.Symbol,


    types.SubscribeOptions{Interval: "1m"})


    }

    View full-size slide

  21. Strategy Skeleton
    Run()
    func (s *Strategy) Run(ctx context.Context,


    orderExecutor bbgo.OrderExecutor,


    session *bbgo.ExchangeSession) error {


    return nil


    }

    View full-size slide

  22. Strategy Skeleton
    Binding KLine Closed Event
    session.MarketDataStream.OnKLineClosed(


    types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {


    bbgo.Notify(kline)


    }))

    View full-size slide

  23. Strategy Skeleton
    Con
    fi
    guration
    exchangeStrategies:


    - on: binance


    pivotshort:


    symbol: ETHUSDT


    interval: 5m


    window: 200
    bbgo.yaml

    View full-size slide

  24. Configuration Decoding

    View full-size slide

  25. Configuration Decoding
    Built-in Types For Unmarshalling Data
    import (


    "github.com/c9s/bbgo/pkg/fixedpoint"


    "github.com/c9s/bbgo/pkg/types"


    )


    type Strategy struct {


    Interval types.Interval `json:"interval"`


    Partial fixedpoint.Value `json:"partial"`


    Duration types.Duration `json:"duration"`


    Quantity fixedpoint.Value `json:"quantity"`


    }


    View full-size slide

  26. Configuration Decoding
    Con
    fi
    guration Sample
    exchangeStrategies:


    - on: binance


    pivotshort:


    symbol: ETHUSDT


    interval: 5m


    partial: 50%


    quantity: 0.01


    duration: 1h30m
    bbgo.yaml

    View full-size slide

  27. Configuration Decoding
    A complex example
    exchangeStrategies:


    - on: binance


    pivotshort:


    symbol: ETHUSDT


    interval: 5m


    window: 200


    breakLow:


    ratio: 0%


    quantity: 10.0


    marketOrder: true


    stopEMARange: 2%


    stopEMA:


    interval: 1h


    window: 99


    resistanceShort:


    enabled: true


    interval: 5m


    window: 80


    quantity: 10.0


    minDistance: 5%


    groupDistance: 1%


    ratio: 1.2%


    numOfLayers: 3


    layerSpread: 0.5%


    exits:


    - roiStopLoss:


    percentage: 0.8%


    - roiTakeProfit:


    percentage: 35%


    - protectiveStopLoss:


    activationRatio: 0.6%


    stopLossRatio: 0.1%


    placeStopOrder: false


    - protectiveStopLoss:


    activationRatio: 5%


    stopLossRatio: 1%


    placeStopOrder: false


    - lowerShadowTakeProfit:


    interval: 30m


    window: 99


    ratio: 3%


    - cumulatedVolumeTakeProfit:


    interval: 5m


    window: 2


    minQuoteVolume: 200_000_000


    View full-size slide

  28. What’s Position?
    *types.Position
    • Position manages your average cost, base quantity and quote quantity.

    • When you buy 1 BTC at 19300, your average cost = 19300, base = 1, quote =
    -19300

    • Then you sell 0.5 BTC at 20000, your average cost = 19300, base = 0.5,
    quote = -9300

    • Then you buy 0.5 BTC at 18000, your average cost = 18650, base = 1, quote
    = -18300

    View full-size slide

  29. Types of Position
    Long Position and Short Position
    • When you hold Long position, it means you hold positive base quantity.

    • You spent quote currency (-19000 USDT) and increase base quantity (+1 BTC)

    • When you hold Short position, it means you hold negative base quantity.

    • You spent base currency (-1 BTC) and increase quote quantity (+19000 USDT)

    • The base asset borrow in the spot margin trading.

    • Or, Sell Short in the futures contract.

    • Or, if you hold some inventory with zero cost basis, you can sell these inventory
    to hold a short position.

    View full-size slide

  30. position := types.NewPositionFromMarket(market)


    profit, netProfit, madeProfit := position.AddTrade(Trade{


    Exchange: ExchangeBinance,


    Price: fixedpoint.NewFromInt(2000),


    Quantity: fixedpoint.NewFromInt(10),


    QuoteQuantity: fixedpoint.NewFromInt(2000 * 10),


    Symbol: "BTCUSDT",


    Side: SideTypeBuy,


    Fee: fixedpoint.NewFromInt(2000 * 10.0).Mul(feeRate).Div(bnbPrice),


    FeeCurrency: "BNB",


    })

    View full-size slide

  31. Order / Trade Management

    View full-size slide

  32. Order / Trade Management
    Tracking Orders and Trade
    • Di
    ff
    erent Strategies shares the same user data stream for each exchange.

    • The strategy itself needs to know which trade belongs to its sent orders.

    • *bbgo.TradeCollector helps you collect the trades of your sent orders.

    • *types.Position will be updated by *bbgo.TradeCollector.

    • *bbgo.ActiveOrderBook helps you manage the open orders.

    • Each strategy can manage its own position and active orders.

    View full-size slide

  33. Order / Trade Management
    *bbgo.TradeCollector
    • When sending orders to exchange, you register the returned order ID to the
    trade collector.

    • TradeCollector receives the trades from the user data stream and update the
    position.

    • Updating position also generates the pro
    fi
    t information.

    View full-size slide

  34. Order / Trade Management
    *bbgo.ActiveOrderBook
    activeOrderBook := bbgo.NewActiveOrderBook(symbol)


    activeOrderBook.BindStream(session.UserDataStream)

    View full-size slide

  35. Order / Trade Management
    *bbgo.ActiveOrderBook
    createdOrders, err := session.Exchange.SubmitOrders(ctx, types.SubmitOrder{


    Symbol: symbol,


    Side: types.SideTypeBuy,


    Type: types.OrderTypeLimit,


    Quantity: fixedpoint.NewFromFloat(10.0),


    Price: fixedpoint.NewFromFloat(19000.0),


    Tag: "short",


    })


    activeOrderBook.Add(createdOrders...)

    View full-size slide

  36. Order / Trade Management
    *bbgo.TradeCollector
    symbol := "BTCUSDT"


    position := types.NewPosition(symbol, "BTC", "USDT")


    orderStore := bbgo.NewOrderStore(symbol)


    tradeCollector := bbgo.NewTradeCollector(symbol, position, orderStore)


    tradeCollector.BindStream(session.UserDataStream)

    View full-size slide

  37. Order / Trade Management
    *bbgo.TradeCollector
    tradeCollector.OnPositionUpdate(func(position *types.Position) {


    bbgo.Notify(position)


    })


    tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {


    bbgo.Notify(trade, profit)


    })

    View full-size slide

  38. Order / Trade Management
    *bbgo.GeneralOrderExecutor
    s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)


    s.orderExecutor.BindProfitStats(s.ProfitStats)


    s.orderExecutor.BindTradeStats(s.TradeStats)


    s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {


    bbgo.Sync(s)


    })


    s.orderExecutor.Bind()

    View full-size slide

  39. Exit Methods

    View full-size slide

  40. Exit Methods
    • ROIStopLoss

    • ROITakePro
    fi
    t

    • TrailingStop

    • ProtectiveStop

    • CumulatedVolumeTakePro
    fi
    t

    • LowerShadowTakePro
    fi
    t

    View full-size slide

  41. Exit Methods
    type Strategy struct {


    ExitMethods bbgo.ExitMethodSet `json:"exits"`


    }

    func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {


    s.ExitMethods.SetAndSubscribe(session, s)


    }



    func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session
    *bbgo.ExchangeSession) error {



    for _, method := range s.ExitMethods {


    method.Bind(session, s.orderExecutor)


    }


    }


    View full-size slide

  42. exits:


    # (0) roiStopLoss is the stop loss percentage of the position ROI (currently the price change)


    - roiStopLoss:


    percentage: 0.8%


    # (1) roiTakeProfit is used to force taking profit by percentage of the position ROI (currently the price change)


    # force to take the profit ROI exceeded the percentage.


    - roiTakeProfit:


    percentage: 35%


    # (2) protective stop loss -- short term


    - protectiveStopLoss:


    activationRatio: 0.6%


    stopLossRatio: 0.1%


    placeStopOrder: false


    # (3) protective stop loss -- long term


    - protectiveStopLoss:


    activationRatio: 5%


    stopLossRatio: 1%


    placeStopOrder: false


    View full-size slide

  43. Back-Testing

    View full-size slide

  44. Back-Testing
    Con
    fi
    guration
    backtest:


    sessions:


    - binance


    startTime: "2022-01-01"


    endTime: "2022-01-30"


    symbols:


    - ETHUSDT

    View full-size slide

  45. Back-Testing
    Con
    fi
    guration
    backtest:


    sessions:


    - binance


    startTime: "2022-01-01"


    endTime: "2022-01-30"


    symbols:


    - ETHUSDT


    accounts:


    binance:


    balances:


    ETH: 10.0


    USDT: 5000.0


    View full-size slide

  46. Back-Testing
    Con
    fi
    guration
    backtest:


    sessions:


    - binance


    startTime: "2022-01-01"


    endTime: "now"


    symbols:


    - ETHUSDT


    accounts:


    binance:


    balances:


    ETH: 10.0


    USDT: 5000.0


    View full-size slide

  47. Back-Testing
    Con
    fi
    guration
    backtest:


    sessions:


    - binance


    startTime: "2022-01-01"


    endTime: "yesterday"


    symbols:


    - ETHUSDT


    accounts:


    binance:


    balances:


    ETH: 10.0


    USDT: 5000.0


    View full-size slide

  48. Back-Testing
    Run back-test
    $ bbgo backtest

    View full-size slide

  49. Back-Testing
    Run back-test
    $ bbgo backtest \

    --con
    fi
    g pivotshort-ETHUSDT.yaml \

    --debug \

    --output output --subdir

    View full-size slide

  50. Back-Testing
    back-test report app
    $ cd apps/backtest-report && yarn dev

    View full-size slide

  51. Back-Testing
    Back-Test Report

    View full-size slide

  52. Back-Testing
    Back-Test Report

    View full-size slide

  53. Back-Test Report Details

    View full-size slide

  54. symbol: ETHUSDT

    winningRatio: "0.94736842"

    numOfLossTrade: 19

    numOfPro
    fi
    tTrade: 18

    grossPro
    fi
    t: "30171.32449936"

    grossLoss: "-3515.41900049"

    pro
    fi
    ts:

    - "1.59999990"

    - "11586.99999990"

    - "20.60000000"

    - "16.89999999"

    - "10.39999989"

    - "12.00000000"

    - "30.70000000"

    - "276.80000000"

    - "267.10000000"

    - "205.80000000"

    - "26.29999999"

    - "24.60000000"

    - "29.90000000"

    - "10716.39999990"

    - "146.24999989"

    - "10.01649999"

    - "175.20000000"

    - "6613.75799990"

    losses:

    - "-36.58000000"

    - "-202.20000000"

    - "-208.20000000"

    - "-203.90000000"

    - "-239.30000000"

    - "-220.30000000"

    - "-6.49700000"

    - "-270.10000010"

    - "-267.20000000"

    - "-248.70000000"

    - "-236.50000000"

    - "-9.19999999"

    - "-268.89999999"

    - "-299.80000000"

    - "-214.03850010"

    - "-170.08900000"

    - "-159.00000000"

    - "-5.38250000"

    - "-249.53200030"

    mostPro
    fi
    tableTrade: "11586.99999990"

    mostLossTrade: "-299.80000000"

    pro
    fi
    tFactor: "8.58256853"

    totalNetPro
    fi
    t: "26655.90549887"
    Initial deposit: 10 ETH, 5000 USDT ~= 43000 USD equity

    Performance: +61% (no leverage)

    View full-size slide

  55. symbol: ETHUSDT

    winningRatio: "0.94736842"

    numOfLossTrade: 19

    numOfPro
    fi
    tTrade: 18

    grossPro
    fi
    t: "30171.32449936"

    grossLoss: "-3515.41900049"

    pro
    fi
    ts:

    - "1.59999990"

    - "11586.99999990"

    - "20.60000000"

    - "16.89999999"

    - "10.39999989"

    - "12.00000000"

    - "30.70000000"

    - "276.80000000"

    - "267.10000000"

    - "205.80000000"

    - "26.29999999"

    - "24.60000000"

    - "29.90000000"

    - "10716.39999990"

    - "146.24999989"

    - "10.01649999"

    - "175.20000000"

    - "6613.75799990"

    losses:

    - "-36.58000000"

    - "-202.20000000"

    - "-208.20000000"

    - "-203.90000000"

    - "-239.30000000"

    - "-220.30000000"

    - "-6.49700000"

    - "-270.10000010"

    - "-267.20000000"

    - "-248.70000000"

    - "-236.50000000"

    - "-9.19999999"

    - "-268.89999999"

    - "-299.80000000"

    - "-214.03850010"

    - "-170.08900000"

    - "-159.00000000"

    - "-5.38250000"

    - "-249.53200030"

    mostPro
    fi
    tableTrade: "11586.99999990"

    mostLossTrade: "-299.80000000"

    pro
    fi
    tFactor: "8.58256853"

    totalNetPro
    fi
    t: "26655.90549887"
    Pro
    fi
    t Loss Details

    View full-size slide

  56. The win/loss ratio is your wins divided by
    your losses. In the example, suppose for the
    sake of simplicity that 60 trades were winners,
    and 40 were losers. Your win/loss ratio would
    be 60/40 = 1.5. That would mean that you are
    winning 50% more often than you are losing.
    symbol: ETHUSDT

    winningRatio: "0.94736842"

    numOfLossTrade: 19

    numOfPro
    fi
    tTrade: 18

    grossPro
    fi
    t: "30171.32449936"

    grossLoss: "-3515.41900049"

    pro
    fi
    ts:

    - "1.59999990"

    - "11586.99999990"

    - "20.60000000"

    - "16.89999999"

    - "10.39999989"

    - "12.00000000"

    - "30.70000000"

    - "276.80000000"

    - "267.10000000"

    - "205.80000000"

    - "26.29999999"

    - "24.60000000"

    - "29.90000000"

    - "10716.39999990"

    - "146.24999989"

    - "10.01649999"

    - "175.20000000"

    - "6613.75799990"

    losses:

    - "-36.58000000"

    - "-202.20000000"

    - "-208.20000000"

    - "-203.90000000"

    - "-239.30000000"

    - "-220.30000000"

    - "-6.49700000"

    - "-270.10000010"

    - "-267.20000000"

    - "-248.70000000"

    - "-236.50000000"

    - "-9.19999999"

    - "-268.89999999"

    - "-299.80000000"

    - "-214.03850010"

    - "-170.08900000"

    - "-159.00000000"

    - "-5.38250000"

    - "-249.53200030"

    mostPro
    fi
    tableTrade: "11586.99999990"

    mostLossTrade: "-299.80000000"

    pro
    fi
    tFactor: "8.58256853"

    totalNetPro
    fi
    t: "26655.90549887"

    View full-size slide

  57. Gross pro
    fi
    t — the sum of all pro
    fi
    table trades
    in terms of money;

    Gross loss — the sum of all unpro
    fi
    table trades
    in terms of money;
    symbol: ETHUSDT

    winningRatio: "0.94736842"

    numOfLossTrade: 19

    numOfPro
    fi
    tTrade: 18

    grossPro
    fi
    t: "30171.32449936"

    grossLoss: "-3515.41900049"

    pro
    fi
    ts:

    - "1.59999990"

    - "11586.99999990"

    - "20.60000000"

    - "16.89999999"

    - "10.39999989"

    - "12.00000000"

    - "30.70000000"

    - "276.80000000"

    - "267.10000000"

    - "205.80000000"

    - "26.29999999"

    - "24.60000000"

    - "29.90000000"

    - "10716.39999990"

    - "146.24999989"

    - "10.01649999"

    - "175.20000000"

    - "6613.75799990"

    losses:

    - "-36.58000000"

    - "-202.20000000"

    - "-208.20000000"

    - "-203.90000000"

    - "-239.30000000"

    - "-220.30000000"

    - "-6.49700000"

    - "-270.10000010"

    - "-267.20000000"

    - "-248.70000000"

    - "-236.50000000"

    - "-9.19999999"

    - "-268.89999999"

    - "-299.80000000"

    - "-214.03850010"

    - "-170.08900000"

    - "-159.00000000"

    - "-5.38250000"

    - "-249.53200030"

    mostPro
    fi
    tableTrade: "11586.99999990"

    mostLossTrade: "-299.80000000"

    pro
    fi
    tFactor: "8.58256853"

    totalNetPro
    fi
    t: "26655.90549887"

    View full-size slide

  58. Pro
    fi
    t Factor—The ratio between gross pro
    fi
    t
    and gross loss in per cents. The one value
    means that pro
    fi
    t equals to loss
    symbol: ETHUSDT

    winningRatio: "0.94736842"

    numOfLossTrade: 19

    numOfPro
    fi
    tTrade: 18

    grossPro
    fi
    t: "30171.32449936"

    grossLoss: "-3515.41900049"

    pro
    fi
    ts:

    - "1.59999990"

    - "11586.99999990"

    - "20.60000000"

    - "16.89999999"

    - "10.39999989"

    - "12.00000000"

    - "30.70000000"

    - "276.80000000"

    - "267.10000000"

    - "205.80000000"

    - "26.29999999"

    - "24.60000000"

    - "29.90000000"

    - "10716.39999990"

    - "146.24999989"

    - "10.01649999"

    - "175.20000000"

    - "6613.75799990"

    losses:

    - "-36.58000000"

    - "-202.20000000"

    - "-208.20000000"

    - "-203.90000000"

    - "-239.30000000"

    - "-220.30000000"

    - "-6.49700000"

    - "-270.10000010"

    - "-267.20000000"

    - "-248.70000000"

    - "-236.50000000"

    - "-9.19999999"

    - "-268.89999999"

    - "-299.80000000"

    - "-214.03850010"

    - "-170.08900000"

    - "-159.00000000"

    - "-5.38250000"

    - "-249.53200030"

    mostPro
    fi
    tableTrade: "11586.99999990"

    mostLossTrade: "-299.80000000"

    pro
    fi
    tFactor: "8.58256853"

    totalNetPro
    fi
    t: "26655.90549887"

    View full-size slide

  59. Optimization

    View full-size slide

  60. Optimization
    Optimizing the strategy parameters
    • A good strategy might have many parameters for di
    ff
    erent scenarios.

    • It’s hard to try the parameters one by one.

    • There are too many combinations.

    • The optimizer helps you
    fi
    nd the best combination.

    • BBGO uses JSON patch syntax to de
    fi
    ne the optimization.

    • You can optimize ANYTHING de
    fi
    ned in the con
    fi
    g
    fi
    le.

    View full-size slide

  61. Optimization
    De
    fi
    ning the optimization ranges
    ---


    executor:


    type: local


    local:


    maxNumberOfProcesses: 10


    matrix:


    - type: iterate


    label: interval


    path: '/exchangeStrategies/0/pivotshort/interval'


    values: [ "1m", "5m", "30m" ]


    - type: range


    path: '/exchangeStrategies/0/pivotshort/window'


    label: window


    min: 100.0


    max: 200.0


    step: 20.0

    View full-size slide

  62. Built-in Strategies
    • supertrend — using super trend indicator to entry and exit.

    • ewo — elliot wave oscillator.

    • bollmaker - bollinger band maker strategy.

    • fmaker - factor-based maker (linear regression)

    • xmaker - market making strategy

    • pivotshort - shorting focus strategy

    View full-size slide