-
Notifications
You must be signed in to change notification settings - Fork 29
/
orders.go
263 lines (244 loc) · 10.9 KB
/
orders.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
package goanda
// Supporting OANDA docs - http://developer.oanda.com/rest-live-v20/order-ep/
import (
"time"
)
type OrderExtensions struct {
Comment string `json:"comment,omitempty"`
ID string `json:"id,omitempty"`
Tag string `json:"tag,omitempty"`
}
type OnFill struct {
TimeInForce string `json:"timeInForce,omitempty"`
Price string `json:"price,omitempty"` // must be a string for float precision
Distance string `json:"distance,omitempty"`
GtdTime string `json:"gtdTime,omitempty"`
ClientExtensions *OrderExtensions `json:"clientExtensions,omitempty"`
}
type OrderBody struct {
ID string `json:"id,omitempty"`
CreateTime time.Time `json:"createTime,omitempty"`
State string `json:"state,omitempty"`
ClientExtensions *OrderExtensions `json:"clientExtensions,omitempty"`
Instrument string `json:"instrument"`
Units int `json:"units"`
TimeInForce string `json:"timeInForce"`
PriceBound string `json:"priceBound,omitempty"`
Type string `json:"type"`
PositionFill string `json:"positionFill,omitempty"`
Price string `json:"price,omitempty"`
TakeProfitOnFill *OnFill `json:"takeProfitOnFill,omitempty"`
StopLossOnFill *OnFill `json:"stopLossOnFill,omitempty"`
GuaranteedStopLossOnFill *OnFill `json:"guaranteedStopLossOnFill,omitempty"`
TrailingStopLossOnFill *OnFill `json:"trailingStopLossOnFill,omitempty"`
TradeClientExtensions *OrderExtensions `json:"tradeClientExtensions,omitempty"`
FillingTransactionID string `json:"fillingTransactionID,omitempty"`
FilledTime time.Time `json:"filledTime,omitempty"`
TradeOpenedID string `json:"tradeOpenedID,omitempty"`
TradeID string `json:"tradeID,omitempty"`
TradeReducedID string `json:"tradeReducedID,omitempty"`
TradeClosedIDs []string `json:"tradeClosedIDs,omitempty"`
CancellingTransactionID string `json:"cancellingTransactionID,omitempty"`
CancelledTime time.Time `json:"cancelledTime,omitempty"`
ReplacesOrderID string `json:"replacesOrderID,omitempty"`
ReplacedByOrderID string `json:"replacedByOrderID,omitempty"`
TriggerCondition string `json:"triggerCondition,omitempty"`
GTDTime time.Time `json:"gtdTime,omitempty"`
Distance string `json:"distance,omitempty"`
}
type OrderPayload struct {
Order OrderBody `json:"order"`
}
type OrderResponse struct {
LastTransactionID string `json:"lastTransactionID"`
OrderCreateTransaction struct {
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
ID string `json:"id"`
Instrument string `json:"instrument"`
PositionFill string `json:"positionFill"`
Reason string `json:"reason"`
Time time.Time `json:"time"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Units string `json:"units"`
UserID int `json:"userID"`
Price string `json:"price,omitempty"`
PriceBound string `json:"priceBound,omitempty"`
Extensions *OrderExtensions `json:"clientExtensions,omitempty"`
TakeProfitOnFill *OnFill `json:"takeProfitOnFill,omitempty"`
StopLossOnFill *OnFill `json:"stopLossOnFill,omitempty"`
GuaranteedStopLossOnFill *OnFill `json:"guaranteedStopLossOnFill,omitempty"`
TrailingStopLossOnFill *OnFill `json:"trailingStopLossOnFill,omitempty"`
TradeClientExtensions *OrderExtensions `json:"tradeClientExtensions,omitempty"`
TriggerCondition string `json:"triggerCondition,omitempty"`
GTDTime time.Time `json:"gtdTime,omitempty"`
Distance string `json:"distance,omitempty"`
} `json:"orderCreateTransaction,omitempty"`
OrderFillTransaction struct {
AccountBalance string `json:"accountBalance"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
Financing string `json:"financing"`
ID string `json:"id"`
Instrument string `json:"instrument"`
OrderID string `json:"orderID"`
Pl string `json:"pl"`
Price string `json:"price"`
Reason string `json:"reason"`
Time time.Time `json:"time"`
TradeOpened struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
} `json:"tradeOpened"`
Type string `json:"type"`
Units string `json:"units"`
UserID int `json:"userID"`
FullPrice FullPrice `json:"fullPrice,omitempty"`
TradesClosed []struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
Financing string `json:"financing"`
RealizedPL string `json:"realizedPL"`
Price string `json:"price"`
} `json:"tradesClosed,omitempty"`
TradeReduced struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
Financing string `json:"financing"`
RealizedPL string `json:"realizedPL"`
Price string `json:"price"`
} `json:"tradeReduced,omitempty"`
} `json:"orderFillTransaction,omitempty"`
OrderCancelTransaction struct {
ID string `json:"id"`
Time time.Time `json:"time"`
Reason string `json:"reason"`
} `json:"orderCancelTransaction,omitempty"`
RelatedTransactionIDs []string `json:"relatedTransactionIDs"`
OrderClientExtensions *OrderExtensions `json:"orderClientExtensions,omitempty"`
TradeClientExtensions *OrderExtensions `json:"tradeClientExtensions,omitempty"`
}
// GetOrderState returns the state of the order based on the transactions in the response
func (or *OrderResponse) GetOrderState() string {
if or.OrderCancelTransaction.ID != "" {
return "CANCELLED"
}
if or.OrderFillTransaction.ID != "" {
return "FILLED"
}
if or.OrderCreateTransaction.ID != "" {
return "PENDING"
}
return "UNKNOWN"
}
type OrderInfo struct {
ID string `json:"id"`
CreateTime time.Time `json:"createTime"`
State string `json:"state"`
ClientExtensions *OrderExtensions `json:"clientExtensions,omitempty"`
Instrument string `json:"instrument"`
Units string `json:"units"` // Keep as string for API response
TimeInForce string `json:"timeInForce"`
PriceBound string `json:"priceBound,omitempty"`
Type string `json:"type"`
PositionFill string `json:"positionFill"`
Price string `json:"price,omitempty"`
TakeProfitOnFill *OnFill `json:"takeProfitOnFill,omitempty"`
StopLossOnFill *OnFill `json:"stopLossOnFill,omitempty"`
GuaranteedStopLossOnFill *OnFill `json:"guaranteedStopLossOnFill,omitempty"`
TrailingStopLossOnFill *OnFill `json:"trailingStopLossOnFill,omitempty"`
TradeClientExtensions *OrderExtensions `json:"tradeClientExtensions,omitempty"`
FillingTransactionID string `json:"fillingTransactionID,omitempty"`
FilledTime time.Time `json:"filledTime,omitempty"`
TradeOpenedID string `json:"tradeOpenedID,omitempty"`
TradeID string `json:"tradeID,omitempty"`
TradeReducedID string `json:"tradeReducedID,omitempty"`
CancellingTransactionID string `json:"cancellingTransactionID,omitempty"`
CancelledTime time.Time `json:"cancelledTime,omitempty"`
ReplacesOrderID string `json:"replacesOrderID,omitempty"`
ReplacedByOrderID string `json:"replacedByOrderID,omitempty"`
TriggerCondition string `json:"triggerCondition,omitempty"`
GTDTime time.Time `json:"gtdTime,omitempty"`
PartialFill string `json:"partialFill,omitempty"`
Distance string `json:"distance,omitempty"`
}
type RetrievedOrders struct {
LastTransactionID string `json:"lastTransactionID"`
Orders []OrderInfo `json:"orders,omitempty"`
}
type RetrievedOrder struct {
Order OrderInfo `json:"order"`
}
type CancelledOrder struct {
OrderCancelTransaction struct {
ID string `json:"id"`
Time time.Time `json:"time"`
UserID int `json:"userID"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
RequestID string `json:"requestID"`
Type string `json:"type"`
OrderID string `json:"orderID"`
ClientOrderID string `json:"clientOrderID"`
Reason string `json:"reason"`
ReplacedByOrderID string `json:"replacedByOrderID"`
} `json:"orderCancelTransaction"`
RelatedTransactionIDs []string `json:"relatedTransactionIDs"`
LastTransactionID string `json:"lastTransactionID"`
}
func (c *Connection) CreateOrder(body OrderPayload) (OrderResponse, error) {
or := OrderResponse{}
err := c.postAndUnmarshal("/accounts/"+c.accountID+"/orders", body, &or)
return or, err
}
func (c *Connection) GetOrders(instrument string) (RetrievedOrders, error) {
endpoint := "/accounts/" + c.accountID + "/orders"
if instrument != "" {
endpoint = endpoint + "?instrument=" + instrument
}
ro := RetrievedOrders{}
err := c.getAndUnmarshal(endpoint, &ro)
return ro, err
}
func (c *Connection) GetPendingOrders() (RetrievedOrders, error) {
ro := RetrievedOrders{}
err := c.getAndUnmarshal("/accounts/"+c.accountID+"/pendingOrders", &ro)
return ro, err
}
func (c *Connection) GetOrder(orderSpecifier string) (RetrievedOrder, error) {
ro := RetrievedOrder{}
err := c.getAndUnmarshal(
"/accounts/"+
c.accountID+
"/orders/"+
orderSpecifier,
&ro,
)
return ro, err
}
func (c *Connection) UpdateOrder(orderSpecifier string, body OrderPayload) (RetrievedOrder, error) {
ro := RetrievedOrder{}
err := c.putAndUnmarshal(
"/accounts/"+
c.accountID+
"/orders/"+
orderSpecifier,
body,
&ro,
)
return ro, err
}
func (c *Connection) CancelOrder(orderSpecifier string) (CancelledOrder, error) {
co := CancelledOrder{}
err := c.putAndUnmarshal(
"/accounts/"+
c.accountID+
"/orders/"+
orderSpecifier+
"/cancel",
nil,
&co,
)
return co, err
}