aastocks

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2020 License: MIT Imports: 11 Imported by: 0

README

aastocks : AAStocks financial market data extractor

Overview

GoDoc Go Report Card codecov Sourcegraph

AAStocks financial market data extractor

Install

go get github.com/horacehylee/aastocks

Example

package main

import (
	"context"
	"log"
	"os"
	"time"

	"github.com/horacehylee/aastocks"
)

// Example demonstrates getting financial data of quote from AAStocks
func Example() {
	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
		}
	}
}

func main() {
    Example()
}

License

MIT.

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

type HistoricalPrice struct {
	Time  time.Time
	Open  float64
	High  float64
	Low   float64
	Close float64
}

HistoricalPrice is the historical price of quote.

type Option

type Option func(q *Quote)

Option for getting symbol from AAStocks

func WithClient

func WithClient(client *http.Client) Option

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

type PriceResult struct {
	Price  float64
	Symbol string
	Time   time.Time
}

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 Get

func Get(symbol string, opts ...Option) (*Quote, error)

Get quote from AAStocks with symbol

func (*Quote) Dividends

func (q *Quote) Dividends() ([]Dividend, error)

Dividends of the quote from AAStocks

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL