-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccounts-trading.go
352 lines (321 loc) · 9.22 KB
/
accounts-trading.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
/*
Copyright (C) 2025 github.com/go-schwab
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see
<https://www.gnu.org/licenses/>.
*/
package trader
import (
"fmt"
"io"
"net/http"
"strings"
"github.com/bytedance/sonic"
)
var (
accountEndpoint string = "https://api.schwabapi.com/trader/v1"
endpointAccountNumbers string = accountEndpoint + "/accounts/accountNumbers"
endpointAccounts string = accountEndpoint + "/accounts"
endpointAccount string = accountEndpoint + "/accounts/%s"
// endpointUserPreference string = accountEndpoint + "/userPreference"
endpointOrders string = accountEndpoint + "/orders"
endpointAccountOrders string = accountEndpoint + "/accounts/%s/orders"
endpointAccountOrder string = accountEndpoint + "/accounts/%s/orders/%s"
// endpointPreviewOrder string = accountEndpoint + "/accounts/%s/previewOrder"
// endpointTransactions string = accountEndpoint + "/accounts/%s/transactions"
endpointTransaction string = accountEndpoint + "/accounts/%s/transactions/%s"
OrderTemplate = `
{
"orderType": "%s",
"session": "%s",
"duration": "%s",
"orderStrategyType": "%s",
"orderLegCollection": [
%s
]
}
`
LegTemplate = `
{
"instruction": "%s",
"quantity": %f,
"instrument": {
"symbol": "%s",
"assetType": "%s"
}
},
`
LegTemplateLast = `
{
"instruction": "%s",
"quantity": %f,
"instrument": {
"symbol": "%s",
"assetType": "%s"
}
},
`
)
// Create a new Market order
func CreateSingleLegOrder(opts ...SingleLegOrderComposition) *SingleLegOrder {
order := &SingleLegOrder{OrderType: "MARKET"}
for _, opt := range opts {
opt(order)
}
return order
}
// Set SingleLegOrder.OrderType
func OrderType(t string) SingleLegOrderComposition {
return func(order *SingleLegOrder) {
order.OrderType = t
}
}
// Set SingleLegOrder.Session
func Session(session string) SingleLegOrderComposition {
return func(order *SingleLegOrder) {
order.Session = session
}
}
// Set SingleLegOrder.Duration
func Duration(duration string) SingleLegOrderComposition {
return func(order *SingleLegOrder) {
order.Duration = duration
}
}
// Set SingleLegOrder.Strategy
func Strategy(strategy string) SingleLegOrderComposition {
return func(order *SingleLegOrder) {
order.Strategy = strategy
}
}
// Set SingleLegOrder.Instruction
func Instruction(instruction string) SingleLegOrderComposition {
return func(order *SingleLegOrder) {
order.Instruction = instruction
}
}
// Set SingleLegOrder.Quantity
func Quantity(quantity float32) SingleLegOrderComposition {
return func(order *SingleLegOrder) {
order.Quantity = quantity
}
}
// Set SingleLegOrder.Instrument
func Instrument(instrument SimpleOrderInstrument) SingleLegOrderComposition {
return func(order *SingleLegOrder) {
order.Instrument = instrument
}
}
func marshalSingleLegOrder(order *SingleLegOrder) string {
return fmt.Sprintf(OrderTemplate, order.OrderType, order.Session, order.Duration, order.Strategy, fmt.Sprintf(LegTemplate, order.Instruction, order.Quantity, order.Instrument.Symbol, order.Instrument.AssetType))
}
func marshalMultiLegOrder(order *MultiLegOrder) string {
var legs string
// UNTESTED
for i, leg := range order.OrderLegCollection {
if i != len(order.OrderLegCollection)-1 {
legs += fmt.Sprintf(LegTemplate, leg.Instruction, leg.Quantity, leg.Instrument.Symbol, leg.Instrument.AssetType)
} else {
legs += fmt.Sprintf(LegTemplateLast, leg.Instruction, leg.Quantity, leg.Instrument.Symbol, leg.Instrument.AssetType)
}
}
return fmt.Sprintf(OrderTemplate)
}
// Submit a single-leg order for the specified encrypted account ID
func (agent *Agent) SubmitSingleLegOrder(hashValue string, order *SingleLegOrder) error {
orderJson := marshalSingleLegOrder(order)
req, err := http.NewRequest("POST", fmt.Sprintf(endpointAccountOrders, hashValue), strings.NewReader(orderJson))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
_, err = agent.Handler(req)
if err != nil {
return err
}
return nil
}
// Get a specific order by account number & order ID
func (agent *Agent) GetOrder(accountNumber, orderID string) (FullOrder, error) {
req, err := http.NewRequest("GET", fmt.Sprintf(endpointAccountOrder, accountNumber, orderID), nil)
if err != nil {
return FullOrder{}, err
}
resp, err := agent.Handler(req)
if err != nil {
return FullOrder{}, err
}
var order FullOrder
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return FullOrder{}, err
}
err = sonic.Unmarshal(body, &order)
if err != nil {
return FullOrder{}, err
}
return order, nil
}
// fromEnteredTime, toEnteredTime format:
// yyyy-MM-ddTHH:mm:ss.SSSZ
func (agent *Agent) GetAccountOrders(accountNumber, fromEnteredTime, toEnteredTime string) ([]FullOrder, error) {
req, err := http.NewRequest("GET", fmt.Sprintf(endpointAccountOrders, accountNumber), nil)
if err != nil {
return []FullOrder{}, err
}
q := req.URL.Query()
q.Add("fromEnteredTime", fromEnteredTime)
q.Add("toEnteredTime", toEnteredTime)
req.URL.RawQuery = q.Encode()
resp, err := agent.Handler(req)
if err != nil {
return []FullOrder{}, err
}
var orders []FullOrder
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []FullOrder{}, err
}
err = sonic.Unmarshal(body, &orders)
if err != nil {
return []FullOrder{}, err
}
return orders, nil
}
// WIP: do not use
// fromEnteredTime, toEnteredTime format:
// yyyy-MM-ddTHH:mm:ss.SSSZ
func (agent *Agent) GetAllOrders(fromEnteredTime, toEnteredTime string) ([]FullOrder, error) {
req, err := http.NewRequest("GET", endpointOrders, nil)
if err != nil {
return []FullOrder{}, err
}
q := req.URL.Query()
q.Add("fromEnteredTime", fromEnteredTime)
q.Add("toEnteredTime", toEnteredTime)
req.URL.RawQuery = q.Encode()
resp, err := agent.Handler(req)
if err != nil {
return []FullOrder{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []FullOrder{}, err
}
var orders []FullOrder
err = sonic.Unmarshal(body, &orders)
if err != nil {
fmt.Println(body)
return []FullOrder{}, err
}
return orders, nil
}
// Get encrypted account numbers for trading
func (agent *Agent) GetAccountNumbers() ([]AccountNumbers, error) {
req, err := http.NewRequest("GET", endpointAccountNumbers, nil)
if err != nil {
return []AccountNumbers{}, err
}
resp, err := agent.Handler(req)
if err != nil {
return []AccountNumbers{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []AccountNumbers{}, err
}
var accountNumbers []AccountNumbers
err = sonic.Unmarshal(body, &accountNumbers)
if err != nil {
return []AccountNumbers{}, err
}
return accountNumbers, nil
}
// Get all accounts associated with the user logged in
func (agent *Agent) GetAccounts(fields ...string) ([]Account, error) {
var fieldsRequest string = strings.Join(fields, ",")
req, err := http.NewRequest("GET", endpointAccounts, nil)
if err != nil {
return []Account{}, err
}
q := req.URL.Query()
q.Add("fields", fieldsRequest)
req.URL.RawQuery = q.Encode()
resp, err := agent.Handler(req)
if err != nil {
return []Account{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []Account{}, err
}
var accounts []Account
err = sonic.Unmarshal(body, &accounts)
if err != nil {
return []Account{}, err
}
return accounts, nil
}
// Get account by encrypted account id
func (agent *Agent) GetAccount(id string, fields ...string) (Account, error) {
var fieldsRequest string = strings.Join(fields, ",")
req, err := http.NewRequest("GET", fmt.Sprintf(endpointAccount, id), nil)
if err != nil {
return Account{}, err
}
q := req.URL.Query()
q.Add("fields", fieldsRequest)
req.URL.RawQuery = q.Encode()
resp, err := agent.Handler(req)
if err != nil {
return Account{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Account{}, err
}
var account Account
err = sonic.Unmarshal(body, &account)
if err != nil {
return Account{}, err
}
return account, nil
}
// Get all transactions for the user logged in
// func (agent *Agent) GetTransactions() ([]Transaction, error) {}
// Get a transaction for a specific account id
func (agent *Agent) GetTransaction(accountNumber, transactionId string) (Transaction, error) {
req, err := http.NewRequest("GET", fmt.Sprintf(endpointTransaction, accountNumber, transactionId), nil)
if err != nil {
return Transaction{}, err
}
resp, err := agent.Handler(req)
if err != nil {
return Transaction{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Transaction{}, err
}
var transaction Transaction
err = sonic.Unmarshal(body, &transaction)
if err != nil {
return Transaction{}, err
}
return transaction, nil
}