-
Notifications
You must be signed in to change notification settings - Fork 9
/
account.go
234 lines (209 loc) · 6.18 KB
/
account.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
package tradier
import (
"encoding/json"
)
type Margin struct {
FedCall int `json:"fed_call"`
MaintenanceCall int `json:"maintenance_call"`
OptionBuyingPower float64 `json:"option_buying_power"`
StockBuyingPower float64 `json:"stock_buying_power"`
StockShortValue float64 `json:"stock_short_value"`
Sweep int
}
type Cash struct {
CashAvailable float64 `json:"cash_available"`
Sweep int
UnsettledFunds float64 `json:"unsettled_funds"`
}
type PDT struct {
DayTradeBuyingPower float64 `json:"day_trade_buying_power"`
FedCall int `json:"fed_call"`
MaintenanceCall int `json:"maintenance_call"`
OptionBuyingPower float64 `json:"option_buying_power"`
StockBuyingPower float64 `json:"stock_buying_power"`
StockShortValue float64 `json:"stock_short_value"`
}
type AccountBalances struct {
AccountNumber string `json:"account_number"`
AccountType string `json:"account_type"`
ClosePL float64 `json:"close_pl"`
CurrentRequirement float64 `json:"current_requirement"`
Equity float64
LongMarketValue float64 `json:"long_market_value"`
MarketValue float64 `json:"market_value"`
OpenPL float64 `json:"open_pl"`
OptionLongValue float64 `json:"option_long_value"`
OptionRequirement float64 `json:"option_requirement"`
OptionShortValue float64 `json:"option_short_value"`
PendingOrdersCount int `json:"pending_orders_count"`
ShortMarketValue float64 `json:"short_market_value"`
StockLongValue float64 `json:"stock_long_value"`
TotalCash float64 `json:"total_cash"`
TotalEquity float64 `json:"total_equity"`
UnclearedFunds float64 `json:"uncleared_funds"`
Margin Margin
Cash Cash
PDT PDT
}
type Position struct {
CostBasis float64 `json:"cost_basis"`
DateAcquired DateTime `json:"date_acquired"`
Id int
Quantity float64
Symbol string
}
type Trade struct {
Commission float64
Description string
Price float64
Quantity float64
Symbol string
TradeType string `json:"trade_type"`
}
type Adjustment struct {
Description string
Quantity float64
}
type Event struct {
Amount float64
Date DateTime
Type string
Trade Trade
Adjustment Adjustment
}
type ClosedPosition struct {
CloseDate DateTime `json:"close_date"`
Cost float64
GainLoss float64 `json:"gain_loss"`
GainLossPercent float64 `json:"gain_loss_percent"`
OpenDate DateTime `json:"open_date"`
Proceeds float64
Quantity float64
Symbol string
Term int
}
const (
// Order classes.
Equity = "equity"
Option = "option"
Multileg = "multileg"
Combo = "combo"
OneTriggersOther = "oto"
OneCancelsOther = "oco"
OneTriggersOneCancelsOther = "otoco"
// Order sides.
Buy = "buy"
BuyToCover = "buy_to_cover"
BuyToOpen = "buy_to_open"
BuyToClose = "buy_to_close"
Sell = "sell"
SellShort = "sell_short"
SellToOpen = "sell_to_open"
SellToClose = "sell_to_close"
// Order types.
MarketOrder = "market"
LimitOrder = "limit"
StopOrder = "stop"
StopLimitOrder = "stop_limit"
Credit = "credit"
Debit = "debit"
Even = "even"
// Order durations.
Day = "day"
GTC = "gtc"
PreMarket = "pre"
PostMarket = "post"
// Option types.
Put = "put"
Call = "call"
// Order statuses.
StatusOK = "ok"
Filled = "filled"
Canceled = "canceled"
Open = "open"
Expired = "expired"
Rejected = "rejected"
Pending = "pending"
PartiallyFilled = "partially_filled"
Submitted = "submitted"
)
type Order struct {
Id int
Type string
Symbol string
OptionSymbol string `json:"option_symbol"`
Side string
Quantity float64
Status string
Duration string
Price float64
StopPrice float64 `json:"stop_price"`
OptionType string `json:"option_type"`
ExpirationDate DateTime `json:"expiration_date"`
Exchange string `json:"exch"`
AverageFillPrice float64 `json:"avg_fill_price"`
ExecutedQuantity float64 `json:"exec_quantity"`
ExecutionExchange string `json:"exec_exch"`
LastFillPrice float64 `json:"last_fill_price"`
LastFillQuantity float64 `json:"last_fill_quantity"`
RemainingQuantity float64 `json:"remaining_quantity"`
CreateDate DateTime `json:"create_date"`
TransactionDate DateTime `json:"transaction_date"`
Class string
NumLegs int `json:"num_legs"`
Legs []Order
Strategy string
}
// If there is only a single event, then tradier sends back
// an object, but if there are multiple events, then it sends
// a list of objects...
type OpenOrders []*Order
func (oo *OpenOrders) UnmarshalJSON(data []byte) error {
orders := make([]*Order, 0)
if err := json.Unmarshal(data, &orders); err == nil {
*oo = orders
return nil
}
order := Order{}
err := json.Unmarshal(data, &order)
if err == nil {
*oo = []*Order{&order}
}
return err
}
// Helper struct for decoding Tradier's response to open orders request,
// which returns null if there are no open orders, a list if there are
// multiple open orders, or a single object if there is just one.
type openOrdersResponse struct {
Orders struct {
Order OpenOrders
}
}
func (oor *openOrdersResponse) UnmarshalJSON(data []byte) error {
// If there are no open orders, then Tradier returns "null".
var noOrders struct {
Orders string
}
err := json.Unmarshal(data, &noOrders)
if err == nil {
return nil
}
// Otherwise, unmarshal the results using the default unmarshaler.
var result struct {
Orders struct {
Order OpenOrders
}
}
err = json.Unmarshal(data, &result)
oor.Orders = result.Orders
return err
}
type OrderPreview struct {
Commission float64
Cost float64
ExtendedHours bool `json:"extended_hours"`
Fees float64
MarginChange float64 `json:"margin_change"`
Quantity float64
Status string
}