add lastUpdate to candle and max

This commit is contained in:
zhangkun9038@dingtalk.com 2024-12-19 20:00:15 +08:00
parent 104143800a
commit b835d2c040
5 changed files with 52 additions and 49 deletions

View File

@ -212,10 +212,10 @@ func (candle *Candle) PushToWriteLogChan(cr *Core) error {
did := candle.InstID + candle.Period + candle.Data[0].(string) did := candle.InstID + candle.Period + candle.Data[0].(string)
candle.Id = HashString(did) candle.Id = HashString(did)
ncd, _ := candle.ToStruct(cr) ncd, _ := candle.ToStruct(cr)
fmt.Println("ncd: ", ncd) logrus.Debug("ncd: ", ncd)
cd, err := json.Marshal(ncd) cd, err := json.Marshal(ncd)
if err != nil { if err != nil {
fmt.Println("PushToWriteLog json marshal candle err: ", err) logrus.Error("PushToWriteLog json marshal candle err: ", err)
} }
candle = ncd candle = ncd
wg := WriteLog{ wg := WriteLog{
@ -257,38 +257,38 @@ func (cl *Candle) ToStruct(core *Core) (*Candle, error) {
// 将字符串转换为 int64 类型的时间戳 // 将字符串转换为 int64 类型的时间戳
ts, err := strconv.ParseInt(cl.Data[0].(string), 10, 64) ts, err := strconv.ParseInt(cl.Data[0].(string), 10, 64)
if err != nil { if err != nil {
fmt.Println("Error parsing timestamp:", err) logrus.Error("Error parsing timestamp:", err)
return nil, err return nil, err
} }
ncd.Timestamp = time.Unix(ts/1000, (ts%1000)*1000000) // 纳秒级别 ncd.Timestamp = time.Unix(ts/1000, (ts%1000)*1000000) // 纳秒级别
op, err := strconv.ParseFloat(cl.Data[1].(string), 64) op, err := strconv.ParseFloat(cl.Data[1].(string), 64)
if err != nil { if err != nil {
fmt.Println("Error parsing string to float64:", err) logrus.Error("Error parsing string to float64:", err)
return nil, err return nil, err
} }
ncd.Open = op ncd.Open = op
hi, err := strconv.ParseFloat(cl.Data[2].(string), 64) hi, err := strconv.ParseFloat(cl.Data[2].(string), 64)
if err != nil { if err != nil {
fmt.Println("Error parsing string to float64:", err) logrus.Error("Error parsing string to float64:", err)
return nil, err return nil, err
} }
ncd.High = hi ncd.High = hi
lo, err := strconv.ParseFloat(cl.Data[3].(string), 64) lo, err := strconv.ParseFloat(cl.Data[3].(string), 64)
if err != nil { if err != nil {
fmt.Println("Error parsing string to float64:", err) logrus.Error("Error parsing string to float64:", err)
return nil, err return nil, err
} }
ncd.Low = lo ncd.Low = lo
clse, err := strconv.ParseFloat(cl.Data[4].(string), 64) clse, err := strconv.ParseFloat(cl.Data[4].(string), 64)
if err != nil { if err != nil {
fmt.Println("Error parsing string to float64:", err) logrus.Error("Error parsing string to float64:", err)
return nil, err return nil, err
} }
ncd.Close = clse ncd.Close = clse
ncd.VolCcy, err = strconv.ParseFloat(cl.Data[6].(string), 64) ncd.VolCcy, err = strconv.ParseFloat(cl.Data[6].(string), 64)
if err != nil { if err != nil {
fmt.Println("Error parsing string to float64:", err) logrus.Error("Error parsing string to float64:", err)
return nil, err return nil, err
} }
if cl.Data[6].(string) == "1" { if cl.Data[6].(string) == "1" {
@ -307,7 +307,7 @@ func (core *Core) SaveUniKey(period string, keyName string, extt time.Duration,
core.RedisLocalCli.Expire(refName, extt) core.RedisLocalCli.Expire(refName, extt)
// 为保证唯一性机制防止SaveToSortSet 被重复执行 // 为保证唯一性机制防止SaveToSortSet 被重复执行
if len(refRes) != 0 { if len(refRes) != 0 {
fmt.Println("refName exist: ", refName) logrus.Error("refName exist: ", refName)
return return
} }
@ -324,9 +324,9 @@ func (core *Core) SaveToSortSet(period string, keyName string, extt time.Duratio
} }
rs, err := core.RedisLocalCli.ZAdd(setName, z).Result() rs, err := core.RedisLocalCli.ZAdd(setName, z).Result()
if err != nil { if err != nil {
fmt.Println("err of ma7|ma30 add to redis:", err) logrus.Error("err of ma7|ma30 add to redis:", err)
} else { } else {
fmt.Println("sortedSet added to redis:", rs, keyName) logrus.Info("sortedSet added to redis:", rs, keyName)
} }
} }
@ -441,11 +441,11 @@ func (core *Core) GetRangeKeyList(pattern string, from time.Time) ([]*simple.Jso
nv := pattern + strconv.FormatInt(v, 10) nv := pattern + strconv.FormatInt(v, 10)
str, err := redisCli.Get(nv).Result() str, err := redisCli.Get(nv).Result()
if err != nil { if err != nil {
fmt.Println("err of redis get key:", nv, err) logrus.Error("err of redis get key:", nv, err)
} }
cur, err := simple.NewJson([]byte(str)) cur, err := simple.NewJson([]byte(str))
if err != nil { if err != nil {
fmt.Println("err of create newJson:", str, err) logrus.Error("err of create newJson:", str, err)
} }
res = append(res, cur) res = append(res, cur)
} }
@ -465,12 +465,12 @@ func (cl *Candle) SetToKey(core *Core) ([]interface{}, error) {
cl.Timestamp = tm cl.Timestamp = tm
dt, err := json.Marshal(cl) dt, err := json.Marshal(cl)
if err != nil { if err != nil {
fmt.Println("candle Save to String err:", err) logrus.Error("candle Save to String err:", err)
} }
fmt.Println("candle Save to String: ", string(dt)) logrus.Info("candle Save to String: ", string(dt))
exp, err := core.PeriodToMinutes(cl.Period) exp, err := core.PeriodToMinutes(cl.Period)
if err != nil { if err != nil {
fmt.Println("err of PeriodToMinutes:", err) logrus.Error("err of PeriodToMinutes:", err)
} }
// expf := float64(exp) * 60 // expf := float64(exp) * 60
expf := utils.Sqrt(float64(exp)) * 100 expf := utils.Sqrt(float64(exp)) * 100
@ -478,19 +478,20 @@ func (cl *Candle) SetToKey(core *Core) ([]interface{}, error) {
curVolstr, _ := data[5].(string) curVolstr, _ := data[5].(string)
curVol, err := strconv.ParseFloat(curVolstr, 64) curVol, err := strconv.ParseFloat(curVolstr, 64)
if err != nil { if err != nil {
fmt.Println("err of convert ts:", err) logrus.Error("err of convert ts:", err)
} }
curVolCcystr, _ := data[6].(string) curVolCcystr, _ := data[6].(string)
curVolCcy, err := strconv.ParseFloat(curVolCcystr, 64) curVolCcy, err := strconv.ParseFloat(curVolCcystr, 64)
curPrice := curVolCcy / curVol curPrice := curVolCcy / curVol
if curPrice <= 0 { if curPrice <= 0 {
fmt.Println("price有问题", curPrice, "dt: ", string(dt), "from:", cl.From) logrus.Error("price有问题", curPrice, "dt: ", string(dt), "from:", cl.From)
err = errors.New("price有问题") err = errors.New("price有问题")
return cl.Data, err return cl.Data, err
} }
redisCli := core.RedisLocalCli redisCli := core.RedisLocalCli
// tm := time.UnixMilli(tsi).Format("2006-01-02 15:04") // tm := time.UnixMilli(tsi).Format("2006-01-02 15:04")
fmt.Println("setToKey:", keyName, "ts: ", "price: ", curPrice, "from:", cl.From) fmt.Println()
logrus.Info("setToKey:", keyName, "ts: ", "price: ", curPrice, "from:", cl.From)
redisCli.Set(keyName, dt, extt).Result() redisCli.Set(keyName, dt, extt).Result()
core.SaveUniKey(cl.Period, keyName, extt, tsi) core.SaveUniKey(cl.Period, keyName, extt, tsi)
return cl.Data, err return cl.Data, err

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
simple "github.com/bitly/go-simplejson" simple "github.com/bitly/go-simplejson"
logrus "github.com/sirupsen/logrus"
) )
type MyConfig struct { type MyConfig struct {
@ -58,12 +59,12 @@ func (cfg MyConfig) Init() (MyConfig, error) {
if err != nil { if err != nil {
jsonStr, err = ioutil.ReadFile("configs/basicConfig.json") jsonStr, err = ioutil.ReadFile("configs/basicConfig.json")
if err != nil { if err != nil {
fmt.Println("err2:", err.Error()) logrus.Error("err2:", err.Error())
return cfg, err return cfg, err
} }
cfg.Config, err = simple.NewJson([]byte(jsonStr)) cfg.Config, err = simple.NewJson([]byte(jsonStr))
if err != nil { if err != nil {
fmt.Println("err2:", err.Error()) logrus.Error("err2:", err.Error())
return cfg, err return cfg, err
} }
cfg.Env = env cfg.Env = env
@ -96,7 +97,7 @@ func (cfg MyConfig) Init() (MyConfig, error) {
func (cfg *MyConfig) GetConfigJson(arr []string) *simple.Json { func (cfg *MyConfig) GetConfigJson(arr []string) *simple.Json {
env := os.Getenv("GO_ENV") env := os.Getenv("GO_ENV")
fmt.Println("env: ", env) logrus.Info("env: ", env)
cfg.Env = env cfg.Env = env
json, err := ioutil.ReadFile("/go/json/basicConfig.json") json, err := ioutil.ReadFile("/go/json/basicConfig.json")
@ -108,11 +109,11 @@ func (cfg *MyConfig) GetConfigJson(arr []string) *simple.Json {
} }
} }
if err != nil { if err != nil {
fmt.Println("read file err: ", err) logrus.Error("read file err: ", err)
} }
rjson, err := simple.NewJson(json) rjson, err := simple.NewJson(json)
if err != nil { if err != nil {
fmt.Println("newJson err: ", err) logrus.Error("newJson err: ", err)
} }
for _, s := range arr { for _, s := range arr {
rjson = rjson.Get(s) rjson = rjson.Get(s)

36
core.go
View File

@ -71,7 +71,7 @@ type SubAction struct {
} }
func (rst *RestQueue) Show(cr *Core) { func (rst *RestQueue) Show(cr *Core) {
fmt.Println("restQueue:", rst.InstId, rst.Bar, rst.Limit) logrus.Info("restQueue:", rst.InstId, rst.Bar, rst.Limit)
} }
func (rst *RestQueue) Save(cr *Core) { func (rst *RestQueue) Save(cr *Core) {
@ -89,12 +89,12 @@ func (rst *RestQueue) Save(cr *Core) {
} }
link := "/api/v5/market/candles?instId=" + rst.InstId + "&bar=" + rst.Bar + limitSec + afterSec + beforeSec link := "/api/v5/market/candles?instId=" + rst.InstId + "&bar=" + rst.Bar + limitSec + afterSec + beforeSec
fmt.Println("restLink: ", link) logrus.Info("restLink: ", link)
rsp, err := cr.v5PublicInvoke(link) rsp, err := cr.v5PublicInvoke(link)
if err != nil { if err != nil {
fmt.Println("cr.v5PublicInvoke err:", err) logrus.Info("cr.v5PublicInvoke err:", err)
} else { } else {
fmt.Println("cr.v5PublicInvoke result count:", len(rsp.Data)) logrus.Info("cr.v5PublicInvoke result count:", len(rsp.Data))
} }
cr.SaveCandle(rst.InstId, rst.Bar, rsp, rst.Duration, rst.WithWs) cr.SaveCandle(rst.InstId, rst.Bar, rsp, rst.Duration, rst.WithWs)
} }
@ -103,7 +103,7 @@ func WriteLogProcess(cr *Core) {
for { for {
wg := <-cr.WriteLogChan wg := <-cr.WriteLogChan
go func(wg *WriteLog) { go func(wg *WriteLog) {
fmt.Println("start writelog: " + wg.Tag + " " + wg.Id) logrus.Info("start writelog: " + wg.Tag + " " + wg.Id)
wg.Process(cr) wg.Process(cr)
}(wg) }(wg)
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
@ -120,9 +120,9 @@ func (core *Core) Init() {
gitBranch := os.Getenv("gitBranchName") gitBranch := os.Getenv("gitBranchName")
commitID := os.Getenv("gitCommitID") commitID := os.Getenv("gitCommitID")
fmt.Println("当前环境: ", core.Env) logrus.Info("当前环境: ", core.Env)
fmt.Println("gitBranch: ", gitBranch) logrus.Info("gitBranch: ", gitBranch)
fmt.Println("gitCommitID: ", commitID) logrus.Info("gitCommitID: ", commitID)
cfg := MyConfig{} cfg := MyConfig{}
cfg, _ = cfg.Init() cfg, _ = cfg.Init()
core.Cfg = &cfg core.Cfg = &cfg
@ -133,7 +133,7 @@ func (core *Core) Init() {
// 跟订单有关的都关掉 // 跟订单有关的都关掉
// core.OrderChan = make(chan *private.Order) // core.OrderChan = make(chan *private.Order)
if err != nil { if err != nil {
fmt.Println("init redis client err: ", err) logrus.Error("init redis client err: ", err)
} }
} }
@ -147,7 +147,7 @@ func (core *Core) GetRedisCliFromConf(conf RedisConfig) (*redis.Client, error) {
if pong == "PONG" && err == nil { if pong == "PONG" && err == nil {
return client, err return client, err
} else { } else {
fmt.Println("redis状态不可用:", conf.Url, conf.Password, conf.Index, err) logrus.Error("redis状态不可用:", conf.Url, conf.Password, conf.Index, err)
} }
return client, nil return client, nil
@ -170,7 +170,7 @@ func (core *Core) GetRemoteRedisLocalCli() (*redis.Client, error) {
if pong == "PONG" && err == nil { if pong == "PONG" && err == nil {
return client, err return client, err
} else { } else {
fmt.Println("redis状态不可用:", ru, rp, ri, err) logrus.Error("redis状态不可用:", ru, rp, ri, err)
} }
return client, nil return client, nil
@ -192,7 +192,7 @@ func (core *Core) GetRedisLocalCli() (*redis.Client, error) {
if pong == "PONG" && err == nil { if pong == "PONG" && err == nil {
return client, err return client, err
} else { } else {
fmt.Println("redis状态不可用:", ru, rp, ri, err) logrus.Error("redis状态不可用:", ru, rp, ri, err)
} }
return client, nil return client, nil
@ -308,7 +308,7 @@ func (core *Core) RestInvoke(subUrl string, method string) (*rest.RESTAPIResult,
rest.SetSimulate(isDemo).SetAPIKey(key, secure, pass) rest.SetSimulate(isDemo).SetAPIKey(key, secure, pass)
response, err := rest.Run(context.Background()) response, err := rest.Run(context.Background())
if err != nil { if err != nil {
fmt.Println("restInvoke1 err:", subUrl, err) logrus.Error("restInvoke1 err:", subUrl, err)
} }
return response, err return response, err
} }
@ -404,7 +404,7 @@ func (core *Core) GetScoreList(count int) []string {
// redisCli := core.RedisLocalCli // redisCli := core.RedisLocalCli
myFocusList := core.Cfg.Config.Get("focusList").MustArray() myFocusList := core.Cfg.Config.Get("focusList").MustArray()
fmt.Println("curList: ", myFocusList) logrus.Debug("curList: ", myFocusList)
lst := []string{} lst := []string{}
for _, v := range myFocusList { for _, v := range myFocusList {
lst = append(lst, v.(string)) lst = append(lst, v.(string))
@ -535,7 +535,7 @@ func (cr *Core) ProcessOrder(od *private.Order) error {
// TODO FIXME cli2 // TODO FIXME cli2
res, _ := cr.RedisLocalCli.Publish(cn, string(bj)).Result() res, _ := cr.RedisLocalCli.Publish(cn, string(bj)).Result()
fmt.Println("order publish res: ", res, " content: ", string(bj)) logrus.Info("order publish res: ", res, " content: ", string(bj))
rsch := ORDER_RESP_PUBLISH + suffix rsch := ORDER_RESP_PUBLISH + suffix
bj1, _ := json.Marshal(res) bj1, _ := json.Marshal(res)
@ -585,9 +585,9 @@ func (cr *Core) AddToGeneralCandleChnl(candle *Candle, channels []string) {
redisCli := cr.RedisLocalCli redisCli := cr.RedisLocalCli
ab, err := json.Marshal(candle) ab, err := json.Marshal(candle)
if err != nil { if err != nil {
fmt.Println("candle marshal err: ", err) logrus.Error("candle marshal err: ", err)
} }
fmt.Println("ab: ", string(ab)) logrus.Debug("ab: ", string(ab))
for _, v := range channels { for _, v := range channels {
suffix := "" suffix := ""
env := os.Getenv("GO_ENV") env := os.Getenv("GO_ENV")
@ -597,7 +597,7 @@ func (cr *Core) AddToGeneralCandleChnl(candle *Candle, channels []string) {
vd := v + suffix vd := v + suffix
_, err := redisCli.Publish(vd, string(ab)).Result() _, err := redisCli.Publish(vd, string(ab)).Result()
if err != nil { if err != nil {
fmt.Println("err of ma7|ma30 add to redis2:", err, candle.From) logrus.Error("err of ma7|ma30 add to redis2:", err, candle.From)
} }
} }
} }

12
maX.go
View File

@ -51,25 +51,25 @@ func (mx MaX) SetToKey(cr *Core) ([]interface{}, error) {
dj, err := json.Marshal(mx) dj, err := json.Marshal(mx)
if err != nil { if err != nil {
fmt.Println("maX SetToKey json marshal err: ", err) logrus.Error("maX SetToKey json marshal err: ", err)
} }
extt, err := cr.GetExpiration(mx.Period) extt, err := cr.GetExpiration(mx.Period)
if err != nil { if err != nil {
fmt.Println("max SetToKey err: ", err) logrus.Error("max SetToKey err: ", err)
return mx.Data, err return mx.Data, err
} }
// fmt.Println(utils.GetFuncName(), " step2 ", mx.InstID, " ", mx.Period) // fmt.Println(utils.GetFuncName(), " step2 ", mx.InstID, " ", mx.Period)
// tm := time.UnixMilli(mx.Ts).Format("01-02 15:04") // tm := time.UnixMilli(mx.Ts).Format("01-02 15:04")
cli := cr.RedisLocalCli cli := cr.RedisLocalCli
if len(string(dj)) == 0 { if len(string(dj)) == 0 {
fmt.Println("mx data is block data: ", mx, string(dj)) logrus.Error("mx data is block data: ", mx, string(dj))
err := errors.New("data is block") err := errors.New("data is block")
return mx.Data, err return mx.Data, err
} }
// fmt.Println(utils.GetFuncName(), " step3 ", mx.InstID, " ", mx.Period) // fmt.Println(utils.GetFuncName(), " step3 ", mx.InstID, " ", mx.Period)
_, err = cli.Set(keyName, dj, extt).Result() _, err = cli.Set(keyName, dj, extt).Result()
if err != nil { if err != nil {
fmt.Println(GetFuncName(), " maXSetToKey err:", err) logrus.Error(GetFuncName(), " maXSetToKey err:", err)
return mx.Data, err return mx.Data, err
} }
// fmt.Println(utils.GetFuncName(), " step4 ", mx.InstID, " ", mx.Period) // fmt.Println(utils.GetFuncName(), " step4 ", mx.InstID, " ", mx.Period)
@ -85,7 +85,7 @@ func Int64ToTime(ts int64) (time.Time, error) {
// 获取东八区(北京时间)的时区信息 // 获取东八区(北京时间)的时区信息
loc, err := time.LoadLocation("Asia/Shanghai") loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil { if err != nil {
fmt.Println("加载时区失败:", err) logrus.Error("加载时区失败:", err)
return t, err return t, err
} }
// 将时间转换为东八区时间 // 将时间转换为东八区时间
@ -95,7 +95,7 @@ func Int64ToTime(ts int64) (time.Time, error) {
func (mx *MaX) PushToWriteLogChan(cr *Core) error { func (mx *MaX) PushToWriteLogChan(cr *Core) error {
s := strconv.FormatFloat(float64(mx.Ts), 'f', 0, 64) s := strconv.FormatFloat(float64(mx.Ts), 'f', 0, 64)
did := mx.InstID + mx.Period + s did := mx.InstID + mx.Period + s
fmt.Println("did of max:", did) logrus.Error("did of max:", did)
mx0 := MaX{} mx0 := MaX{}
mx0.InstID = mx.InstID mx0.InstID = mx.InstID
mx0.Period = mx.Period mx0.Period = mx.Period

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
logrus "github.com/sirupsen/logrus"
"math" "math"
"math/rand" "math/rand"
"runtime" "runtime"
@ -103,7 +104,7 @@ func JsonToMap(str string) (map[string]interface{}, error) {
err := json.Unmarshal([]byte(str), &tempMap) err := json.Unmarshal([]byte(str), &tempMap)
if err != nil { if err != nil {
fmt.Println("Unmarshal err: ", err, str) logrus.Error("Unmarshal err: ", err, str)
} }
return tempMap, err return tempMap, err