Skip to content

Commit

Permalink
major:
Browse files Browse the repository at this point in the history
ref: #67, v0.9.0
desc:
- refactor utils.go to reflect removal of previous oauth implementation in favor of x/oauth2, vis-a-vis go-schwab/oauth2ns
- check -> isErrNil
  • Loading branch information
samjtro committed Oct 25, 2024
1 parent 63570f8 commit 2502992
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 190 deletions.
78 changes: 42 additions & 36 deletions accounts-trading.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ import (
"github.com/bytedance/sonic"
)

/* TODO:
[ ] http.NewRequest -> agent.client
*/

var (
accountEndpoint string = "https://api.schwabapi.com/trader/v1"

// Accounts
endpointAccountNumbers string = accountEndpoint + "/accounts/accountNumbers"
endpointAccounts string = accountEndpoint + "/accounts"
endpointAccount string = accountEndpoint + "/accounts/%s"
//endpointUserPreference string = accountEndpoint + "/userPreference"
// endpointUserPreference string = accountEndpoint + "/userPreference"

// Orders
endpointOrders string = accountEndpoint + "/orders"
endpointAccountOrders string = accountEndpoint + "/accounts/%s/orders"
endpointAccountOrder string = accountEndpoint + "/accounts/%s/orders/%s"
//endpointPreviewOrder string = accountEndpoint + "/accounts/%s/previewOrder"
// endpointPreviewOrder string = accountEndpoint + "/accounts/%s/previewOrder"

// Transactions
//endpointTransactions string = accountEndpoint + "/accounts/%s/transactions"
// endpointTransactions string = accountEndpoint + "/accounts/%s/transactions"
endpointTransaction string = accountEndpoint + "/accounts/%s/transactions/%s"
)

Expand Down Expand Up @@ -315,8 +319,10 @@ type SimpleOrderInstrument struct {
AssetType string // EQUITY
}

type SingleLegOrderComposition func(order *SingleLegOrder)
type MultiLegSimpleOrderComposition func(order *MultiLegOrder)
type (
SingleLegOrderComposition func(order *SingleLegOrder)
MultiLegSimpleOrderComposition func(order *MultiLegOrder)
)

