forked from zricethezav/go-tdameritrade
-
Notifications
You must be signed in to change notification settings - Fork 28
/
txnHistory.go
134 lines (119 loc) · 5.28 KB
/
txnHistory.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
package tdameritrade
import (
"context"
"fmt"
"github.com/google/go-querystring/query"
)
// Transactions is a slice of transactions
type Transactions []*Transaction
// Transaction represents a single transaction
type Transaction struct {
Type string `json:"type"`
ClearingReferenceNumber string `json:"clearingReferenceNumber"`
SubAccount string `json:"subAccount"`
SettlementDate string `json:"settlementDate"`
OrderID string `json:"orderId"`
SMA float64 `json:"sma"`
RequirementReallocationAmount float64 `json:"requirementReallocationAmount"`
DayTradeBuyingPowerEffect float64 `json:"dayTradeBuyingPowerEffect"`
NetAmount float64 `json:"netAmount"`
TransactionDate string `json:"transactionDate"`
OrderDate string `json:"orderDate"`
TransactionSubType string `json:"transactionSubType"`
TransactionID int64 `json:"transactionId"`
CashBalanceEffectFlag bool `json:"cashBalanceEffectFlag"`
Description string `json:"description"`
ACHStatus string `json:"achStatus"`
AccruedInterest float64 `json:"accruedInterest"`
Fees TransactionFees `json:"fees"`
TransactionItem TransactionItem `json:"transactionItem"`
}
// TransactionFees contains fees related to the transaction
type TransactionFees struct {
AdditionalFee float64 `json:"additionalFee"`
CdscFee float64 `json:"cdscFee"`
Commission float64 `json:"commission"`
OptRegFee float64 `json:"optRegFee"`
OtherCharges float64 `json:"otherCharges"`
RFee float64 `json:"rFee"`
RegFee float64 `json:"regFee"`
SecFee float64 `json:"secFee"`
}
// TransactionItem is an item within a transaction response
type TransactionItem struct {
AccountID int32 `json:"accountId"`
Amount float64 `json:"amount"`
Price float64 `json:"price"`
Cost float64 `json:"cost"`
ParentOrderKey int32 `json:"parentOrderKey"`
ParentChildIndicator string `json:"parentChildIndicator"`
Instruction string `json:"instruction"`
PositionEffect string `json:"positionEffect"`
Instrument TransactionInstrument `json:"instrument"`
}
// TransactionInstrument is the instrumnet traded within a transaction
type TransactionInstrument struct {
Symbol string `json:"symbol"`
UnderlyingSymbol string `json:"underlyingSymbol"`
OptionExpirationDate string `json:"optionExpirationDate"`
OptionStrikePrice float64 `json:"optionStrikePrice"`
PutCall string `json:"putCall"`
CUSIP string `json:"cusip"`
Description string `json:"description"`
AssetType string `json:"assetType"`
BondMaturityDate string `json:"bondMaturityDate"`
BondInterestRate float64 `json:"bondInterestRate"`
}
// TransactionHistoryOptions is parsed and translated to query options in the https request
type TransactionHistoryOptions struct {
Type string `url:"type,omitempty"`
Symbol string `url:"symbol,omitempty"`
// ISO8601 format, day granularity yyyy-MM-dd
StartDate string `url:"startDate,omitempty"`
// ISO8601 format, day granularity yyyy-MM-dd
EndDate string `url:"endDate,omitempty"`
}
// TransactionHistoryService handles communication with the transaction history related methods of
// the TDAmeritrade API.
//
// TDAmeritrade API docs: https://developer.tdameritrade.com/transaction-history/apis
type TransactionHistoryService struct {
client *Client
}
// GetTransaction gets a specific transaction by account
// TDAmeritrade API Docs: https://developer.tdameritrade.com/transaction-history/apis/get/accounts/%7BaccountId%7D/transactions/%7BtransactionId%7D-0
func (s *TransactionHistoryService) GetTransaction(ctx context.Context, accountID string, transactionID string) (*Transaction, *Response, error) {
u := fmt.Sprintf("accounts/%s/transactions/%s", accountID, transactionID)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
txn := new(Transaction)
resp, err := s.client.Do(ctx, req, txn)
if err != nil {
return nil, resp, err
}
return txn, resp, nil
}
// GetTransactions gets all transaction by account
// TDAmeritrade API Docs: https://developer.tdameritrade.com/transaction-history/apis/get/accounts/%7BaccountId%7D/transactions-0
func (s *TransactionHistoryService) GetTransactions(ctx context.Context, accountID string, opts *TransactionHistoryOptions) (*Transactions, *Response, error) {
u := fmt.Sprintf("accounts/%s/transactions", accountID)
if opts != nil {
q, err := query.Values(opts)
if err != nil {
return nil, nil, err
}
u = fmt.Sprintf("%s?%s", u, q.Encode())
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
txns := new(Transactions)
resp, err := s.client.Do(ctx, req, txns)
if err != nil {
return nil, resp, err
}
return txns, resp, nil
}