-
Notifications
You must be signed in to change notification settings - Fork 2
/
trade.go
191 lines (165 loc) · 4.59 KB
/
trade.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package uexchange
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
func (c *Client) sendTradeTask(
orderType string,
pairSymbol string,
amount,
price float64,
) (int64, error) {
reqFields, err := url.ParseQuery(fmt.Sprintf(
"amount=%v&price=%v",
amount, price,
))
if err != nil {
return 0, fmt.Errorf("parse query: %w", err)
}
reqFields.Add("pair", pairSymbol)
body, err := c.sendRequest(c.getAPIURL("market/"+orderType), requestTypePOST, reqFields)
if err != nil {
return 0, err
}
// decode response
var response APITradeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return 0, fmt.Errorf("decode response: %w", err)
}
if !response.Success {
return 0, fmt.Errorf("send trade task: %s", response.Error)
}
return response.Result.OrderID, nil
}
// Buy currency. returns order ID
func (c *Client) Buy(pairSymbol string, amount, price float64) (int64, error) {
return c.sendTradeTask("buy", pairSymbol, amount, price)
}
// Sell currency. returns order ID
func (c *Client) Sell(pairSymbol string, amount, price float64) (int64, error) {
return c.sendTradeTask("sell", pairSymbol, amount, price)
}
// Hold or Unhold order
func (c *Client) Hold(orderID int64) error {
reqFields := url.Values{}
reqFields.Add("order_id", strconv.FormatInt(orderID, 10))
body, err := c.sendRequest(c.getAPIURL("market/hold"), requestTypePOST, reqFields)
if err != nil {
return err
}
// decode response
var response APITradeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return fmt.Errorf("decode response: %w", err)
}
if !response.Success {
return fmt.Errorf("send trade task: %s", response.Error)
}
return nil
}
// Cancel the specified order
func (c *Client) Cancel(orderID int64) error {
reqFields := url.Values{}
reqFields.Add("order_id", strconv.FormatInt(orderID, 10))
body, err := c.sendRequest(c.getAPIURL("market/cancel"), "POST", reqFields)
if err != nil {
return err
}
// decode response
var response APITradeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return fmt.Errorf("decode response: %w", err)
}
if !response.Success {
return fmt.Errorf("send trade task: %s", response.Error)
}
return nil
}
// GetPairs - get trading pairs list
func (c *Client) GetPairs() ([]PairsDataContainer, error) {
body, err := c.sendRequest(c.getAPIURL("market/pairs"), requestTypeGET, url.Values{})
if err != nil {
return nil, err
}
// decode response
var response APIPairsResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
if !response.Success {
return nil, fmt.Errorf("send trade task: %s", response.Error)
}
return response.Result.Pairs, nil
}
// GetOrderBook by trade pair
func (c *Client) GetOrderBook(pairSymbol string) (*BookValueDataContainer, error) {
reqFields := url.Values{}
reqFields.Add("pair", pairSymbol)
body, err := c.sendRequest(c.getAPIURL("market/panel"), requestTypePOST, reqFields)
if err != nil {
return nil, err
}
// decode response
var response APIBookValueResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
if !response.Success {
return nil, fmt.Errorf("send trade task: %s", response.Error)
}
return &response.Result, nil
}
// GetMarketCurrenciesList - get exchange currencies list
func (c *Client) GetMarketCurrenciesList(pairSymbol string) (*CurrenciesListData, error) {
reqFields := url.Values{}
reqFields.Add("pair", pairSymbol)
body, err := c.sendRequest(c.getAPIURL("market/curlist"), "GET", reqFields)
if err != nil {
return nil, err
}
// decode response
var response APICurrenciesListResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
if !response.Success {
return nil, fmt.Errorf("send trade task: %s", response.Error)
}
return &response.Result, nil
}
func (c *Client) GetPairPrice(pairCode string) (PairPriceData, error) {
result := PairPriceData{
PairCode: pairCode,
}
pairs, err := c.GetPairs()
if err != nil {
return result, fmt.Errorf("get pairs: %w", err)
}
for i := 0; i < len(pairs); i++ {
pairData := pairs[i]
if pairData.Pair.PairCode == pairCode {
bookData, err := c.GetOrderBook(pairData.Pair.PairCode)
if err != nil {
return result, fmt.Errorf("get order book: %w", err)
}
if len(bookData.Buy) > 0 {
result.BestAskPrice = bookData.Buy[0].Price
}
if len(bookData.Sell) > 0 {
result.BestBidPrice = bookData.Sell[0].Price
}
return result, nil
} else {
continue
}
}
return result, fmt.Errorf("pair %s not found", pairCode)
}