-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpositions_test.go
243 lines (220 loc) · 7.56 KB
/
positions_test.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
package goanda
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestGetOpenPositions(t *testing.T) {
defer logTestResult(t, "GetOpenPositions")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/accounts/test-account/openPositions" {
t.Errorf("Unexpected path: %s", r.URL.Path)
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
response := OpenPositions{
LastTransactionID: "1000",
Positions: []struct {
Instrument string `json:"instrument"`
Long struct {
AveragePrice string `json:"averagePrice"`
Pl string `json:"pl"`
ResettablePL string `json:"resettablePL"`
TradeIDs []string `json:"tradeIDs"`
Units string `json:"units"`
UnrealizedPL string `json:"unrealizedPL"`
} `json:"long"`
Pl string `json:"pl"`
ResettablePL string `json:"resettablePL"`
Short struct {
AveragePrice string `json:"averagePrice"`
Pl string `json:"pl"`
ResettablePL string `json:"resettablePL"`
TradeIDs []string `json:"tradeIDs"`
Units string `json:"units"`
UnrealizedPL string `json:"unrealizedPL"`
} `json:"short"`
UnrealizedPL string `json:"unrealizedPL"`
}{
{
Instrument: "EUR_USD",
Long: struct {
AveragePrice string `json:"averagePrice"`
Pl string `json:"pl"`
ResettablePL string `json:"resettablePL"`
TradeIDs []string `json:"tradeIDs"`
Units string `json:"units"`
UnrealizedPL string `json:"unrealizedPL"`
}{
AveragePrice: "1.1000",
Pl: "10.00",
ResettablePL: "10.00",
TradeIDs: []string{"1", "2"},
Units: "100",
UnrealizedPL: "5.00",
},
Pl: "10.00",
ResettablePL: "10.00",
UnrealizedPL: "5.00",
},
},
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
c := &Connection{
hostname: server.URL,
accountID: "test-account",
client: *server.Client(),
}
positions, err := c.GetOpenPositions()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if positions.LastTransactionID != "1000" {
t.Errorf("Expected LastTransactionID to be 1000, got %s", positions.LastTransactionID)
}
if len(positions.Positions) != 1 {
t.Fatalf("Expected 1 position, got %d", len(positions.Positions))
}
position := positions.Positions[0]
if position.Instrument != "EUR_USD" {
t.Errorf("Expected Instrument to be EUR_USD, got %s", position.Instrument)
}
if position.Long.AveragePrice != "1.1000" {
t.Errorf("Expected Long.AveragePrice to be 1.1000, got %s", position.Long.AveragePrice)
}
if position.Long.Units != "100" {
t.Errorf("Expected Long.Units to be 100, got %s", position.Long.Units)
}
}
func TestClosePosition(t *testing.T) {
defer logTestResult(t, "ClosePosition")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/accounts/test-account/positions/EUR_USD/close" {
t.Errorf("Unexpected path: %s", r.URL.Path)
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
if r.Method != "PUT" {
t.Errorf("Expected PUT request, got %s", r.Method)
http.Error(w, "Invalid method", http.StatusMethodNotAllowed)
return
}
var payload ClosePositionPayload
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
t.Errorf("Failed to decode request body: %v", err)
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
response := ModifiedTrade{
LastTransactionID: "1000",
OrderCreateTransaction: struct {
Type string `json:"type"`
Instrument string `json:"instrument"`
Units string `json:"units"`
TimeInForce string `json:"timeInForce"`
PositionFill string `json:"positionFill"`
Reason string `json:"reason"`
TradeClose struct {
Units string `json:"units"`
TradeID string `json:"tradeID"`
} `json:"tradeClose"`
ID string `json:"id"`
UserID int `json:"userID"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
RequestID string `json:"requestID"`
Time time.Time `json:"time"`
}{
Type: "MARKET_ORDER",
Instrument: "EUR_USD",
Units: "-100",
Reason: "POSITION_CLOSEOUT",
ID: "1000",
AccountID: "test-account",
Time: time.Now(),
},
OrderFillTransaction: struct {
Type string `json:"type"`
Instrument string `json:"instrument"`
Units string `json:"units"`
Price string `json:"price"`
FullPrice FullPrice `json:"fullPrice"`
PL string `json:"pl"`
Financing string `json:"financing"`
Commission string `json:"commission"`
AccountBalance string `json:"accountBalance"`
TradeOpened string `json:"tradeOpened"`
TimeInForce string `json:"timeInForce"`
PositionFill string `json:"positionFill"`
Reason string `json:"reason"`
TradesClosed []struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
RealizedPL string `json:"realizedPL"`
Financing string `json:"financing"`
} `json:"tradesClosed"`
TradeReduced struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
RealizedPL string `json:"realizedPL"`
Financing string `json:"financing"`
} `json:"tradeReduced"`
ID string `json:"id"`
UserID int `json:"userID"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
RequestID string `json:"requestID"`
OrderID string `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Time time.Time `json:"time"`
}{
Type: "ORDER_FILL",
Instrument: "EUR_USD",
Units: "-100",
Price: "1.1000",
PL: "10.00",
ID: "1001",
AccountID: "test-account",
Time: time.Now(),
},
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
c := &Connection{
hostname: server.URL,
accountID: "test-account",
client: *server.Client(),
}
payload := ClosePositionPayload{
LongUnits: "100",
ShortUnits: "0",
}
modifiedTrade, err := c.ClosePosition("EUR_USD", payload)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if modifiedTrade.LastTransactionID != "1000" {
t.Errorf("Expected LastTransactionID to be 1000, got %s", modifiedTrade.LastTransactionID)
}
if modifiedTrade.OrderCreateTransaction.Type != "MARKET_ORDER" {
t.Errorf("Expected OrderCreateTransaction.Type to be MARKET_ORDER, got %s", modifiedTrade.OrderCreateTransaction.Type)
}
if modifiedTrade.OrderCreateTransaction.Instrument != "EUR_USD" {
t.Errorf("Expected OrderCreateTransaction.Instrument to be EUR_USD, got %s", modifiedTrade.OrderCreateTransaction.Instrument)
}
if modifiedTrade.OrderCreateTransaction.Units != "-100" {
t.Errorf("Expected OrderCreateTransaction.Units to be -100, got %s", modifiedTrade.OrderCreateTransaction.Units)
}
if modifiedTrade.OrderFillTransaction.Type != "ORDER_FILL" {
t.Errorf("Expected OrderFillTransaction.Type to be ORDER_FILL, got %s", modifiedTrade.OrderFillTransaction.Type)
}
if modifiedTrade.OrderFillTransaction.Price != "1.1000" {
t.Errorf("Expected OrderFillTransaction.Price to be 1.1000, got %s", modifiedTrade.OrderFillTransaction.Price)
}
}