utils

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2019 License: MIT Imports: 18 Imported by: 0

README

gogrs - utils

GoDoc Build Status

Documentation

Overview

Package utils - 套件所需的公用工具(總和、平均、序列差、持續天數、 民國日期解析、簡單亂數、標準差、簡單 net/http 快取).

Index

Examples

Constants

View Source
const (
	TWSEURL     string = "http://mis.twse.com.tw"
	TWSEHOST    string = "http://www.twse.com.tw"
	OTCHOST     string = "http://www.tpex.org.tw"
	HOME        string = "/stock/index.jsp"
	OTCCSV      string = "/ch/stock/aftertrading/daily_trading_info/st43_download.php?d=%d/%02d&stkno=%s&r=%%d"           // year, mon, stock, rand
	OTCLISTCSV  string = "/web/stock/aftertrading/otc_quotes_no1430/stk_wn1430_download.php?l=zh-tw&d=%s&se=%s&s=0,asc,0" // date, cate
	TWSECSV     string = "/exchangeReport/STOCK_DAY?response=csv&date=%d%02d%02d&stockNo=%s"
	TWSELISTCSV string = "/exchangeReport/MI_INDEX?response=csv&date=%d%02d%02d&type=%s" // year, mon, day, type
	TWSEREAL    string = "/stock/api/getStockInfo.jsp?ex_ch=%s_%s.tw&json=1&delay=0&_=%d"
	QFIISTOP20  string = "/fund/MI_QFIIS_sort_20?response=csv&date=%d%02d%02d"   // yyyymmdd
	BFI82U      string = "/fund/BFI82U?response=csv&dayDate=%d%02d%02d&type=day" // yyyymmdd
	T86         string = "/fund/T86?response=csv&date=%d%02d%02d&selectType=ALL"
	TWTXXU      string = "/fund/%s?response=csv&date=%d%02d%02d"
	TWMTSS      string = "/exchangeReport/MI_MARGN?response=csv&date=%d%02d%02d&selectType=%s"
	S3CSV       string = "https://s3-ap-northeast-1.amazonaws.com/toomore/gogrs/list.csv"
)

TWSE base url.

View Source
const OTCDURTION = 0

與OTC訪問需間隔的時間差

View Source
const TWSEDURTION = 5

與TWSE訪問需間隔的時間差

View Source
const TempFolderName = ".gogrscache"

TempFolderName 快取資料夾名稱

Variables

View Source
var ExchangeMap = map[string]bool{"tse": true, "otc": true}

ExchangeMap is simple to check tse or otc.

View Source
var HTTPClient = &http.Client{Transport: &http.Transport{
	Proxy: http.ProxyFromEnvironment,
	Dial: (&net.Dialer{
		Timeout:   0,
		KeepAlive: 0,
	}).Dial,
	TLSHandshakeTimeout: 1 * time.Second,
},
}

HTTPClient is default func and fixed http too many open files.

View Source
var TaipeiTimeZone = time.FixedZone("Asia/Taipei", 8*3600)

TaipeiTimeZone is for time.Data() setting.

Functions

func AvgFloat64

func AvgFloat64(data []float64) float64

AvgFloat64 計算平均(float64)

Example
fmt.Println(AvgFloat64([]float64{1.1, 2.2, 9.9}))
Output:

4.4

func AvgUint64

func AvgUint64(data []uint64) uint64

AvgUint64 計算平均(uint64)

Example
fmt.Println(AvgUint64([]uint64{1, 2, 9}))
Output:

4

func CalDiffFloat64

func CalDiffFloat64(listA, listB []float64) []float64

CalDiffFloat64 計算兩序列的差(listA - listB)

Example
var sample1 = []float64{1.1, 2.2, 3.3, 4.4, 5.5}
var sample2 = []float64{5.5, 4.4, 3.3, 2.2, 2.2}
fmt.Println(CalDiffFloat64(sample1, sample2))
Output:

[-4.4 -2.2 0 2.2 3.3]

func CalDiffInt64

func CalDiffInt64(listA, listB []int64) []int64

CalDiffInt64 計算兩序列的差(listA - listB)

Example
var sample1 = []int64{1, 2, 3, 4, 5}
var sample2 = []int64{5, 4, 3, 2, 2}
fmt.Println(CalDiffInt64(sample1, sample2))
Output:

[-4 -2 0 2 3]

func CalLHPower

func CalLHPower(final, low, high []float64) []float64

CalLHPower 計算兩序列與終點的比例

func CountCountineFloat64

func CountCountineFloat64(data []float64) (int, bool)

CountCountineFloat64 計算最後一個數值為正或負值的持續天數(float64)

