优化protocol.Trades,能直接生成完整的分时k线,和实际误差很小

This commit is contained in:
injoyai
2025-10-13 15:01:27 +08:00
parent 8fb069b855
commit f1da1182ce
3 changed files with 101 additions and 32 deletions

View File

@@ -233,6 +233,14 @@ func FixKlineTime(ks []*Kline) []*Kline {
type Klines []*Kline type Klines []*Kline
// LastPrice 获取最后一个K线的收盘价
func (this Klines) LastPrice() Price {
if len(this) == 0 {
return 0
}
return this[len(this)-1].Close
}
func (this Klines) Len() int { func (this Klines) Len() int {
return len(this) return len(this)
} }

View File

@@ -3,6 +3,7 @@ package protocol
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/injoyai/base/types"
"github.com/injoyai/conv" "github.com/injoyai/conv"
"time" "time"
) )
@@ -121,52 +122,108 @@ func (trade) Decode(bs []byte, c TradeCache) (*TradeResp, error) {
type Trades []*Trade type Trades []*Trade
func (this Trades) Kline() (k *Kline, err error) { // Klines 合并分时成交成k线
k = &Kline{} func (this Trades) Klines() Klines {
for i, v := range this { //按天分割
switch i { m := make(types.SortMap[int64, Trades])
for _, v := range this {
//获取当天零点的时间戳
unix := time.Date(v.Time.Year(), v.Time.Month(), v.Time.Day(), 0, 0, 0, 0, v.Time.Location()).Unix()
m[unix] = append(m[unix], v)
}
//按天排序
mKline := types.SortMap[int64, Klines]{}
for date, v := range m {
//生成一分钟k线
t := time.Unix(date, 0)
mKline[date] = v.klinesForDay(t)
}
//按时间排序
lss := mKline.Sort()
ls := Klines{}
for _, v := range lss {
ls = append(ls, v...)
}
return ls
}
// Kline 合并分时成交成1个k线,注意分时成交时间保持一致
func (this Trades) Kline(t time.Time, last Price) *Kline {
k := &Kline{
Time: t,
Last: last,
Open: last,
High: last,
Low: last,
Close: last,
}
first := 0
for _, v := range this {
if v.Price <= 0 {
continue
}
switch first {
case 0: case 0:
k.Time = v.Time
k.Open = v.Price k.Open = v.Price
k.High = v.Price k.High = v.Price
k.Low = v.Price k.Low = v.Price
k.Close = v.Price k.Close = v.Price
case len(this) - 1: default:
k.Close = v.Price k.High = conv.Select(k.High < v.Price, v.Price, k.High)
k.Low = conv.Select(k.Low > v.Price, v.Price, k.Low)
} }
k.High = conv.Select(v.Price > k.High, v.Price, k.High) k.Close = v.Price
k.Low = conv.Select(v.Price < k.Low, v.Price, k.Low)
k.Volume += int64(v.Volume) k.Volume += int64(v.Volume)
k.Amount += v.Amount() k.Amount += v.Price * Price(v.Volume) * 100
first++
} }
return return k
} }
// Klines1 1分K线 // kline1 生成一分钟k线,一天
func (this Trades) Klines1() (Klines, error) { func (this Trades) klinesForDay(date time.Time) Klines {
m := make(map[int64]Trades) _930 := 570 //9:30 的分钟
for _, v := range this { _1130 := 690 //11:30 的分钟
//小于9点30的数据归类到9点30 _1300 := 780 //13:00 的分钟
if v.Time.Hour() == 9 && v.Time.Minute() < 30 { _1500 := 900 //15:00 的分钟
v.Time = time.Date(v.Time.Year(), v.Time.Month(), v.Time.Day(), 9, 30, 0, 0, v.Time.Location()) keys := []int(nil)
} //早上
//15:00之前和11:30之前+1 m := map[int]Trades{}
if (v.Time.Hour() >= 13 && v.Time.Hour() < 15) || (v.Time.Hour() == 11 && v.Time.Minute() < 30) || v.Time.Hour() < 11 { for i := 1; i <= 120; i++ {
v.Time = v.Time.Add(time.Minute) keys = append(keys, _930+i)
} m[_930+i] = []*Trade{}
m[v.Time.Unix()] = append(m[v.Time.Unix()], v)
} }
//下午
ls := Klines(nil) for i := 1; i <= 120; i++ {
for _, v := range m { keys = append(keys, _1300+i)
k, err := v.Kline() m[_1300+i] = []*Trade{}
if err != nil { }
return nil, err //获取开盘价,有可能前几分钟没有数据,先遍历一遍
var open Price
for _, v := range this {
if v.Price > 0 {
open = v.Price
break
} }
}
//分组,按
for _, v := range this {
ms := minutes(v.Time)
t := conv.Select(ms <= _930, _930, ms)
t++
t = conv.Select(t > _1130 && t <= _1300, _1130, t)
t = conv.Select(t > _1500, _1500, t)
m[t] = append(m[t], v)
}
//合并
ls := []*Kline(nil)
for _, v := range keys {
k := m[v].Kline(time.Date(date.Year(), date.Month(), date.Day(), v/60, v%60, 0, 0, date.Location()), open)
open = k.Close
ls = append(ls, k) ls = append(ls, k)
} }
ls.Sort() return ls
return ls, nil
} }
type TradeCache struct { type TradeCache struct {

View File

@@ -317,3 +317,7 @@ func AddPrefix(code string) string {
} }
return code return code
} }
func minutes(t time.Time) int {
return t.Hour()*60 + t.Minute()
}