forked from santegoeds/oanda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labs.go
547 lines (468 loc) · 15.3 KB
/
labs.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// 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 (
"encoding/json"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
type Period int64
const (
Hour Period = Period(time.Hour)
Day Period = 24 * Hour
Week Period = 7 * Day
Month Period = 2592000
Year Period = 31536000
)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Calendar
type CalendarEvent struct {
Title string `json:"title"`
Timestamp int64 `json:"timestamp"`
Unit string `json:"unit"`
Currency string `json:"currency"`
Forecast float64 `json:"forecast,string"`
Previous float64 `json:"previous,string"`
Actual float64 `json:"actual,string"`
Market float64 `json:"market,string"`
}
func (ce CalendarEvent) String() string {
t := time.Unix(0, ce.Timestamp*1000)
return fmt.Sprintf("CalendarEvent{Title: %s, Timestamp: %s, Unit: %s, Currency: %s, "+
"Forecast: %v, Previous: %v, Actual: %v, Market: %v}", ce.Title,
t.Format(time.RFC3339), ce.Unit, ce.Currency, ce.Forecast, ce.Previous, ce.Actual,
ce.Market)
}
// Calendar returns and array of economic calendar events associated with an instrument. Events
// can include economic indicator data or they can solely be be news about important meetings.
//
// See http://developer.oanda.com/docs/v1/forex-labs/#calendar for further information.
func (c *Client) Calendar(instrument string, period Period) ([]CalendarEvent, error) {
instrument = strings.ToUpper(instrument)
u, err := url.Parse("/labs/v1/calendar")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("instrument", instrument)
q.Set("period", strconv.Itoa(int(period)))
u.RawQuery = q.Encode()
ces := make([]CalendarEvent, 0)
if err = getAndDecode(c, u.String(), &ces); err != nil {
return nil, err
}
return ces, nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// PositionRatios
type PositionRatio struct {
Timestamp Time
LongRatio float64
ExchangeRate float64
}
type PositionRatios struct {
Instrument string
DisplayName string
Ratios []PositionRatio
}
func (pr PositionRatios) String() string {
return fmt.Sprintf("PositionRatios{Instrument: %s, DisplayName: %s, Ratios: %v}",
pr.Instrument, pr.DisplayName, pr.Ratios)
}
func (pr *PositionRatios) UnmarshalJSON(data []byte) error {
v := struct {
Data map[string]struct {
Data [][]float64 `json:"data"`
Label string `json:"label"`
} `json:"data"`
}{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
for instrument, data := range v.Data {
pr.Instrument = instrument
pr.DisplayName = data.Label
pr.Ratios = make([]PositionRatio, len(data.Data))
for i, ratio := range data.Data {
pr.Ratios[i].Timestamp = Time(strconv.Itoa(int(ratio[0])))
pr.Ratios[i].LongRatio = ratio[1]
pr.Ratios[i].ExchangeRate = ratio[2]
}
}
return nil
}
// PositionRatios returns daily position ratios for an instrument. A position ratio is
// the percentage of Oanda clients that have a Long/Short position.
//
// See http://developer.oanda.com/docs/v1/forex-labs/#historical-position-ratios for further
// information.
func (c *Client) PositionRatios(instrument string, period Period) (*PositionRatios, error) {
instrument = strings.ToUpper(instrument)
u, err := url.Parse("/labs/v1/historical_position_ratios")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("instrument", instrument)
q.Set("period", strconv.Itoa(int(period)))
u.RawQuery = q.Encode()
pr := PositionRatios{}
if err = getAndDecode(c, u.String(), &pr); err != nil {
return nil, err
}
return &pr, nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Spreads
type Spread struct {
Timestamp Time
Spread float64
}
func (s Spread) String() string {
return fmt.Sprintf("Spread{Timestamp: %v, Spread: %f}", s.Timestamp, s.Spread)
}
func (s *Spread) UnmarshalJSON(data []byte) error {
v := []float64{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
s.Timestamp = Time(strconv.Itoa(int(v[0])))
s.Spread = v[1]
return nil
}
type Spreads struct {
Max []Spread `json:"max"`
Avg []Spread `json:"avg"`
Min []Spread `json:"min"`
}
func (s Spreads) String() string {
return fmt.Sprintf("Spreads{Max: %v, Avg: %v, Min: %v}", s.Max, s.Avg, s.Min)
}
// Spreads returns historical spread data for a specific period in 15 min intervals. If unique is
// true then adjacent duplicate spreads are omitted.
//
// See http://developer.oanda.com/docs/v1/forex-labs/#spreads for further information.
func (c *Client) Spreads(instrument string, period Period, unique bool) (*Spreads, error) {
instrument = strings.ToUpper(instrument)
u, err := url.Parse("/labs/v1/spreads")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("instrument", instrument)
q.Set("period", strconv.Itoa(int(period)))
if unique {
q.Set("unique", "1")
} else {
q.Set("unique", "0")
}
u.RawQuery = q.Encode()
s := Spreads{}
if err = getAndDecode(c, u.String(), &s); err != nil {
return nil, err
}
return &s, nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// CommitmentsOfTraders
type CommitmentsOfTraders struct {
Date int64 `json:"date"`
Price float64 `json:"price,string"`
OverallInterest int `json:"oi,string"`
NonCommercialLong int `json:"ncl,string"`
NonCommercialShort int `json:"ncs,string"`
Unit string `json:"unit"`
}
func (c CommitmentsOfTraders) String() string {
t := time.Unix(0, c.Date*1000)
return fmt.Sprintf("CommitmentsOfTraders{Date: %s, Price: %f, OverallInterest: %d, "+
"NonCommercialLong: %d, NonCommercialShort: %d, Unit: %s}", t.Format(time.RFC3339), c.Price,
c.OverallInterest, c.NonCommercialLong, c.NonCommercialShort,
c.Unit)
}
// CommitmentsOfTraders returns up to 4 years of commitments of traders.
//
// The commitments of traders report is released by the CFTC and provides a breakdown of each
// Tuesday's open interest.
func (c *Client) CommitmentsOfTraders(instrument string) ([]CommitmentsOfTraders, error) {
instrument = strings.ToUpper(instrument)
u, err := url.Parse("/labs/v1/commitments_of_traders")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("instrument", instrument)
u.RawQuery = q.Encode()
m := make(map[string][]CommitmentsOfTraders)
if err = requestAndDecode(c, "GET", u.String(), nil, &m); err != nil {
return nil, err
}
cot, ok := m[instrument]
if !ok {
return nil, fmt.Errorf("No CommitmentsOfTraders found for instrument %s", instrument)
}
return cot, nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// OrderBooks
// Pricepoint defines the number of orders and positions at a certain price.
type PricePoint struct {
Price float64
OrdersShort float64 `json:"os"`
OrdersLong float64 `json:"ol"`
PositionsShort float64 `json:"ps"`
PositionsLong float64 `json:"pl"`
}
func (pp PricePoint) String() string {
return fmt.Sprintf("PricePoint{Price: %f, OrdersShort: %f, OrdersLong: %f, "+
"PositionsShort: %f, PositionsLong: %f}", pp.Price, pp.OrdersShort,
pp.OrdersLong, pp.PositionsShort, pp.PositionsLong)
}
// OrderBook represents the order book at a specific time.
type OrderBook struct {
Timestamp Time
MarketPrice float64
PricePoints []PricePoint
}
func (ob OrderBook) String() string {
return fmt.Sprintf("OrderBook{Timestamp: %v, MarketPrice: %f, PricePoints %v}",
ob.Timestamp, ob.MarketPrice, ob.PricePoints)
}
func (ob *OrderBook) UnmarshalJSON(data []byte) error {
v := struct {
MarketPrice *float64 `json:"rate"`
PricePoints map[string]PricePoint `json:"price_points"`
}{
MarketPrice: &ob.MarketPrice,
PricePoints: make(map[string]PricePoint),
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
ob.PricePoints = make([]PricePoint, 0, len(v.PricePoints))
for priceStr, pp := range v.PricePoints {
if price, err := strconv.ParseFloat(priceStr, 64); err != nil {
return err
} else {
pp.Price = price
}
ob.PricePoints = append(ob.PricePoints, pp)
}
return nil
}
type OrderBooks []OrderBook
func (obs *OrderBooks) UnmarshalJSON(data []byte) error {
m := make(map[string]OrderBook)
if err := json.Unmarshal(data, &m); err != nil {
return err
}
for timeStr, ob := range m {
ob.Timestamp = Time(timeStr)
*obs = append(*obs, ob)
}
return nil
}
// Orderbook returns historic order book data.
//
// See http://developer.oanda.com/docs/v1/forex-labs/#orderbook for further information.
func (c *Client) OrderBooks(instrument string, period Period) (OrderBooks, error) {
instrument = strings.ToUpper(instrument)
u, err := url.Parse("/labs/v1/orderbook_data")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("instrument", instrument)
q.Set("period", strconv.Itoa(int(period)))
u.RawQuery = q.Encode()
obs := make(OrderBooks, 0)
if err = getAndDecode(c, u.String(), &obs); err != nil {
return nil, err
}
obs.Sort()
return obs, nil
}
type pricePointSorter struct {
pricePoints []PricePoint
}
type orderBookSorter struct {
orderBooks OrderBooks
}
func (obs *orderBookSorter) Len() int { return len(obs.orderBooks) }
func (obs *orderBookSorter) Swap(i, j int) {
obs.orderBooks[i], obs.orderBooks[j] = obs.orderBooks[j], obs.orderBooks[i]
}
func (obs *orderBookSorter) Less(i, j int) bool {
return obs.orderBooks[i].Timestamp.UnixMicro() < obs.orderBooks[j].Timestamp.UnixMicro()
}
func (obs *OrderBooks) Sort() {
sort.Sort(&orderBookSorter{*obs})
for i := range *obs {
(*obs)[i].Sort()
}
}
func (pps *pricePointSorter) Len() int { return len(pps.pricePoints) }
func (pps *pricePointSorter) Swap(i, j int) {
pps.pricePoints[i], pps.pricePoints[j] = pps.pricePoints[j], pps.pricePoints[i]
}
func (pps *pricePointSorter) Less(i, j int) bool {
return pps.pricePoints[i].Price < pps.pricePoints[j].Price
}
func (ob *OrderBook) Sort() {
pps := pricePointSorter{ob.PricePoints}
sort.Sort(&pps)
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// AutochartistPattern
type AutochartistArg interface {
applyAutochartistArg(url.Values)
}
func (i Instrument) applyAutochartistArg(v url.Values) {
v.Set("instrument", strings.ToUpper(string(i)))
}
func (p Period) applyAutochartistArg(v url.Values) {
v.Set("period", strconv.Itoa(int(p)))
}
type Quality int
func (q Quality) applyAutochartistArg(v url.Values) {
v.Set("quality", strconv.Itoa(int(q)))
}
type Direction string
func (d Direction) applyAutochartistArg(v url.Values) {
v.Set("direction", string(d))
}
const (
Bullish Direction = "bullish"
Bearish Direction = "bearish"
)
type Stats struct {
Total int `json:"total"`
Percent float64 `json:"percent"`
Correct int `json:"correct"`
}
func (s Stats) String() string {
return fmt.Sprintf("Stats{Total: %d, Percent: %v, Correct: %d}", s.Total, s.Percent,
s.Correct)
}
type HistoricalStats struct {
HourOfDay Stats `json:"hourofday"`
Pattern Stats `json:"pattern"`
Symbol Stats `json:"symbol"`
}
func (s HistoricalStats) String() string {
return fmt.Sprintf("HistoricalStats{HourOfDay: %v, Pattern: %v, Symbol: %v}", s.HourOfDay,
s.Pattern, s.Symbol)
}
type AutochartistSignalMeta struct {
Completed int `json:"completed"`
Scores struct {
Uniformity int `json:"uniformity"`
Quality int `json:"quality"`
Breakout int `json:"breakout"`
InitialTrend int `json:"initialtrend"`
Clarity int `json:"clarity"`
}
Probability float64 `json:"probability"`
Interval int `json:"interval"`
Direction int `json:"direction"`
Pattern string `json:"pattern"`
Length int `json:"length"`
HistoricalStats HistoricalStats `json:"historicalstats"`
TrendType string `json:"trendtype"`
}
func (m AutochartistSignalMeta) String() string {
return fmt.Sprintf("Meta{Completed: %v, Scores{Uniformity: %v, Quality: %v, Breakout: %v, "+
"InitialTrend: %v, Clarity: %v}, Probability: %v, Interval: %v, Direction: %v, "+
"Pattern: %v, Length: %v, HistoricalStats: %v, TrendType: %v}", m.Completed,
m.Scores.Uniformity, m.Scores.Quality, m.Scores.Breakout, m.Scores.InitialTrend,
m.Scores.Clarity, m.Probability, m.Interval, m.Direction, m.Pattern, m.Length,
m.HistoricalStats, m.TrendType)
}
type Point struct {
X0 int64 `json:"x0"`
X1 int64 `json:"x1"`
Y0 float64 `json:"y0"`
Y1 float64 `json:"y1"`
}
func (p Point) String() string {
x0, x1 := time.Unix(0, p.X0*1000), time.Unix(0, p.X1*1000)
return fmt.Sprintf("Point{X0: %s, X1: %s, Y0: %v, Y1: %v}", x0.Format(time.RFC3339),
x1.Format(time.RFC3339), p.Y0, p.Y1)
}
type Prediction struct {
TimeTo int64 `json:"timeto"`
TimeFrom int64 `json:"timefrom"`
PriceHigh float64 `json:"pricehigh"`
PriceLow float64 `json:"pricelow"`
}
func (p Prediction) String() string {
tt, tf := time.Unix(0, p.TimeTo*1000), time.Unix(0, p.TimeFrom*1000)
return fmt.Sprintf("Prediction{TimeTo: %s, TimeFrom: %s, PriceHigh: %v, PriceLow: %v}",
tt.Format(time.RFC3339), tf.Format(time.RFC3339), p.PriceHigh, p.PriceLow)
}
type AutochartistSignalData struct {
PatternEndTime int64
Points struct {
Resistance Point `json:"resistance"`
Support Point `json:"support"`
} `json:"points"`
Prediction Prediction `json:"prediction"`
}
func (d AutochartistSignalData) String() string {
pet := time.Unix(0, d.PatternEndTime*1000)
return fmt.Sprintf("Data{PatternEndTime: %s, Points{Resistance: %v, Support: %v}, "+
"Prediction: %v}", pet.Format(time.RFC3339), d.Points.Resistance, d.Points.Support,
d.Prediction)
}
type AutochartistSignal struct {
Meta AutochartistSignalMeta `json:"meta"`
Id Id `json:"id"`
Instrument string `json:"instrument"`
Type string `json:"type"`
Data AutochartistSignalData `json:"data"`
}
func (s AutochartistSignal) String() string {
return fmt.Sprintf("Signal{Id: %v, Instrument %v, Type: %v, Data: %v, Meta: %v}", s.Id,
s.Instrument, s.Type, s.Data, s.Meta)
}
type AutochartistPattern struct {
Signals []AutochartistSignal `json:"signals"`
Provider string `json:"provider"`
}
func (p AutochartistPattern) String() string {
return fmt.Sprintf("AutochartistPattern{Provider: %v, Signals: %v}", p.Provider,
p.Signals)
}
// AutochartistPattern
func (c *Client) AutochartistPattern(arg ...AutochartistArg) (*AutochartistPattern, error) {
u, err := url.Parse("/labs/v1/signal/autochartist")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("type", "chartpattern")
for _, a := range arg {
a.applyAutochartistArg(q)
}
u.RawQuery = q.Encode()
pattern := AutochartistPattern{}
if err := getAndDecode(c, u.String(), &pattern); err != nil {
return nil, err
}
return &pattern, nil
}