-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuobi.go
More file actions
166 lines (145 loc) · 4.07 KB
/
Copy pathhuobi.go
File metadata and controls
166 lines (145 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/rcolombo/tickertock/types"
"github.com/shopspring/decimal"
)
type Huobi struct {
types.BaseExchange
HTTPClient http.Client
APIKey string
APISecret string
TradingAccount Account
}
func New(apiKey, apiSecret string) (*Huobi, error) {
h := Huobi{
HTTPClient: http.Client{Timeout: time.Second * 10},
APIKey: apiKey,
APISecret: apiSecret,
}
accounts, err := h.GetAccounts()
if err != nil {
return nil, fmt.Errorf("Error retrieving accounts: %v", err)
}
if len(accounts.Data) != 1 {
return nil, fmt.Errorf("Expected exactly 1 account, but got something else: %+v", accounts.Data)
}
h.TradingAccount = accounts.Data[0]
return &h, nil
}
func (h *Huobi) Symbolize(cp types.CurrencyPair) string {
base := strings.ToLower(cp.Base)
quote := strings.ToLower(cp.Quote)
if base == "bcc" {
base = "bch"
} else if quote == "bcc" {
quote = "bch"
}
return base + quote
}
func (h *Huobi) SubscribeForPair(cp types.CurrencyPair) (chan *types.BookOrder, chan bool) {
var (
subscribeTo = make(chan types.CurrencyPair, 1000)
disconnectCh = make(chan bool)
outgoing = make(chan *types.BookOrder, 1000000)
)
newBookOrderCh, disconnectForPairCh := h.GetOrderBookUpdates(subscribeTo)
subscribeTo <- cp
go func() {
for {
select {
case <-disconnectCh:
disconnectForPairCh <- cp
return
case nbo := <-newBookOrderCh:
outgoing <- nbo
}
}
}()
return outgoing, disconnectCh
}
func (h *Huobi) SubscribePair(cp types.CurrencyPair) (*types.Book, chan bool) {
var (
book *types.Book
subscribeTo = make(chan types.CurrencyPair, 1000)
disconnectCh = make(chan bool)
readyCh = make(chan bool)
)
newBookOrderCh, disconnectForPairCh := h.GetOrderBookUpdates(subscribeTo)
subscribeTo <- cp
book = types.NewBook(10)
h.SetBook(cp, book)
go func() {
var bidsLoaded, asksLoaded, sent bool
for {
select {
case <-disconnectCh:
disconnectForPairCh <- cp
return
case nbo := <-newBookOrderCh:
newHigh, newLow := book.ProcessBookOrder(*nbo)
bidsLoaded = bidsLoaded || newHigh
asksLoaded = asksLoaded || newLow
if bidsLoaded && asksLoaded && !sent {
readyCh <- true
sent = true
}
}
}
}()
select {
case <-readyCh:
}
return book, disconnectCh
}
func (h *Huobi) Name() string {
return "Huobi"
}
func (h *Huobi) GetFees(cp types.CurrencyPair, isBuy bool) (decimal.Decimal, decimal.Decimal) {
log.Fatal("[Huobi] `GetFees' not implemented!")
return decimal.RequireFromString("0"), decimal.RequireFromString("0")
}
func (h *Huobi) PriceTickSize(cp types.CurrencyPair) *decimal.Decimal {
log.Fatal("[Huobi] `PriceTickSize' not implemented!")
return nil
}
func (h *Huobi) LotSize(cp types.CurrencyPair) *decimal.Decimal {
log.Fatal("[Huobi] `LotSize' not implemented!")
return nil
}
func (h *Huobi) MinBet(cp types.CurrencyPair) (*decimal.Decimal, bool, error) {
log.Fatal("[Huobi] `MinBet' not implemented!")
return nil, false, nil
}
func (h *Huobi) MaxBet(cp types.CurrencyPair) (*decimal.Decimal, bool, error) {
log.Fatal("[Huobi] `MaxBet' not implemented!")
return nil, false, nil
}
func (h *Huobi) GetAllBalances() (map[string]decimal.Decimal, error) {
log.Fatal("[Huobi] `GetAllBalances' not implemented!")
return map[string]decimal.Decimal{}, nil
}
func (h *Huobi) UpdateBalancesFromTrade(t types.Trade) {
log.Fatal("[Huobi] `UpdateBalancesFromTrade' not implemented!")
}
func (h *Huobi) Tradeable(cp types.CurrencyPair) bool {
log.Fatal("[Huobi] `Tradeable' not implemented!")
return false
}
func (h *Huobi) GetTicker(cp types.CurrencyPair) (*types.Ticker, error) {
log.Fatal("[Huobi] `GetTicker' not implemented!")
return nil, nil
}
func (h *Huobi) Subscribe() chan *types.BookOrder {
log.Fatal("[Huobi] `Subscribe' not implemented!")
outgoing := make(chan *types.BookOrder)
return outgoing
}
func (h *Huobi) LimitOrder(cp types.CurrencyPair, isBuy bool, amount, price decimal.Decimal, timeout time.Duration) (types.Order, error) {
log.Fatal("[Huobi] `LimitOrder' not implemented!")
return types.Order{}, nil
}