From 84b0ec6f6c58092c1d2a3eae557001aba92acddb Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Tue, 20 May 2025 17:10:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0income,=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=9C=AA=E6=9D=A5=E6=94=B6=E7=9B=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Income/main.go | 36 +++++++++++++++++++++++ extend/income.go | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 example/Income/main.go create mode 100644 extend/income.go diff --git a/example/Income/main.go b/example/Income/main.go new file mode 100644 index 0000000..0381dfb --- /dev/null +++ b/example/Income/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "github.com/injoyai/logs" + "github.com/injoyai/tdx/extend" + "time" +) + +func main() { + code := "sz000001" + + pull := extend.NewPullKline(extend.PullKlineConfig{ + Codes: []string{code}, + Tables: []string{extend.Day}, + }) + + //m, err := tdx.NewManage(nil) + //logs.PanicErr(err) + + //err = pull.Run(context.Background(), m) + //logs.PanicErr(err) + + ks, err := pull.DayKlines(code) + logs.PanicErr(err) + + t := time.Now().AddDate(0, -1, -9) + logs.Debug(t.Format(time.DateOnly)) + ls := extend.DoIncomes(ks, t, 5, 10, 20) + + logs.Debug(len(ls)) + + for _, v := range ls { + logs.Info(v) + } + +} diff --git a/extend/income.go b/extend/income.go new file mode 100644 index 0000000..59767de --- /dev/null +++ b/extend/income.go @@ -0,0 +1,65 @@ +package extend + +import ( + "fmt" + "github.com/injoyai/tdx/protocol" + "time" +) + +func DoIncomes(ks Klines, startAt time.Time, days ...int) Incomes { + year, month, day := startAt.Date() + start := time.Date(year, month, day, 15, 0, 0, 0, startAt.Location()).Unix() + for i, v := range ks { + if v.Date >= start { + ks = ks[i:] + break + } + } + + ls := Incomes{} + + for _, v := range days { + if v < len(ks) { + x := ks[v] + ls = append(ls, &Income{ + Offset: v, + Time: time.Unix(x.Date, 0), + Source: protocol.K{ + Open: ks[0].Open, + High: ks[0].High, + Low: ks[0].Low, + Close: ks[0].Close, + }, + Current: protocol.K{ + Open: x.Open, + High: x.High, + Low: x.Low, + Close: x.Close, + }, + }) + } + } + + return ls +} + +type Incomes []*Income + +type Income struct { + Offset int //偏移量 + Time time.Time //时间 + Source protocol.K //源 + Current protocol.K //当前 +} + +func (this *Income) String() string { + return fmt.Sprintf("偏移: %d, 时间: %s, 涨幅: %.1f%%", this.Offset, this.Time.Format(time.DateOnly), this.RiseRate()*100) +} + +func (this *Income) Rise() protocol.Price { + return this.Current.Close - this.Source.Close +} + +func (this *Income) RiseRate() float64 { + return this.Rise().Float64() / this.Source.Close.Float64() +}