mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80d6d6dfc5 | ||
|
|
a27461740a | ||
|
|
7a9d59f8f1 | ||
|
|
cba35308f6 | ||
|
|
424e259f2b | ||
|
|
3ea8d50cd7 | ||
|
|
09116ee8e8 | ||
|
|
31fcc0b3a3 | ||
|
|
296b7197a1 | ||
|
|
b4f748306a | ||
|
|
0cfd21f8fb | ||
|
|
1c9fd75876 | ||
|
|
e4de354a66 | ||
|
|
6062da9cb2 | ||
|
|
0414cbebc3 | ||
|
|
b221dbc3e2 | ||
|
|
51d8d06fbd | ||
|
|
9df96f0707 | ||
|
|
53cc5fd743 | ||
|
|
3e4c7a16ff | ||
|
|
4114bc72dd | ||
|
|
cd5cf290f1 |
151
client.go
151
client.go
@@ -1,21 +1,30 @@
|
|||||||
package tdx
|
package tdx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/injoyai/base/maps"
|
"github.com/injoyai/base/maps"
|
||||||
"github.com/injoyai/base/maps/wait/v2"
|
"github.com/injoyai/base/maps/wait/v2"
|
||||||
"github.com/injoyai/conv"
|
"github.com/injoyai/conv"
|
||||||
"github.com/injoyai/ios"
|
"github.com/injoyai/ios"
|
||||||
"github.com/injoyai/ios/client"
|
"github.com/injoyai/ios/client"
|
||||||
"github.com/injoyai/ios/module/tcp"
|
|
||||||
"github.com/injoyai/logs"
|
"github.com/injoyai/logs"
|
||||||
"github.com/injoyai/tdx/protocol"
|
"github.com/injoyai/tdx/protocol"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LevelNone = 0
|
||||||
|
LevelDebug = 2
|
||||||
|
LevelWrite = 3
|
||||||
|
LevelRead = 4
|
||||||
|
LevelInfo = 5
|
||||||
|
LevelError = 7
|
||||||
|
LevelAll = 999
|
||||||
|
)
|
||||||
|
|
||||||
// WithDebug 是否打印通讯数据
|
// WithDebug 是否打印通讯数据
|
||||||
func WithDebug(b ...bool) client.Option {
|
func WithDebug(b ...bool) client.Option {
|
||||||
return func(c *client.Client) {
|
return func(c *client.Client) {
|
||||||
@@ -23,6 +32,12 @@ func WithDebug(b ...bool) client.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithLevel(level int) client.Option {
|
||||||
|
return func(c *client.Client) {
|
||||||
|
c.Logger.SetLevel(level)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithRedial 断线重连
|
// WithRedial 断线重连
|
||||||
func WithRedial(b ...bool) client.Option {
|
func WithRedial(b ...bool) client.Option {
|
||||||
return func(c *client.Client) {
|
return func(c *client.Client) {
|
||||||
@@ -30,17 +45,15 @@ func WithRedial(b ...bool) client.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dial 与服务器建立连接
|
|
||||||
func Dial(addr string, op ...client.Option) (cli *Client, err error) {
|
|
||||||
if !strings.Contains(addr, ":") {
|
|
||||||
addr += ":7709"
|
|
||||||
}
|
|
||||||
return DialWith(tcp.NewDial(addr), op...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialDefault 默认连接方式
|
// DialDefault 默认连接方式
|
||||||
func DialDefault(op ...client.Option) (cli *Client, err error) {
|
func DialDefault(op ...client.Option) (cli *Client, err error) {
|
||||||
return DialWith(NewHostDial(Hosts), op...)
|
op = append([]client.Option{WithRedial()}, op...)
|
||||||
|
return DialHostsRange(Hosts, op...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial 与服务器建立连接
|
||||||
|
func Dial(addr string, op ...client.Option) (cli *Client, err error) {
|
||||||
|
return DialWith(NewTCPDial(addr), op...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialHosts 与服务器建立连接,多个服务器轮询,开启重试生效
|
// DialHosts 与服务器建立连接,多个服务器轮询,开启重试生效
|
||||||
@@ -53,6 +66,11 @@ func DialHostsRandom(hosts []string, op ...client.Option) (cli *Client, err erro
|
|||||||
return DialWith(NewRandomDial(hosts), op...)
|
return DialWith(NewRandomDial(hosts), op...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialHostsRange 遍历设置的服务地址进行连接,成功则结束遍历
|
||||||
|
func DialHostsRange(hosts []string, op ...client.Option) (cli *Client, err error) {
|
||||||
|
return DialWith(NewRangeDial(hosts), op...)
|
||||||
|
}
|
||||||
|
|
||||||
// DialWith 与服务器建立连接
|
// DialWith 与服务器建立连接
|
||||||
func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
|
func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
|
||||||
|
|
||||||
@@ -85,7 +103,17 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
|
|||||||
|
|
||||||
go cli.Client.Run()
|
go cli.Client.Run()
|
||||||
|
|
||||||
return cli, nil
|
/*
|
||||||
|
部分接口需要通过代码信息计算得出
|
||||||
|
*/
|
||||||
|
codesOnce.Do(func() {
|
||||||
|
//初始化代码管理
|
||||||
|
if DefaultCodes == nil {
|
||||||
|
DefaultCodes, err = NewCodes(cli, "./codes.db")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return cli, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@@ -215,6 +243,12 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
|
|||||||
|
|
||||||
// GetQuote 获取盘口五档报价
|
// GetQuote 获取盘口五档报价
|
||||||
func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
||||||
|
if DefaultCodes == nil {
|
||||||
|
return nil, errors.New("DefaultCodes未初始化")
|
||||||
|
}
|
||||||
|
for i := range codes {
|
||||||
|
codes[i] = DefaultCodes.AddExchange(codes[i])
|
||||||
|
}
|
||||||
f, err := protocol.MQuote.Frame(codes...)
|
f, err := protocol.MQuote.Frame(codes...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -223,7 +257,38 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return result.(protocol.QuotesResp), nil
|
quotes := result.(protocol.QuotesResp)
|
||||||
|
|
||||||
|
{ //todo 临时处理下先,后续优化,感觉有问题
|
||||||
|
//判断长度和预期是否一致
|
||||||
|
if len(quotes) != len(codes) {
|
||||||
|
return nil, fmt.Errorf("预期%d个,实际%d个", len(codes), len(quotes))
|
||||||
|
}
|
||||||
|
if DefaultCodes == nil {
|
||||||
|
return nil, errors.New("DefaultCodes未初始化")
|
||||||
|
}
|
||||||
|
for i, code := range codes {
|
||||||
|
m := DefaultCodes.Get(code)
|
||||||
|
if m == nil {
|
||||||
|
return nil, fmt.Errorf("未查询到代码[%s]相关信息", code)
|
||||||
|
}
|
||||||
|
for ii, v := range quotes[i].SellLevel {
|
||||||
|
quotes[i].SellLevel[ii].Price = m.Price(v.Price)
|
||||||
|
}
|
||||||
|
for ii, v := range quotes[i].BuyLevel {
|
||||||
|
quotes[i].BuyLevel[ii].Price = m.Price(v.Price)
|
||||||
|
}
|
||||||
|
quotes[i].K = protocol.K{
|
||||||
|
Last: m.Price(quotes[i].K.Last),
|
||||||
|
Open: m.Price(quotes[i].K.Open),
|
||||||
|
High: m.Price(quotes[i].K.High),
|
||||||
|
Low: m.Price(quotes[i].K.Low),
|
||||||
|
Close: m.Price(quotes[i].K.Close),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return quotes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMinute 获取分时数据,todo 解析好像不对,先用历史数据
|
// GetMinute 获取分时数据,todo 解析好像不对,先用历史数据
|
||||||
@@ -322,6 +387,12 @@ func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.Histo
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
// GetIndex 获取指数,接口是和k线一样的,但是解析不知道怎么区分(解析方式不一致),所以加一个方法
|
// GetIndex 获取指数,接口是和k线一样的,但是解析不知道怎么区分(解析方式不一致),所以加一个方法
|
||||||
func (this *Client) GetIndex(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) {
|
func (this *Client) GetIndex(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||||
code = protocol.AddPrefix(code)
|
code = protocol.AddPrefix(code)
|
||||||
@@ -342,7 +413,7 @@ func (this *Client) GetIndexUntil(Type uint8, code string, f func(k *protocol.Kl
|
|||||||
size := uint16(800)
|
size := uint16(800)
|
||||||
var last *protocol.Kline
|
var last *protocol.Kline
|
||||||
for start := uint16(0); ; start += size {
|
for start := uint16(0); ; start += size {
|
||||||
r, err := this.GetKline(Type, code, start, size)
|
r, err := this.GetIndex(Type, code, start, size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -368,6 +439,11 @@ func (this *Client) GetIndexUntil(Type uint8, code string, f func(k *protocol.Kl
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetIndexAll 获取全部k线数据
|
||||||
|
func (this *Client) GetIndexAll(Type uint8, code string) (*protocol.KlineResp, error) {
|
||||||
|
return this.GetIndexUntil(Type, code, func(k *protocol.Kline) bool { return false })
|
||||||
|
}
|
||||||
|
|
||||||
func (this *Client) GetIndexDay(code string, start, count uint16) (*protocol.KlineResp, error) {
|
func (this *Client) GetIndexDay(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||||
return this.GetIndex(protocol.TypeKlineDay, code, start, count)
|
return this.GetIndex(protocol.TypeKlineDay, code, start, count)
|
||||||
}
|
}
|
||||||
@@ -376,6 +452,31 @@ func (this *Client) GetIndexDayUntil(code string, f func(k *protocol.Kline) bool
|
|||||||
return this.GetIndexUntil(protocol.TypeKlineDay, code, f)
|
return this.GetIndexUntil(protocol.TypeKlineDay, code, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Client) GetIndexDayAll(code string) (*protocol.KlineResp, error) {
|
||||||
|
return this.GetIndexAll(protocol.TypeKlineDay, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Client) GetIndexWeekAll(code string) (*protocol.KlineResp, error) {
|
||||||
|
return this.GetIndexAll(protocol.TypeKlineWeek, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Client) GetIndexMonthAll(code string) (*protocol.KlineResp, error) {
|
||||||
|
return this.GetIndexAll(protocol.TypeKlineMonth, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Client) GetIndexQuarterAll(code string) (*protocol.KlineResp, error) {
|
||||||
|
return this.GetIndexAll(protocol.TypeKlineQuarter, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Client) GetIndexYearAll(code string) (*protocol.KlineResp, error) {
|
||||||
|
return this.GetIndexAll(protocol.TypeKlineYear, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
// GetKline 获取k线数据,推荐收盘之后获取,否则会获取到当天的数据
|
// GetKline 获取k线数据,推荐收盘之后获取,否则会获取到当天的数据
|
||||||
func (this *Client) GetKline(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) {
|
func (this *Client) GetKline(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||||
code = protocol.AddPrefix(code)
|
code = protocol.AddPrefix(code)
|
||||||
@@ -424,27 +525,7 @@ func (this *Client) GetKlineUntil(Type uint8, code string, f func(k *protocol.Kl
|
|||||||
|
|
||||||
// GetKlineAll 获取全部k线数据
|
// GetKlineAll 获取全部k线数据
|
||||||
func (this *Client) GetKlineAll(Type uint8, code string) (*protocol.KlineResp, error) {
|
func (this *Client) GetKlineAll(Type uint8, code string) (*protocol.KlineResp, error) {
|
||||||
resp := &protocol.KlineResp{}
|
return this.GetKlineUntil(Type, code, func(k *protocol.Kline) bool { return false })
|
||||||
size := uint16(800)
|
|
||||||
var last *protocol.Kline
|
|
||||||
for start := uint16(0); ; start += size {
|
|
||||||
r, err := this.GetKline(Type, code, start, size)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if last != nil && len(r.List) > 0 {
|
|
||||||
last.Last = r.List[len(r.List)-1].Close
|
|
||||||
}
|
|
||||||
if len(r.List) > 0 {
|
|
||||||
last = r.List[0]
|
|
||||||
}
|
|
||||||
resp.Count += r.Count
|
|
||||||
resp.List = append(r.List, resp.List...)
|
|
||||||
if r.Count < size {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetKlineMinute 获取一分钟k线数据,每次最多800条,最多只能获取24000条数据
|
// GetKlineMinute 获取一分钟k线数据,每次最多800条,最多只能获取24000条数据
|
||||||
|
|||||||
182
codes.go
182
codes.go
@@ -1,16 +1,25 @@
|
|||||||
package tdx
|
package tdx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/injoyai/conv"
|
||||||
"github.com/injoyai/logs"
|
"github.com/injoyai/logs"
|
||||||
"github.com/injoyai/tdx/protocol"
|
"github.com/injoyai/tdx/protocol"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"xorm.io/core"
|
"xorm.io/core"
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 增加单例,部分数据需要通过Codes里面的信息计算
|
||||||
|
var (
|
||||||
|
DefaultCodes *Codes
|
||||||
|
codesOnce sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
func NewCodes(c *Client, filename string) (*Codes, error) {
|
func NewCodes(c *Client, filename string) (*Codes, error) {
|
||||||
|
|
||||||
//如果文件夹不存在就创建
|
//如果文件夹不存在就创建
|
||||||
@@ -27,69 +36,168 @@ func NewCodes(c *Client, filename string) (*Codes, error) {
|
|||||||
if err := db.Sync2(new(CodeModel)); err != nil {
|
if err := db.Sync2(new(CodeModel)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := db.Sync2(new(UpdateModel)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
update := new(UpdateModel)
|
||||||
|
{ //查询或者插入一条数据
|
||||||
|
has, err := db.Get(update)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if !has {
|
||||||
|
if _, err := db.Insert(update); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cc := &Codes{
|
cc := &Codes{
|
||||||
Client: c,
|
Client: c,
|
||||||
db: db,
|
db: db,
|
||||||
Codes: nil,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//设置定时器,每天早上9点更新数据
|
{ //设置定时器,每天早上9点更新数据
|
||||||
task := cron.New(cron.WithSeconds())
|
task := cron.New(cron.WithSeconds())
|
||||||
task.AddFunc("0 0 9 * * *", func() {
|
task.AddFunc("0 0 9 * * *", func() {
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
if err := cc.Update(); err == nil {
|
if err := cc.Update(); err == nil {
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
logs.Err(err)
|
||||||
|
<-time.After(time.Minute * 5)
|
||||||
}
|
}
|
||||||
logs.Err(err)
|
})
|
||||||
<-time.After(time.Minute * 5)
|
task.Start()
|
||||||
}
|
}
|
||||||
})
|
|
||||||
task.Start()
|
|
||||||
|
|
||||||
return cc, cc.Update()
|
{ //判断是否更新过,更新过则不更新
|
||||||
|
now := time.Now()
|
||||||
|
node := time.Date(now.Year(), now.Month(), now.Day(), 9, 0, 0, 0, time.Local)
|
||||||
|
updateTime := time.Unix(update.Time, 0)
|
||||||
|
if now.Sub(node) > 0 {
|
||||||
|
//当前时间在9点之后,且更新时间在9点之前,需要更新
|
||||||
|
if updateTime.Sub(node) < 0 {
|
||||||
|
return cc, cc.Update()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//当前时间在9点之前,且更新时间在上个节点之前
|
||||||
|
if updateTime.Sub(node.Add(time.Hour*24)) < 0 {
|
||||||
|
return cc, cc.Update()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//从缓存中加载
|
||||||
|
return cc, cc.Update(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Codes struct {
|
type Codes struct {
|
||||||
*Client //客户端
|
*Client //客户端
|
||||||
db *xorm.Engine //数据库实例
|
db *xorm.Engine //数据库实例
|
||||||
Codes map[string]*CodeModel //股票缓存
|
Map map[string]*CodeModel //股票缓存
|
||||||
|
list []*CodeModel //列表方式缓存
|
||||||
|
exchanges map[string][]string //交易所缓存
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName 获取股票名称
|
// GetName 获取股票名称
|
||||||
func (this *Codes) GetName(code string) string {
|
func (this *Codes) GetName(code string) string {
|
||||||
if v, ok := this.Codes[code]; ok {
|
if v, ok := this.Map[code]; ok {
|
||||||
return v.Name
|
return v.Name
|
||||||
}
|
}
|
||||||
return "未知"
|
return "未知"
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
|
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
|
||||||
func (this *Codes) GetStocks() []string {
|
func (this *Codes) GetStocks(limits ...int) []string {
|
||||||
|
limit := conv.DefaultInt(-1, limits...)
|
||||||
ls := []string(nil)
|
ls := []string(nil)
|
||||||
for k, _ := range this.Codes {
|
for _, m := range this.list {
|
||||||
if protocol.IsStock(k) {
|
code := m.FullCode()
|
||||||
ls = append(ls, k)
|
if protocol.IsStock(code) {
|
||||||
|
ls = append(ls, code)
|
||||||
|
}
|
||||||
|
if limit > 0 && len(ls) >= limit {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ls
|
return ls
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Codes) Update() error {
|
func (this *Codes) Get(code string) *CodeModel {
|
||||||
codes, err := this.Code(false)
|
return this.Map[code]
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetExchange 获取股票交易所,这里的参数不需要带前缀
|
||||||
|
func (this *Codes) GetExchange(code string) protocol.Exchange {
|
||||||
|
if len(code) == 6 {
|
||||||
|
switch {
|
||||||
|
case code[:1] == "6":
|
||||||
|
return protocol.ExchangeSH
|
||||||
|
case code[:1] == "0":
|
||||||
|
return protocol.ExchangeSZ
|
||||||
|
case code[:2] == "30":
|
||||||
|
return protocol.ExchangeSZ
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var exchange string
|
||||||
|
exchanges := this.exchanges[code]
|
||||||
|
if len(exchanges) >= 1 {
|
||||||
|
exchange = exchanges[0]
|
||||||
|
}
|
||||||
|
if len(code) == 8 {
|
||||||
|
exchange = code[0:2]
|
||||||
|
}
|
||||||
|
switch exchange {
|
||||||
|
case protocol.ExchangeSH.String():
|
||||||
|
return protocol.ExchangeSH
|
||||||
|
case protocol.ExchangeSZ.String():
|
||||||
|
return protocol.ExchangeSZ
|
||||||
|
default:
|
||||||
|
return protocol.ExchangeSH
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Codes) AddExchange(code string) string {
|
||||||
|
if exchanges := this.exchanges[code]; len(exchanges) == 1 {
|
||||||
|
return exchanges[0] + code
|
||||||
|
}
|
||||||
|
if len(code) == 6 {
|
||||||
|
switch {
|
||||||
|
case code[:1] == "6":
|
||||||
|
return protocol.ExchangeSH.String() + code
|
||||||
|
case code[:1] == "0":
|
||||||
|
return protocol.ExchangeSZ.String() + code
|
||||||
|
case code[:2] == "30":
|
||||||
|
return protocol.ExchangeSZ.String() + code
|
||||||
|
}
|
||||||
|
return this.GetExchange(code).String() + code
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update 更新数据,从服务器或者数据库
|
||||||
|
func (this *Codes) Update(byDB ...bool) error {
|
||||||
|
codes, err := this.GetCodes(len(byDB) > 0 && byDB[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
codeMap := make(map[string]*CodeModel)
|
codeMap := make(map[string]*CodeModel)
|
||||||
|
exchanges := make(map[string][]string)
|
||||||
for _, code := range codes {
|
for _, code := range codes {
|
||||||
codeMap[code.Exchange+code.Code] = code
|
codeMap[code.Exchange+code.Code] = code
|
||||||
|
exchanges[code.Code] = append(exchanges[code.Code], code.Exchange)
|
||||||
}
|
}
|
||||||
this.Codes = codeMap
|
this.Map = codeMap
|
||||||
return nil
|
this.list = codes
|
||||||
|
this.exchanges = exchanges
|
||||||
|
//更新时间
|
||||||
|
_, err = this.db.Update(&UpdateModel{Time: time.Now().Unix()})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Code 更新股票并返回结果
|
// GetCodes 更新股票并返回结果
|
||||||
func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) {
|
func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
|
||||||
|
|
||||||
//2. 查询数据库所有股票
|
//2. 查询数据库所有股票
|
||||||
list := []*CodeModel(nil)
|
list := []*CodeModel(nil)
|
||||||
@@ -151,7 +259,7 @@ func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, v := range update {
|
for _, v := range update {
|
||||||
if _, err := session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name").Update(v); err != nil {
|
if _, err := session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name,LastPrice").Update(v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -165,6 +273,14 @@ func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateModel struct {
|
||||||
|
Time int64 //更新时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*UpdateModel) TableName() string {
|
||||||
|
return "update"
|
||||||
|
}
|
||||||
|
|
||||||
type CodeModel struct {
|
type CodeModel struct {
|
||||||
ID int64 `json:"id"` //主键
|
ID int64 `json:"id"` //主键
|
||||||
Name string `json:"name"` //名称,有时候名称会变,例STxxx
|
Name string `json:"name"` //名称,有时候名称会变,例STxxx
|
||||||
@@ -177,10 +293,18 @@ type CodeModel struct {
|
|||||||
InDate int64 `json:"inDate" xorm:"created"` //创建时间
|
InDate int64 `json:"inDate" xorm:"created"` //创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CodeModel) TableName() string {
|
func (*CodeModel) TableName() string {
|
||||||
return "codes"
|
return "codes"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *CodeModel) FullCode() string {
|
||||||
|
return this.Exchange + this.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CodeModel) Price(p protocol.Price) protocol.Price {
|
||||||
|
return p * protocol.Price(math.Pow10(int(3-this.Decimal)))
|
||||||
|
}
|
||||||
|
|
||||||
func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {
|
func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {
|
||||||
session := db.NewSession()
|
session := db.NewSession()
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|||||||
36
dial.go
36
dial.go
@@ -3,12 +3,21 @@ package tdx
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/injoyai/ios"
|
"github.com/injoyai/ios"
|
||||||
|
"github.com/injoyai/ios/module/tcp"
|
||||||
|
"github.com/injoyai/logs"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func NewTCPDial(addr string) ios.DialFunc {
|
||||||
|
if !strings.Contains(addr, ":") {
|
||||||
|
addr += ":7709"
|
||||||
|
}
|
||||||
|
return tcp.NewDial(addr)
|
||||||
|
}
|
||||||
|
|
||||||
func NewHostDial(hosts []string) ios.DialFunc {
|
func NewHostDial(hosts []string) ios.DialFunc {
|
||||||
if len(hosts) == 0 {
|
if len(hosts) == 0 {
|
||||||
hosts = Hosts
|
hosts = Hosts
|
||||||
@@ -43,3 +52,30 @@ func NewRandomDial(hosts []string) ios.DialFunc {
|
|||||||
return c, addr, err
|
return c, addr, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewRangeDial(hosts []string) ios.DialFunc {
|
||||||
|
if len(hosts) == 0 {
|
||||||
|
hosts = Hosts
|
||||||
|
}
|
||||||
|
return func(ctx context.Context) (c ios.ReadWriteCloser, _ string, err error) {
|
||||||
|
for i, addr := range hosts {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, "", ctx.Err()
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
if !strings.Contains(addr, ":") {
|
||||||
|
addr += ":7709"
|
||||||
|
}
|
||||||
|
c, err = net.Dial("tcp", addr)
|
||||||
|
if err == nil {
|
||||||
|
return c, addr, nil
|
||||||
|
}
|
||||||
|
if i < len(hosts)-1 {
|
||||||
|
//最后一个错误返回出去
|
||||||
|
logs.Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
11
example/DialHostsRange/main.go
Normal file
11
example/DialHostsRange/main.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/injoyai/logs"
|
||||||
|
"github.com/injoyai/tdx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
_, err := tdx.DialHostsRange([]string{"1", "2", "127.0.0.1"})
|
||||||
|
logs.PrintErr(err)
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@ func main() {
|
|||||||
b1cb74001c00000000000d005100bd00789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13
|
b1cb74001c00000000000d005100bd00789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13
|
||||||
b1cb74000c02000000003e05ac00ac000102020000303030303031601294121a1c2d4eadabcf0ed412aae5fc01afb0024561124fbcc08301afa47900b2e3174100bf68871a4201b741b6144302bb09af334403972e96354504ac09b619560e00000000f8ff601201363030303038b60fba04060607429788a70efa04ada37ab2531c12974d91e7449dbc354184b6010001844bad324102b5679ea1014203a65abd8d0143048a6ba4dd01440587e101b3d2029613000000000000b60f
|
b1cb74000c02000000003e05ac00ac000102020000303030303031601294121a1c2d4eadabcf0ed412aae5fc01afb0024561124fbcc08301afa47900b2e3174100bf68871a4201b741b6144302bb09af334403972e96354504ac09b619560e00000000f8ff601201363030303038b60fba04060607429788a70efa04ada37ab2531c12974d91e7449dbc354184b6010001844bad324102b5679ea1014203a65abd8d0143048a6ba4dd01440587e101b3d2029613000000000000b60f
|
||||||
*/
|
*/
|
||||||
resp, err := c.GetQuote("sz000001", "sh600008")
|
resp, err := c.GetQuote("000001", "600000", "159558", "010504")
|
||||||
logs.PanicErr(err)
|
logs.PanicErr(err)
|
||||||
|
|
||||||
for _, v := range resp {
|
for _, v := range resp {
|
||||||
|
|||||||
24
example/PullKline/main.go
Normal file
24
example/PullKline/main.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/injoyai/logs"
|
||||||
|
"github.com/injoyai/tdx"
|
||||||
|
"github.com/injoyai/tdx/extend"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
m, err := tdx.NewManage(nil)
|
||||||
|
logs.PanicErr(err)
|
||||||
|
|
||||||
|
err = extend.NewPullKline(
|
||||||
|
[]string{"sz000001"},
|
||||||
|
[]string{extend.Year},
|
||||||
|
filepath.Join(tdx.DefaultDatabaseDir, "kline"),
|
||||||
|
1,
|
||||||
|
).Run(context.Background(), m)
|
||||||
|
logs.PanicErr(err)
|
||||||
|
|
||||||
|
}
|
||||||
268
extend/pull-kline.go
Normal file
268
extend/pull-kline.go
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
package extend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
_ "github.com/glebarez/go-sqlite"
|
||||||
|
"github.com/injoyai/base/chans"
|
||||||
|
"github.com/injoyai/logs"
|
||||||
|
"github.com/injoyai/tdx"
|
||||||
|
"github.com/injoyai/tdx/protocol"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"xorm.io/core"
|
||||||
|
"xorm.io/xorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Minute = "minute"
|
||||||
|
Minute5 = "5minute"
|
||||||
|
Minute15 = "15minute"
|
||||||
|
Minute30 = "30minute"
|
||||||
|
Hour = "hour"
|
||||||
|
Day = "day"
|
||||||
|
Week = "week"
|
||||||
|
Month = "month"
|
||||||
|
Quarter = "quarter"
|
||||||
|
Year = "year"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
AllKlineType = []string{Minute, Minute5, Minute15, Minute30, Hour, Day, Week, Month, Quarter, Year}
|
||||||
|
KlineTableMap = map[string]*KlineTable{
|
||||||
|
Minute: NewKlineTable("MinuteKline", func(c *tdx.Client) KlineHandler { return c.GetKlineMinuteUntil }),
|
||||||
|
Minute5: NewKlineTable("Minute5Kline", func(c *tdx.Client) KlineHandler { return c.GetKline5MinuteUntil }),
|
||||||
|
Minute15: NewKlineTable("Minute15Kline", func(c *tdx.Client) KlineHandler { return c.GetKline15MinuteUntil }),
|
||||||
|
Minute30: NewKlineTable("Minute30Kline", func(c *tdx.Client) KlineHandler { return c.GetKline30MinuteUntil }),
|
||||||
|
Hour: NewKlineTable("HourKline", func(c *tdx.Client) KlineHandler { return c.GetKlineHourUntil }),
|
||||||
|
Day: NewKlineTable("DayKline", func(c *tdx.Client) KlineHandler { return c.GetKlineDayUntil }),
|
||||||
|
Week: NewKlineTable("WeekKline", func(c *tdx.Client) KlineHandler { return c.GetKlineWeekUntil }),
|
||||||
|
Month: NewKlineTable("MonthKline", func(c *tdx.Client) KlineHandler { return c.GetKlineMonthUntil }),
|
||||||
|
Quarter: NewKlineTable("QuarterKline", func(c *tdx.Client) KlineHandler { return c.GetKlineQuarterUntil }),
|
||||||
|
Year: NewKlineTable("YearKline", func(c *tdx.Client) KlineHandler { return c.GetKlineYearUntil }),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewPullKline(codes, tables []string, dir string, limit int) *PullKline {
|
||||||
|
_tables := []*KlineTable(nil)
|
||||||
|
for _, v := range tables {
|
||||||
|
_tables = append(_tables, KlineTableMap[v])
|
||||||
|
}
|
||||||
|
return &PullKline{
|
||||||
|
tables: _tables,
|
||||||
|
dir: dir,
|
||||||
|
Codes: codes,
|
||||||
|
limit: limit,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type PullKline struct {
|
||||||
|
tables []*KlineTable
|
||||||
|
dir string //数据目录
|
||||||
|
Codes []string //指定的代码
|
||||||
|
limit int //并发数量
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *PullKline) Name() string {
|
||||||
|
return "拉取k线数据"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
|
||||||
|
limit := chans.NewWaitLimit(uint(this.limit))
|
||||||
|
|
||||||
|
//1. 获取所有股票代码
|
||||||
|
codes := this.Codes
|
||||||
|
if len(codes) == 0 {
|
||||||
|
codes = m.Codes.GetStocks()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range codes {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
limit.Add()
|
||||||
|
go func(code string) {
|
||||||
|
defer limit.Done()
|
||||||
|
|
||||||
|
//连接数据库
|
||||||
|
db, err := xorm.NewEngine("sqlite", filepath.Join(this.dir, code+".db"))
|
||||||
|
if err != nil {
|
||||||
|
logs.Err(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db.SetMapper(core.SameMapper{})
|
||||||
|
db.DB().SetMaxOpenConns(1)
|
||||||
|
|
||||||
|
for _, table := range this.tables {
|
||||||
|
if table == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Sync2(table)
|
||||||
|
|
||||||
|
//2. 获取最后一条数据
|
||||||
|
last := new(Kline)
|
||||||
|
if _, err = db.Table(table).Desc("Date").Get(last); err != nil {
|
||||||
|
logs.Err(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//3. 从服务器获取数据
|
||||||
|
insert := Klines{}
|
||||||
|
err = m.Do(func(c *tdx.Client) error {
|
||||||
|
insert, err = this.pull(code, last.Date, table.Handler(c))
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logs.Err(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//4. 插入数据库
|
||||||
|
err = tdx.NewSessionFunc(db, func(session *xorm.Session) error {
|
||||||
|
for i, v := range insert {
|
||||||
|
if i == 0 {
|
||||||
|
if _, err := session.Table(table).Where("Date >= ?", v.Date).Delete(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := session.Table(table).Insert(v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
logs.PrintErr(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}(v)
|
||||||
|
}
|
||||||
|
limit.Wait()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *PullKline) pull(code string, lastDate int64, f func(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error)) (Klines, error) {
|
||||||
|
|
||||||
|
if lastDate == 0 {
|
||||||
|
lastDate = protocol.ExchangeEstablish.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := f(code, func(k *protocol.Kline) bool {
|
||||||
|
return k.Time.Unix() <= lastDate
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ks := Klines{}
|
||||||
|
for _, v := range resp.List {
|
||||||
|
ks = append(ks, &Kline{
|
||||||
|
Code: code,
|
||||||
|
Date: v.Time.Unix(),
|
||||||
|
Open: v.Open,
|
||||||
|
High: v.High,
|
||||||
|
Low: v.Low,
|
||||||
|
Close: v.Close,
|
||||||
|
Volume: v.Volume,
|
||||||
|
Amount: v.Amount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return ks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Kline struct {
|
||||||
|
Code string `json:"code" xorm:"-"` //代码
|
||||||
|
Date int64 `json:"date"` //时间节点 2006-01-02 15:00
|
||||||
|
Open protocol.Price `json:"open"` //开盘价
|
||||||
|
High protocol.Price `json:"high"` //最高价
|
||||||
|
Low protocol.Price `json:"low"` //最低价
|
||||||
|
Close protocol.Price `json:"close"` //收盘价
|
||||||
|
Volume int64 `json:"volume"` //成交量
|
||||||
|
Amount protocol.Price `json:"amount"` //成交额
|
||||||
|
InDate int64 `json:"inDate" xorm:"created"` //创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
type Klines []*Kline
|
||||||
|
|
||||||
|
func (this Klines) Less(i, j int) bool { return this[i].Code > this[j].Code }
|
||||||
|
|
||||||
|
func (this Klines) Swap(i, j int) { this[i], this[j] = this[j], this[i] }
|
||||||
|
|
||||||
|
func (this Klines) Len() int { return len(this) }
|
||||||
|
|
||||||
|
func (this Klines) Sort() { sort.Sort(this) }
|
||||||
|
|
||||||
|
// Kline 计算多个K线,成一个K线
|
||||||
|
func (this Klines) Kline() *Kline {
|
||||||
|
if this == nil {
|
||||||
|
return new(Kline)
|
||||||
|
}
|
||||||
|
k := new(Kline)
|
||||||
|
for i, v := range this {
|
||||||
|
switch i {
|
||||||
|
case 0:
|
||||||
|
k.Open = v.Open
|
||||||
|
k.High = v.High
|
||||||
|
k.Low = v.Low
|
||||||
|
k.Close = v.Close
|
||||||
|
case len(this) - 1:
|
||||||
|
k.Close = v.Close
|
||||||
|
k.Date = v.Date
|
||||||
|
}
|
||||||
|
if v.High > k.High {
|
||||||
|
k.High = v.High
|
||||||
|
}
|
||||||
|
if v.Low < k.Low {
|
||||||
|
k.Low = v.Low
|
||||||
|
}
|
||||||
|
k.Volume += v.Volume
|
||||||
|
k.Amount += v.Amount
|
||||||
|
}
|
||||||
|
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge 合并K线
|
||||||
|
func (this Klines) Merge(n int) Klines {
|
||||||
|
if this == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ks := []*Kline(nil)
|
||||||
|
for i := 0; i < len(this); i += n {
|
||||||
|
if i+n > len(this) {
|
||||||
|
ks = append(ks, this[i:].Kline())
|
||||||
|
} else {
|
||||||
|
ks = append(ks, this[i:i+n].Kline())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ks
|
||||||
|
}
|
||||||
|
|
||||||
|
type KlineHandler func(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error)
|
||||||
|
|
||||||
|
func NewKlineTable(tableName string, handler func(c *tdx.Client) KlineHandler) *KlineTable {
|
||||||
|
return &KlineTable{
|
||||||
|
tableName: tableName,
|
||||||
|
Handler: handler,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type KlineTable struct {
|
||||||
|
Kline `xorm:"extends"`
|
||||||
|
tableName string
|
||||||
|
Handler func(c *tdx.Client) KlineHandler `xorm:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *KlineTable) TableName() string {
|
||||||
|
return this.tableName
|
||||||
|
}
|
||||||
30
manage.go
30
manage.go
@@ -7,6 +7,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultDatabaseDir = "./data/database"
|
||||||
|
)
|
||||||
|
|
||||||
func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
||||||
//初始化配置
|
//初始化配置
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
@@ -16,22 +20,15 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
|||||||
cfg.Hosts = Hosts
|
cfg.Hosts = Hosts
|
||||||
}
|
}
|
||||||
if cfg.CodesDir == "" {
|
if cfg.CodesDir == "" {
|
||||||
cfg.CodesDir = "./data/database"
|
cfg.CodesDir = DefaultDatabaseDir
|
||||||
}
|
}
|
||||||
if cfg.WorkdayDir == "" {
|
if cfg.WorkdayDir == "" {
|
||||||
cfg.WorkdayDir = "./data/database"
|
cfg.WorkdayDir = DefaultDatabaseDir
|
||||||
}
|
|
||||||
|
|
||||||
//连接池
|
|
||||||
p, err := NewPool(func() (*Client, error) {
|
|
||||||
return DialHosts(cfg.Hosts, op...)
|
|
||||||
}, cfg.Number)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//代码
|
//代码
|
||||||
codesClient, err := DialHosts(cfg.Hosts, op...)
|
DefaultCodes = &Codes{}
|
||||||
|
codesClient, err := DialHostsRange(cfg.Hosts, op...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -40,9 +37,18 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
DefaultCodes = codes
|
||||||
|
|
||||||
|
//连接池
|
||||||
|
p, err := NewPool(func() (*Client, error) {
|
||||||
|
return DialHostsRange(cfg.Hosts, op...)
|
||||||
|
}, cfg.Number)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
//工作日
|
//工作日
|
||||||
workdayClient, err := DialHosts(cfg.Hosts, op...)
|
workdayClient, err := DialHostsRange(cfg.Hosts, op...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
1
pool.go
1
pool.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/injoyai/base/safe"
|
"github.com/injoyai/base/safe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewPool 简易版本的连接池
|
||||||
func NewPool(dial func() (*Client, error), number int) (*Pool, error) {
|
func NewPool(dial func() (*Client, error), number int) (*Pool, error) {
|
||||||
if number <= 0 {
|
if number <= 0 {
|
||||||
number = 1
|
number = 1
|
||||||
|
|||||||
@@ -77,7 +77,10 @@ func (this *Kline) RisePrice() Price {
|
|||||||
|
|
||||||
// RiseRate 涨跌比例/涨跌幅,第一个数据不准,仅做参考
|
// RiseRate 涨跌比例/涨跌幅,第一个数据不准,仅做参考
|
||||||
func (this *Kline) RiseRate() float64 {
|
func (this *Kline) RiseRate() float64 {
|
||||||
return float64(this.RisePrice()) / float64(this.Open) * 100
|
if this.Last == 0 {
|
||||||
|
return float64(this.Close-this.Open) / float64(this.Open) * 100
|
||||||
|
}
|
||||||
|
return float64(this.Close-this.Last) / float64(this.Last) * 100
|
||||||
}
|
}
|
||||||
|
|
||||||
type kline struct{}
|
type kline struct{}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (this *Workday) Update() error {
|
|||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
if lastWorkday == nil || lastWorkday.Unix < IntegerDay(now).Unix() {
|
if lastWorkday == nil || lastWorkday.Unix < IntegerDay(now).Unix() {
|
||||||
resp, err := this.Client.GetKlineDayAll("sh000001")
|
resp, err := this.Client.GetIndexDayAll("sh000001")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Err(err)
|
logs.Err(err)
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user