修复codes的LastPrice价格不对的问题,只做了简单验证

This commit is contained in:
injoyai
2025-05-29 09:19:34 +08:00
parent c5f62c15b3
commit bc45a72d8d
5 changed files with 56 additions and 14 deletions

View File

@@ -3,20 +3,18 @@ package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/example/common"
"github.com/injoyai/tdx/protocol"
)
func main() {
c, err := tdx.Dial("124.71.187.122:7709")
logs.PanicErr(err)
common.Test(func(c *tdx.Client) {
resp, err := c.GetCode(protocol.ExchangeSH, 369)
logs.PanicErr(err)
resp, err := c.GetCode(protocol.ExchangeSH, 369)
logs.PanicErr(err)
for i, v := range resp.List {
logs.Debug(i, v)
}
logs.Debug("总数:", resp.Count)
select {}
for i, v := range resp.List {
logs.Debug(i, v, v.LastPrice)
}
logs.Debug("总数:", resp.Count)
})
}

View File

@@ -49,7 +49,7 @@ func (code) Decode(bs []byte) (*CodeResp, error) {
Multiple: Uint16(bs[6:8]),
Name: string(UTF8ToGBK(bs[8:16])),
Decimal: int8(bs[20]),
LastPrice: getVolume(Uint32(bs[21:25])),
LastPrice: getVolume2(Uint32(bs[21:25])),
}
//logs.Debug(bs[25:29]) //26和28字节 好像是枚举(基本是44,45和34,35)
bs = bs[29:]

View File

@@ -19,7 +19,10 @@ func Test_stockKline_Decode(t *testing.T) {
t.Error(err)
return
}
resp, err := MKline.Decode(bs, 9)
resp, err := MKline.Decode(bs, KlineCache{
Type: 9,
Kind: "",
})
if err != nil {
t.Error(err)
return

View File

@@ -195,6 +195,46 @@ func getVolume(val uint32) (volume float64) {
return
}
func getVolume2(val uint32) float64 {
ivol := int32(val)
logpoint := ivol >> 24 // 提取最高字节原8*3移位
hleax := (ivol >> 16) & 0xff // 提取次高字节
lheax := (ivol >> 8) & 0xff // 提取第三字节
lleax := ivol & 0xff // 提取最低字节
dwEcx := logpoint*2 - 0x7f // 基础指数计算
dbl_xmm6 := math.Exp2(float64(dwEcx)) // 核心指数计算仅一次
// 计算dbl_xmm4
var dbl_xmm4 float64
if hleax > 0x80 {
// 高位分支:合并指数计算
dbl_xmm4 = dbl_xmm6 * (64.0 + float64(hleax&0x7f)) / 64.0
} else {
// 低位分支:复用核心指数
dbl_xmm4 = dbl_xmm6 * float64(hleax) / 128.0
}
// 计算缩放因子
scale := 1.0
if (hleax & 0x80) != 0 {
scale = 2.0
}
// 预计算常量的倒数,优化除法
const (
inv32768 = 1.0 / 32768.0 // 2^15
inv8388608 = 1.0 / 8388608.0 // 2^23
)
// 计算低位分量
dbl_xmm3 := dbl_xmm6 * float64(lheax) * inv32768 * scale
dbl_xmm1 := dbl_xmm6 * float64(lleax) * inv8388608 * scale
// 合计最终结果
return dbl_xmm6 + dbl_xmm4 + dbl_xmm3 + dbl_xmm1
}
// IsStock 是否是股票,示例sz000001
func IsStock(code string) bool {
if len(code) != 8 {

View File

@@ -20,5 +20,6 @@ func TestUTF8ToGBK(t *testing.T) {
func Test_getVolume(t *testing.T) {
t.Log(getVolume(1237966432))
t.Log(getVolume(1237966432))
t.Log(getVolume2(1237966432))
}