Merge pull request #89 from adambhayes/fix-filters

Fix filters
master
Brandon Lee Camilleri 3 years ago committed by GitHub
commit b73f207fcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 60
      filter.go

@ -4,7 +4,10 @@
package mop package mop
import "strings" import (
"strings"
"strconv"
)
// Filter gets called to sort stock quotes by one of the columns. The // Filter gets called to sort stock quotes by one of the columns. The
// setup is rather lengthy; there should probably be more concise way // setup is rather lengthy; there should probably be more concise way
@ -20,32 +23,47 @@ func NewFilter(profile *Profile) *Filter {
} }
} }
// Changes money and % notation to a plain float for math, comparisons.
func stringToNumber (numberString string) float64 {
// If the string "$3.6B" is passed in, the returned float will be 3.6E+09.
// If 0.03% is passed in, the returned float will be 0.03 (NOT 0.0003!).
newString := strings.TrimSpace(numberString) // Take off whitespace.
newString = strings.Replace(newString,"$","",1) // Remove the $ symbol.
newString = strings.Replace(newString,"%","",1) // Remove the $ symbol.
newString = strings.Replace(newString,"K","E+3",1) // Thousand (kilo)
newString = strings.Replace(newString,"M","E+6",1) // Million
newString = strings.Replace(newString,"B","E+9",1) // Billion
newString = strings.Replace(newString,"T","E+12",1) // Trillion
finalValue, _ := strconv.ParseFloat(newString, 64)
return finalValue
}
// Apply builds a list of sort interface based on current sort // Apply builds a list of sort interface based on current sort
// order, then calls sort.Sort to do the actual job. // order, then calls sort.Sort to do the actual job.
func (filter *Filter) Apply(stocks []Stock) []Stock { func (filter *Filter) Apply(stocks []Stock) []Stock {
var filteredStocks []Stock var filteredStocks []Stock
for _, stock := range stocks { for _, stock := range stocks {
var values = map[string]interface{}{ var values = make(map[string]interface{})
"ticker": strings.TrimSpace(stock.Ticker), // Make conversions from the strings to floats where necessary.
"last": m(stock.LastTrade), values["ticker"] = strings.TrimSpace(stock.Ticker) // Remains string
"change": c(stock.Change), values["last"] = stringToNumber(stock.LastTrade)
"changePercent": c(stock.ChangePct), values["change"] = stringToNumber(stock.Change)
"open": m(stock.Open), values["changePercent"] = stringToNumber(stock.ChangePct)
"low": m(stock.Low), values["open"] = stringToNumber(stock.Open)
"high": m(stock.High), values["low"] = stringToNumber(stock.Low)
"low52": m(stock.Low52), values["high"] = stringToNumber(stock.High)
"high52": m(stock.High52), values["low52"] = stringToNumber(stock.Low52)
"volume": m(stock.Volume), values["high52"] = stringToNumber(stock.High52)
"avgVolume": m(stock.AvgVolume), values["dividend"] = stringToNumber(stock.Dividend)
"pe": m(stock.PeRatio), values["yield"] = stringToNumber(stock.Yield)
"peX": m(stock.PeRatioX), values["mktCap"] = stringToNumber(stock.MarketCap)
"dividend": m(stock.Dividend), values["mktCapX"] = stringToNumber(stock.MarketCapX)
"yield": m(stock.Yield), values["volume"] = stringToNumber(stock.Volume)
"mktCap": m(stock.MarketCap), values["avgVolume"] = stringToNumber(stock.AvgVolume)
"mktCapX": m(stock.MarketCapX), values["pe"] = stringToNumber(stock.PeRatio)
"advancing": stock.Advancing, values["peX"] = stringToNumber(stock.PeRatioX)
} values["advancing"] = stock.Advancing // Remains bool.
result, err := filter.profile.filterExpression.Evaluate(values) result, err := filter.profile.filterExpression.Evaluate(values)

Loading…
Cancel
Save