Exemplo n.º 1
0
func csvQuotes(symbols []string) ([]fquery.Quote, error) {
	v := url.Values{}

	/* which symbols? */
	v.Set("s", strings.Join(symbols, ","))

	/* what values do we want?
	 * description avaiable here: http://www.jarloo.com/yahoo_finance/
	 * v.Set("f",
	 * "aa2bb2b3b4cc1c3c6c8dd1d2ee1e7e8e9ghjkg1g3g4g5g6ii5j1j3j4j5j6k1k2k4k5ll1l2l3mm2m3m4m5m6m7m8nn4opp1p2p5p6qrr1r2r5r6r7ss1s7t1t7t8vv1v7ww1w4xy" */
	v.Set("f", "nsxabpoghkjl1m3m4ydr1")

	req, err := Csv(QuotesUrl, v)
	if err != nil {
		return nil, err
	}
	defer req.Close()
	r := csv.NewReader(req)

	results := make([]fquery.Quote, 0, len(symbols))
	for {
		fields, err := r.Read()
		if err == io.EOF {
			break
		} else if err != nil {
			return nil, err
		}

		res := fquery.Quote{
			Name:             fields[0],
			Symbol:           fields[1],
			Ask:              floatOr(fields[3]),
			Bid:              floatOr(fields[4]),
			Open:             floatOr(fields[6]),
			PreviousClose:    floatOr(fields[5]),
			LastTradePrice:   floatOr(fields[11]),
			DayLow:           floatOr(fields[7]),
			DayHigh:          floatOr(fields[8]),
			YearLow:          floatOr(fields[9]),
			YearHigh:         floatOr(fields[10]),
			Ma50:             floatOr(fields[12]),
			Ma200:            floatOr(fields[13]),
			DividendYield:    floatOr(fields[14]),
			DividendPerShare: floatOr(fields[15]),
		}

		tm, err := time.Parse(fields[16], util.FmtMonthDay)
		if err == nil {
			res.DividendExDate = tm
		}

		results = append(results, res)
	}

	return results, nil
}
Exemplo n.º 2
0
func yqlQuotes(symbols []string) ([]fquery.Quote, error) {
	if len(symbols) == 0 {
		return nil, nil
	}

	quotedSymbols := util.MapStr(func(s string) string {
		return `"` + s + `"`
	}, symbols)
	query := fmt.Sprintf(`SELECT * FROM %s WHERE symbol IN (%s)`,
		YahooTables.Quotes, strings.Join(quotedSymbols, ","))
	fmt.Println("Quotes query = ", query)

	raw, err := Yql(query)
	if err != nil {
		return nil, err
	}

	/* json responses for just a single symbols are slightly different from
	 * the ones for multiple symbols. */
	var quotes []YqlJsonQuote
	if len(symbols) == 1 {
		var sresp YqlJsonSingleQuoteResponse
		err = json.Unmarshal(raw, &sresp)
		if err != nil {
			return nil, err
		}
		quotes = []YqlJsonQuote{sresp.Query.Results.Quote}
	} else {
		var resp YqlJsonQuoteResponse
		err = json.Unmarshal(raw, &resp)
		if err != nil {
			return nil, err
		}
		quotes = resp.Query.Results.Quote
	}

	results := make([]fquery.Quote, 0, len(quotes))
	for _, rawres := range quotes {
		rawres.Process()

		res := fquery.Quote{
			Name:             rawres.Name,
			Symbol:           rawres.Symbol,
			Updated:          time.Now(),
			Volume:           int64(rawres.Volume),
			AvgDailyVolume:   int64(rawres.AverageDailyVolume),
			Bid:              float64(rawres.Bid),
			Ask:              float64(rawres.Ask),
			Open:             float64(rawres.Open),
			PreviousClose:    float64(rawres.PreviousClose),
			LastTradePrice:   float64(rawres.LastTradePrice),
			Ma50:             float64(rawres.Ma50),
			Ma200:            float64(rawres.Ma200),
			DayLow:           float64(rawres.DayLow),
			DayHigh:          float64(rawres.DayHigh),
			YearLow:          float64(rawres.YearLow),
			YearHigh:         float64(rawres.YearHigh),
			EarningsPerShare: float64(rawres.EarningsPerShare),
			DividendPerShare: float64(rawres.DividendPerShare),
			DividendYield:    float64(rawres.DividendYield / 100),
			PeRatio:          float64(rawres.PeRatio),
		}
		if rawres.ExDividendDate != nil {
			res.DividendExDate = rawres.ExDividendDate.GetTime()
		}
		results = append(results, res)
	}

	return results, nil
}