Skip to content

Commit

Permalink
Merge pull request #11 from jacks821/historicquote
Browse files Browse the repository at this point in the history
Adds a historical quote function to Quote
  • Loading branch information
ackleymi committed Jan 31, 2019
2 parents ac8be96 + a147d3d commit 35659a5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions quote/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"strings"

chart "github.com/piquette/finance-go/chart"
finance "github.com/piquette/finance-go"
"github.com/piquette/finance-go/form"
"github.com/piquette/finance-go/iter"
"github.com/piquette/finance-go/datetime"
)

// Client is used to invoke quote APIs.
Expand Down Expand Up @@ -40,6 +42,22 @@ func (i *Iter) Quote() *finance.Quote {
return i.Current().(*finance.Quote)
}

//Gives the Day's Close when you input a historical date
func GetHistoricalQuote(symbol string, month int, day int, year int) (*finance.ChartBar, error) {
p := &chart.Params{
Symbol: symbol,
Start: &datetime.Datetime{Month: month, Day: day, Year: year},
End: &datetime.Datetime{Month: month, Day: day, Year: year},
Interval: datetime.OneDay,
}
iter := chart.Get(p)
for iter.Next() {
b := iter.Bar()
return b, nil
}
return nil, iter.Err()
}

// Get returns an Quote quote that matches the parameters specified.
func Get(symbol string) (*finance.Quote, error) {
i := List([]string{symbol})
Expand Down
25 changes: 25 additions & 0 deletions quote/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ func TestGetRegularMarketQuote(t *testing.T) {
assert.Equal(t, tests.TestEquitySymbol, q.Symbol)
}

func TestHistoricalQuote(t *testing.T) {
TestMonth := 1
TestDay := 11
TestYear := 2018

q, err := GetHistoricalQuote(tests.TestEquitySymbol, TestMonth, TestDay, TestYear)

assert.Nil(t, err)
assert.NotNil(t, q)
high, _ := q.High.Float64()
low, _ := q.Low.Float64()
open, _ := q.Open.Float64()
close, _ := q.Open.Float64()
assert.Equal(t, 188.13999938964844, high)
assert.Equal(t, 187.5500030517578, low)
assert.Equal(t, 187.75, close)
assert.Equal(t, 187.75, open)
}

func TestBadSymbolBar(t *testing.T) {
chart, err := GetHistoricalQuote("BADSYMBOL", 1, 11, 2018)
assert.Nil(t, chart)
assert.NotNil(t, err)
}

func TestGetPostMarketQuote(t *testing.T) {
tests.SetMarket(finance.MarketStatePost)

Expand Down
3 changes: 3 additions & 0 deletions testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const (
TestForexPairSymbol = "USDGBP=X"
TestCryptoPairSymbol = "BTC-USD"
TestStraddleSymbol = "AMD"
TestMonth = 1
TestDay = 11
TestYear = 2018
)

func init() {
Expand Down
10 changes: 10 additions & 0 deletions yfin.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ type ChartBar struct {
Timestamp int
}

type OHLCHistoric struct {
Open float64
Low float64
High float64
Close float64
AdjClose float64
Volume int
Timestamp int
}

// ChartMeta is meta data associated with a chart response.
type ChartMeta struct {
Currency string `json:"currency"`
Expand Down

0 comments on commit 35659a5

Please sign in to comment.