Skip to content

Commit

Permalink
fetching nearest date in case of stock market closed
Browse files Browse the repository at this point in the history
  • Loading branch information
guneyin committed Nov 15, 2024
1 parent af099ff commit 2b9ddc3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
29 changes: 17 additions & 12 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,19 @@ func (pd *pdate) Unix() int64 {
return time.Time(*pd).Unix()
}

func isToday(d time.Time) bool {
return d.Format(time.DateOnly) == time.Now().Format(time.DateOnly)
}

func (pd *pdate) Set(dt time.Time) {
d, _ := time.Parse(time.DateOnly, dt.Format("2006-01-02"))
d = d.Add(time.Hour * 20)
if isToday(dt) {
dt = time.Now()
} else {
y, m, d := dt.Date()
dt = time.Date(y, m, d, 11, 0, 0, 0, dt.Location())
}

*pd = pdate(d)
*pd = pdate(dt)
}

type period struct {
Expand Down Expand Up @@ -136,11 +144,6 @@ func (a *api) getQuote(symbols []string, dates ...time.Time) (*QuoteList, error)
return
}

if len(data.Chart.Result) == 0 {
q.SetError(errNoDataFound)
return
}

q.Name = data.Chart.Result[0].Meta.ShortName
q.Price = fp.FromFloat(data.Chart.Result[0].Meta.RegularMarketPrice, fp.TRY)

Expand All @@ -152,7 +155,8 @@ func (a *api) getQuote(symbols []string, dates ...time.Time) (*QuoteList, error)
return
}

h.SetBegin(p.Begin().String(), data.Chart.Result[0].Indicators.Adjclose[0].Adjclose[0])
dt, cp := data.getClosePrice()
h.SetBegin(dt, cp)

data, err = a.fetchYahooChart(symbol, p.End().Unix())
if err != nil {
Expand All @@ -164,8 +168,8 @@ func (a *api) getQuote(symbols []string, dates ...time.Time) (*QuoteList, error)
q.SetError(errHistoryDataNotFound)
return
}

h.SetEnd(p.End().String(), data.Chart.Result[0].Indicators.Adjclose[0].Adjclose[0])
dt, cp = data.getClosePrice()
h.SetEnd(dt, cp)

if h.IsValid() {
ratio := h.End.Price.Sub(h.Begin.Price).Mul(100).Float64() / h.End.Price.Float64()
Expand Down Expand Up @@ -199,7 +203,8 @@ func (a *api) fetchYahooChart(symbol string, ts int64) (*quoteDTO, error) {
}).
SetSuccessResult(&data)

url := fmt.Sprintf(yahooChartPath, symbol, ts, ts)
tsBegin := time.Unix(ts, 0).AddDate(0, 0, -15).Unix()
url := fmt.Sprintf(yahooChartPath, symbol, tsBegin, ts)
r, err := rq.Get(url)
if err != nil {
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions dto.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gobist

import "time"

type quoteDTO struct {
Chart struct {
Result []struct {
Expand Down Expand Up @@ -79,6 +81,16 @@ func (q quoteDTO) adjCloseCheck() bool {
return true
}

func (q quoteDTO) getClosePrice() (time.Time, float64) {
tsSlice := q.Chart.Result[0].Timestamp
closeSlice := q.Chart.Result[0].Indicators.Adjclose[0].Adjclose

ts := tsSlice[len(tsSlice)-1]
cp := closeSlice[len(closeSlice)-1]

return time.Unix(int64(ts), 0), cp
}

type symbolListResponse struct {
TotalCount int `json:"totalCount"`
Data []struct {
Expand Down
2 changes: 1 addition & 1 deletion examples/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func main() {
fmt.Println(fmt.Sprintf("%-10s %-30s %-20s %-20s %-20s %-15s %-30s", "Symbol", "Name", "Current Price", "History Begin", "History End", "Change", "Error"))
for _, item := range q.Items {
fmt.Println(fmt.Sprintf("%-10s %-30s %-20f %-20f %-20f %-15f %-30s",
item.Symbol, item.Name, item.Price, item.History.Begin.Price, item.History.End.Price, item.History.Change.ByRatio, item.Error))
item.Symbol, item.Name, item.Price.Float64(), item.History.Begin.Price.Float64(), item.History.End.Price.Float64(), item.History.Change.ByRatio.Float64(), item.Error))
}
}
5 changes: 3 additions & 2 deletions gobist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func TestBist_GetQuote(t *testing.T) {
func TestBist_GetQuoteWithHistory(t *testing.T) {
bist := New()

d, _ := time.Parse(time.DateOnly, "2024-10-03")
q, err := bist.GetQuoteWithHistory(symbols, d)
d1, _ := time.Parse(time.DateOnly, "2024-10-06")
d2, _ := time.Parse(time.DateOnly, "2024-10-13")
q, err := bist.GetQuoteWithHistory(symbols, d1, d2)
assertError(t, err)
assertNotNil(t, q)

Expand Down
9 changes: 5 additions & 4 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
fp "github.com/nikolaydubina/fpmoney"
"time"
)

type Quote struct {
Expand All @@ -29,12 +30,12 @@ type History struct {
Change HistoryChange `json:"change,omitempty"`
}

func (h *History) SetBegin(d string, price float64) {
h.Begin = HistoryData{d, fp.FromFloat(price, fp.TRY)}
func (h *History) SetBegin(dt time.Time, price float64) {
h.Begin = HistoryData{dt.Format(time.DateOnly), fp.FromFloat(price, fp.TRY)}
}

func (h *History) SetEnd(d string, price float64) {
h.End = HistoryData{d, fp.FromFloat(price, fp.TRY)}
func (h *History) SetEnd(dt time.Time, price float64) {
h.End = HistoryData{dt.Format(time.DateOnly), fp.FromFloat(price, fp.TRY)}
}

func (h *History) IsValid() bool {
Expand Down

0 comments on commit 2b9ddc3

Please sign in to comment.