mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加income,用于计算未来收益
This commit is contained in:
36
example/Income/main.go
Normal file
36
example/Income/main.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
65
extend/income.go
Normal file
65
extend/income.go
Normal file
@@ -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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user