forked from AwolDes/goanda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrument.go
149 lines (130 loc) · 4.09 KB
/
instrument.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
package goanda
// Supporting OANDA docs - http://developer.oanda.com/rest-live-v20/instrument-ep/
import (
"time"
)
type Candle struct {
Open float64 `json:"o,string"`
Close float64 `json:"c,string"`
Low float64 `json:"l,string"`
High float64 `json:"h,string"`
}
type Candles struct {
Complete bool `json:"complete"`
Volume int `json:"volume"`
Time time.Time `json:"time"`
Mid Candle `json:"mid"`
}
type BidAskCandles struct {
Candles []struct {
Ask struct {
C float64 `json:"c,string"`
H float64 `json:"h,string"`
L float64 `json:"l,string"`
O float64 `json:"o,string"`
} `json:"ask"`
Bid struct {
C float64 `json:"c,string"`
H float64 `json:"h,string"`
L float64 `json:"l,string"`
O float64 `json:"o,string"`
} `json:"bid"`
Complete bool `json:"complete"`
Time time.Time `json:"time"`
Volume int `json:"volume"`
} `json:"candles"`
}
type InstrumentHistory struct {
Instrument string `json:"instrument"`
Granularity string `json:"granularity"`
Candles []Candles `json:"candles"`
}
type Bucket struct {
Price string `json:"price"`
LongCountPercent string `json:"longCountPercent"`
ShortCountPercent string `json:"shortCountPercent"`
}
type BrokerBook struct {
Instrument string `json:"instrument"`
Time time.Time `json:"time"`
Price string `json:"price"`
BucketWidth string `json:"bucketWidth"`
Buckets []Bucket `json:"buckets"`
}
type InstrumentPricing struct {
Time time.Time `json:"time"`
Prices []struct {
Type string `json:"type"`
Time time.Time `json:"time"`
Bids []struct {
Price float64 `json:"price,string"`
Liquidity int `json:"liquidity"`
} `json:"bids"`
Asks []struct {
Price float64 `json:"price,string"`
Liquidity int `json:"liquidity"`
} `json:"asks"`
CloseoutBid float64 `json:"closeoutBid,string"`
CloseoutAsk float64 `json:"closeoutAsk,string"`
Status string `json:"status"`
Tradeable bool `json:"tradeable"`
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"`
QuoteHomeConversionFactors struct {
PositiveUnits string `json:"positiveUnits"`
NegativeUnits string `json:"negativeUnits"`
} `json:"quoteHomeConversionFactors"`
Instrument string `json:"instrument"`
} `json:"prices"`
}
func (c *OandaConnection) GetCandles(instrument string, count string, granularity string) InstrumentHistory {
endpoint := "/instruments/" + instrument + "/candles?count=" + count + "&granularity=" + granularity
candles := c.Request(endpoint)
data := InstrumentHistory{}
unmarshalJson(candles, &data)
return data
}
func (c *OandaConnection) GetBidAskCandles(instrument string, count string, granularity string) BidAskCandles {
endpoint := "/instruments/" + instrument + "/candles?count=" + count + "&granularity=" + granularity + "&price=BA"
candles := c.Request(endpoint)
data := BidAskCandles{}
unmarshalJson(candles, &data)
return data
}
func (c *OandaConnection) OrderBook(instrument string) BrokerBook {
endpoint := "/instruments/" + instrument + "/orderBook"
orderbook := c.Request(endpoint)
data := BrokerBook{}
unmarshalJson(orderbook, &data)
return data
}
func (c *OandaConnection) PositionBook(instrument string) BrokerBook {
endpoint := "/instruments/" + instrument + "/positionBook"
orderbook := c.Request(endpoint)
data := BrokerBook{}
unmarshalJson(orderbook, &data)
return data
}
func (c *OandaConnection) GetInstrumentPrice(instrument string) InstrumentPricing {
endpoint := "/accounts/" + c.accountID + "/pricing?instruments=" + instrument
pricing := c.Request(endpoint)
data := InstrumentPricing{}
unmarshalJson(pricing, &data)
return data
}