153 lines
3.2 KiB
Go
153 lines
3.2 KiB
Go
package moneymoney
|
|
|
|
import (
|
|
"encoding/gob"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/474420502/structure/tree/treelist"
|
|
"github.com/klauspost/compress/zstd"
|
|
)
|
|
|
|
func TestMoney(t *testing.T) {
|
|
HistoryRun()
|
|
}
|
|
|
|
// 统计历史关系参数.
|
|
func TestCase3(t *testing.T) {
|
|
cday := GetDate("2017-04-15")
|
|
citer := DateStocks.Iterator()
|
|
citer.SeekToFirst()
|
|
citer.SeekGE(cday.Unix())
|
|
|
|
for citer.Valid() {
|
|
|
|
stocks := citer.Value()
|
|
todayIter := stocks.Iterator() // 获取当天 所有股票的迭代器
|
|
|
|
cmpiter := citer.Clone()
|
|
|
|
var i = 0
|
|
|
|
// 向后移动的天数
|
|
for cmpiter.Valid() {
|
|
|
|
todayIter.SeekToFirst()
|
|
cmpday := cmpiter.Value()
|
|
|
|
stocks.Traverse(func(s *treelist.Slice[int64, *Stock]) bool {
|
|
|
|
todaystock := s.Value
|
|
if todaystock.Extend == nil {
|
|
todaystock.Extend = &StockExtend{
|
|
MaxPriceDays: 0,
|
|
MinPriceDays: 100000000000,
|
|
}
|
|
todaystock.Extend.UpsDownsRatioDays = append(todaystock.Extend.UpsDownsRatioDays, &UpsDownsDays{})
|
|
}
|
|
|
|
// 根据股票代码 获取对比的股票
|
|
c, ok := cmpday.Get(todaystock.Code)
|
|
if ok {
|
|
|
|
// 对比的历史前移时间股票
|
|
var ClosingPrice float64 = 0
|
|
|
|
if c.ClosingPrice == 0 {
|
|
ClosingPrice = c.PreviousClosingPrice
|
|
} else {
|
|
ClosingPrice = c.ClosingPrice
|
|
}
|
|
|
|
if ClosingPrice != 0 {
|
|
if ClosingPrice > todaystock.Extend.MaxPriceDays {
|
|
todaystock.Extend.MaxPriceDays = ClosingPrice
|
|
}
|
|
|
|
if ClosingPrice < todaystock.Extend.MinPriceDays {
|
|
todaystock.Extend.MinPriceDays = ClosingPrice
|
|
}
|
|
|
|
// 满足相邻天数的处理
|
|
|
|
// TODO: 其他的统计处理
|
|
}
|
|
|
|
last := todaystock.Extend.UpsDownsRatioDays[len(todaystock.Extend.UpsDownsRatioDays)-1]
|
|
last.DownsCount++
|
|
last.UpsCount++
|
|
if _, ok := CountedDays[i]; ok {
|
|
last.Ratio = ((ClosingPrice - todaystock.ClosingPrice) / ClosingPrice)
|
|
todaystock.Extend.UpsDownsRatioDays = append(todaystock.Extend.UpsDownsRatioDays, &UpsDownsDays{
|
|
UpsCount: last.UpsCount,
|
|
DownsCount: last.DownsCount,
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
})
|
|
|
|
if i >= 1<<7 {
|
|
break
|
|
}
|
|
|
|
cmpiter.Prev()
|
|
i++
|
|
}
|
|
|
|
todayIter.SeekToFirst()
|
|
for todayIter.Valid() {
|
|
s := todayIter.Value()
|
|
if s.Extend != nil {
|
|
// if len(s.Extend.UpsDownsRatioDays) <= 1 {
|
|
// log.Println(s.Name, s.Date.Local().String(), s.Code)
|
|
// }
|
|
s.Extend.UpsDownsRatioDays = s.Extend.UpsDownsRatioDays[0 : len(s.Extend.UpsDownsRatioDays)-1]
|
|
}
|
|
todayIter.Next()
|
|
}
|
|
|
|
citer.Next()
|
|
}
|
|
|
|
var stocks []*Stock
|
|
|
|
DateStocks.Traverse(func(s *treelist.Slice[int64, *treelist.Tree[int64, *Stock]]) bool {
|
|
s.Value.Traverse(func(s *treelist.Slice[int64, *Stock]) bool {
|
|
stocks = append(stocks, s.Value)
|
|
return true
|
|
})
|
|
return true
|
|
})
|
|
|
|
if stocks != nil {
|
|
f, err := os.OpenFile("./stocks1.gob", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer f.Close()
|
|
|
|
enc, err := zstd.NewWriter(f)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer enc.Close()
|
|
|
|
err = gob.NewEncoder(enc).Encode(stocks)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func TestCase2(t *testing.T) {
|
|
GetFromAPI() // 获取基础数据
|
|
}
|
|
|
|
func TestCase1(t *testing.T) {
|
|
main()
|
|
}
|