This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
rates.go
335 lines (288 loc) · 10.4 KB
/
rates.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2014 Tjerk Santegoeds
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oanda
import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
type InterestRate struct {
Bid float64 `json:"bid"`
Ask float64 `json:"ask"`
}
func (ir InterestRate) String() string {
return fmt.Sprintf("InterestRate{Bid: %v, Ask: %v}", ir.Bid, ir.Ask)
}
type InstrumentInfo struct {
DisplayName string `json:"displayName"`
Pip float64 `json:"pip,string"`
MaxTradeUnits int `json:"maxTradeUnits"`
Precision float64 `json:"precision,string"`
MaxTrailingStop float64 `json:"maxTrailingStop"`
MinTrailingStop float64 `json:"minTrailingStop"`
MarginRate float64 `json:"marginRate"`
Halted bool `json:"halted"`
InterestRate map[string]InterestRate `json:"interestRate"`
}
func (ii InstrumentInfo) String() string {
return fmt.Sprintf(
"InstrumentInfo{\n"+
" DisplayName: %v,\n"+
" Pip: %v,\n"+
" MaxTradeUnits: %v\n"+
" Precision: %v\n"+
" MaxTrailingStop: %v\n"+
" MinTrailingStop: %v\n"+
" MarginRate: %v\n"+
" Halted: %v\n"+
" InterestRate: %v\n"+
"}",
ii.DisplayName, ii.Pip, ii.MaxTradeUnits, ii.Precision, ii.MaxTrailingStop,
ii.MinTrailingStop, ii.MarginRate, ii.Halted, ii.InterestRate)
}
type InstrumentField string
const (
DisplayNameField InstrumentField = "displayName"
PipField InstrumentField = "pip"
MaxTradeUnitsField InstrumentField = "maxTradeUnits"
PrecisionField InstrumentField = "precision"
MaxTrailingStopField InstrumentField = "maxTrailingStop"
MinTrailingStopField InstrumentField = "minTrailingStop"
MarginRateField InstrumentField = "marginRate"
HaltedField InstrumentField = "halted"
InterestRateField InstrumentField = "interestRate"
)
// Instruments returns instrument information. Only the specified instruments are returned if instruments
// is not nil. If fields is not nil additional information fields is included.
//
// See http://developer.oanda.com/docs/v1/rates/#get-an-instrument-list for further information.
func (c *Client) Instruments(instruments []string, fields []InstrumentField) (map[string]InstrumentInfo, error) {
u, err := url.Parse("/v1/instruments")
if err != nil {
return nil, err
}
q := u.Query()
if len(instruments) > 0 {
q.Set("instruments", strings.ToUpper(strings.Join(instruments, ",")))
}
if len(fields) > 0 {
ss := make([]string, len(fields))
for i, v := range fields {
ss[i] = string(v)
}
q.Set("fields", strings.Join(ss, ","))
}
if c.accountId != 0 {
q.Set("accountId", strconv.FormatUint(uint64(c.accountId), 10))
}
u.RawQuery = q.Encode()
v := struct {
Instruments []struct {
Instrument string `json:"instrument"`
InstrumentInfo
} `json:"instruments"`
}{}
if err = getAndDecode(c, u.String(), &v); err != nil {
return nil, err
}
info := make(map[string]InstrumentInfo)
for _, in := range v.Instruments {
info[in.Instrument] = in.InstrumentInfo
}
return info, nil
}
type (
// Granularity determines the interval at which historic instrument prices are converted into candles.
Granularity string
)
const (
S5 Granularity = "S5"
S10 Granularity = "S10"
S15 Granularity = "S15"
S30 Granularity = "S30"
M1 Granularity = "M1"
M2 Granularity = "M2"
M3 Granularity = "M3"
M5 Granularity = "M5"
M10 Granularity = "M10"
M15 Granularity = "M15"
M30 Granularity = "M30"
H1 Granularity = "H1"
H2 Granularity = "H2"
H3 Granularity = "H3"
H4 Granularity = "H4"
H6 Granularity = "H6"
H8 Granularity = "H8"
H12 Granularity = "H12"
D Granularity = "D"
W Granularity = "W"
M Granularity = "M"
)
// CandlesArg implements optional arguments for MidpointCandles and BidAskCandles.
type CandlesArg interface {
applyCandlesArg(url.Values)
}
type (
// Optional argument for PollMidpriceCandles and PollBidAskCandles to specify the start time from which
// instrument history should be included.
//
// See http://developer.oanda.com/docs/v1/rates/#retrieve-instrument-history for further information.
StartTime time.Time
// Optional argument for PollMidpriceCandles and PollBidAskCandles to specify the end time of until which
// instrument history should be included.
//
// See http://developer.oanda.com/docs/v1/rates/#retrieve-instrument-history for further information.
EndTime time.Time
// Optional argument for PollMidpriceCandles and PollBidAskCandles to indicate whether the candle that
// starts at StartTime should be included.
//
// See http://developer.oanda.com/docs/v1/rates/#retrieve-instrument-history for further information.
IncludeFirst bool
// Optional argument for PollMidpriceCandles and PollBidAskCandles to indicate the hour at which
// candles hould be aligned. Only relevant for hourly or greater granularities.
//
// See http://developer.oanda.com/docs/v1/rates/#retrieve-instrument-history for further information.
DailyAlignment int
// Optional argument for PollMidpriceCandles and PollBidAskCandles that indicates the timezone to use
// when aligning candles with DailyAlignment.
//
// See http://developer.oanda.com/docs/v1/rates/#retrieve-instrument-history for further information.
AlignmentTimezone time.Location
// Optional argument for PollMidpriceCandles and PollBidAskCandles to indicate the weekday at which
// candles should be aligned. Only relevant for weekly granularity.
//
// See http://developer.oanda.com/docs/v1/rates/#retrieve-instrument-history for further information.
WeeklyAlignment time.Weekday
)
func (c Count) applyCandlesArg(v url.Values) {
optionalArgs(v).SetInt("count", int(c))
}
func (s StartTime) applyCandlesArg(v url.Values) {
optionalArgs(v).SetTime("start", time.Time(s))
}
func (e EndTime) applyCandlesArg(v url.Values) {
optionalArgs(v).SetTime("end", time.Time(e))
}
func (b IncludeFirst) applyCandlesArg(v url.Values) {
optionalArgs(v).SetBool("includeFirst", bool(b))
}
func (da DailyAlignment) applyCandlesArg(v url.Values) {
optionalArgs(v).SetInt("dailyAlignment", int(da))
}
func (atz AlignmentTimezone) applyCandlesArg(v url.Values) {
loc := time.Location(atz)
v.Set("alignmentTimezone", loc.String())
}
func (wa WeeklyAlignment) applyCandlesArg(v url.Values) {
optionalArgs(v).SetStringer("weeklyAlignment", time.Weekday(wa))
}
// MidpointCandles represents instrument history with a specific granularity.
type MidpointCandles struct {
Instrument string `json:"instrument"`
Granularity Granularity `json:"granularity"`
Candles []MidpointCandle `json:"candles"`
}
func (c MidpointCandles) String() string {
return fmt.Sprintf("MidpointCandles{Instrument: %s, Granularity: %v, Candles: %v}",
c.Instrument, c.Granularity, c.Candles)
}
// BidAskCandles represents Bid and Ask instrument history with a specific granularity.
type BidAskCandles struct {
Instrument string `json:"instrument"`
Granularity Granularity `json:"granularity"`
Candles []BidAskCandle `json:"candles"`
}
func (c BidAskCandles) String() string {
return fmt.Sprintf("BidAskCandles{Instrument: %s, Granularity: %v, Candles: %v}", c.Instrument,
c.Granularity, c.Candles)
}
// PollMidpointCandles returns historical midpoint prices for an instrument.
func (c *Client) PollMidpointCandles(instrument string, granularity Granularity,
args ...CandlesArg) (*MidpointCandles, error) {
u, err := c.newCandlesURL(instrument, granularity, "midpoint", args...)
if err != nil {
return nil, err
}
candles := MidpointCandles{}
if err = getAndDecode(c, u.String(), &candles); err != nil {
return nil, err
}
return &candles, nil
}
// PollBidAskCandles returns historical bid- and ask prices for an instrument.
func (c *Client) PollBidAskCandles(instrument string, granularity Granularity,
args ...CandlesArg) (*BidAskCandles, error) {
u, err := c.newCandlesURL(instrument, granularity, "bidask", args...)
if err != nil {
return nil, err
}
candles := BidAskCandles{}
if err = getAndDecode(c, u.String(), &candles); err != nil {
return nil, err
}
return &candles, nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Private
func (c *Client) newCandlesURL(instrument string, granularity Granularity, candleFormat string,
args ...CandlesArg) (*url.URL, error) {
u, err := url.Parse("/v1/candles")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("candleFormat", candleFormat)
q.Set("granularity", string(granularity))
q.Set("instrument", strings.ToUpper(instrument))
for _, arg := range args {
arg.applyCandlesArg(q)
}
u.RawQuery = q.Encode()
return u, err
}
type MidpointCandle struct {
Time Time `json:"time"`
OpenMid float64 `json:"openMid"`
HighMid float64 `json:"highMid"`
LowMid float64 `json:"lowMid"`
CloseMid float64 `json:"closeMid"`
Volume int `json:"volume"`
Complete bool `json:"complete"`
}
func (c MidpointCandle) String() string {
return fmt.Sprintf("MidpointCandle{Time: %v, OpenMid: %f, HighMid: %f, LowMid: %f, "+
"CloseMid: %f, Volume: %d, Complete: %v}", c.Time, c.OpenMid, c.HighMid, c.LowMid,
c.CloseMid, c.Volume, c.Complete)
}
type BidAskCandle struct {
Time Time `json:"time"`
OpenBid float64 `json:"openBid"`
OpenAsk float64 `json:"openAsk"`
HighBid float64 `json:"highBid"`
HighAsk float64 `json:"highAsk"`
LowBid float64 `json:"lowBid"`
LowAsk float64 `json:"lowAsk"`
CloseBid float64 `json:"closeBid"`
CloseAsk float64 `json:"closeAsk"`
Volume int `json:"volume"`
Complete bool `json:"complete"`
}
func (c BidAskCandle) String() string {
return fmt.Sprintf("BidAskCandle{Time: %v, OpenBid: %f, OpenAsk: %f, HighBid: %f, "+
"HighAsk: %f, LowBid: %f, LowAsk: %f, CloseBid: %f, CloseAsk: %f, "+
"Volume: %d, Complete: %v}", c.Time, c.OpenBid, c.OpenAsk, c.HighBid,
c.HighAsk, c.LowBid, c.LowAsk, c.CloseBid, c.CloseAsk, c.Volume, c.Complete)
}