// Create a new Market order
func CreateSingleLegOrder(opts ...SingleLegOrderComposition) *SingleLegOrder {
Expand Down Expand Up @@ -431,45 +437,45 @@ func marshalMultiLegOrder(order *MultiLegOrder) string {
func (agent *Agent) SubmitSingleLegOrder(hashValue string, order *SingleLegOrder) error {
orderJson := marshalSingleLegOrder(order)
req, err := http.NewRequest("POST", fmt.Sprintf(endpointAccountOrders, hashValue), strings.NewReader(orderJson))
check(err)
isErrNil(err)
req.Header.Set("Content-Type", "application/json")
_, err = agent.Handler(req)
check(err)
isErrNil(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)
check(err)
isErrNil(err)
resp, err := agent.Handler(req)
check(err)
isErrNil(err)
var order FullOrder
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
check(err)
isErrNil(err)
err = sonic.Unmarshal(body, &order)
check(err)
isErrNil(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)
check(err)
isErrNil(err)
q := req.URL.Query()
q.Add("fromEnteredTime", fromEnteredTime)
q.Add("toEnteredTime", toEnteredTime)
req.URL.RawQuery = q.Encode()
resp, err := agent.Handler(req)
check(err)
isErrNil(err)
var orders []FullOrder
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
check(err)
isErrNil(err)
err = sonic.Unmarshal(body, &orders)
check(err)
isErrNil(err)
return orders, nil
}

Expand All @@ -478,82 +484,82 @@ func (agent *Agent) GetAccountOrders(accountNumber, fromEnteredTime, toEnteredTi
// yyyy-MM-ddTHH:mm:ss.SSSZ
func (agent *Agent) GetAllOrders(fromEnteredTime, toEnteredTime string) ([]FullOrder, error) {
req, err := http.NewRequest("GET", endpointOrders, nil)
check(err)
isErrNil(err)
q := req.URL.Query()
q.Add("fromEnteredTime", fromEnteredTime)
q.Add("toEnteredTime", toEnteredTime)
req.URL.RawQuery = q.Encode()
resp, err := agent.Handler(req)
check(err)
isErrNil(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
check(err)
isErrNil(err)
var orders []FullOrder
/*err = sonic.Unmarshal(body, &orders)
check(err)*/
isErrNil(err)*/
fmt.Println(body)
return orders, nil
}

// Get encrypted account numbers for trading
func (agent *Agent) GetAccountNumbers() ([]AccountNumbers, error) {
req, err := http.NewRequest("GET", endpointAccountNumbers, nil)
check(err)
isErrNil(err)
resp, err := agent.Handler(req)
check(err)
isErrNil(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
check(err)
isErrNil(err)
var accountNumbers []AccountNumbers
err = sonic.Unmarshal(body, &accountNumbers)
check(err)
isErrNil(err)
return accountNumbers, nil
}

// Get all accounts associated with the user logged in
func (agent *Agent) GetAccounts() ([]Account, error) {
req, err := http.NewRequest("GET", endpointAccounts, nil)
check(err)
isErrNil(err)
resp, err := agent.Handler(req)
check(err)
isErrNil(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
check(err)
isErrNil(err)
var accounts []Account
err = sonic.Unmarshal(body, &accounts)
check(err)
isErrNil(err)
return accounts, nil
}

// Get account by encrypted account id
func (agent *Agent) GetAccount(id string) (Account, error) {
req, err := http.NewRequest("GET", fmt.Sprintf(endpointAccount, id), nil)
check(err)
isErrNil(err)
resp, err := agent.Handler(req)
check(err)
isErrNil(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
check(err)
isErrNil(err)
var account Account
err = sonic.Unmarshal(body, &account)
check(err)
isErrNil(err)
return account, nil
}

// Get all transactions for the user logged in
//func (agent *Agent) GetTransactions() ([]Transaction, error) {}
// 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)
check(err)
isErrNil(err)
resp, err := agent.Handler(req)
check(err)
isErrNil(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
check(err)
isErrNil(err)
var transaction Transaction
err = sonic.Unmarshal(body, &transaction)
check(err)
isErrNil(err)
return transaction, nil
}
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-schwab/trader

go 1.22.4
go 1.23.2

require github.com/joho/godotenv v1.5.1

Expand All @@ -9,7 +9,16 @@ require (
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/go-schwab/oauth2ns v0.0.0-20241015193425-e8abfd05a439 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/nmrshll/rndm-go v0.0.0-20170430161430-8da3024e53de // indirect
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177 // indirect
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
)
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/go-schwab/oauth2ns v0.0.0-20241015193425-e8abfd05a439 h1:y/AmrAYZNvqI7IaMLllCjKlqOBbtYTe/KKtE+gip2c8=
github.com/go-schwab/oauth2ns v0.0.0-20241015193425-e8abfd05a439/go.mod h1:69d3XNxDSeVmN6g+8a0RT+a13ZxDc5LRKODEZB3d5go=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/nmrshll/rndm-go v0.0.0-20170430161430-8da3024e53de h1:j+mSQhCm1H2d7apFbM5ODqrTultUvF3jt//DcRNkxVM=
github.com/nmrshll/rndm-go v0.0.0-20170430161430-8da3024e53de/go.mod h1:OeEnWnbCrUWnPl1xSCGM5/qtWqZ4L15KOAjR/wmxhXc=
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177 h1:nRlQD0u1871kaznCnn1EvYiMbum36v7hw1DLPEjds4o=
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177/go.mod h1:ao5zGxj8Z4x60IOVYZUbDSmt3R8Ddo080vEgPosHpak=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -32,6 +47,12 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
Loading

0 comments on commit 2502992

Please sign in to comment.