Example
var sample1 = []float64{-1, 0, 1, 2, 3}
fmt.Println(CountCountineFloat64(sample1))

var sample2 = []float64{1, 0, -1, -2, -3}
fmt.Println(CountCountineFloat64(sample2))
Output:

3 true
3 false

func Dbg added in v1.5.0

func Dbg(fmt_ string, args ...interface{})

func Dbgln added in v1.5.0

func Dbgln(args ...interface{})

func DeltaFloat64

func DeltaFloat64(data []float64) []float64

DeltaFloat64 計算數列間距差

Example
fmt.Println(DeltaFloat64([]float64{-1.1, 0, 1.1, 2.2, 3.3}))
Output:

[1.1 1.1 1.1 1.0999999999999996]

func DeltaInt64

func DeltaInt64(data []int64) []int64

DeltaInt64 計算數列間距差

Example
fmt.Println(DeltaInt64([]int64{-1, 0, 1, 2, 3}))
Output:

[1 1 1 1]

func DownloadFile added in v1.5.0

func DownloadFile(filepath string, url string) error

func GetMD5FilePath added in v1.5.0

func GetMD5FilePath(f StockCsvFile) string

func GetOSRamdiskPath

func GetOSRamdiskPath(goos string) string

GetOSRamdiskPath try to get RamDisk path.

func ParseDate

func ParseDate(strDate string) time.Time

ParseDate is to parse "104/01/13" format.

Example
fmt.Println(ParseDate("104/09/29"))
fmt.Println(ParseDate("104/9/29"))
fmt.Println(ParseDate("104/9/2"))
Output:

2015-09-29 00:00:00 +0800 Asia/Taipei
2015-09-29 00:00:00 +0800 Asia/Taipei
2015-09-02 00:00:00 +0800 Asia/Taipei

func RandInt

func RandInt() int

RandInt return random int.

func RecoveryStockBackup added in v1.5.0

func RecoveryStockBackup(date string) error

func SD

func SD(list []float64) float64

SD 計算標準差

func SDUint64

func SDUint64(list []uint64) float64

SDUint64 計算標準差

func SumFloat64

func SumFloat64(data []float64) float64

SumFloat64 計算總和(float64)

func SumUint64

func SumUint64(data []uint64) uint64

SumUint64 計算總和(uint64)

func ThanPastFloat64

func ThanPastFloat64(data []float64, days int, max bool) bool

ThanPastFloat64 計算最後一個數值是否為過去幾天最大或最小(float64)

func ThanPastUint64

func ThanPastUint64(data []uint64, days int, max bool) bool

ThanPastUint64 計算最後一個數值是否為過去幾天最大或最小(uint64)

func ThanSumPastFloat64

func ThanSumPastFloat64(data []float64, days int, max bool) bool

ThanSumPastFloat64 計算最後一個數值是否為過去幾天的總和大或小(float64)

func ThanSumPastUint64

func ThanSumPastUint64(data []uint64, days int, max bool) bool

ThanSumPastUint64 計算最後一個數值是否為過去幾天的總和大或小(uint64)

func Untar added in v1.5.0

func Untar(dst string, r io.Reader) error

Types

type HTTPCache

type HTTPCache struct {
	Dir string
	// contains filtered or unexported fields
}

HTTPCache net/http 快取功能

Example

目前可以支援 http.Get / http.PostForm 取得資料並儲存

hc := NewHTTPCache("/run/shm/", "utf8") // linux

ex1, _ := hc.Get("http://httpbin.org/get", false)
ex2, _ := hc.Get("http://httpbin.org/get?q=%d", true)
ex3, _ := hc.PostForm("http://httpbin.org/post", url.Values{"name": {"Toomore"}})

fmt.Printf("%s %s %s", ex1, ex2, ex3)
Output:

func NewHTTPCache

func NewHTTPCache(dir string, fromEncoding string) *HTTPCache

NewHTTPCache New 一個 HTTPCache.

dir 為暫存位置,fromEncoding 來源檔案的編碼,一律轉換為 utf8

func (*HTTPCache) FlushAll

func (hc *HTTPCache) FlushAll()

FlushAll 清除快取

func (HTTPCache) Get

func (hc HTTPCache) Get(url string, rand bool) ([]byte, error)

Get 透過 http.Get 取得檔案或從暫存中取得檔案

rand 為是否支援網址帶入亂數值,url 需有 '%d' 格式。

func (HTTPCache) PostForm

func (hc HTTPCache) PostForm(url string, data url.Values) ([]byte, error)

PostForm 透過 http.PostForm 取得檔案或從暫存中取得檔案

type StockCsvFile added in v1.5.0

type StockCsvFile interface {
	URL() string
}

Jump to

Keyboard shortcuts

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