Documentation ¶
Overview ¶
Package aastocks is an extractor for AAStocks financial market data.
Quote ¶
Quote can be fetched from AAStocks with its symbol.
quote, err := aastocks.Get("00006") if err != nil { logger.Fatal(err) } ... Use quote to get its financial data (i.e. for its dividends and historical price).
Real Time Prices ¶
Prices can be served in real time by polling AAStocks for its price. Context can be used to control and stop the real time prices.
priceChan, errChan := quote.ServePrice(context.Background(), 5*time.Second) for { select { case p := <-priceChan: logger.Printf("price: %v\n", p) case err = <-errChan: logger.Printf("error: %v\n", err) } }
Example ¶
Example demonstrates getting financial data of quote from AAStocks
package main import ( "context" "log" "os" "time" "github.com/horacehylee/aastocks" ) func main() { logger := log.New(os.Stdout, "", log.Flags()) // Getting quote from AAStocks symbol := "00006" quote, err := aastocks.Get(symbol) if err != nil { logger.Fatal(err) } logger.Printf("Quote: %+v\n", quote) // Getting dividends of the quote from AAStocks d, err := quote.Dividends() if err != nil { logger.Fatal(err) } logger.Printf("Dividends count: %v\n", len(d)) // Getting historical prices of the quote from AAStocks prices, err := quote.HistoricalPrices(aastocks.Hourly) if err != nil { logger.Fatal(err) } logger.Printf("Historical prices count: %v\n", len(prices)) ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() // Continuously serving latest price of the quote from AAStocks priceChan, errChan := quote.ServePrices(ctx, 2*time.Second) for { select { case p := <-priceChan: logger.Printf("Price: %+v\n", p) case err = <-errChan: logger.Printf("Error: %v\n", err) case <-ctx.Done(): return } } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dividend ¶
type Dividend struct { AnnounceDate time.Time YearEnded time.Time Event string Particular string Type string ExDate time.Time PayableDate time.Time }
Dividend fetched from AAStocks
type HistoricalPrice ¶
HistoricalPrice is the historical price of quote.
type Option ¶
type Option func(q *Quote)
Option for getting symbol from AAStocks
func WithClient ¶
WithClient to customize the HTTP client used for AAStocks
type PriceFrequency ¶
type PriceFrequency int
PriceFrequency is the frequency of historical data to be provided.
const ( // Hourly price frequency Hourly PriceFrequency = 23 // Daily price frequency Daily PriceFrequency = 56 // Weekly price frequency Weekly PriceFrequency = 67 // Monthly price frequency Monthly PriceFrequency = 68 )
type PriceResult ¶
PriceResult is the result of serving real time prices.
type Quote ¶
type Quote struct { Symbol string Name string Price float64 Price52WLow float64 Price52WHigh float64 Yield float64 PeRatio float64 PbRatio float64 Lots int Eps float64 UpdateTime time.Time // contains filtered or unexported fields }
Quote of AAStocks data
func (*Quote) HistoricalPrices ¶
func (q *Quote) HistoricalPrices(frequency PriceFrequency) ([]HistoricalPrice, error)
HistoricalPrices fetches historical price of the quote from AAStocks.
func (*Quote) ServePrices ¶
func (q *Quote) ServePrices(ctx context.Context, delay time.Duration) (<-chan PriceResult, <-chan error)
ServePrices continuously fetching latest price from AAStocks. It will start goroutine to fetch real time prices.