forked from AwolDes/goanda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
99 lines (85 loc) · 3.02 KB
/
transactions.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
package goanda
import (
"net/url"
"time"
)
// Supporting OANDA docs - http://developer.oanda.com/rest-live-v20/transaction-ep/
type TransactionPages struct {
Count int `json:"count"`
From time.Time `json:"from"`
LastTransactionID string `json:"lastTransactionID"`
PageSize int `json:"pageSize"`
Pages []string `json:"pages"`
To time.Time `json:"to"`
}
type Transaction struct {
LastTransactionID string `json:"lastTransactionID"`
Transaction struct {
AccountBalance string `json:"accountBalance"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
Financing string `json:"financing"`
ID string `json:"id"`
Instrument string `json:"instrument"`
OrderID string `json:"orderID"`
Pl string `json:"pl"`
Price string `json:"price"`
Reason string `json:"reason"`
Time time.Time `json:"time"`
TradeOpened struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
} `json:"tradeOpened"`
Type string `json:"type"`
Units string `json:"units"`
UserID int `json:"userID"`
} `json:"transaction"`
}
type Transactions struct {
LastTransactionID string `json:"lastTransactionID"`
Transactions []struct {
AccountBalance string `json:"accountBalance"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
Financing string `json:"financing"`
ID string `json:"id"`
Instrument string `json:"instrument"`
OrderID string `json:"orderID"`
Pl string `json:"pl"`
Price string `json:"price"`
Reason string `json:"reason"`
Time time.Time `json:"time"`
TradeOpened struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
} `json:"tradeOpened"`
Type string `json:"type"`
Units string `json:"units"`
UserID int `json:"userID"`
} `json:"transactions"`
}
// https://golang.org/pkg/time/#Time.AddDate
// https://play.golang.org/p/Dw7D4JJ7EC
func (c *OandaConnection) GetTransactions(from time.Time, to time.Time) TransactionPages {
toTime := to.Format(time.RFC3339)
fromTime := from.Format(time.RFC3339)
endpoint := "/accounts/" + c.accountID + "/transactions?to=" + url.QueryEscape(toTime) + "&from=" + url.QueryEscape(fromTime)
response := c.Request(endpoint)
data := TransactionPages{}
unmarshalJson(response, &data)
return data
}
func (c *OandaConnection) GetTransaction(ticket string) Transaction {
endpoint := "/accounts/" + c.accountID + "/transactions/" + ticket
response := c.Request(endpoint)
data := Transaction{}
unmarshalJson(response, &data)
return data
}
func (c *OandaConnection) GetTransactionsSinceId(id string) Transactions {
endpoint := "/accounts/" + c.accountID + "/transactions/sinceid?id=" + id
response := c.Request(endpoint)
data := Transactions{}
unmarshalJson(response, &data)
return data
}