goths

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: BSD-3-Clause Imports: 5 Imported by: 0

README

GoThs

GoDoc

GoThs 使用 Go (Golang) 开发的股票行情API接口.

项目介绍

项目参考 panghu1103的 trade-api

其他还有很多优秀的开源项目 pytdx gotdx

Documentation

Overview

对A股中沪深股票,指数,板块,债券和基金提供k线,分时成交和5档报价等行情功能。

GetSecurityBars,GetTransactionData和GetHistoryMinuteTimeData请求:

ths := goths.NewGoths(map[string]any{})

resp, err := ths.GetSecurityBars("1A0001", goths.MarketUSHI, -10, 0, goths.FuquanNo, goths.KlineDay)
...
resp, err := ths.GetTransactionData"1A0001", goths.MarketUSHI, 100)
...
resp, err := ths.GetHistoryMinuteTimeData("1A0001", goths.MarketUSHI, "20220729")

Examples

package main

import (
	"github.com/bensema/goths"
	"github.com/panghu1103/quote"
	"log"
	"time"
)

func main() {

	var ops = map[string]any{
		"username": "xxxxxx",
		"password": "xxxxxx",
		"addr":     "xxxxxx",
	}

	ths := goths.NewGoths(ops)
	quoteApi := quote.NewQuote(ops)

	ths.SetQuote(quoteApi)

	if err := ths.Connect(); err != nil {
		log.Fatal(err)
	}

	var req string

	req = "id=200&instance=815498240&zipversion=2&codelist=399001,399006&market=USZI&datatype=5,6,10,19"
	startTime := time.Now()
	results, err := ths.QueryDataAndParse(req)
	if err != nil {
		log.Fatal(err)
	}
	for _, result := range results {
		log.Println(result)
	}
	endTime := time.Now()
	log.Println("条数:", len(results), " 耗时:", endTime.Sub(startTime))

}

Output:

2022/07/31 10:20:04 map[code:USZI399001 prev_close:12428.72 price:12266.92 todo_32773:327680 turnover:6041.8436]
2022/07/31 10:20:04 map[code:USZI399006 prev_close:2705.9 price:2670.45 todo_32773:327680 turnover:2016.1737]
2022/07/31 10:20:04 条数: 2  耗时: 32.457397ms

Index

Examples

Constants

View Source
const (
	MarketUSHI = "USHI" // 上海指数
	MarketUSHA = "USHA" // 上海A股
	MarketUSHB = "USHB" // 上海B股
	MarketUSHD = "USHD" // 上海可转债
	MarketUSHJ = "USHJ" // 上海基金
	MarketUSHT = "USHT" //
	MarketUSHP = "USHP" //
	MarketUSZP = "USZP" //
	MarketUZZI = "UZZI" //
	MarketURFI = "URFI" // 沪深板块?
	MarketURFA = "URFA" // 沪深板块?

	MarketUIFF = "UIFF" // 股指期货

	MarketUFII = "UFII" // 国外指数
	MarketUCMI = "UCMI" // 国外指数

	MarketUNQS = "UNQS" // 美股
	MarketUNYN = "UNYN" // 美股
	MarketUNQQ = "UNQQ" // 美股
	MarketUNYA = "UNYA" // 美股
	MarketUUSD = "UUSD" // 美股
	MarketUUSA = "UUSA" // 美股

	MarketUFXB = "UFXB" // 外汇
	MarketUFXC = "UFXC" // 外汇
	MarketUFXR = "UFXR" // 外汇

	MarketUCMN = "UCMN" // 国外期货
	MarketUCMS = "UCMS" // 国外期货
	MarketUFIS = "UFIS" // 国外期货

	MarketUCFS = "UCFS" // 国内期货
	MarketUCFD = "UCFD" // 国内期货
	MarketUCFZ = "UCFZ" // 国内期货

	MarketUSTA = "USTA" // 股转
	MarketUSTB = "USTB" // 股转
	MarketUSTT = "USTT" // 股转

	MarketUSTI = "USTI" // 股转指数

	MarketUSOO = "USOO" // 期权
	MarketUZOO = "UZOO" // 期权
	MarketUCFT = "UCFT" // 期权
	MarketUCFL = "UCFL" // 期权
	MarketUCFX = "UCFX" // 期权
	MarketUIFB = "UIFB" // 期权

	MarketUHKI = "UHKI" // 港股指数
	MarketUSZI = "USZI" // 深圳指数
	MarketUSZA = "USZA" // 深圳A股
	MarketUSZB = "USZB" // 深圳B股
	MarketUSZJ = "USZJ" // 深圳基金
	MarketUSZD = "USZD" // 深圳可转债

)

Market 请求市场字段。

