-
Notifications
You must be signed in to change notification settings - Fork 29
/
pricing_test.go
152 lines (142 loc) · 4.29 KB
/
pricing_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
package goanda
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestGetPricingForInstruments(t *testing.T) {
defer logTestResult(t, "GetPricingForInstruments")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/accounts/test-account/pricing" {
t.Errorf("Unexpected path: %s", r.URL.Path)
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
instruments := r.URL.Query().Get("instruments")
if instruments != "EUR_USD,USD_JPY" {
t.Errorf("Unexpected instruments: %s", instruments)
http.Error(w, "Invalid instruments", http.StatusBadRequest)
return
}
response := Pricings{
Prices: []struct {
Asks []struct {
Liquidity int `json:"liquidity"`
Price string `json:"price"`
} `json:"asks"`
Bids []struct {
Liquidity int `json:"liquidity"`
Price string `json:"price"`
} `json:"bids"`
CloseoutAsk string `json:"closeoutAsk"`
CloseoutBid string `json:"closeoutBid"`
Instrument string `json:"instrument"`
QuoteHomeConversionFactors struct {
NegativeUnits string `json:"negativeUnits"`
PositiveUnits string `json:"positiveUnits"`
} `json:"quoteHomeConversionFactors"`
Status string `json:"status"`
Time time.Time `json:"time"`
UnitsAvailable struct {
Default struct {
Long string `json:"long"`
Short string `json:"short"`
} `json:"default"`
OpenOnly struct {
Long string `json:"long"`
Short string `json:"short"`
} `json:"openOnly"`
ReduceFirst struct {
Long string `json:"long"`
Short string `json:"short"`
} `json:"reduceFirst"`
ReduceOnly struct {
Long string `json:"long"`
Short string `json:"short"`
} `json:"reduceOnly"`
} `json:"unitsAvailable"`
}{
{
Asks: []struct {
Liquidity int `json:"liquidity"`
Price string `json:"price"`
}{
{Liquidity: 10000000, Price: "1.10050"},
},
Bids: []struct {
Liquidity int `json:"liquidity"`
Price string `json:"price"`
}{
{Liquidity: 10000000, Price: "1.10040"},
},
CloseoutAsk: "1.10060",
CloseoutBid: "1.10030",
Instrument: "EUR_USD",
Status: "tradeable",
Time: time.Now(),
},
{
Asks: []struct {
Liquidity int `json:"liquidity"`
Price string `json:"price"`
}{
{Liquidity: 10000000, Price: "109.500"},
},
Bids: []struct {
Liquidity int `json:"liquidity"`
Price string `json:"price"`
}{
{Liquidity: 10000000, Price: "109.490"},
},
CloseoutAsk: "109.510",
CloseoutBid: "109.480",
Instrument: "USD_JPY",
Status: "tradeable",
Time: time.Now(),
},
},
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
c := &Connection{
hostname: server.URL,
accountID: "test-account",
client: *server.Client(),
}
pricings, err := c.GetPricingForInstruments([]string{"EUR_USD", "USD_JPY"})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(pricings.Prices) != 2 {
t.Fatalf("Expected 2 prices, got %d", len(pricings.Prices))
}
eurUsd := pricings.Prices[0]
if eurUsd.Instrument != "EUR_USD" {
t.Errorf("Expected first instrument to be EUR_USD, got %s", eurUsd.Instrument)
}
if eurUsd.Status != "tradeable" {
t.Errorf("Expected EUR_USD status to be tradeable, got %s", eurUsd.Status)
}
if len(eurUsd.Asks) == 0 || eurUsd.Asks[0].Price != "1.10050" {
t.Errorf("Unexpected EUR_USD ask price: %v", eurUsd.Asks)
}
if len(eurUsd.Bids) == 0 || eurUsd.Bids[0].Price != "1.10040" {
t.Errorf("Unexpected EUR_USD bid price: %v", eurUsd.Bids)
}
usdJpy := pricings.Prices[1]
if usdJpy.Instrument != "USD_JPY" {
t.Errorf("Expected second instrument to be USD_JPY, got %s", usdJpy.Instrument)
}
if usdJpy.Status != "tradeable" {
t.Errorf("Expected USD_JPY status to be tradeable, got %s", usdJpy.Status)
}
if len(usdJpy.Asks) == 0 || usdJpy.Asks[0].Price != "109.500" {
t.Errorf("Unexpected USD_JPY ask price: %v", usdJpy.Asks)
}
if len(usdJpy.Bids) == 0 || usdJpy.Bids[0].Price != "109.490" {
t.Errorf("Unexpected USD_JPY bid price: %v", usdJpy.Bids)
}
}