-
Notifications
You must be signed in to change notification settings - Fork 0
/
futures.go
301 lines (274 loc) · 8.87 KB
/
futures.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package rest
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type AddMarginRequest struct {
Amount int64 `json:"amount"`
Pid string `json:"pid"`
}
type AddMarginResponse struct {
Pid string `json:"pid"`
ID int `json:"id"`
Type string `json:"type"`
TakeprofitWi string `json:"takeprofit_wi"`
Takeprofit int `json:"takeprofit"`
StoplossWi string `json:"stoploss_wi"`
Stoploss int `json:"stoploss"`
Side string `json:"side"`
Quantity int `json:"quantity"`
Price int `json:"price"`
Pl int `json:"pl"`
MarketWi string `json:"market_wi"`
MarketFilledTs string `json:"market_filled_ts"`
MarginWi string `json:"margin_wi"`
Margin int `json:"margin"`
Liquidation int `json:"liquidation"`
Leverage int `json:"leverage"`
CreationTs string `json:"creation_ts"`
}
type CancelAllResponse struct {
Data []struct {
Pid string `json:"pid"`
ExitPrice int `json:"exit_price"`
ClosedTs string `json:"closed_ts"`
Closed bool `json:"closed"`
Pl int `json:"pl"`
} `json:"data"`
}
type CloseAllResponse CancelAllResponse
type CancelResponse struct {
Pid string `json:"pid"`
}
type CarryFeesHistoryResponse struct {
Data []struct {
Fixing string `json:"fixing"`
Index int `json:"index"`
} `json:"data"`
}
type CashInResponse struct {
Amount int `json:"amount"`
Pid string `json:"pid"`
}
type CloseResponse struct {
Pid string `json:"pid"`
ExitPrice int `json:"exit_price"`
ClosedTs string `json:"closed_ts"`
Closed bool `json:"closed"`
Pl int `json:"pl"`
}
type PositionsResponse struct {
Positions []struct {
Pid string `json:"pid"`
ID int `json:"id"`
Type string `json:"type"`
TakeprofitWi string `json:"takeprofit_wi"`
Takeprofit int `json:"takeprofit"`
StoplossWi string `json:"stoploss_wi"`
Stoploss int `json:"stoploss"`
Sign int `json:"sign"`
Side string `json:"side"`
Quantity int `json:"quantity"`
Price int `json:"price"`
Pl int `json:"pl"`
MarketWi string `json:"market_wi"`
MarketFilledTs int64 `json:"market_filled_ts"`
MarginWi string `json:"margin_wi"`
Margin int `json:"margin"`
Liquidation int `json:"liquidation"`
Leverage int `json:"leverage"`
ExitPrice int `json:"exit_price"`
CreationTs int64 `json:"creation_ts"`
ClosedTs int64 `json:"closed_ts"`
Closed bool `json:"closed"`
Canceled bool `json:"canceled"`
SumCarryFees int `json:"sum_carry_fees"`
} `json:"positions"`
}
type NewPositionRequest struct {
Type OrderType `json:"type"`
Side OrderSide `json:"side"`
Price float64 `json:"price"`
Margin int `json:"margin"`
Stoploss float64 `json:"stoploss"`
Takeprofit float64 `json:"takeprofit"`
Quantity float64 `json:"quantity"`
Leverage float64 `json:"leverage"`
}
type NewPositionResponse struct {
Pid string `json:"pid"`
ID int `json:"id"`
Type string `json:"type"`
TakeprofitWi string `json:"takeprofit_wi"`
Takeprofit float64 `json:"takeprofit"`
StoplossWi string `json:"stoploss_wi"`
Stoploss float64 `json:"stoploss"`
Side string `json:"side"`
Quantity float64 `json:"quantity"`
Price float64 `json:"price"`
Pl float64 `json:"pl"`
MarketWi string `json:"market_wi"`
MarketFilledTs string `json:"market_filled_ts"`
MarginWi string `json:"margin_wi"`
Margin int `json:"margin"`
Liquidation float64 `json:"liquidation"`
Leverage float64 `json:"leverage"`
CreationTs string `json:"creation_ts"`
}
type UpdateResponse struct {
Pid string `json:"pid"`
Type string `json:"type"`
Value float64 `json:"value"`
}
type BidAndOfferHistoryResponse struct {
Data []struct {
Time int64 `json:"time"`
Bid int `json:"bid"`
Offer int `json:"offer"`
} `json:"data"`
}
type FixingHistoryResponse struct {
Data []struct {
Ts int64 `json:"ts"`
ID string `json:"id"`
FixingPrice int `json:"fixing_price"`
FeePercentValue float64 `json:"fee_percent_value"`
} `json:"data"`
}
type IndexHistoryResponse struct {
Data []struct {
Time int64 `json:"time"`
Index int `json:"index"`
} `json:"data"`
}
type InstrumentResponse struct {
MaxPositionsCount int `json:"max_positions_count"`
MaxMargin int `json:"max_margin"`
MaxLeverage int `json:"max_leverage"`
CarryFees float64 `json:"carry_fees"`
}
type TickerResponse struct {
Bid float64 `json:"bid"`
Offer float64 `json:"offer"`
Index float64 `json:"index"`
}
// AddMargin - Add margin to a running position.
func (api *LNMarkets) AddMargin(amount int64, pid string) (*AddMarginResponse, error) {
response := &AddMarginResponse{}
request := AddMarginRequest{
Amount: amount,
Pid: pid,
}
body, err := json.Marshal(request)
if err != nil {
return nil, err
}
if err := api.request(http.MethodPost, "futures/add-margin", true, nil, body, &response); err != nil {
return nil, err
}
return response, nil
}
// Cancel - Cancel the position linked to the given pid.
// Only works on positions that are not currently filled.
func (api *LNMarkets) Cancel(pid string) (*CancelResponse, error) {
response := &CancelResponse{}
if err := api.request(http.MethodPost, "futures/cancel", true, nil, nil, &response); err != nil {
return nil, err
}
return response, nil
}
// CancelAll - Cancel all open positions.
func (api *LNMarkets) CancelAll() (*CancelAllResponse, error) {
response := &CancelAllResponse{}
if err := api.request(http.MethodDelete, "futures/all/cancel", true, nil, nil, &response); err != nil {
return nil, err
}
return response, nil
}
// Close - Close the user position.
// The PL will be calculated against the current bid or offer depending on the side of the position.
func (api *LNMarkets) Close() (*CloseResponse, error) {
response := &CloseResponse{}
if err := api.request(http.MethodDelete, "futures", true, nil, nil, &response); err != nil {
return nil, err
}
return response, nil
}
// CloseAll - Close every user position.
// The PL will be calculated against the current bid or offer depending on the side of the position.
func (api *LNMarkets) CloseAll() (*CloseAllResponse, error) {
response := &CloseAllResponse{}
if err := api.request(http.MethodDelete, "futures/all/close", true, nil, nil, &response); err != nil {
return nil, err
}
return response, nil
}
// CarryFeesHistory - Retrieves carry fees for user.
func (api *LNMarkets) CarryFeesHistory(from int64, to int64, limit int64) (*CarryFeesHistoryResponse, error) {
response := &CarryFeesHistoryResponse{}
params := make(url.Values)
params.Add("from", fmt.Sprint(from))
params.Add("to", fmt.Sprint(to))
params.Add("limit", fmt.Sprint(limit))
if err := api.request(http.MethodGet, "futures/carry-fees", true, params, nil, &response); err != nil {
return nil, err
}
return response, nil
}
// CashIn - Retrieves part of one running positions PL.
func (api *LNMarkets) CashIn(pid string) (*CashInResponse, error) {
response := &CashInResponse{}
if err := api.request(http.MethodPost, "futures/cash-in", true, nil, nil, &response); err != nil {
return nil, err
}
return response, nil
}
// Positions - Fetch users positions.
func (api *LNMarkets) Positions() (*PositionsResponse, error) {
response := &PositionsResponse{}
if err := api.request(http.MethodGet, "futures", true, nil, nil, &response); err != nil {
return nil, err
}
return response, nil
}
// NewPosition - Send the order form parameters to add a new position in database.
// If type="l", the property price must be included in the request to know when the position should be filled.
// You can choose to use the margin or the quantity as a parameter, the other will be calculated with the one you chose.
func (api *LNMarkets) NewPosition(t OrderType, s OrderSide, p float64, m int, sl, tp, q, l float64) (*NewPositionResponse, error) {
response := &NewPositionResponse{}
request := NewPositionRequest{
Type: t,
Side: s,
Price: p,
Margin: m,
Stoploss: sl,
Takeprofit: tp,
Quantity: q,
Leverage: l,
}
body, err := json.Marshal(request)
if err != nil {
return nil, err
}
if err := api.request(http.MethodPost, "futures", true, nil, body, &response); err != nil {
return nil, err
}
return response, nil
}
// ------
// ------
// ------
// ------
// ------
// ------
// ------
// Ticker - Retrieves the futures ticker.
func (api *LNMarkets) Ticker() (*TickerResponse, error) {
response := &TickerResponse{}
if err := api.request(http.MethodGet, "futures/ticker", false, nil, nil, &response); err != nil {
return nil, err
}
return response, nil
}