规则 A:A股 B:B股 H:港股 Q:权证 Z:债券 K:板块 X:指数 F:基金 U:美股 G:沪港通 D:叠加 All:所有 QH:期货。

View Source
const (
	Fuquanqian = "Q" // 前复权
	Fuquanhou  = "B" // 后复权
	FuquanNo   = ""  // 不复权
)

复权

View Source
const (
	Kline1m        = 0x3001 // 1分钟k
	Kline5m        = 0x3005 // 5分钟k
	Kline15m       = 0x300f // 15分钟k
	Kline30m       = 0x301e // 30分钟k
	Kline60m       = 0x303c // 60分钟k
	Kline120m      = 0x3078 // 120分钟k
	KlineDay       = 0x4000 // 日k
	KlineWeek      = 0x5001 // 周k
	KlineMoon      = 0x6001 // 月k
	KlineQuarterly = 0x6003 // 季k
	KlineYear      = 0x7001 // 年k
)

K线

View Source
const (
	ZipVersion = "2"
)

Zip版本

Variables

View Source
var ErrEmptyData = errors.New("error empty data")

空数据

Functions

func FieldName

func FieldName(num uint32) string

FieldName 数字转换对应的字段名

如果未找到对应字段会返回 todo_num

func LoadQuote

func LoadQuote(pluginPath string, ops map[string]any) (any, error)

加载Quote插件类似于加载DLL

Types

type Quote

type Quote interface {
	// 连接
	Connect() error

	// 关闭连接
	DisConnect() error

	// 配置 请求数据前线配置option
	Ops(ops map[string]any) error

	// 请求数据
	Write(req string) error

	// 读取数据
	Read() (any, error)
}

type QuoteResponse

type QuoteResponse interface {
	// 头信息
	Head() map[string]any

	// 主要数据再body
	ArrBody() []map[uint32]any

	// 辅助body额外信息
	DicExt() map[uint32]any

	// 额外信息
	Extra() any
}

type Ths

type Ths struct {
	// contains filtered or unexported fields
}

func NewGoths

func NewGoths(ops map[string]any) *Ths

func (*Ths) Connect

func (ths *Ths) Connect() error

Connect quote连接行情接口,使用使用前必须先调用Connect

Example
package main

import (
	"github.com/bensema/goths"

	"log"
)

func main() {
	var ops = map[string]any{
		"username": "xxxxxx",
		"password": "xxxxxx",
		"addr":     "xxxxxx",
	}

	ths := goths.NewGoths(ops)
	quote, err := goths.LoadQuote("./quote.so", ops)
	if err != nil {
		log.Fatal(err)
	}
	ths.SetQuote(quote.(goths.Quote))

	if err := ths.Connect(); err != nil {
		log.Println(err)
	}

}
Output:

nil

func (*Ths) DisConnect

func (ths *Ths) DisConnect() error

DisConnect 断开quote行情

func (*Ths) GetHistoryMinuteTimeData

func (ths *Ths) GetHistoryMinuteTimeData(code string, market string, date string) ([]map[string]any, error)

GetHistoryMinuteTimeData 获取历史分时成交单图数据 可能用于做分时间图

date=0 表示当天

func (*Ths) GetSecurityBars

func (ths *Ths) GetSecurityBars(code string, market string, start int, end int, fuquan string, period int) ([]map[string]any, error)

GetSecurityBars K线

code: 股票代码

market: 对应市场

start: start是倒序,如果想查20条 start就对应 -20

end: 结束点,到最新对应0

fuquan Q:前复权 B:后复权 空为不复权

period: 对应kline项

Example
package main

import (
	"github.com/bensema/goths"

	"log"
)

func main() {
	var ops = map[string]any{
		"username": "xxxxxx",
		"password": "xxxxxx",
		"addr":     "xxxxxx",
	}

	ths := goths.NewGoths(ops)
	quote, err := goths.LoadQuote("./quote.so", ops)
	if err != nil {
		log.Fatal(err)
	}
	ths.SetQuote(quote.(goths.Quote))

	if err := ths.Connect(); err != nil {
		log.Fatal(err)
	}

	results, err := ths.GetSecurityBars("1A0001", goths.MarketUSHI, -10, 0, goths.FuquanNo, goths.KlineDay)
	if err != nil {
		log.Fatal(err)
	}

	for _, result := range results {
		log.Println(result)
	}
	log.Println("条数:", len(results))

}
Output:

