Compare commits

...

7 Commits

Author SHA1 Message Date
injoyai
e76043dc29 可以自定义连接方式 2025-03-24 16:33:52 +08:00
injoyai
ff7fc6aba0 增加RangDial等待时间 2025-03-24 16:29:03 +08:00
injoyai
0c27cc4276 默认开启日志,日志等级为Info 2025-03-24 16:28:30 +08:00
injoyai
80d6d6dfc5 优化 2025-03-20 13:37:34 +08:00
injoyai
a27461740a Merge remote-tracking branch 'origin/master' 2025-03-20 13:19:05 +08:00
injoyai
7a9d59f8f1 优化 2025-03-20 13:18:49 +08:00
钱纯净
cba35308f6 优化GetQuote方法 2025-03-19 22:47:46 +08:00
7 changed files with 66 additions and 18 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/injoyai/conv"
"github.com/injoyai/ios"
"github.com/injoyai/ios/client"
"github.com/injoyai/ios/module/common"
"github.com/injoyai/logs"
"github.com/injoyai/tdx/protocol"
"runtime/debug"
@@ -15,6 +16,16 @@ import (
"time"
)
const (
LevelNone = common.LevelNone
LevelDebug = common.LevelDebug
LevelWrite = common.LevelWrite
LevelRead = common.LevelRead
LevelInfo = common.LevelInfo
LevelError = common.LevelError
LevelAll = common.LevelAll
)
// WithDebug 是否打印通讯数据
func WithDebug(b ...bool) client.Option {
return func(c *client.Client) {
@@ -22,6 +33,12 @@ func WithDebug(b ...bool) client.Option {
}
}
func WithLevel(level int) client.Option {
return func(c *client.Client) {
c.Logger.SetLevel(level)
}
}
// WithRedial 断线重连
func WithRedial(b ...bool) client.Option {
return func(c *client.Client) {
@@ -31,6 +48,7 @@ func WithRedial(b ...bool) client.Option {
// DialDefault 默认连接方式
func DialDefault(op ...client.Option) (cli *Client, err error) {
op = append([]client.Option{WithRedial()}, op...)
return DialHostsRange(Hosts, op...)
}
@@ -63,7 +81,8 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
}
cli.Client, err = client.Dial(dial, func(c *client.Client) {
c.Logger.Debug(false) //关闭日志打印
c.Logger.Debug(true) //关闭日志打印
c.Logger.SetLevel(LevelInfo) //设置日志级别
c.Logger.WithHEX() //以HEX显示
c.SetOption(op...) //自定义选项
c.Event.OnReadFrom = protocol.ReadFrom //分包
@@ -229,6 +248,9 @@ 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...)
if err != nil {
return nil, err

View File

@@ -97,7 +97,7 @@ type Codes struct {
db *xorm.Engine //数据库实例
Map map[string]*CodeModel //股票缓存
list []*CodeModel //列表方式缓存
exchanges map[string]string //交易所缓存
exchanges map[string][]string //交易所缓存
}
// GetName 获取股票名称
@@ -140,7 +140,11 @@ func (this *Codes) GetExchange(code string) protocol.Exchange {
return protocol.ExchangeSZ
}
}
exchange := this.exchanges[code]
var exchange string
exchanges := this.exchanges[code]
if len(exchanges) >= 1 {
exchange = exchanges[0]
}
if len(code) == 8 {
exchange = code[0:2]
}
@@ -154,6 +158,24 @@ func (this *Codes) GetExchange(code string) protocol.Exchange {
}
}
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])
@@ -161,10 +183,10 @@ func (this *Codes) Update(byDB ...bool) error {
return err
}
codeMap := make(map[string]*CodeModel)
exchanges := make(map[string]string)
exchanges := make(map[string][]string)
for _, code := range codes {
codeMap[code.Exchange+code.Code] = code
exchanges[code.Code] = code.Exchange
exchanges[code.Code] = append(exchanges[code.Code], code.Exchange)
}
this.Map = codeMap
this.list = codes

View File

@@ -74,6 +74,7 @@ func NewRangeDial(hosts []string) ios.DialFunc {
if i < len(hosts)-1 {
//最后一个错误返回出去
logs.Err(err)
<-time.After(time.Second * 3)
}
}
return

View File

@@ -21,7 +21,7 @@ func main() {
b1cb74001c00000000000d005100bd00789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13
b1cb74000c02000000003e05ac00ac000102020000303030303031601294121a1c2d4eadabcf0ed412aae5fc01afb0024561124fbcc08301afa47900b2e3174100bf68871a4201b741b6144302bb09af334403972e96354504ac09b619560e00000000f8ff601201363030303038b60fba04060607429788a70efa04ada37ab2531c12974d91e7449dbc354184b6010001844bad324102b5679ea1014203a65abd8d0143048a6ba4dd01440587e101b3d2029613000000000000b60f
*/
resp, err := c.GetQuote("sz000001", "sh600000", "sz159558")
resp, err := c.GetQuote("000001", "600000", "159558", "010504")
logs.PanicErr(err)
for _, v := range resp {

View File

@@ -16,19 +16,19 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
if cfg == nil {
cfg = &ManageConfig{}
}
if len(cfg.Hosts) == 0 {
cfg.Hosts = Hosts
}
if cfg.CodesDir == "" {
cfg.CodesDir = DefaultDatabaseDir
}
if cfg.WorkdayDir == "" {
cfg.WorkdayDir = DefaultDatabaseDir
}
if cfg.Dial == nil {
cfg.Dial = DialDefault
}
//代码
DefaultCodes = &Codes{}
codesClient, err := DialHostsRange(cfg.Hosts, op...)
codesClient, err := cfg.Dial(op...)
if err != nil {
return nil, err
}
@@ -41,14 +41,14 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
//连接池
p, err := NewPool(func() (*Client, error) {
return DialHostsRange(cfg.Hosts, op...)
return cfg.Dial(op...)
}, cfg.Number)
if err != nil {
return nil, err
}
//工作日
workdayClient, err := DialHostsRange(cfg.Hosts, op...)
workdayClient, err := cfg.Dial(op...)
if err != nil {
return nil, err
}
@@ -85,8 +85,8 @@ func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) {
}
type ManageConfig struct {
Hosts []string //服务端IP
Number int //客户端数量
CodesDir string //代码数据库位置
WorkdayDir string //工作日数据库位置
Number int //客户端数量
CodesDir string //代码数据库位置
WorkdayDir string //工作日数据库位置
Dial func(op ...client.Option) (cli *Client, err error) //默认连接方式
}

View File

@@ -77,7 +77,10 @@ func (this *Kline) RisePrice() Price {
// RiseRate 涨跌比例/涨跌幅,第一个数据不准,仅做参考
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{}

View File

@@ -77,7 +77,7 @@ func (this *Workday) Update() error {
now := time.Now()
if lastWorkday == nil || lastWorkday.Unix < IntegerDay(now).Unix() {
resp, err := this.Client.GetKlineDayAll("sh000001")
resp, err := this.Client.GetIndexDayAll("sh000001")
if err != nil {
logs.Err(err)
return err