Skip to content

Commit

Permalink
Merge pull request #63 from alpacahq/feature/portfolio-history
Browse files Browse the repository at this point in the history
Add support for Portfolio History endpoint
  • Loading branch information
ttt733 authored Mar 2, 2020
2 parents 8571b86 + 954032a commit 58d5670
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
30 changes: 30 additions & 0 deletions alpaca/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ type AccountActvity struct {
PerShareAmount decimal.Decimal `json:"per_share_amount"`
}

type PortfolioHistory struct {
BaseValue decimal.Decimal `json:"base_value"`
Equity []decimal.Decimal `json:"equity"`
ProfitLoss []decimal.Decimal `json:"profit_loss"`
ProfitLossPct []decimal.Decimal `json:"profit_loss_pct"`
Timeframe RangeFreq `json:"timeframe"`
Timestamp []int64 `json:"timestamp"`
}

type PlaceOrderRequest struct {
AccountID string `json:"-"`
AssetKey *string `json:"symbol"`
Expand Down Expand Up @@ -288,6 +297,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
40 changes: 40 additions & 0 deletions alpaca/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,42 @@ 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))

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

portfolioHistory := PortfolioHistory{}
if err = unmarshal(resp, &portfolioHistory); err != nil {
return nil, err
}

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 +624,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 58d5670

Please sign in to comment.