2022/07/31 09:26:58 map[close:3228.06 code:USHI1A0001 high:3288.92 low:3228.06 open:3261.38 time:20220715 todo_32773:2162688 turnover:4608.7702 volume:38015.637 期转现成交量:0]
2022/07/31 09:26:58 map[close:3278.1 code:USHI1A0001 high:3278.47 low:3226.23 open:3235.09 time:20220718 todo_32773:2162688 turnover:4233.5727 volume:34834.602 期转现成交量:0]
2022/07/31 09:26:58 map[close:3279.43 code:USHI1A0001 high:3283.93 low:3256.02 open:3278.72 time:20220719 todo_32773:2162688 turnover:3960.4236 volume:31565.192 期转现成交量:0]
2022/07/31 09:26:58 map[close:3304.72 code:USHI1A0001 high:3308.35 low:3287.74 open:3291.55 time:20220720 todo_32773:2162688 turnover:3834.2275 volume:30151.512 期转现成交量:0]
2022/07/31 09:26:58 map[close:3272 code:USHI1A0001 high:3300.53 low:3272 open:3297.66 time:20220721 todo_32773:2162688 turnover:4206.9658 volume:33235.871 期转现成交量:0]
2022/07/31 09:26:58 map[close:3269.97 code:USHI1A0001 high:3293.64 low:3246.73 open:3276.2 time:20220722 todo_32773:2162688 turnover:3835.7601 volume:30069.007 期转现成交量:0]
2022/07/31 09:26:58 map[close:3250.39 code:USHI1A0001 high:3273.18 low:3243.03 open:3269.71 time:20220725 todo_32773:2162688 turnover:3482.8187 volume:27124.574 期转现成交量:0]
2022/07/31 09:26:58 map[close:3277.44 code:USHI1A0001 high:3282.41 low:3246.04 open:3254.19 time:20220726 todo_32773:2162688 turnover:3344.2159 volume:25946.868 期转现成交量:0]
2022/07/31 09:26:58 map[close:3275.76 code:USHI1A0001 high:3282.57 low:3265.73 open:3271.78 time:20220727 todo_32773:2162688 turnover:3397.6824 volume:24913.149 期转现成交量:0]
2022/07/31 09:26:58 map[close:3282.58 code:USHI1A0001 high:3305.71 low:3277.11 open:3287.5 time:20220728 todo_32773:2162688 turnover:3959.2556 volume:28805.506 期转现成交量:0]
2022/07/31 09:26:58 map[close:3253.24 code:USHI1A0001 high:3294.8 low:3246.37 open:3282.81 time:20220729 todo_32773:2162688 turnover:4105.5309 volume:30733.105 期转现成交量:0]
2022/07/31 09:26:58 条数: 11

func (*Ths) GetTransactionData

func (ths *Ths) GetTransactionData(code string, market string, count int) ([]map[string]any, error)

GetTransactionData 获取分时成交单

func (*Ths) GetTransactionTickData

func (ths *Ths) GetTransactionTickData(code string, market string, count int) ([]map[string]any, error)

GetTransactionTickData 逐笔成交

func (*Ths) QueryData

func (ths *Ths) QueryData(req string) ([]map[uint32]any, error)

QueryData 查询数据返回初步处理后的数据

func (*Ths) QueryDataAndParse

func (ths *Ths) QueryDataAndParse(req string) ([]map[string]any, error)

QueryDataAndParse 查询数据返回初步处理后的加工对应field name

Example
package main

import (
	"github.com/bensema/goths"

	"log"
	"time"
)

func main() {
	var ops = map[string]any{
		"username": "xxxxxx",
		"password": "xxxxxx",
		"addr":     "xxxxxx",
	}

	ths := goths.NewGoths(ops)
	quote, err := goths.LoadQuote("./quote.so", ops)
	if err != nil {
		log.Fatal(err)
	}
	ths.SetQuote(quote.(goths.Quote))

	if err := ths.Connect(); err != nil {
		log.Fatal(err)
	}

	req := "id=200&instance=815498240&zipversion=2&codelist=399001,399006&market=USZI&datatype=5,6,10,19"
	startTime := time.Now()
	results, err := ths.QueryDataAndParse(req)
	if err != nil {
		log.Fatal(err)
	}
	for _, result := range results {
		log.Println(result)
	}
	endTime := time.Now()
	log.Println("条数:", len(results), " 耗时:", endTime.Sub(startTime))

}
Output:

2022/07/31 10:20:04 map[code:USZI399001 prev_close:12428.72 price:12266.92 todo_32773:327680 turnover:6041.8436]
2022/07/31 10:20:04 map[code:USZI399006 prev_close:2705.9 price:2670.45 todo_32773:327680 turnover:2016.1737]
2022/07/31 10:20:04 条数: 2  耗时: 32.457397ms

func (*Ths) Read

func (ths *Ths) Read() (QuoteResponse, error)

Read 读取返回

func (*Ths) SetQuote

func (ths *Ths) SetQuote(quote Quote)

SetQuote 设置行情实例

func (*Ths) Write

func (ths *Ths) Write(req string) error

Write 请求

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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