Skip to content

Commit

Permalink
Add portfolio history endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ttt733 committed Feb 27, 2020
1 parent 8571b86 commit 8dcca49
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions alpaca/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ type AccountActvity struct {
PerShareAmount decimal.Decimal `json:"per_share_amount"`
}

type PortfolioHistory struct {
Arrays []interface{} `json:"arrays"`
Attributes []map[string]string `json:"attributes"`
Timeframe RangeFreq `json:"timeframe"`
BaseValue float64 `json:"base_value"`
}

type PlaceOrderRequest struct {
AccountID string `json:"-"`
AssetKey *string `json:"symbol"`
Expand Down Expand Up @@ -288,6 +295,27 @@ const (
All TradeConfirmEmail = "all"
)

type HistoryPeriod string

const (
Month1 HistoryPeriod = "1M"
Month3 HistoryPeriod = "3M"
Month6 HistoryPeriod = "6M"
Year HistoryPeriod = "1A"
AllHistory HistoryPeriod = "all"
Intraday HistoryPeriod = "intraday"
)

type RangeFreq string

const (
Min1 RangeFreq = "1Min"
Min5 RangeFreq = "5Min"
Min15 RangeFreq = "15Min"
Hour1 RangeFreq = "1H"
Day1 RangeFreq = "1D"
)

// stream

// ClientMsg is the standard message sent by clients of the stream interface
Expand Down
30 changes: 30 additions & 0 deletions alpaca/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ func (c *Client) GetAccountActivities(activityType *string, opts *AccountActivit
return activities, nil
}

func (c *Client) GetPortfolioHistory(startDate, endDate *time.Time, period *HistoryPeriod, timeframe *RangeFreq, extended bool) (*PortfolioHistory, error) {
var u *url.URL
var err error
u, err = url.Parse(fmt.Sprintf("%s/%s/account/portfolio/history", base, apiVersion))
if err != nil {
return nil, err
}

q := u.Query()
if startDate != nil {
q.Set("start_date", startDate.Format(time.RFC3339))
}
if endDate != nil {
q.Set("end_date", endDate.Format(time.RFC3339))
}
if period != nil {
q.Set("period", string(*period))
}
if timeframe != nil {
q.Set("period", string(*timeframe))
}
q.Set("extended", strconv.FormatBool(extended))

return &PortfolioHistory{}, 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 @@ -588,6 +614,10 @@ func GetAccountActivities(activityType *string, opts *AccountActivitiesRequest)
return DefaultClient.GetAccountActivities(activityType, opts)
}

func GetPortfolioHistory(startDate, endDate *time.Time, period *HistoryPeriod, timeframe *RangeFreq, extended bool) (*PortfolioHistory, error) {
return DefaultClient.GetPortfolioHistory(startDate, endDate, period, timeframe, extended)
}

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

0 comments on commit 8dcca49

Please sign in to comment.