示例#1
0
// The markup contains a JSON object with all the data for Vanguard's page
// and it is all on one line, so here I split the markup up into lines,
// scan until find the one that has the variable in it, chop off the start
// of the line which leaves just the JSON object which I can then parse
// and extract the useful data from.
func (this vanguard) makePrice(markup string) entities.Price {
	for _, line := range strings.Split(markup, "\n") {
		if strings.Index(line, "pricenav =") > -1 {
			jsonv := []byte(line[30 : len(line)-2])
			var f VanguardJson
			err := json.Unmarshal(jsonv, &f)
			if err != nil {
				fmt.Fprintln(os.Stderr, "Could not parse the Vanguard page: ", err)
				os.Exit(1)
			}
			for _, profile := range f.Fund_price {
				for _, pricedata := range profile.Price {
					if pricedata.MeasureType.MeasureCode == "SELL" {
						date, _ := time.Parse(
							"2006-01-02T15:04:05-07:00",
							pricedata.AsOfDate,
						)
						return entities.NewPrice(
							date,
							time.Date(2009, time.November, 10, 17, 0, 0, 0, time.UTC),
							string(this),
							strconv.FormatFloat(pricedata.Price, 'f', -1, 64),
						)
					}
				}
			}
		}
	}
	return entities.Price{}
}
示例#2
0
// The markup contains a JSON object with all the data for Bloomberg's page
// and it is all on one line, so here I split the markup up into lines,
// scan until find the one that has the variable in it, chop off the start
// of the line which leaves just the JSON object which I can then parse
// and extract the useful data from.
func (this bloomberg) makePrice(markup string) entities.Price {
	for _, line := range strings.Split(markup, "\n") {
		if strings.Index(line, "bootstrappedData: ") > -1 {
			jsonv := []byte(line[18:len(line)])
			jsonParsed, err := gabs.ParseJSON(jsonv)
			if err != nil {
				fmt.Fprintln(os.Stderr, "Could not parse the Bloomberg page: ", err)
				os.Exit(1)
			}
			children, ok := jsonParsed.Children()
			if ok != nil {
				fmt.Fprintln(os.Stderr, "Could not parse the Bloomberg page")
				os.Exit(1)
			}
			for _, child := range children {
				if !child.Exists("basicQuote", "price") {
					continue
				}
				price := child.Path("basicQuote.price").Data().(float64)
				date := child.Path("basicQuote.priceDate").Data().(string)
				parsedDate, _ := time.Parse("1/2/2006", date)
				timed := child.Path("basicQuote.priceTime").Data().(string)
				parsedTime, _ := time.Parse("3:04 PM", timed)
				newPrice := entities.NewPrice(parsedDate, parsedTime, string(this),
					strconv.FormatFloat(price, 'f', -1, 64),
				)
				return newPrice
			}
		}
	}
	return entities.Price{}
}
示例#3
0
func (this yahoo) makePrice(csv string) entities.Price {
	split := strings.Split(csv, ",")
	date, _ := time.Parse("\"1/2/2006\"", split[2])
	clock, _ := time.Parse("\"15:04pm\"", split[3])
	symsplit := strings.Split(split[0], "\"")
	qtsplit := strings.Split(symsplit[1], ".")
	price := strings.TrimSpace(split[4])
	commodity := qtsplit[0]
	return entities.NewPrice(date, clock, commodity, price)
}
示例#4
0
func (this quandl) makePrice(data GoldData) entities.Price {
	datestring := data.Dataset.Data[0][0].(string)
	valuefloat := data.Dataset.Data[0][1].(float64)
	valuestring := strconv.FormatFloat(valuefloat, 'f', -1, 64)
	/*
		split := strings.Split(data.Dataset.Data[0][0].(string), ",")
		date, _ := time.Parse("\"1/2/2006\"", split[2])
		clock, _ := time.Parse("\"15:04pm\"", split[3])
		symsplit := strings.Split(split[0], "\"")
		qtsplit := strings.Split(symsplit[1], ".")
		price := strings.TrimSpace(split[4])
		commodity := qtsplit[0]
		return entities.NewPrice(date, clock, commodity, price)
	*/
	date, _ := time.Parse("2006-01-02", datestring)
	clock, _ := time.Parse("15:04", "17:00")
	return entities.NewPrice(date, clock, "GOLD", valuestring)
}