/** Fetch the latest stock quote for a given stock symbol. */ func GetQuote(y *yql.YQL, symbol string) (Quote, error) { query := yql.BuildQuery([]string{"*"}, []string{quoteTable}, []string{"symbol = \"" + symbol + "\""}, []bool{true}) r, err := y.Query(query) if err != nil { return Quote{}, err } q := Quote{} q.Populate((r.Results["quote"]).(map[string]interface{})) return q, nil }
/** Fetch the latest stock quote for a group of given stock symbols. */ func GetQuotes(y *yql.YQL, names []string) ([]Quote, error) { symbols := make([]string, len(names)) for i, s := range names { symbols[i] = "symbol = \"" + s + "\"" } query := yql.BuildQuery([]string{"*"}, []string{quoteTable}, symbols, []bool{false}) r, err := y.Query(query) if err != nil { return nil, err } quotes := r.Results["quote"].([]interface{}) ret := make([]Quote, len(names)) for i, q := range quotes { z := Quote{} z.Populate(q.(map[string]interface{})) ret[i] = z } return ret, nil }
/** Fetch historical data for a given stock symbol. */ func GetHistoricalData(y *yql.YQL, symbol, start, end string) (*StockHistory, error) { query := yql.BuildQuery([]string{"*"}, []string{historyTable}, []string{"symbol = \"" + symbol + "\"", "startDate = \"" + start + "\"", "endDate = \"" + end + "\""}, []bool{true}) r, err := y.Query(query) if err != nil { return nil, err } s := new(StockHistory) s.Symbol = symbol s.StartDate = start s.EndDate = end h := make([]HistoricalQuote, len(r.Results["quote"].([]interface{}))) for i, q := range r.Results["quote"].([]interface{}) { dayQuote := HistoricalQuote{} dayQuote.Populate(q.(map[string]interface{})) s.totalClose += dayQuote.Close h[i] = dayQuote } s.History = h return s, nil }