Share
Embed
Transcript
64 slides
-
Design Concept • Simple, Simple and Simple. • Easy to
Use and Develop. • Single Binary To Deliver, Install, Use. -
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 -
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 -
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 -
ExchangeSession & API Server Binance Spot Session MarketDataStream UserDataStream Common
Exchange API Binance API Server WebSocket WebSocket RESTful API *bbgo.ExchangeSession -
exchangeStrategies: - on: binance grid: symbol: BTCUSDT quantity: 0.001 gridNumber:
20 profitSpread: 1000.0 upperPrice: 30_000.0 lowerPrice: 28_000.0 bbgo.yaml -
Strategy Skeleton Strategy Struct package pivotshort type Strategy struct {
Symbol string `json:"symbol"` } pkg/strategy/pivotshort/strategy.go -
Strategy Skeleton Registering Strategy package pivotshort const ID = "pivotshort"
func init() { bbgo.RegisterStrategy(ID, &Strategy{}) } -
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. -
Strategy Skeleton ID methods const ID = "pivotshort" func (s
*Strategy) ID() string { return ID } func (s *Strategy) InstanceID() string { return ID + ":" + s.Symbol } -
Strategy Skeleton Subscribe func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { session.Subscribe(types.KLineChannel,
s.Symbol, types.SubscribeOptions{Interval: "1m"}) } -
Strategy Skeleton Run() func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor,
session *bbgo.ExchangeSession) error { return nil } -
Strategy Skeleton Binding KLine Closed Event session.MarketDataStream.OnKLineClosed( types.KLineWith(s.Symbol, s.Interval, func(kline
types.KLine) { bbgo.Notify(kline) })) -
Strategy Skeleton Con fi guration exchangeStrategies: - on: binance pivotshort:
symbol: ETHUSDT interval: 5m window: 200 bbgo.yaml -
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"` } -
Configuration Decoding Con fi guration Sample exchangeStrategies: - on: binance
pivotshort: symbol: ETHUSDT interval: 5m partial: 50% quantity: 0.01 duration: 1h30m bbgo.yaml -
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 -
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 -
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. -
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", }) -
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. -
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. -
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...) -
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) -
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) }) -
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() -
Exit Methods • ROIStopLoss • ROITakePro fi t • TrailingStop
• ProtectiveStop • CumulatedVolumeTakePro fi t • LowerShadowTakePro fi t -
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) } } -
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 -
Back-Testing Con fi guration backtest: sessions: - binance startTime: "2022-01-01"
endTime: "2022-01-30" symbols: - ETHUSDT -
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 -
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 -
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 -
Back-Testing Run back-test $ bbgo backtest \ --con fi g
pivotshort-ETHUSDT.yaml \ --debug \ --output output --subdir -
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) -
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 -
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" -
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" -
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" -
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. -
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 -
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