twse

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package twse provides HTTP client to interact with the Taiwan Stock Exchange Corporation (TWSE) server.

Create one by NewClient with throttling implementation internally (you will be banned by the TWSE if query too frequently) to query quotes of all stocks in a day:

client := twse.NewClient(time.Second * 2)
qs, _ := client.FetchDayQuotes(time.Date(2021, 3, 24, 0, 0, 0, 0, time.UTC))

q := qs["2330"]
fmt.Println(q.Code, q.Name, q.Open, q.Close)

You can also create Client with your own HTTP client:

client := &twse.Client{HttpClient: &http.Client{}}

Error handling

Use type assertions or errors.As provided since Go 1.13 to check errors

if err != nil {
    var qe *twse.QuotaExceededError
    if errors.As(err, &e) {
        // You are unfortunately banned by the TWSE server.
    }
}

If panic happens, it is possibly due to the API change on the TWSE server side.

Example (Basic)
package main

import (
	"fmt"
	"time"

	"github.com/chehsunliu/tshakutshai/pkg/client/twse"
)

func main() {
	var client = twse.NewClient(time.Second * 3)

	date := time.Date(2021, time.March, 29, 0, 0, 0, 0, time.UTC)

	dayQuotes, err := client.FetchDayQuotes(date)
	if err != nil {
		panic(fmt.Sprintf("error fetching day quotes: %v", err))
	}

	quotes, err := client.FetchDailyQuotes("2330", 2021, time.February)
	if err != nil {
		panic(fmt.Sprintf("error fetching daily quotes of TSMC: %v", err))
	}

	fmt.Printf("%s: %v\n", dayQuotes["2330"].Name, dayQuotes["2330"].Close)
	fmt.Printf("%s: %v\n", dayQuotes["2454"].Name, dayQuotes["2454"].Close)

	for i := range quotes {
		fmt.Printf("%v: %v\n", quotes[i].Date.Format("20060102"), quotes[i].Close)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// HttpClient is the actual object that interacts with the TWSE server. It must not be nil; otherwise,
	// it will panic during fetching data.
	HttpClient tkthttp.Client
}

Client is a crawler gathering data from the TWSE server.

func NewClient

func NewClient(minInterval time.Duration) *Client

NewClient returns a new Client, which intervals between each query are not less than minInterval.

func (*Client) FetchDailyQuotes

func (c *Client) FetchDailyQuotes(code string, year int, month time.Month) ([]Quote, error)

FetchDailyQuotes return a Quote slice containing daily quotes on the month of the year.

func (*Client) FetchDayQuotes

func (c *Client) FetchDayQuotes(date time.Time) (map[string]Quote, error)

FetchDayQuotes returns a map that maps stock symbols to their corresponding quotes on that date.

func (*Client) FetchMonthlyQuotes

func (c *Client) FetchMonthlyQuotes(code string, year int) ([]Quote, error)

FetchMonthlyQuotes return a Quote slice containing monthly quotes of the year.

func (*Client) FetchYearlyQuotes

func (c *Client) FetchYearlyQuotes(code string) ([]Quote, error)

FetchYearlyQuotes return a Quote slice containing yearly quotes of all time.

type ConnectionError

type ConnectionError struct {
	Message string
}

ConnectionError is an error returned by Fetch functions when having problem to connect to the TWSE server.

func (*ConnectionError) Error

func (e *ConnectionError) Error() string

type QuotaExceededError

type QuotaExceededError struct {
	Message string
}

QuotaExceededError is an error returned by Fetch functions when query the TWSE server too frequently. Typically it takes around 1 hour to get back to normal.

func (*QuotaExceededError) Error

func (e *QuotaExceededError) Error() string

type Quote

type Quote struct {
	// Code/symbol of a stock, e.g. 0050 and 2330.
	Code string
	// Name is the Chinese stock name and only available in Client.FetchDayQuotes.
	Name string
	// Date represents the date in Client.FetchDayQuotes and Client.FetchDailyQuotes. You should ignore
	// the day field in Client.FetchMonthlyQuotes and even the month field in Client.FetchYearlyQuotes.
	Date time.Time

	Volume       uint64
	Transactions uint64
	Value        uint64

	// If no transactions are made, i.e. Transactions equals to zero, they will all zeros. Note that
	// Open and Close are only meaningful in Client.FetchDayQuotes and Client.FetchDailyQuotes.
	High  float64
	Low   float64
	Open  float64
	Close float64

	// These two fields are only used in Client.FetchYearlyQuotes.
	DateOfHigh time.Time
	DateOfLow  time.Time
}

Quote is the basic unit returned by the Fetch functions.

Jump to

Keyboard shortcuts

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