Skip to content

Commit 35659a5

Browse files
authored
Merge pull request piquette#11 from jacks821/historicquote
Adds a historical quote function to Quote
2 parents ac8be96 + a147d3d commit 35659a5

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

quote/client.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"strings"
66

7+
chart "github.com/piquette/finance-go/chart"
78
finance "github.com/piquette/finance-go"
89
"github.com/piquette/finance-go/form"
910
"github.com/piquette/finance-go/iter"
11+
"github.com/piquette/finance-go/datetime"
1012
)
1113

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

45+
//Gives the Day's Close when you input a historical date
46+
func GetHistoricalQuote(symbol string, month int, day int, year int) (*finance.ChartBar, error) {
47+
p := &chart.Params{
48+
Symbol: symbol,
49+
Start: &datetime.Datetime{Month: month, Day: day, Year: year},
50+
End: &datetime.Datetime{Month: month, Day: day, Year: year},
51+
Interval: datetime.OneDay,
52+
}
53+
iter := chart.Get(p)
54+
for iter.Next() {
55+
b := iter.Bar()
56+
return b, nil
57+
}
58+
return nil, iter.Err()
59+
}
60+
4361
// Get returns an Quote quote that matches the parameters specified.
4462
func Get(symbol string) (*finance.Quote, error) {
4563
i := List([]string{symbol})

quote/client_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ func TestGetRegularMarketQuote(t *testing.T) {
1919
assert.Equal(t, tests.TestEquitySymbol, q.Symbol)
2020
}
2121

22+
func TestHistoricalQuote(t *testing.T) {
23+
TestMonth := 1
24+
TestDay := 11
25+
TestYear := 2018
26+
27+
q, err := GetHistoricalQuote(tests.TestEquitySymbol, TestMonth, TestDay, TestYear)
28+
29+
assert.Nil(t, err)
30+
assert.NotNil(t, q)
31+
high, _ := q.High.Float64()
32+
low, _ := q.Low.Float64()
33+
open, _ := q.Open.Float64()
34+
close, _ := q.Open.Float64()
35+
assert.Equal(t, 188.13999938964844, high)
36+
assert.Equal(t, 187.5500030517578, low)
37+
assert.Equal(t, 187.75, close)
38+
assert.Equal(t, 187.75, open)
39+
}
40+
41+
func TestBadSymbolBar(t *testing.T) {
42+
chart, err := GetHistoricalQuote("BADSYMBOL", 1, 11, 2018)
43+
assert.Nil(t, chart)
44+
assert.NotNil(t, err)
45+
}
46+
2247
func TestGetPostMarketQuote(t *testing.T) {
2348
tests.SetMarket(finance.MarketStatePost)
2449

testing/testing.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const (
3333
TestForexPairSymbol = "USDGBP=X"
3434
TestCryptoPairSymbol = "BTC-USD"
3535
TestStraddleSymbol = "AMD"
36+
TestMonth = 1
37+
TestDay = 11
38+
TestYear = 2018
3639
)
3740

3841
func init() {

yfin.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ type ChartBar struct {
249249
Timestamp int
250250
}
251251

252+
type OHLCHistoric struct {
253+
Open float64
254+
Low float64
255+
High float64
256+
Close float64
257+
AdjClose float64
258+
Volume int
259+
Timestamp int
260+
}
261+
252262
// ChartMeta is meta data associated with a chart response.
253263
type ChartMeta struct {
254264
Currency string `json:"currency"`

0 commit comments

Comments
 (0)