-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathorder.go
301 lines (265 loc) · 11.6 KB
/
order.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 igmarkets
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// OTCPositionCloseRequest - request struct for closing positions
type OTCPositionCloseRequest struct {
DealID string `json:"dealId,omitempty"`
Direction string `json:"direction"` // "BUY" or "SELL"
Epic string `json:"epic,omitempty"`
Expiry string `json:"expiry,omitempty"`
Level string `json:"level,omitempty"`
OrderType string `json:"orderType"`
QuoteID string `json:"quoteId,omitempty"`
Size float64 `json:"size"` // Deal size
TimeInForce string `json:"timeInForce,omitempty"` // "EXECUTE_AND_ELIMINATE" or "FILL_OR_KILL"
}
// AffectedDeal - part of order confirmation
type AffectedDeal struct {
DealID string `json:"dealId"`
Constant string `json:"constant"` // "FULLY_CLOSED"
}
// DealReference - deal reference struct for responses
type DealReference struct {
DealReference string `json:"dealReference"`
}
// OTCOrderDeleteRequest - request struct for deleting orders
type OTCOrderDeleteRequest struct {
DealId string `json:"dealId,omitempty"`
Direction string `json:"direction"` // "BUY" or "SELL"
Epic string `json:"epic,omitempty"`
Expiry string `json:"expiry,omitempty"`
Level float64 `json:"level,omitempty"`
OrderType string `json:"orderType"` // LIMIT/MARKET/QUOTE
QuoteID string `json:"quoteId,omitempty"`
Size float64 `json:"size"` // Deal size
TimeInForce string `json:"timeInForce,omitempty"` // "EXECUTE_AND_ELIMINATE" or "FILL_OR_KILL"
}
// OTCOrderRequest - request struct for placing orders
type OTCOrderRequest struct {
Epic string `json:"epic"`
Level float64 `json:"level,omitempty"`
ForceOpen bool `json:"forceOpen"`
OrderType string `json:"orderType"` // LIMIT/MARKET/QUOTE
CurrencyCode string `json:"currencyCode"`
Direction string `json:"direction"` // "BUY" or "SELL"
Expiry string `json:"expiry"`
Size float64 `json:"size"` // Deal size
StopDistance float64 `json:"stopDistance,omitempty"`
StopLevel float64 `json:"stopLevel,omitempty"`
LimitDistance float64 `json:"limitDistance,omitempty"`
LimitLevel float64 `json:"limitLevel,omitempty"`
QuoteID string `json:"quoteId,omitempty"`
TimeInForce string `json:"timeInForce,omitempty"` // "EXECUTE_AND_ELIMINATE" or "FILL_OR_KILL"
TrailingStop bool `json:"trailingStop"`
TrailingStopIncrement float64 `json:"trailingStopIncrement,omitempty"`
GuaranteedStop bool `json:"guaranteedStop"`
DealReference string `json:"dealReference,omitempty"`
}
// WorkingOrderData - Subset of OTCWorkingOrder
type WorkingOrderData struct {
CreatedDate string `json:"createdDate"`
CreatedDateUTC string `json:"createdDateUTC"`
CurrencyCode string `json:"currencyCode"`
DealID string `json:"dealId"`
Direction string `json:"direction"` // "BUY" or "SELL"
DMA bool `json:"dma"`
Epic string `json:"epic"`
GoodTillDate string `json:"goodTillDate"`
GoodTillDateISO string `json:"goodTillDateISO"`
GuaranteedStop bool `json:"guaranteedStop"`
LimitDistance float64 `json:"limitDistance"`
OrderLevel float64 `json:"orderLevel"`
OrderSize float64 `json:"orderSize"` // Deal size
OrderType string `json:"orderType"`
StopDistance float64 `json:"stopDistance"`
TimeInForce string `json:"timeInForce,omitempty"` // "EXECUTE_AND_ELIMINATE" or "FILL_OR_KILL"
}
// OTCDealConfirmation - Deal confirmation
type OTCDealConfirmation struct {
Epic string `json:"epic"`
AffectedDeals []AffectedDeal `json:"affectedDeals"`
Level float64 `json:"level"`
ForceOpen bool `json:"forceOpen"`
DealStatus string `json:"dealStatus"`
Reason string `json:"reason"`
Status string `json:"status"`
OrderType string `json:"orderType"`
Profit float64 `json:"profit"`
ProfitCurrency string `json:"profitCurrency"`
CurrencyCode string `json:"currencyCode"`
Direction string `json:"direction"` // "BUY" or "SELL"
Expiry string `json:"expiry,omitempty"`
Size float64 `json:"size"` // Deal size
StopDistance float64 `json:"stopDistance"`
StopLevel float64 `json:"stopLevel"`
LimitDistance float64 `json:"limitDistance,omitempty"`
LimitLevel float64 `json:"limitLevel"`
QuoteID string `json:"quoteId,omitempty"`
TimeInForce string `json:"timeInForce,omitempty"` // "EXECUTE_AND_ELIMINATE" or "FILL_OR_KILL"
TrailingStop bool `json:"trailingStop"`
TrailingStopIncrement float64 `json:"trailingIncrement"`
GuaranteedStop bool `json:"guaranteedStop"`
DealReference string `json:"dealReference,omitempty"`
DealId string `json:"dealId,omitempty"`
}
// OTCUpdateOrderRequest - request struct for updating orders
type OTCUpdateOrderRequest struct {
StopLevel float64 `json:"stopLevel"`
LimitLevel float64 `json:"limitLevel"`
TrailingStop bool `json:"trailingStop"`
TrailingStopIncrement string `json:"trailingStopIncrement,omitempty"`
}
// OTCWorkingOrderRequest - request struct for placing workingorders
type OTCWorkingOrderRequest struct {
CurrencyCode string `json:"currencyCode"`
DealReference string `json:"dealReference,omitempty"`
Direction string `json:"direction"` // "BUY" or "SELL"
Epic string `json:"epic"`
Expiry string `json:"expiry"`
ForceOpen bool `json:"forceOpen"`
GoodTillDate string `json:"goodTillDate,omitempty"`
GuaranteedStop bool `json:"guaranteedStop"`
Level float64 `json:"level"`
LimitDistance string `json:"limitDistance,omitempty"`
LimitLevel string `json:"limitLevel,omitempty"`
Size float64 `json:"size"` // Deal size
StopDistance string `json:"stopDistance,omitempty"`
StopLevel string `json:"stopLevel,omitempty"`
TimeInForce string `json:"timeInForce,omitempty"` // "GOOD_TILL_CANCELLED", "GOOD_TILL_DATE"
Type string `json:"type"`
}
// WorkingOrders - Working orders
type WorkingOrders struct {
WorkingOrders []OTCWorkingOrder `json:"workingOrders"`
}
// OTCWorkingOrder - Part of WorkingOrders
type OTCWorkingOrder struct {
MarketData MarketData `json:"marketData"`
WorkingOrderData WorkingOrderData `json:"workingOrderData"`
}
// DeletePositionsOTC - Closes one or more OTC positions
func (ig *IGMarkets) DeletePositionsOTC(ctx context.Context, deleteReq OTCOrderDeleteRequest) (*DealReference, error) {
bodyReq, err := json.Marshal(&deleteReq)
fmt.Println(string(bodyReq))
req, err := http.NewRequest("POST", ig.APIURL+"/gateway/deal/positions/otc", bytes.NewReader(bodyReq))
if err != nil {
return nil, fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
req.Header.Set("_method", "DELETE") // FFS IG...
dealRef, err := ig.doRequest(ctx, req, 1, DealReference{})
if err != nil {
return nil, err
}
return dealRef.(*DealReference), err
}
// PlaceOTCWorkingOrder - Place an OTC workingorder
func (ig *IGMarkets) PlaceOTCWorkingOrder(ctx context.Context, order OTCWorkingOrderRequest) (*DealReference, error) {
bodyReq, err := json.Marshal(&order)
if err != nil {
return nil, fmt.Errorf("igmarkets: unable to marshal JSON: %v", err)
}
req, err := http.NewRequest("POST", ig.APIURL+"/gateway/deal/workingorders/otc", bytes.NewReader(bodyReq))
if err != nil {
return nil, fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 2, DealReference{})
if err != nil {
return nil, err
}
return igResponseInterface.(*DealReference), err
}
// GetOTCWorkingOrders - Get all working orders
func (ig *IGMarkets) GetOTCWorkingOrders(ctx context.Context) (*WorkingOrders, error) {
bodyReq := new(bytes.Buffer)
req, err := http.NewRequest("GET", ig.APIURL+"/gateway/deal/workingorders/", bodyReq)
if err != nil {
return nil, fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 2, WorkingOrders{})
if err != nil {
return nil, err
}
igResponse, _ := igResponseInterface.(*WorkingOrders)
return igResponse, err
}
// DeleteOTCWorkingOrder - Delete OTC working order
func (ig *IGMarkets) DeleteOTCWorkingOrder(ctx context.Context, dealRef string) (*DealReference, error) {
bodyReq := new(bytes.Buffer)
req, err := http.NewRequest("DELETE", ig.APIURL+"/gateway/deal/workingorders/otc/"+dealRef, bodyReq)
if err != nil {
return nil, fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 2, DealReference{})
if err != nil {
return nil, err
}
return igResponseInterface.(*DealReference), nil
}
// PlaceOTCOrder - Place an OTC order
func (ig *IGMarkets) PlaceOTCOrder(ctx context.Context, order OTCOrderRequest) (*DealReference, error) {
bodyReq, err := json.Marshal(&order)
if err != nil {
return nil, fmt.Errorf("igmarkets: cannot marshal: %v", err)
}
req, err := http.NewRequest("POST", ig.APIURL+"/gateway/deal/positions/otc", bytes.NewReader(bodyReq))
if err != nil {
return nil, fmt.Errorf("igmarkets: cannot create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 2, DealReference{})
if err != nil {
return nil, err
}
return igResponseInterface.(*DealReference), nil
}
// UpdateOTCOrder - Update an exisiting OTC order
func (ig *IGMarkets) UpdateOTCOrder(ctx context.Context, dealID string, order OTCUpdateOrderRequest) (*DealReference, error) {
bodyReq, err := json.Marshal(&order)
if err != nil {
return nil, fmt.Errorf("igmarkets: cannot marshal: %v", err)
}
req, err := http.NewRequest("PUT", ig.APIURL+"/gateway/deal/positions/otc/"+dealID, bytes.NewReader(bodyReq))
if err != nil {
return nil, fmt.Errorf("igmarkets: cannot create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 2, DealReference{})
if err != nil {
return nil, err
}
return igResponseInterface.(*DealReference), err
}
// CloseOTCPosition - Close an OTC position
func (ig *IGMarkets) CloseOTCPosition(ctx context.Context, close OTCPositionCloseRequest) (*DealReference, error) {
bodyReq, err := json.Marshal(&close)
if err != nil {
return nil, fmt.Errorf("igmarkets: cannot marshal: %v", err)
}
req, err := http.NewRequest("POST", ig.APIURL+"/gateway/deal/positions/otc", bytes.NewReader(bodyReq))
if err != nil {
return nil, fmt.Errorf("igmarkets: cannot create HTTP request: %v", err)
}
req.Header.Set("_method", "DELETE")
igResponseInterface, err := ig.doRequest(ctx, req, 1, DealReference{})
if err != nil {
return nil, err
}
return igResponseInterface.(*DealReference), nil
}
// GetDealConfirmation - Check if the given order was closed/filled
func (ig *IGMarkets) GetDealConfirmation(ctx context.Context, dealRef string) (*OTCDealConfirmation, error) {
bodyReq := new(bytes.Buffer)
req, err := http.NewRequest("GET", ig.APIURL+"/gateway/deal/confirms/"+dealRef, bodyReq)
if err != nil {
return nil, fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 1, OTCDealConfirmation{})
if err != nil {
return nil, err
}
igResponse, _ := igResponseInterface.(*OTCDealConfirmation)
return igResponse, nil
}