forked from tensam/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcmarkets.go
159 lines (134 loc) · 4.05 KB
/
btcmarkets.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
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
package main
import (
"strconv"
"strings"
"time"
"log"
"fmt"
)
const (
BTCMARKETS_API_URL = "https://api.btcmarkets.net"
BTCMARKETS_API_VERSION = "0"
)
type BTCMarkets struct {
Name string
Enabled bool
Verbose bool
Websocket bool
PollingDelay time.Duration
Fee float64
APIKey, APISecret string
}
type BTCMarketsTicker struct {
BestBID float64
BestAsk float64
LastPrice float64
Currency string
Instrument string
Timestamp int64
}
func (b *BTCMarkets) SetDefaults() {
b.Name = "BTC Markets"
b.Enabled = true
b.Fee = 0.85
b.Verbose = false
b.Websocket = false
b.PollingDelay = 10
}
func (b *BTCMarkets) GetName() (string) {
return b.Name
}
func (b *BTCMarkets) SetEnabled(enabled bool) {
b.Enabled = enabled
}
func (b *BTCMarkets) IsEnabled() (bool) {
return b.Enabled
}
func (b *BTCMarkets) SetAPIKeys(apiKey, apiSecret string) {
b.APIKey = apiKey
b.APISecret = apiSecret
}
func (b *BTCMarkets) GetFee() (float64) {
return b.Fee
}
func (b *BTCMarkets) Run() {
if b.Verbose {
log.Printf("%s polling delay: %ds.\n", b.GetName(), b.PollingDelay)
}
for b.Enabled {
go func() {
BTCMarketsBTC := b.GetTicker("BTC")
BTCMarketsBTCLastUSD, _ := ConvertCurrency(BTCMarketsBTC.LastPrice, "AUD", "USD")
BTCMarketsBTCBestBidUSD, _ := ConvertCurrency(BTCMarketsBTC.BestBID, "AUD", "USD")
BTCMarketsBTCBestAskUSD, _ := ConvertCurrency(BTCMarketsBTC.BestAsk, "AUD", "USD")
log.Printf("BTC Markets BTC: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", BTCMarketsBTCLastUSD, BTCMarketsBTC.LastPrice, BTCMarketsBTCBestBidUSD, BTCMarketsBTC.BestBID, BTCMarketsBTCBestAskUSD, BTCMarketsBTC.BestAsk)
AddExchangeInfo(b.GetName(), "BTC", BTCMarketsBTCLastUSD, 0)
}()
go func() {
BTCMarketsLTC := b.GetTicker("LTC")
BTCMarketsLTCLastUSD, _ := ConvertCurrency(BTCMarketsLTC.LastPrice, "AUD", "USD")
BTCMarketsLTCBestBidUSD, _ := ConvertCurrency(BTCMarketsLTC.BestBID, "AUD", "USD")
BTCMarketsLTCBestAskUSD, _ := ConvertCurrency(BTCMarketsLTC.BestAsk, "AUD", "USD")
log.Printf("BTC Markets LTC: Last %f (%f) Bid %f (%f) Ask %f (%f)", BTCMarketsLTCLastUSD, BTCMarketsLTC.LastPrice, BTCMarketsLTCBestBidUSD, BTCMarketsLTC.BestBID, BTCMarketsLTCBestAskUSD, BTCMarketsLTC.BestAsk)
AddExchangeInfo(b.GetName(), "LTC", BTCMarketsLTCLastUSD, 0)
}()
time.Sleep(time.Second * b.PollingDelay)
}
}
func (b *BTCMarkets) GetTicker(symbol string) (BTCMarketsTicker) {
ticker := BTCMarketsTicker{}
path := fmt.Sprintf("/market/%s/AUD/tick", symbol)
err := SendHTTPGetRequest(BTCMARKETS_API_URL + path, true, &ticker)
if err != nil {
log.Println(err)
return BTCMarketsTicker{}
}
return ticker
}
func (b *BTCMarkets) GetOrderbook(symbol string) {
path := fmt.Sprintf("/market/%s/AUD/orderbook", symbol)
err := SendHTTPGetRequest(BTCMARKETS_API_URL + path, true, nil)
if err != nil {
log.Println(err)
}
}
func (b *BTCMarkets) GetTrades(symbol, since string) {
path := ""
if len(since) > 0 {
path = fmt.Sprintf("/market/%s/AUD/trades?since=%s", symbol, since)
} else {
path = fmt.Sprintf("/market/%s/AUD/trades", symbol)
}
err := SendHTTPGetRequest(BTCMARKETS_API_URL + path, true, nil)
if err != nil {
log.Println(err)
}
}
func (b *BTCMarkets) SendAuthenticatedRequest(reqType, path, data string) (error) {
nonce := strconv.FormatInt(time.Now().Unix(), 10)
request := ""
if len(data) > 0 {
request = path + "\n" + nonce + "\n" + data
} else {
request = path + "\n" + nonce + "\n"
}
hmac := GetHMAC(HASH_SHA512, []byte(request), []byte(b.APISecret))
if b.Verbose {
log.Printf("Sending %s request to %s path %s with params %s\n", reqType, BTCMARKETS_API_URL + path, path, request)
}
headers := make(map[string]string)
headers["Accept"] = "application/json"
headers["Content-Type"] = "application/json"
headers["Accept-Charset"] = "UTF-8"
headers["apikey"] = b.APIKey
headers["timestamp"] = nonce
headers["signature"] = Base64Encode(hmac)
resp, err := SendHTTPRequest(reqType, BTCMARKETS_API_URL + path, headers, strings.NewReader(""))
if err != nil {
return err
}
if b.Verbose {
log.Printf("Recieved raw: %s\n", resp)
}
return nil
}