-
Notifications
You must be signed in to change notification settings - Fork 29
/
trades.go
211 lines (195 loc) · 7.42 KB
/
trades.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
package goanda
// Supporting OANDA docs - http://developer.oanda.com/rest-live-v20/trade-ep/
import (
"time"
)
type ReceivedTrades struct {
LastTransactionID string `json:"lastTransactionID"`
Trades []Trade `json:"trades"`
}
type ReceivedTrade struct {
LastTransactionID string `json:"lastTransactionID"`
Trade Trade `json:"trade"`
}
type Trade struct {
ID string `json:"id"`
Instrument string `json:"instrument"`
Price string `json:"price"`
OpenTime time.Time `json:"openTime"`
State string `json:"state"`
InitialUnits string `json:"initialUnits"`
InitialMarginRequired string `json:"initialMarginRequired"`
CurrentUnits string `json:"currentUnits"`
RealizedPL string `json:"realizedPL"`
UnrealizedPL string `json:"unrealizedPL"`
MarginUsed string `json:"marginUsed"`
AverageClosePrice string `json:"averageClosePrice,omitempty"`
ClosingTransactionIDs []string `json:"closingTransactionIDs,omitempty"`
Financing string `json:"financing"`
CloseTime time.Time `json:"closeTime,omitempty"`
ClientExtensions *OrderExtensions `json:"clientExtensions,omitempty"`
TakeProfitOrder *TakeProfitOrder `json:"takeProfitOrder,omitempty"`
StopLossOrder *StopLossOrder `json:"stopLossOrder,omitempty"`
TrailingStopLossOrder *TrailingStopLossOrder `json:"trailingStopLossOrder,omitempty"`
}
type TakeProfitOrder struct {
ID string `json:"id"`
CreateTime time.Time `json:"createTime"`
Type string `json:"type"`
TradeID string `json:"tradeID"`
ClientTradeID string `json:"clientTradeID,omitempty"`
Price string `json:"price"`
TimeInForce string `json:"timeInForce"`
TriggerCondition string `json:"triggerCondition"`
State string `json:"state"`
ClientExtensions *OrderExtensions `json:"clientExtensions,omitempty"`
}
type StopLossOrder struct {
ID string `json:"id"`
CreateTime time.Time `json:"createTime"`
Type string `json:"type"`
TradeID string `json:"tradeID"`
ClientTradeID string `json:"clientTradeID,omitempty"`
Price string `json:"price"`
TimeInForce string `json:"timeInForce"`
TriggerCondition string `json:"triggerCondition"`
State string `json:"state"`
ClientExtensions *OrderExtensions `json:"clientExtensions,omitempty"`
Guaranteed bool `json:"guaranteed"`
}
type TrailingStopLossOrder struct {
ID string `json:"id"`
CreateTime time.Time `json:"createTime"`
Type string `json:"type"`
TradeID string `json:"tradeID"`
ClientTradeID string `json:"clientTradeID,omitempty"`
Distance string `json:"distance"`
TimeInForce string `json:"timeInForce"`
TriggerCondition string `json:"triggerCondition"`
State string `json:"state"`
ClientExtensions *OrderExtensions `json:"clientExtensions,omitempty"`
}
type CloseTradePayload struct {
Units string `json:"units"`
}
type ModifiedTrade struct {
OrderCreateTransaction struct {
Type string `json:"type"`
Instrument string `json:"instrument"`
Units string `json:"units"`
TimeInForce string `json:"timeInForce"`
PositionFill string `json:"positionFill"`
Reason string `json:"reason"`
TradeClose struct {
Units string `json:"units"`
TradeID string `json:"tradeID"`
} `json:"tradeClose"`
ID string `json:"id"`
UserID int `json:"userID"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
RequestID string `json:"requestID"`
Time time.Time `json:"time"`
} `json:"orderCreateTransaction"`
OrderFillTransaction struct {
Type string `json:"type"`
Instrument string `json:"instrument"`
Units string `json:"units"`
Price string `json:"price"`
FullPrice FullPrice `json:"fullPrice"`
PL string `json:"pl"`
Financing string `json:"financing"`
Commission string `json:"commission"`
AccountBalance string `json:"accountBalance"`
TradeOpened string `json:"tradeOpened"`
TimeInForce string `json:"timeInForce"`
PositionFill string `json:"positionFill"`
Reason string `json:"reason"`
TradesClosed []struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
RealizedPL string `json:"realizedPL"`
Financing string `json:"financing"`
} `json:"tradesClosed"`
TradeReduced struct {
TradeID string `json:"tradeID"`
Units string `json:"units"`
RealizedPL string `json:"realizedPL"`
Financing string `json:"financing"`
} `json:"tradeReduced"`
ID string `json:"id"`
UserID int `json:"userID"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
RequestID string `json:"requestID"`
OrderID string `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Time time.Time `json:"time"`
} `json:"orderFillTransaction"`
OrderCancelTransaction struct {
Type string `json:"type"`
OrderID string `json:"orderID"`
Reason string `json:"reason"`
ID string `json:"id"`
UserID int `json:"userID"`
AccountID string `json:"accountID"`
BatchID string `json:"batchID"`
RequestID string `json:"requestID"`
Time time.Time `json:"time"`
} `json:"orderCancelTransaction"`
RelatedTransactionIDs []string `json:"relatedTransactionIDs"`
LastTransactionID string `json:"lastTransactionID"`
}
type FullPrice struct {
CloseoutBid string `json:"closeoutBid"`
CloseoutAsk string `json:"closeoutAsk"`
Timestamp string `json:"timestamp"`
Bids []PriceLevel `json:"bids"`
Asks []PriceLevel `json:"asks"`
}
type PriceLevel struct {
Price string `json:"price"`
Liquidity string `json:"liquidity"`
}
func (c *Connection) GetTradesForInstrument(instrument string) (ReceivedTrades, error) {
rt := ReceivedTrades{}
err := c.getAndUnmarshal(
"/accounts/"+
c.accountID+
"/trades"+
"?instrument="+
instrument,
&rt,
)
return rt, err
}
func (c *Connection) GetOpenTrades() (ReceivedTrades, error) {
rt := ReceivedTrades{}
err := c.getAndUnmarshal("/accounts/"+c.accountID+"/openTrades", &rt)
return rt, err
}
func (c *Connection) GetTrade(ticket string) (ReceivedTrade, error) {
rt := ReceivedTrade{}
err := c.getAndUnmarshal(
"/accounts/"+
c.accountID+
"/trades/"+
ticket,
&rt,
)
return rt, err
}
// Default is close the whole position using the string "ALL" in body.units
func (c *Connection) ReduceTradeSize(ticket string, body CloseTradePayload) (ModifiedTrade, error) {
mt := ModifiedTrade{}
err := c.putAndUnmarshal(
"/accounts/"+
c.accountID+
"/trades/"+
ticket+
"/close",
body,
&mt,
)
return mt, err
}