Skip to content

Commit

Permalink
decimal library updated
Browse files Browse the repository at this point in the history
  • Loading branch information
guneyin committed Nov 16, 2024
1 parent 2b9ddc3 commit 3975e61
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
13 changes: 8 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"errors"
"fmt"
"github.com/imroc/req/v3"
fp "github.com/nikolaydubina/fpmoney"
"github.com/shopspring/decimal"

"net/http"
"sync"
"time"
Expand Down Expand Up @@ -145,7 +146,7 @@ func (a *api) getQuote(symbols []string, dates ...time.Time) (*QuoteList, error)
}

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

if !p.IsSingleDay() {
h := History{}
Expand All @@ -172,10 +173,12 @@ func (a *api) getQuote(symbols []string, dates ...time.Time) (*QuoteList, error)
h.SetEnd(dt, cp)

if h.IsValid() {
ratio := h.End.Price.Sub(h.Begin.Price).Mul(100).Float64() / h.End.Price.Float64()
bp, _ := decimal.NewFromString(h.Begin.Price)
ep, _ := decimal.NewFromString(h.End.Price)
ratio := ep.Sub(bp).Mul(decimal.NewFromInt(100)).Div(ep)
h.Change = HistoryChange{
ByRatio: fp.FromFloat(ratio, fp.TRY),
ByAmount: h.End.Price.Sub(h.Begin.Price),
ByRatio: ratio.Truncate(2).String(),
ByAmount: ep.Sub(bp).Truncate(2).String(),
}

q.History = h
Expand Down
8 changes: 5 additions & 3 deletions examples/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func main() {
log.Fatal(err)
}

fmt.Println(fmt.Sprintf("%-10s %-30s %-20s %-20s %-20s %-15s %-30s", "Symbol", "Name", "Current Price", "History Begin", "History End", "Change", "Error"))
const layout = "%-10s %-30s %-20s %-20s %-20s %-15s %-30s"
fmt.Println(fmt.Sprintf(layout, "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.Float64(), item.History.Begin.Price.Float64(), item.History.End.Price.Float64(), item.History.Change.ByRatio.Float64(), item.Error))
fmt.Println(fmt.Sprintf(layout,
item.Symbol, item.Name, item.Price, item.History.Begin.Price,
item.History.End.Price, item.History.Change.ByRatio, item.Error))
}
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.0

require (
github.com/imroc/req/v3 v3.46.1
github.com/nikolaydubina/fpmoney v1.2.0
github.com/shopspring/decimal v1.4.0
)

require (
Expand All @@ -15,7 +15,6 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/nikolaydubina/fpdecimal v0.19.3 // indirect
github.com/onsi/ginkgo/v2 v2.20.2 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.47.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ github.com/imroc/req/v3 v3.46.1 h1:oahr2hBTb3AaFI4P6jkN0Elj2ZVKJcdQ/IjWqeIKjvc=
github.com/imroc/req/v3 v3.46.1/go.mod h1:weam9gmyb00QnOtu6HXSnk44dNFkIUQb5QdMx13FeUU=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/nikolaydubina/fpdecimal v0.19.3 h1:N02pjbRdmAH+5S61pKeCmEWNiSjt6vn2Uglnltpag8o=
github.com/nikolaydubina/fpdecimal v0.19.3/go.mod h1:FtuJCNRO3SumT8lfJNp5IZ8kcnPzx7blxiUe3VcTTsY=
github.com/nikolaydubina/fpmoney v1.2.0 h1:UtfpUN3k9qdoNYhlOOKoDFqvZORZdTJAEorMIAiETdE=
github.com/nikolaydubina/fpmoney v1.2.0/go.mod h1:QFj+kY2wvwVWe5TKp07hoxGWcyD6MLAMt3YhyN+5Lpc=
github.com/onsi/ginkgo/v2 v2.20.2 h1:7NVCeyIWROIAheY21RLS+3j2bb52W0W82tkberYytp4=
github.com/onsi/ginkgo/v2 v2.20.2/go.mod h1:K9gyxPIlb+aIvnZ8bd9Ak+YP18w3APlR+5coaZoE2ag=
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
Expand All @@ -37,6 +33,8 @@ github.com/quic-go/quic-go v0.47.0 h1:yXs3v7r2bm1wmPTYNLKAAJTHMYkPEsfYJmTazXrCZ7
github.com/quic-go/quic-go v0.47.0/go.mod h1:3bCapYsJvXGZcipOHuu7plYtaV6tnF+z7wIFsU0WK9E=
github.com/refraction-networking/utls v1.6.7 h1:zVJ7sP1dJx/WtVuITug3qYUq034cDq9B2MR1K67ULZM=
github.com/refraction-networking/utls v1.6.7/go.mod h1:BC3O4vQzye5hqpmDTWUqi4P5DDhzJfkV1tdqtawQIH0=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
Expand Down
24 changes: 12 additions & 12 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package gobist
import (
"encoding/json"
"fmt"
fp "github.com/nikolaydubina/fpmoney"
"github.com/shopspring/decimal"
"time"
)

type Quote struct {
Symbol string `json:"symbol"`
Name string `json:"name"`
Price fp.Amount `json:"price"`
History History `json:"history,omitempty"`
Error string `json:"error,omitempty"`
Symbol string `json:"symbol"`
Name string `json:"name"`
Price string `json:"price"`
History History `json:"history,omitempty"`
Error string `json:"error,omitempty"`
}

func (q *Quote) ToJson() string {
Expand All @@ -31,25 +31,25 @@ type History struct {
}

func (h *History) SetBegin(dt time.Time, price float64) {
h.Begin = HistoryData{dt.Format(time.DateOnly), fp.FromFloat(price, fp.TRY)}
h.Begin = HistoryData{dt.Format(time.DateOnly), decimal.NewFromFloat(price).Truncate(2).String()}
}

func (h *History) SetEnd(dt time.Time, price float64) {
h.End = HistoryData{dt.Format(time.DateOnly), fp.FromFloat(price, fp.TRY)}
h.End = HistoryData{dt.Format(time.DateOnly), decimal.NewFromFloat(price).Truncate(2).String()}
}

func (h *History) IsValid() bool {
return h.Begin.Date != "" && h.End.Date != ""
}

type HistoryData struct {
Date string `json:"date,omitempty"`
Price fp.Amount `json:"price,omitempty"`
Date string `json:"date,omitempty"`
Price string `json:"price,omitempty"`
}

type HistoryChange struct {
ByRatio fp.Amount `json:"byRatio"`
ByAmount fp.Amount `json:"byAmount"`
ByRatio string `json:"byRatio"`
ByAmount string `json:"byAmount"`
}

type Symbol struct {
Expand Down

0 comments on commit 3975e61

Please sign in to comment.