Skip to content

Commit

Permalink
refactor: watchlist and summary to use assets
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Aug 22, 2021
1 parent 686dd46 commit 885beae
Show file tree
Hide file tree
Showing 9 changed files with 535 additions and 519 deletions.
18 changes: 10 additions & 8 deletions internal/quote/yahoo/yahoo.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,14 @@ func transformResponseQuotes(responseQuotes []ResponseQuote) []c.AssetQuote {
}

// GetAssetQuotes issues a HTTP request to retrieve quotes from the API and process the response
func GetAssetQuotes(client resty.Client, symbols []string) []c.AssetQuote {
symbolsString := strings.Join(symbols, ",")
url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&symbols=%s", symbolsString)
res, _ := client.R().
SetResult(Response{}).
Get(url)

return transformResponseQuotes((res.Result().(*Response)).QuoteResponse.Quotes)
func GetAssetQuotes(client resty.Client, symbols []string) func() []c.AssetQuote {
return func() []c.AssetQuote {
symbolsString := strings.Join(symbols, ",")
url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&symbols=%s", symbolsString)
res, _ := client.R().
SetResult(Response{}).
Get(url)

return transformResponseQuotes((res.Result().(*Response)).QuoteResponse.Quotes)
}
}
14 changes: 7 additions & 7 deletions internal/quote/yahoo/yahoo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = Describe("Yahoo Quote", func() {
return resp, nil
})

output := GetAssetQuotes(*client, []string{"NET"})
output := GetAssetQuotes(*client, []string{"NET"})()
Expect(output[0].QuotePrice.Price).To(Equal(84.98))
Expect(output[0].QuotePrice.PricePrevClose).To(Equal(84.00))
Expect(output[0].QuotePrice.PriceOpen).To(Equal(85.22))
Expand Down Expand Up @@ -84,7 +84,7 @@ var _ = Describe("Yahoo Quote", func() {
return resp, nil
})

output := GetAssetQuotes(*client, []string{"NET"})
output := GetAssetQuotes(*client, []string{"NET"})()
Expect(output[0].QuotePrice.Price).To(Equal(86.03))
Expect(output[0].QuotePrice.Change).To(Equal(1.0399933))
Expect(output[0].QuotePrice.ChangePercent).To(Equal(1.2238094))
Expand Down Expand Up @@ -116,7 +116,7 @@ var _ = Describe("Yahoo Quote", func() {
return resp, nil
})

output := GetAssetQuotes(*client, []string{"NET"})
output := GetAssetQuotes(*client, []string{"NET"})()
Expect(output[0].QuotePrice.Price).To(Equal(84.98))
Expect(output[0].QuotePrice.Change).To(Equal(3.0800018))
Expect(output[0].QuotePrice.ChangePercent).To(Equal(3.7606857))
Expand Down Expand Up @@ -153,7 +153,7 @@ var _ = Describe("Yahoo Quote", func() {
return resp, nil
})

output := GetAssetQuotes(*client, []string{"NET"})
output := GetAssetQuotes(*client, []string{"NET"})()
Expect(output[0].QuotePrice.Price).To(Equal(86.02))
Expect(output[0].QuotePrice.Change).To(Equal(4.1199951))
Expect(output[0].QuotePrice.ChangePercent).To(Equal(4.9844951))
Expand Down Expand Up @@ -187,7 +187,7 @@ var _ = Describe("Yahoo Quote", func() {
return resp, nil
})

output := GetAssetQuotes(*client, []string{"NET"})
output := GetAssetQuotes(*client, []string{"NET"})()
expectedPrice := 84.98
expectedChange := 3.0800018
expectedChangePercent := 3.7606857
Expand Down Expand Up @@ -224,7 +224,7 @@ var _ = Describe("Yahoo Quote", func() {
return resp, nil
})

output := GetAssetQuotes(*client, []string{"NET"})
output := GetAssetQuotes(*client, []string{"NET"})()
Expect(output[0].QuotePrice.Price).To(Equal(84.98))
Expect(output[0].QuotePrice.Change).To(Equal(3.0800018))
Expect(output[0].QuotePrice.ChangePercent).To(Equal(3.7606857))
Expand Down Expand Up @@ -261,7 +261,7 @@ var _ = Describe("Yahoo Quote", func() {
return resp, nil
})

output := GetAssetQuotes(*client, []string{"NET"})
output := GetAssetQuotes(*client, []string{"NET"})()
Expect(output[0].QuotePrice.Price).To(Equal(86.02))
Expect(output[0].QuotePrice.Change).To(Equal(4.1199951))
Expect(output[0].QuotePrice.ChangePercent).To(Equal(4.9844951))
Expand Down
115 changes: 57 additions & 58 deletions internal/sorter/sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package sorter
import (
"sort"

p "github.com/achannarasappa/ticker/internal/position"
q "github.com/achannarasappa/ticker/internal/quote"
c "github.com/achannarasappa/ticker/internal/common"
)

// Sorter represents a function that sorts quotes
type Sorter func(quotes []q.Quote, positions map[string]p.Position) []q.Quote
type Sorter func([]c.Asset) []c.Asset

// NewSorter creates a sorting function
func NewSorter(sort string) Sorter {
Expand All @@ -24,116 +23,116 @@ var sortDict = map[string]Sorter{
"user": sortByUser,
}

func sortByUser(quoteIn []q.Quote, positions map[string]p.Position) []q.Quote {
func sortByUser(assetsIn []c.Asset) []c.Asset {

quoteCount := len(quoteIn)
assetCount := len(assetsIn)

if quoteCount <= 0 {
return quoteIn
if assetCount <= 0 {
return assetsIn
}

quotes := make([]q.Quote, quoteCount)
copy(quotes, quoteIn)
assets := make([]c.Asset, assetCount)
copy(assets, assetsIn)

sort.SliceStable(quotes, func(i, j int) bool {
sort.SliceStable(assets, func(i, j int) bool {

prevIndex := quoteCount
nextIndex := quoteCount
prevIndex := assetCount
nextIndex := assetCount

if position, ok := positions[quotes[i].Symbol]; ok {
prevIndex = position.AggregatedLot.OrderIndex
if assets[i].Holding != (c.Holding{}) {
prevIndex = assets[i].Meta.OrderIndex
}

if position, ok := positions[quotes[j].Symbol]; ok {
nextIndex = position.AggregatedLot.OrderIndex
if assets[j].Holding != (c.Holding{}) {
nextIndex = assets[j].Meta.OrderIndex
}

return nextIndex > prevIndex
})

return quotes
return assets

}

func sortByAlpha(quoteIn []q.Quote, positions map[string]p.Position) []q.Quote {
func sortByAlpha(assetsIn []c.Asset) []c.Asset {

quoteCount := len(quoteIn)
assetCount := len(assetsIn)

if quoteCount <= 0 {
return quoteIn
if assetCount <= 0 {
return assetsIn
}

quotes := make([]q.Quote, quoteCount)
copy(quotes, quoteIn)
assets := make([]c.Asset, assetCount)
copy(assets, assetsIn)

sort.SliceStable(quotes, func(i, j int) bool {
return quotes[j].Symbol > quotes[i].Symbol
sort.SliceStable(assets, func(i, j int) bool {
return assets[j].Symbol > assets[i].Symbol
})

return quotes
return assets
}

func sortByValue(quoteIn []q.Quote, positions map[string]p.Position) []q.Quote {
func sortByValue(assetsIn []c.Asset) []c.Asset {

quoteCount := len(quoteIn)
assetCount := len(assetsIn)

if quoteCount <= 0 {
return quoteIn
if assetCount <= 0 {
return assetsIn
}

quotes := make([]q.Quote, quoteCount)
copy(quotes, quoteIn)
assets := make([]c.Asset, assetCount)
copy(assets, assetsIn)

activeQuotes, inactiveQuotes := splitActiveQuotes(quotes)
activeAssets, inactiveAssets := splitActiveAssets(assets)

sort.SliceStable(inactiveQuotes, func(i, j int) bool {
return positions[inactiveQuotes[j].Symbol].Value < positions[inactiveQuotes[i].Symbol].Value
sort.SliceStable(inactiveAssets, func(i, j int) bool {
return inactiveAssets[j].Holding.Value < inactiveAssets[i].Holding.Value
})

sort.SliceStable(activeQuotes, func(i, j int) bool {
return positions[activeQuotes[j].Symbol].Value < positions[activeQuotes[i].Symbol].Value
sort.SliceStable(activeAssets, func(i, j int) bool {
return activeAssets[j].Holding.Value < activeAssets[i].Holding.Value
})

return append(activeQuotes, inactiveQuotes...)
return append(activeAssets, inactiveAssets...)
}

func sortByChange(quoteIn []q.Quote, positions map[string]p.Position) []q.Quote {
func sortByChange(assetsIn []c.Asset) []c.Asset {

quoteCount := len(quoteIn)
assetCount := len(assetsIn)

if quoteCount <= 0 {
return quoteIn
if assetCount <= 0 {
return assetsIn
}

quotes := make([]q.Quote, quoteCount)
copy(quotes, quoteIn)
assets := make([]c.Asset, assetCount)
copy(assets, assetsIn)

activeQuotes, inactiveQuotes := splitActiveQuotes(quotes)
activeAssets, inactiveAssets := splitActiveAssets(assets)

sort.SliceStable(activeQuotes, func(i, j int) bool {
return activeQuotes[j].ChangePercent < activeQuotes[i].ChangePercent
sort.SliceStable(activeAssets, func(i, j int) bool {
return activeAssets[j].QuotePrice.ChangePercent < activeAssets[i].QuotePrice.ChangePercent
})

sort.SliceStable(inactiveQuotes, func(i, j int) bool {
return inactiveQuotes[j].ChangePercent < inactiveQuotes[i].ChangePercent
sort.SliceStable(inactiveAssets, func(i, j int) bool {
return inactiveAssets[j].QuotePrice.ChangePercent < inactiveAssets[i].QuotePrice.ChangePercent
})

return append(activeQuotes, inactiveQuotes...)
return append(activeAssets, inactiveAssets...)

}

func splitActiveQuotes(quotes []q.Quote) ([]q.Quote, []q.Quote) {
func splitActiveAssets(assets []c.Asset) ([]c.Asset, []c.Asset) {

activeQuotes := make([]q.Quote, 0)
inactiveQuotes := make([]q.Quote, 0)
activeAssets := make([]c.Asset, 0)
inactiveAssets := make([]c.Asset, 0)

for _, quote := range quotes {
if quote.IsActive {
activeQuotes = append(activeQuotes, quote)
for _, asset := range assets {
if asset.Exchange.IsActive {
activeAssets = append(activeAssets, asset)
} else {
inactiveQuotes = append(inactiveQuotes, quote)
inactiveAssets = append(inactiveAssets, asset)
}
}

return activeQuotes, inactiveQuotes
return activeAssets, inactiveAssets
}
Loading

0 comments on commit 885beae

Please sign in to comment.