mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
Compare commits
17 Commits
v0.0.37
...
530de4fa5a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
530de4fa5a | ||
|
|
ffe0cb2c92 | ||
|
|
4d1487f0b6 | ||
|
|
bd3863d3c5 | ||
|
|
5fb1a212c6 | ||
|
|
f381d72ec4 | ||
|
|
5fa572f298 | ||
|
|
61b2e737b3 | ||
|
|
5654065954 | ||
|
|
d7dd7fe0bf | ||
|
|
9fb1a3b651 | ||
|
|
f8a24c0cf1 | ||
|
|
ad0abbe6ba | ||
|
|
0b35006323 | ||
|
|
3b823e2e54 | ||
|
|
5c8091ac26 | ||
|
|
d7b6963bd6 |
34
client.go
34
client.go
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/injoyai/ios/module/common"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/internal/bse"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"runtime/debug"
|
||||
"sync/atomic"
|
||||
@@ -181,6 +182,11 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
|
||||
|
||||
}
|
||||
|
||||
// SetTimeout 设置超时时间
|
||||
func (this *Client) SetTimeout(t time.Duration) {
|
||||
this.Wait.SetTimeout(t)
|
||||
}
|
||||
|
||||
// SendFrame 发送数据,并等待响应
|
||||
func (this *Client) SendFrame(f *protocol.Frame, cache ...any) (any, error) {
|
||||
f.MsgID = atomic.AddUint32(&this.msgID, 1)
|
||||
@@ -225,7 +231,7 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
|
||||
//不放在extend包时防止循环引用
|
||||
//todo 这是临时方案,等通达信有北交所代码列表时再改
|
||||
if exchange == protocol.ExchangeBJ {
|
||||
codes, err := GetBjCodes()
|
||||
codes, err := bse.GetCodes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -299,7 +305,8 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
||||
return nil, errors.New("DefaultCodes未初始化")
|
||||
}
|
||||
//不是股票代码的话,根据codes的信息加上前缀
|
||||
codes[i] = DefaultCodes.AddExchange(codes[i])
|
||||
//codes[i] = DefaultCodes.AddExchange(codes[i])
|
||||
codes[i] = protocol.AddPrefix(codes[i])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -440,7 +447,12 @@ func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16
|
||||
}
|
||||
|
||||
// GetHistoryTradeFull 获取上市至今的分时成交
|
||||
func (this *Client) GetHistoryTradeFull(code string) (protocol.Trades, error) {
|
||||
func (this *Client) GetHistoryTradeFull(code string, w *Workday) (protocol.Trades, error) {
|
||||
return this.GetHistoryTradeBefore(code, w, time.Now())
|
||||
}
|
||||
|
||||
// GetHistoryTradeBefore 获取上市至今的分时成交
|
||||
func (this *Client) GetHistoryTradeBefore(code string, w *Workday, before time.Time) (protocol.Trades, error) {
|
||||
ls := protocol.Trades(nil)
|
||||
resp, err := this.GetKlineMonthAll(code)
|
||||
if err != nil {
|
||||
@@ -451,14 +463,20 @@ func (this *Client) GetHistoryTradeFull(code string) (protocol.Trades, error) {
|
||||
}
|
||||
start := time.Date(resp.List[0].Time.Year(), resp.List[0].Time.Month(), 1, 0, 0, 0, 0, resp.List[0].Time.Location())
|
||||
var res *protocol.TradeResp
|
||||
for ; start.Before(time.Now()); start = start.Add(time.Hour * 24) {
|
||||
res, err = this.GetHistoryTradeDay(start.Format("20060102"), code)
|
||||
w.Range(start, before, func(t time.Time) bool {
|
||||
for i := 0; i < 3; i++ {
|
||||
res, err = this.GetHistoryTradeDay(t.Format("20060102"), code)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return false
|
||||
}
|
||||
ls = append(ls, res.List...)
|
||||
}
|
||||
return ls, nil
|
||||
return true
|
||||
})
|
||||
return ls, err
|
||||
}
|
||||
|
||||
// GetHistoryTradeDay 获取历史某天分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
|
||||
|
||||
35
codes.go
35
codes.go
@@ -15,6 +15,14 @@ import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type ICodes interface {
|
||||
Get(code string) *CodeModel
|
||||
GetName(code string) string
|
||||
GetStocks(limit ...int) []string
|
||||
GetETFs(limit ...int) []string
|
||||
Update() error
|
||||
}
|
||||
|
||||
// DefaultCodes 增加单例,部分数据需要通过Codes里面的信息计算
|
||||
var DefaultCodes *Codes
|
||||
|
||||
@@ -312,28 +320,35 @@ func (*UpdateModel) TableName() string {
|
||||
}
|
||||
|
||||
type CodeModel struct {
|
||||
ID int64 `json:"id"` //主键
|
||||
Name string `json:"name"` //名称,有时候名称会变,例STxxx
|
||||
Code string `json:"code" xorm:"index"` //代码
|
||||
Exchange string `json:"exchange" xorm:"index"` //交易所
|
||||
Multiple uint16 `json:"multiple"` //倍数
|
||||
Decimal int8 `json:"decimal"` //小数位
|
||||
LastPrice float64 `json:"lastPrice"` //昨收价格
|
||||
EditDate int64 `json:"editDate" xorm:"updated"` //修改时间
|
||||
InDate int64 `json:"inDate" xorm:"created"` //创建时间
|
||||
ID int64 `json:"id"` //主键
|
||||
Name string `json:"name"` //名称,有时候名称会变,例STxxx
|
||||
Code string `json:"code" xorm:"index"` //代码
|
||||
Exchange string `json:"exchange" xorm:"index"` //交易所
|
||||
Multiple uint16 `json:"multiple"` //倍数
|
||||
Decimal int8 `json:"decimal"` //小数位
|
||||
LastPrice float64 `json:"lastPrice"` //昨收价格
|
||||
FloatStock float64 `json:"floatStock"` //流通股
|
||||
TotalStock float64 `json:"totalStock"` //总股本
|
||||
EditDate int64 `json:"editDate" xorm:"updated"` //修改时间
|
||||
InDate int64 `json:"inDate" xorm:"created"` //创建时间
|
||||
}
|
||||
|
||||
func (*CodeModel) TableName() string {
|
||||
return "codes"
|
||||
}
|
||||
|
||||
// FullCode 获取完整代码 sz000001
|
||||
func (this *CodeModel) FullCode() string {
|
||||
return this.Exchange + this.Code
|
||||
}
|
||||
|
||||
// Turnover 换手率
|
||||
func (this *CodeModel) Turnover(volume float64) float64 {
|
||||
return volume / (this.FloatStock * 100)
|
||||
}
|
||||
|
||||
func (this *CodeModel) Price(p protocol.Price) protocol.Price {
|
||||
return protocol.Price(float64(p) * math.Pow10(int(2-this.Decimal)))
|
||||
//return p * protocol.Price(math.Pow10(int(2-this.Decimal)))
|
||||
}
|
||||
|
||||
func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {
|
||||
|
||||
284
codes_v2.go
Normal file
284
codes_v2.go
Normal file
@@ -0,0 +1,284 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/injoyai/base/maps"
|
||||
"github.com/injoyai/base/types"
|
||||
"github.com/injoyai/conv"
|
||||
"github.com/injoyai/ios"
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/internal/xorms"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type Codes2Option func(*Codes2)
|
||||
|
||||
func WithFilename(filename string) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.filename = filename
|
||||
}
|
||||
}
|
||||
|
||||
func WithSpec(spec string) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.spec = spec
|
||||
}
|
||||
}
|
||||
|
||||
func WithKey(key string) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.key = key
|
||||
}
|
||||
}
|
||||
|
||||
func WithRetry(retry int) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.retry = retry
|
||||
}
|
||||
}
|
||||
|
||||
func NewCodes2(op ...Codes2Option) (*Codes2, error) {
|
||||
cs := &Codes2{
|
||||
filename: filepath.Join(DefaultDatabaseDir, "codes.db"),
|
||||
spec: "10 0 9 * * *",
|
||||
key: "codes",
|
||||
retry: 3,
|
||||
dial: NewRangeDial(Hosts),
|
||||
dialOption: nil,
|
||||
}
|
||||
|
||||
for _, o := range op {
|
||||
o(cs)
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
// 初始化连接
|
||||
cs.c, err = DialWith(cs.dial, cs.dialOption...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 初始化数据库
|
||||
cs.db, err = xorms.NewSqlite(cs.filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = cs.db.Sync2(new(CodeModel), new(UpdateModel)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 立即更新
|
||||
err = cs.Update()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 定时更新
|
||||
cr := cron.New(cron.WithSeconds())
|
||||
_, err = cr.AddFunc(cs.spec, func() {
|
||||
for i := 0; i < 3; i++ {
|
||||
if err := cs.Update(); err != nil {
|
||||
logs.Err(err)
|
||||
<-time.After(time.Minute * 5)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cr.Start()
|
||||
|
||||
return cs, nil
|
||||
}
|
||||
|
||||
var _ ICodes = &Codes2{}
|
||||
|
||||
type Codes2 struct {
|
||||
filename string //数据库文件
|
||||
spec string //定时规则
|
||||
key string //标识
|
||||
retry int //重试次数
|
||||
dial ios.DialFunc //连接
|
||||
dialOption []client.Option //
|
||||
|
||||
/*
|
||||
内部字段
|
||||
*/
|
||||
|
||||
c *Client //
|
||||
db *xorms.Engine //
|
||||
stocks types.List[string] //缓存
|
||||
etfs types.List[string] //缓存
|
||||
m *maps.Generic[string, *CodeModel] //缓存
|
||||
}
|
||||
|
||||
func (this *Codes2) Get(code string) *CodeModel {
|
||||
v, _ := this.m.Get(code)
|
||||
return v
|
||||
}
|
||||
|
||||
func (this *Codes2) GetName(code string) string {
|
||||
v, _ := this.m.Get(code)
|
||||
if v == nil {
|
||||
return "未知"
|
||||
}
|
||||
return v.Name
|
||||
}
|
||||
|
||||
func (this *Codes2) GetStocks(limit ...int) []string {
|
||||
size := conv.Default(this.stocks.Len(), limit...)
|
||||
return this.stocks.Limit(size)
|
||||
}
|
||||
|
||||
func (this *Codes2) GetETFs(limit ...int) []string {
|
||||
size := conv.Default(this.etfs.Len(), limit...)
|
||||
return this.etfs.Limit(size)
|
||||
}
|
||||
|
||||
func (this *Codes2) updated() (bool, error) {
|
||||
update := new(UpdateModel)
|
||||
{ //查询或者插入一条数据
|
||||
has, err := this.db.Where("`Key`=?", this.key).Get(update)
|
||||
if err != nil {
|
||||
return true, err
|
||||
} else if !has {
|
||||
update.Key = this.key
|
||||
if _, err = this.db.Insert(update); err != nil {
|
||||
return true, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
{ //判断是否更新过,更新过则不更新
|
||||
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 false, nil
|
||||
}
|
||||
} else {
|
||||
//当前时间在9点之前,且更新时间在上个节点之前
|
||||
if updateTime.Sub(node.Add(time.Hour*24)) < 0 {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (this *Codes2) Update() error {
|
||||
|
||||
codes, err := this.update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stocks := []string(nil)
|
||||
etfs := []string(nil)
|
||||
for _, v := range codes {
|
||||
fullCode := v.FullCode()
|
||||
switch {
|
||||
case protocol.IsStock(fullCode):
|
||||
stocks = append(stocks, fullCode)
|
||||
case protocol.IsETF(fullCode):
|
||||
etfs = append(etfs, fullCode)
|
||||
}
|
||||
}
|
||||
|
||||
this.stocks = stocks
|
||||
this.etfs = etfs
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCodes 更新股票并返回结果
|
||||
func (this *Codes2) update() ([]*CodeModel, error) {
|
||||
|
||||
if this.c == nil {
|
||||
return nil, errors.New("client is nil")
|
||||
}
|
||||
|
||||
//2. 查询数据库所有股票
|
||||
list := []*CodeModel(nil)
|
||||
if err := this.db.Find(&list); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//如果更新过,则不更新
|
||||
updated, err := this.updated()
|
||||
if err == nil && updated {
|
||||
return list, nil
|
||||
}
|
||||
|
||||
mCode := make(map[string]*CodeModel, len(list))
|
||||
for _, v := range list {
|
||||
mCode[v.FullCode()] = v
|
||||
}
|
||||
|
||||
//3. 从服务器获取所有股票代码
|
||||
insert := []*CodeModel(nil)
|
||||
update := []*CodeModel(nil)
|
||||
for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ, protocol.ExchangeBJ} {
|
||||
resp, err := this.c.GetCodeAll(exchange)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range resp.List {
|
||||
code := &CodeModel{
|
||||
Name: v.Name,
|
||||
Code: v.Code,
|
||||
Exchange: exchange.String(),
|
||||
Multiple: v.Multiple,
|
||||
Decimal: v.Decimal,
|
||||
LastPrice: v.LastPrice,
|
||||
}
|
||||
if val, ok := mCode[exchange.String()+v.Code]; ok {
|
||||
if val.Name != v.Name {
|
||||
update = append(update, code)
|
||||
}
|
||||
delete(mCode, exchange.String()+v.Code)
|
||||
} else {
|
||||
insert = append(insert, code)
|
||||
list = append(list, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//4. 插入或者更新数据库
|
||||
err = this.db.SessionFunc(func(session *xorm.Session) error {
|
||||
for _, v := range mCode {
|
||||
if _, err = session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Delete(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, v := range insert {
|
||||
if _, err := session.Insert(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, v := range update {
|
||||
if _, err = session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name,LastPrice").Update(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//更新时间
|
||||
_, err = this.db.Where("`Key`=?", this.key).Update(&UpdateModel{Time: time.Now().Unix()})
|
||||
return list, err
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
common.Test(func(c *tdx.Client) {
|
||||
resp, err := c.GetHistoryMinuteTradeDay("20251010", "sh000001")
|
||||
resp, err := c.GetHistoryMinuteTradeDay("20170704", "sh000001")
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp.List {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
common.Test(func(c *tdx.Client) {
|
||||
resp, err := c.GetKlineDay("838971", 0, 20)
|
||||
resp, err := c.GetKlineDay("920992", 0, 20)
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp.List {
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
package extend
|
||||
|
||||
import (
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/internal/bse"
|
||||
)
|
||||
|
||||
func GetBjCodes() ([]*tdx.BjCode, error) {
|
||||
return tdx.GetBjCodes()
|
||||
func GetBjCodes() ([]string, error) {
|
||||
cs, err := bse.GetCodes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ls := []string(nil)
|
||||
for _, v := range cs {
|
||||
ls = append(ls, "bj"+v.Code)
|
||||
}
|
||||
return ls, nil
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tdx
|
||||
package bse
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -13,15 +13,15 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// UrlBjCodes 最后跟的是时间戳(ms),但是随便什么时间戳都能请求成功
|
||||
UrlBjCodes = "https://www.bse.cn/nqhqController/nqhq_en.do?callback=jQuery3710848510589806625_%d"
|
||||
// UrlCodes 最后跟的是时间戳(ms),但是随便什么时间戳都能请求成功
|
||||
UrlCodes = "https://www.bse.cn/nqhqController/nqhq_en.do?callback=jQuery3710848510589806625_%d"
|
||||
)
|
||||
|
||||
func GetBjCodes() ([]*BjCode, error) {
|
||||
list := []*BjCode(nil)
|
||||
func GetCodes() ([]*Code, error) {
|
||||
list := []*Code(nil)
|
||||
//这个200预防下bug,除非北京上市公司有4000个
|
||||
for page := 0; page < 200; page++ {
|
||||
ls, done, err := getBjCodes(page)
|
||||
ls, done, err := getCodes(page)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,9 +35,9 @@ func GetBjCodes() ([]*BjCode, error) {
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func getBjCodes(page int) (_ []*BjCode, last bool, err error) {
|
||||
func getCodes(page int) (_ []*Code, last bool, err error) {
|
||||
|
||||
url := fmt.Sprintf(UrlBjCodes, time.Now().UnixMilli())
|
||||
url := fmt.Sprintf(UrlCodes, time.Now().UnixMilli())
|
||||
|
||||
bodyStr := "page=" + conv.String(page) + "&type_en=%5B%22B%22%5D&sortfield=hqcjsl&sorttype=desc&xxfcbj_en=%5B2%5D&zqdm="
|
||||
|
||||
@@ -68,7 +68,7 @@ func getBjCodes(page int) (_ []*BjCode, last bool, err error) {
|
||||
|
||||
bs = bs[i+1 : len(bs)-1]
|
||||
|
||||
ls := []*BjCodes(nil)
|
||||
ls := []*Codes(nil)
|
||||
err = json.Unmarshal(bs, &ls)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
@@ -81,14 +81,14 @@ func getBjCodes(page int) (_ []*BjCode, last bool, err error) {
|
||||
return ls[0].Data, ls[0].LastPage, nil
|
||||
}
|
||||
|
||||
type BjCodes struct {
|
||||
Data []*BjCode `json:"content"`
|
||||
TotalNumber int `json:"totalElements"`
|
||||
TotalPage int `json:"totalPages"`
|
||||
LastPage bool `json:"lastPage"`
|
||||
type Codes struct {
|
||||
Data []*Code `json:"content"`
|
||||
TotalNumber int `json:"totalElements"`
|
||||
TotalPage int `json:"totalPages"`
|
||||
LastPage bool `json:"lastPage"`
|
||||
}
|
||||
|
||||
type BjCode struct {
|
||||
type Code struct {
|
||||
Date string `json:"hqjsrq"` //日期
|
||||
Code string `json:"hqzqdm"` //代码
|
||||
Name string `json:"hqzqjc"` //名称
|
||||
192
internal/gbbq/gbbq.go
Normal file
192
internal/gbbq/gbbq.go
Normal file
File diff suppressed because one or more lines are too long
118
internal/xorms/engine.go
Normal file
118
internal/xorms/engine.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package xorms
|
||||
|
||||
import (
|
||||
_ "github.com/glebarez/go-sqlite"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"xorm.io/core"
|
||||
"xorm.io/xorm"
|
||||
"xorm.io/xorm/schemas"
|
||||
)
|
||||
|
||||
func NewMysql(dsn string, options ...Option) (*Engine, error) {
|
||||
return New("mysql", dsn, options...)
|
||||
}
|
||||
|
||||
func NewSqlite(filename string, options ...Option) (*Engine, error) {
|
||||
dir, _ := filepath.Split(filename)
|
||||
_ = os.MkdirAll(dir, 0777)
|
||||
//sqlite是文件数据库,只能打开一次(即一个连接)
|
||||
options = append(options, WithMaxOpenConns(1))
|
||||
return New("sqlite", filename, options...)
|
||||
}
|
||||
|
||||
/*
|
||||
New 需要手动引用驱动
|
||||
mysql _ "github.com/go-sql-driver/mysql"
|
||||
sqlite _ "github.com/glebarez/go-sqlite"
|
||||
sqlserver _ "github.com/denisenkom/go-mssqldb"
|
||||
*/
|
||||
func New(Type, dsn string, options ...Option) (*Engine, error) {
|
||||
db, err := xorm.NewEngine(Type, dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//默认同步字段
|
||||
WithSyncField(true)(db)
|
||||
for _, v := range options {
|
||||
v(db)
|
||||
}
|
||||
return &Engine{Engine: db}, nil
|
||||
}
|
||||
|
||||
type Engine struct {
|
||||
*xorm.Engine
|
||||
}
|
||||
|
||||
func (this *Engine) TableName(v any) string {
|
||||
return this.Engine.TableName(v)
|
||||
}
|
||||
|
||||
func (this *Engine) Tables() []*schemas.Table {
|
||||
list, _ := this.DBMetas()
|
||||
return list
|
||||
}
|
||||
|
||||
// SetTablePrefix 前缀
|
||||
func (this *Engine) SetTablePrefix(s string) *Engine {
|
||||
this.SetTableMapper(core.NewPrefixMapper(core.SameMapper{}, s))
|
||||
return this
|
||||
}
|
||||
|
||||
// SetSyncField 字段同步
|
||||
func (this *Engine) SetSyncField() *Engine {
|
||||
this.SetMapper(core.SameMapper{})
|
||||
return this
|
||||
}
|
||||
|
||||
// SetConnMaxLifetime 设置连接超时时间(超时会断开连接)
|
||||
func (this *Engine) SetConnMaxLifetime(d time.Duration) *Engine {
|
||||
this.DB().SetConnMaxLifetime(d)
|
||||
return this
|
||||
}
|
||||
|
||||
// SetMaxIdleConns 设置空闲数(一直连接不断开)
|
||||
func (this *Engine) SetMaxIdleConns(n int) *Engine {
|
||||
this.DB().SetMaxIdleConns(n)
|
||||
return this
|
||||
}
|
||||
|
||||
// SetMaxOpenConns 设置连接数(超出最大数量会等待)
|
||||
func (this *Engine) SetMaxOpenConns(n int) *Engine {
|
||||
this.DB().SetMaxOpenConns(n)
|
||||
return this
|
||||
}
|
||||
|
||||
// NewSession 新建自动关闭事务
|
||||
func (this *Engine) NewSession() *Session {
|
||||
return newSession(this.Engine.Where(""))
|
||||
}
|
||||
|
||||
func (this *Engine) SessionFunc(fn func(session *xorm.Session) error) error {
|
||||
return NewSessionFunc(this.Engine, fn)
|
||||
}
|
||||
|
||||
func (this *Engine) Like(param, arg string) *Session {
|
||||
return newSession(this.Engine.Where(param+" like ?", "%"+arg+"%"))
|
||||
}
|
||||
|
||||
func (this *Engine) Desc(colNames ...string) *Session {
|
||||
return newSession(this.Engine.Desc(colNames...))
|
||||
}
|
||||
|
||||
func (this *Engine) Asc(colNames ...string) *Session {
|
||||
return newSession(this.Engine.Asc(colNames...))
|
||||
}
|
||||
|
||||
func (this *Engine) Limit(limit int, start ...int) *Session {
|
||||
if limit > 0 {
|
||||
return newSession(this.Engine.Limit(limit, start...))
|
||||
}
|
||||
return newSession(this.Engine.Where(""))
|
||||
}
|
||||
|
||||
func (this *Engine) Where(query any, args ...any) *Session {
|
||||
return newSession(this.Engine.Where(query, args...))
|
||||
}
|
||||
70
internal/xorms/option.go
Normal file
70
internal/xorms/option.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package xorms
|
||||
|
||||
import (
|
||||
"github.com/injoyai/conv"
|
||||
"github.com/injoyai/conv/cfg"
|
||||
"time"
|
||||
"xorm.io/core"
|
||||
"xorm.io/xorm"
|
||||
"xorm.io/xorm/names"
|
||||
)
|
||||
|
||||
type Option func(*xorm.Engine)
|
||||
|
||||
func WithCfg(path ...string) Option {
|
||||
return WithDMap(cfg.Default.GetDMap(conv.Default[string]("database", path...)))
|
||||
}
|
||||
|
||||
func WithDMap(m *conv.Map) Option {
|
||||
return func(e *xorm.Engine) {
|
||||
if v := m.GetVar("fieldSync"); !v.IsNil() {
|
||||
WithSyncField(v.Bool())(e)
|
||||
}
|
||||
if v := m.GetVar("tablePrefix"); !v.IsNil() {
|
||||
WithTablePrefix(v.String())(e)
|
||||
}
|
||||
if v := m.GetVar("connMaxLifetime"); !v.IsNil() {
|
||||
WithConnMaxLifetime(v.Duration())(e)
|
||||
}
|
||||
if v := m.GetVar("maxIdleConns"); !v.IsNil() {
|
||||
WithMaxIdleConns(v.Int())(e)
|
||||
}
|
||||
if v := m.GetVar("maxOpenConns"); !v.IsNil() {
|
||||
WithMaxOpenConns(v.Int())(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithTablePrefix(prefix string) Option {
|
||||
return func(e *xorm.Engine) {
|
||||
e.SetTableMapper(core.NewPrefixMapper(core.SameMapper{}, prefix))
|
||||
}
|
||||
}
|
||||
|
||||
func WithSyncField(b bool) Option {
|
||||
return func(e *xorm.Engine) {
|
||||
if b {
|
||||
e.SetMapper(core.SameMapper{})
|
||||
} else {
|
||||
e.SetMapper(names.NewCacheMapper(new(names.SnakeMapper)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithConnMaxLifetime(d time.Duration) Option {
|
||||
return func(e *xorm.Engine) {
|
||||
e.DB().SetConnMaxLifetime(d)
|
||||
}
|
||||
}
|
||||
|
||||
func WithMaxIdleConns(n int) Option {
|
||||
return func(e *xorm.Engine) {
|
||||
e.DB().SetMaxIdleConns(n)
|
||||
}
|
||||
}
|
||||
|
||||
func WithMaxOpenConns(n int) Option {
|
||||
return func(e *xorm.Engine) {
|
||||
e.DB().SetMaxOpenConns(n)
|
||||
}
|
||||
}
|
||||
61
internal/xorms/session.go
Normal file
61
internal/xorms/session.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package xorms
|
||||
|
||||
import "xorm.io/xorm"
|
||||
|
||||
type Session struct {
|
||||
*xorm.Session
|
||||
}
|
||||
|
||||
func newSession(session *xorm.Session) *Session {
|
||||
return &Session{session}
|
||||
}
|
||||
|
||||
func (this *Session) Like(param, arg string) *Session {
|
||||
this.Session.Where(param+" like ?", "%"+arg+"%")
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Session) Desc(colNames ...string) *Session {
|
||||
this.Session.Desc(colNames...)
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Session) Asc(colNames ...string) *Session {
|
||||
this.Session.Asc(colNames...)
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Session) Limit(limit int, start ...int) *Session {
|
||||
if limit > 0 {
|
||||
this.Session.Limit(limit, start...)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Session) Where(query any, args ...any) *Session {
|
||||
this.Session.Where(query, args...)
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Session) And(query any, args ...any) *Session {
|
||||
this.Session.And(query, args...)
|
||||
return this
|
||||
}
|
||||
|
||||
func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {
|
||||
session := db.NewSession()
|
||||
defer session.Close()
|
||||
if err := session.Begin(); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if err := fn(session); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if err := session.Commit(); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
119
internal/zip/zip_func.go
Normal file
119
internal/zip/zip_func.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package zip
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Encode 压缩文件
|
||||
// @filePath,文件路径
|
||||
// @zipName,压缩名称
|
||||
func Encode(filePath, zipName string) error {
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
os.MkdirAll(filepath.Dir(zipName), os.ModePerm)
|
||||
|
||||
zipFile, err := os.Create(zipName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zipFile.Close()
|
||||
zipWriter := zip.NewWriter(zipFile)
|
||||
defer zipWriter.Close()
|
||||
|
||||
return compareZip(file, zipWriter, "", !strings.HasSuffix(filePath, "/"))
|
||||
|
||||
}
|
||||
|
||||
// 压缩文件
|
||||
func compareZip(file *os.File, zipWriter *zip.Writer, prefix string, join bool) error {
|
||||
defer file.Close()
|
||||
fileInfo, err := file.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
header, err := zip.FileInfoHeader(fileInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if join {
|
||||
header.Name = filepath.Join(prefix, header.Name)
|
||||
prefix = filepath.Join(prefix, fileInfo.Name())
|
||||
header.Name = strings.ReplaceAll(header.Name, "\\", "/")
|
||||
if fileInfo.IsDir() {
|
||||
header.Name += "/"
|
||||
} else {
|
||||
header.Method = zip.Deflate //压缩的关键
|
||||
}
|
||||
writer, err := zipWriter.CreateHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !fileInfo.IsDir() {
|
||||
_, err = io.Copy(writer, file)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
fileInfoChildList, err := file.Readdir(-1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, fileInfoChild := range fileInfoChildList {
|
||||
fileChild, err := os.Open(filepath.Join(file.Name(), fileInfoChild.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := compareZip(fileChild, zipWriter, prefix, true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// Decode 解压zip
|
||||
func Decode(zipName, filePath string) error {
|
||||
r, err := zip.OpenReader(zipName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
for _, k := range r.Reader.File {
|
||||
if k.FileInfo().IsDir() {
|
||||
if err := os.MkdirAll(filepath.Join(filePath, k.Name), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err := func() error {
|
||||
f, err := k.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
w, err := os.Create(filepath.Join(filePath, k.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer w.Close()
|
||||
_, err = io.Copy(w, f)
|
||||
return err
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -293,6 +293,7 @@ func (this Klines) Merge(n int) Klines {
|
||||
if n <= 1 {
|
||||
return this
|
||||
}
|
||||
|
||||
ks := Klines(nil)
|
||||
ls := Klines(nil)
|
||||
for i := 0; ; i++ {
|
||||
|
||||
@@ -210,7 +210,7 @@ func (this Trades) klinesForDay(date time.Time) Klines {
|
||||
//分组,按
|
||||
for _, v := range this {
|
||||
ms := minutes(v.Time)
|
||||
t := conv.Select(ms <= _930, _930, ms)
|
||||
t := conv.Select(ms < _930, _930, ms)
|
||||
t++
|
||||
t = conv.Select(t > _1130 && t <= _1300, _1130, t)
|
||||
t = conv.Select(t > _1500, _1500, t)
|
||||
|
||||
@@ -135,10 +135,10 @@ func basePrice(code string) Price {
|
||||
return 1
|
||||
}
|
||||
switch code[:2] {
|
||||
case "60", "30", "68", "00", "92", "43":
|
||||
case "60", "30", "68", "00", "92", "43", "39":
|
||||
return 1
|
||||
default:
|
||||
return 10
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,11 +281,11 @@ func IsETF(code string) bool {
|
||||
code = strings.ToLower(code)
|
||||
switch {
|
||||
case code[0:2] == ExchangeSH.String() &&
|
||||
(code[2:5] == "510" || code[2:5] == "511" || code[2:5] == "512" || code[2:5] == "513" || code[2:5] == "515"):
|
||||
(code[2:4] == "51" || code[2:4] == "56" || code[2:4] == "58"):
|
||||
return true
|
||||
|
||||
case code[0:2] == ExchangeSZ.String() &&
|
||||
(code[2:5] == "159"):
|
||||
(code[2:4] == "15" || code[2:4] == "16"):
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
21
workday.go
21
workday.go
@@ -6,6 +6,7 @@ import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/injoyai/base/maps"
|
||||
"github.com/injoyai/conv"
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
@@ -16,6 +17,14 @@ import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func DialWorkday(op ...client.Option) (*Workday, error) {
|
||||
c, err := DialDefault(op...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewWorkdaySqlite(c)
|
||||
}
|
||||
|
||||
func NewWorkdayMysql(c *Client, dsn string) (*Workday, error) {
|
||||
|
||||
//连接数据库
|
||||
@@ -89,7 +98,7 @@ func (this *Workday) Update() error {
|
||||
|
||||
//获取沪市指数的日K线,用作历史是否节假日的判断依据
|
||||
//判断日K线是否拉取过
|
||||
|
||||
|
||||
//获取全部工作日
|
||||
all := []*WorkdayModel(nil)
|
||||
if err := this.db.Find(&all); err != nil {
|
||||
@@ -119,6 +128,10 @@ func (this *Workday) Update() error {
|
||||
}
|
||||
}
|
||||
|
||||
if len(inserts) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err = this.db.Insert(inserts)
|
||||
return err
|
||||
|
||||
@@ -146,11 +159,11 @@ func (this *Workday) RangeYear(year int, f func(t time.Time) bool) {
|
||||
)
|
||||
}
|
||||
|
||||
// Range 遍历指定范围的工作日
|
||||
// Range 遍历指定范围的工作日,推荐start带上时间15:00,这样当天小于15点不会触发
|
||||
func (this *Workday) Range(start, end time.Time, f func(t time.Time) bool) {
|
||||
start = conv.Select(start.Before(protocol.ExchangeEstablish), protocol.ExchangeEstablish, start)
|
||||
now := IntegerDay(time.Now())
|
||||
end = conv.Select(end.After(now), now, end).Add(1)
|
||||
//now := IntegerDay(time.Now())
|
||||
//end = conv.Select(end.After(now), now, end).Add(1)
|
||||
for ; start.Before(end); start = start.Add(time.Hour * 24) {
|
||||
if this.Is(start) {
|
||||
if !f(start) {
|
||||
|
||||
Reference in New Issue
Block a user