Skip to content

Commit

Permalink
Merge pull request #57 from alpacahq/feature/account-activities
Browse files Browse the repository at this point in the history
Add support for account activities API
  • Loading branch information
ttt733 authored Oct 24, 2019
2 parents a3d065c + 9d89e15 commit c286eb5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
26 changes: 26 additions & 0 deletions alpaca/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ type AccountConfigurations struct {
TradeSuspendedByUser bool `json:"trade_suspended_by_user"`
}

type AccountActvity struct {
ID string `json:"id"`
ActivityType string `json:"activity_type"`
TransactionTime time.Time `json:"transaction_time"`
Type string `json:"type"`
Price decimal.Decimal `json:"price"`
Qty decimal.Decimal `json:"qty"`
Side string `json:"side"`
Symbol string `json:"symbol"`
LeavesQty decimal.Decimal `json:"leaves_qty"`
CumQty decimal.Decimal `json:"cum_qty"`
Date time.Time `json:"date"`
NetAmount decimal.Decimal `json:"net_amount"`
Description string `json:"description"`
PerShareAmount decimal.Decimal `json:"per_share_amount"`
}

type PlaceOrderRequest struct {
AccountID string `json:"-"`
AssetKey *string `json:"symbol"`
Expand Down Expand Up @@ -190,6 +207,15 @@ type AccountConfigurationsRequest struct {
TradeSuspendedByUser *bool `json:"trade_suspended_by_user"`
}

type AccountActivitiesRequest struct {
ActivityTypes *[]string `json:"activity_types"`
Date *time.Time `json:"date"`
Until *time.Time `json:"until"`
After *time.Time `json:"after"`
Direction *string `json:"direction"`
PageSize *int `json:"page_size"`
}

type Side string

const (
Expand Down
53 changes: 53 additions & 0 deletions alpaca/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,55 @@ func (c *Client) UpdateAccountConfigurations(newConfigs AccountConfigurationsReq
return configs, nil
}

func (c *Client) GetAccountActivities(activityType *string, opts *AccountActivitiesRequest) ([]AccountActvity, error) {
var u *url.URL
var err error
if activityType == nil {
u, err = url.Parse(fmt.Sprintf("%s/%s/account/activities", base, apiVersion))
} else {
u, err = url.Parse(fmt.Sprintf("%s/%s/account/activities/%s", base, apiVersion, *activityType))
}
if err != nil {
return nil, err
}

q := u.Query()
if opts != nil {
if opts.ActivityTypes != nil {
q.Set("activity_types", strings.Join(*opts.ActivityTypes, ","))
}
if opts.Date != nil {
q.Set("date", opts.Date.String())
}
if opts.Until != nil {
q.Set("until", opts.Until.String())
}
if opts.After != nil {
q.Set("after", opts.After.String())
}
if opts.Direction != nil {
q.Set("direction", *opts.Direction)
}
if opts.PageSize != nil {
q.Set("page_size", string(*opts.PageSize))
}
}

u.RawQuery = q.Encode()

resp, err := c.get(u)
if err != nil {
return nil, err
}

activities := []AccountActvity{}

if err = unmarshal(resp, &activities); err != nil {
return nil, err
}
return activities, nil
}

// ListPositions lists the account's open positions.
func (c *Client) ListPositions() ([]Position, error) {
u, err := url.Parse(fmt.Sprintf("%s/%s/positions", base, apiVersion))
Expand Down Expand Up @@ -531,6 +580,10 @@ func UpdateAccountConfigurations(newConfigs AccountConfigurationsRequest) (*Acco
return DefaultClient.UpdateAccountConfigurations(newConfigs)
}

func GetAccountActivities(activityType *string, opts *AccountActivitiesRequest) ([]AccountActvity, error) {
return DefaultClient.GetAccountActivities(activityType, opts)
}

// ListPositions lists the account's open positions
// using the default Alpaca client.
func ListPositions() ([]Position, error) {
Expand Down

0 comments on commit c286eb5

Please sign in to comment.