-
Notifications
You must be signed in to change notification settings - Fork 0
/
market.go
134 lines (121 loc) · 3.46 KB
/
market.go
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
package itbit
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"strconv"
"time"
)
// Symbol represents a trading pair.
type Symbol string
const (
// SymbolXBTUSD is the USD/BTC trading pair.
SymbolXBTUSD = "XBTUSD"
// SymbolXBTSGD is the USD/SGD trading pair.
SymbolXBTSGD = "XBTSGD"
// SymbolXBTEUR is the USD/EUR trading pair.
SymbolXBTEUR = "XBTEUR"
)
// Ticker represents market data.
type Ticker struct {
Pair Symbol `json:"pair"`
Bid float64 `json:"bid,string"`
BidAmt float64 `json:"bidAmt,string"`
Ask float64 `json:"ask,string"`
AskAmt float64 `json:"askAmt,string"`
LastPrice float64 `json:"lastPrice,string"`
LastAmt float64 `json:"lastAmt,string"`
Volume24h float64 `json:"volume24h,string"`
VolumeToday float64 `json:"volumeToday,string"`
High24h float64 `json:"high24h,string"`
Low24h float64 `json:"low24h,string"`
HighToday float64 `json:"highToday,string"`
LowToday float64 `json:"lowToday,string"`
OpenToday float64 `json:"openToday,string"`
VwapToday float64 `json:"vwapToday,string"`
Vwap24h float64 `json:"vwap24h,string"`
ServerTimeUTC time.Time `json:"serverTimeUTC,string"`
}
func (t *Ticker) String () string {
return fmt.Sprintf("%0.2f - %0.2f %s", t.Bid, t.Ask, t.Pair)
}
// Order represents an order on the order book.
type Order []string
func (o Order) String () string {
a, err := o.Amount()
if err != nil {
a = 0
}
p, err := o.Price()
if err != nil {
p = 0
}
return fmt.Sprintf("%0.8f @ %0.2f", a, p)
}
// Price returns the price from the order.
func (o Order) Price () (float64, error) {
if len(o) < 1 {
return 0, errors.New("invalid order")
}
p, err := strconv.ParseFloat(o[0], 64)
if err != nil {
return 0, errors.Wrap(err, "parsing price")
}
return p, nil
}
// Amount returns the price from the order.
func (o Order) Amount () (float64, error) {
if len(o) < 2 {
return 0, errors.New("invalid order")
}
a, err := strconv.ParseFloat(o[1], 64)
if err != nil {
return 0, errors.Wrap(err, "parsing amount")
}
return a, nil
}
// OrderBook represents open orders.
type OrderBook struct {
Asks []Order `json:"asks"`
Bids []Order `json:"bids"`
}
// GetTicker gets the market data for a symbol.
func GetTicker(conf *Config, symbol Symbol) (*Ticker, error) {
if err := validateSymbol(symbol); err != nil {
return nil, err
}
body, err := doReq(conf, "GET", fmt.Sprintf("/markets/%s/ticker", symbol), false,nil)
if err != nil {
return nil, errors.Wrap(err, "getting ticker from itBit")
}
defer body.Close()
dec := json.NewDecoder(body)
resp := &Ticker{}
if err := dec.Decode(resp); err!= nil {
return nil, errors.Wrap(err, "decoding JSON")
}
return resp, nil
}
// GetOrderBook gets a list of the open orders.
func GetOrderBook(conf *Config, symbol Symbol) (*OrderBook, error) {
if err := validateSymbol(symbol); err != nil {
return nil, err
}
body, err := doReq(conf, "GET", fmt.Sprintf("/markets/%s/order_book", symbol), false,nil)
if err != nil {
return nil, errors.Wrap(err, "getting ticker from itBit")
}
defer body.Close()
dec := json.NewDecoder(body)
resp := &OrderBook{}
if err := dec.Decode(resp); err!= nil {
return nil, errors.Wrap(err, "decoding JSON")
}
return resp, nil
}
func validateSymbol (symbol Symbol) error {
if symbol != SymbolXBTUSD && symbol != SymbolXBTSGD && symbol != SymbolXBTEUR {
return errors.Errorf("invalid symbol %s", symbol)
}
return nil
}