mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
临时修改,关于盘口价格小数位不对的问题
This commit is contained in:
42
client.go
42
client.go
@@ -1,6 +1,7 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/injoyai/base/maps"
|
||||
"github.com/injoyai/base/maps/wait/v2"
|
||||
@@ -85,7 +86,17 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
|
||||
|
||||
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 {
|
||||
@@ -215,6 +226,9 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
|
||||
|
||||
// GetQuote 获取盘口五档报价
|
||||
func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
||||
if DefaultCodes == nil {
|
||||
return nil, errors.New("DefaultCodes未初始化")
|
||||
}
|
||||
f, err := protocol.MQuote.Frame(codes...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -223,7 +237,31 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(protocol.QuotesResp), nil
|
||||
quotes := result.(protocol.QuotesResp)
|
||||
|
||||
{ //临时处理下先,后续优化
|
||||
//判断长度和预期是否一致
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return quotes, nil
|
||||
}
|
||||
|
||||
// GetMinute 获取分时数据,todo 解析好像不对,先用历史数据
|
||||
|
||||
53
codes.go
53
codes.go
@@ -4,13 +4,21 @@ import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
"xorm.io/core"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// 增加单例,部分数据需要通过Codes里面的信息计算
|
||||
var (
|
||||
DefaultCodes *Codes
|
||||
codesOnce sync.Once
|
||||
)
|
||||
|
||||
func NewCodes(c *Client, filename string) (*Codes, error) {
|
||||
|
||||
//如果文件夹不存在就创建
|
||||
@@ -27,6 +35,21 @@ func NewCodes(c *Client, filename string) (*Codes, error) {
|
||||
if err := db.Sync2(new(CodeModel)); err != nil {
|
||||
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{
|
||||
Client: c,
|
||||
@@ -47,7 +70,10 @@ func NewCodes(c *Client, filename string) (*Codes, error) {
|
||||
})
|
||||
task.Start()
|
||||
|
||||
return cc, cc.Update()
|
||||
//判断是否更新过,更新过则不更新
|
||||
//time.Unix(update.Time,0).A
|
||||
|
||||
return cc, cc.Update(true)
|
||||
}
|
||||
|
||||
type Codes struct {
|
||||
@@ -75,8 +101,15 @@ func (this *Codes) GetStocks() []string {
|
||||
return ls
|
||||
}
|
||||
|
||||
func (this *Codes) Update() error {
|
||||
codes, err := this.Code(false)
|
||||
func (this *Codes) Get(code string) *CodeModel {
|
||||
if v, ok := this.Codes[code]; ok {
|
||||
return v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Codes) Update(byCache ...bool) error {
|
||||
codes, err := this.Code(len(byCache) > 0 && byCache[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -165,6 +198,14 @@ func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) {
|
||||
|
||||
}
|
||||
|
||||
type UpdateModel struct {
|
||||
Time int64 //更新时间
|
||||
}
|
||||
|
||||
func (*UpdateModel) TableName() string {
|
||||
return "update"
|
||||
}
|
||||
|
||||
type CodeModel struct {
|
||||
ID int64 `json:"id"` //主键
|
||||
Name string `json:"name"` //名称,有时候名称会变,例STxxx
|
||||
@@ -177,10 +218,14 @@ type CodeModel struct {
|
||||
InDate int64 `json:"inDate" xorm:"created"` //创建时间
|
||||
}
|
||||
|
||||
func (c *CodeModel) TableName() string {
|
||||
func (*CodeModel) TableName() string {
|
||||
return "codes"
|
||||
}
|
||||
|
||||
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 {
|
||||
session := db.NewSession()
|
||||
defer session.Close()
|
||||
|
||||
Reference in New Issue
Block a user