github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/ws/public-feed-ingest/main.go (about) 1 package main 2 3 import ( 4 "log" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/book" 7 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/candle" 8 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/event" 9 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/status" 10 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/ticker" 11 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/trades" 12 "github.com/bitfinexcom/bitfinex-api-go/pkg/mux" 13 ) 14 15 func main() { 16 m := mux. 17 New(). 18 TransformRaw(). 19 Start() 20 21 pairs := []string{ 22 "BTCUSD", 23 "BTCUST", 24 "BTCXCH", 25 "ETHBTC", 26 "ETHEUR", 27 "ETHGBP", 28 "ETHJPY", 29 "ETHUSD", 30 "ETHUST", 31 } 32 33 for _, pair := range pairs { 34 tradePld := event.Subscribe{ 35 Event: "subscribe", 36 Channel: "trades", 37 Symbol: "t" + pair, 38 } 39 40 tickPld := event.Subscribe{ 41 Event: "subscribe", 42 Channel: "ticker", 43 Symbol: "t" + pair, 44 } 45 46 candlesPld := event.Subscribe{ 47 Event: "subscribe", 48 Channel: "candles", 49 Key: "trade:1m:t" + pair, 50 } 51 52 rawBookPld := event.Subscribe{ 53 Event: "subscribe", 54 Channel: "book", 55 Precision: "R0", 56 Symbol: "t" + pair, 57 } 58 59 bookPld := event.Subscribe{ 60 Event: "subscribe", 61 Channel: "book", 62 Precision: "P0", 63 Frequency: "F0", 64 Symbol: "t" + pair, 65 } 66 67 m.Subscribe(tradePld) 68 m.Subscribe(tickPld) 69 m.Subscribe(candlesPld) 70 m.Subscribe(rawBookPld) 71 m.Subscribe(bookPld) 72 } 73 74 derivStatusPld := event.Subscribe{ 75 Event: "subscribe", 76 Channel: "status", 77 Key: "deriv:tBTCF0:USTF0", 78 } 79 80 liqStatusPld := event.Subscribe{ 81 Event: "subscribe", 82 Channel: "status", 83 Key: "liq:global", 84 } 85 86 fundingPairTrade := event.Subscribe{ 87 Event: "subscribe", 88 Channel: "trades", 89 Symbol: "fUSD", 90 } 91 92 m.Subscribe(derivStatusPld) 93 m.Subscribe(liqStatusPld) 94 m.Subscribe(fundingPairTrade) 95 96 crash := make(chan error) 97 98 go func() { 99 // if listener will fail, program will exit by passing error to crash channel 100 crash <- m.Listen(func(msg interface{}, err error) { 101 if err != nil { 102 log.Printf("non crucial error received: %s\n", err) 103 } 104 105 switch v := msg.(type) { 106 case event.Info: 107 log.Printf("%T: %+v\n", v, v) 108 case trades.TradeSnapshot: 109 log.Printf("%T: %+v\n", v, v) 110 case trades.FundingTradeSnapshot: 111 log.Printf("%T: %+v\n", v, v) 112 case trades.TradeExecutionUpdate: 113 log.Printf("%T: %+v\n", v, v) 114 case trades.TradeExecuted: 115 log.Printf("%T: %+v\n", v, v) 116 case trades.FundingTradeExecutionUpdate: 117 log.Printf("%T: %+v\n", v, v) 118 case trades.FundingTradeExecuted: 119 log.Printf("%T: %+v\n", v, v) 120 case *ticker.Ticker: 121 log.Printf("%T: %+v\n", v, v) 122 case *ticker.Snapshot: 123 log.Printf("%T: %+v\n", v, v) 124 case *book.Book: 125 log.Printf("%T: %+v\n", v, v) 126 case *book.Snapshot: 127 log.Printf("%T: %+v\n", v, v) 128 case *candle.Candle: 129 log.Printf("%T: %+v\n", v, v) 130 case *candle.Snapshot: 131 log.Printf("%T: %+v\n", v, v) 132 case *status.Derivative: 133 log.Printf("%T: %+v\n", v, v) 134 case *status.DerivativesSnapshot: 135 log.Printf("%T: %+v\n", v, v) 136 case *status.Liquidation: 137 log.Printf("%T: %+v\n", v, v) 138 case *status.LiquidationsSnapshot: 139 log.Printf("%T: %+v\n", v, v) 140 default: 141 log.Printf("raw/unrecognized msg: %T: %s\n", v, v) 142 } 143 }) 144 }() 145 146 log.Fatal(<-crash) 147 }