money-money/main_test.go

197 lines
4.1 KiB
Go
Raw Normal View History

2022-06-20 18:17:29 +00:00
package moneymoney
2022-07-03 00:38:06 +00:00
import (
"log"
"sort"
"testing"
"time"
"github.com/474420502/structure/tree/treelist"
)
const 亿 = 100000000
func GetDate(date string) time.Time {
ts, err := time.ParseInLocation("2006-01-02", date, time.Local)
if err != nil {
panic(err)
}
return ts
}
var CurrentDay = "2017-03-15"
func GetStocksByCondition(cday time.Time) []*Stock {
var stocks []*Stock
// cday := GetDate(CurrentDay)
start := cday.Add(-time.Hour * 24 * 14)
log.Println(start.Local(), cday.Local())
// cur, err := cstock.Find(context.TODO(), bson.M{
// "日期": bson.M{"$gte": start, "$lte": cday},
// // "涨跌幅": bson.M{"$gte": -15.0, "$lte": -2.0},
// // // "换手率": bson.M{"$gte": 1.0},
// // "流通市值": bson.M{"$gte": 100 * 亿},
// })
// if err != nil {
// panic(err)
// }
// log.Println(cur.Current)
// // var total = 0.0
// var stocks []*Stock
// for cur.Next(context.TODO()) {
// var s Stock
// err := cur.Decode(&s)
// if err != nil {
// panic(err)
// }
// // clf.Add(&s)
// var daymap *treelist.Tree[int64]
// idaymap, ok := DateStocks.Get(s.Date.Unix())
// // daymap, ok := DateStocks[s.Date.Unix()]
// if !ok {
// idaymap = treelist.New(compare.Any[int64])
// DateStocks.Put(s.Date.Unix(), idaymap)
// // DateStocks[s.Date.Unix()] = daymap
// }
// daymap = idaymap.(*treelist.Tree[int64])
// daymap.Put(s.Code, &s)
// }
// istartStock, _ := DateStocks.Get(start.Unix())
siter := DateStocks.Iterator()
siter.SeekGE(start.Unix())
siter.Valid()
{
ssiter := siter.Value().(*treelist.Tree[int64]).Iterator()
ssiter.SeekToFirst()
ss := ssiter.Value().(*Stock)
log.Printf("%s", ss.Date.Local().Format("2006-01-02"))
}
iter := DateStocks.Iterator()
log.Println(iter.SeekLE(cday.Unix()), iter.Valid())
endStock := iter.Value().(*treelist.Tree[int64])
log.Println(DateStocks.Size(), endStock.Size())
endStock.Traverse(func(s *treelist.Slice[int64]) bool {
var ok bool
citer := siter.Clone()
stock := s.Value.(*Stock)
if stock.MarketValue <= 100*亿 {
return true
}
startStock := citer.Value().(*treelist.Tree[int64])
var istock any
for {
istock, ok = startStock.Get(stock.Code)
if ok {
break
}
citer.Next()
if !citer.Valid() {
return true
}
}
fstock := istock.(*Stock)
if fstock.ClosingPrice == 0 {
return true
}
if stock.ClosingPrice == 0 {
return true
}
stock.MinPrice = fstock.ClosingPrice
stock.MaxPrice = stock.ClosingPrice
stock.UpsDownsRatio = ((stock.ClosingPrice - fstock.ClosingPrice) / fstock.ClosingPrice)
// log.Println(stock.UpsDownsRatio, s)
stocks = append(stocks, stock)
return true
})
sort.Slice(stocks, func(i, j int) bool {
return stocks[i].UpsDownsRatio < stocks[j].UpsDownsRatio
})
return stocks
}
func TestMoney(t *testing.T) {
var money float64 = 0.0
cday := GetDate("2017-05-15")
for money < 1.0 {
selectStocks := GetStocksByCondition(cday)
log.Println(len(selectStocks))
for _, s := range selectStocks[0:15] {
log.Printf("%.4f%% %s %s %f %f", s.UpsDownsRatio*100.0, s.Date.Local().Format("2006-01-02"), s.Name, s.MinPrice, s.MaxPrice)
// log.Println(s.UpsDownsRatio, s)
}
selectStocks = selectStocks[0:15]
start := (len(selectStocks) - 10) / 2
selectStocks = selectStocks[start : start+10]
for _, s := range selectStocks {
log.Printf("%.4f%% %s", s.UpsDownsRatio*100.0, s.Name)
}
// TODO: 测试 收益
iter := DateStocks.Iterator()
iter.SeekGT(cday.Unix())
for {
var total float64 = 0.0
iter.Next()
cstocks := iter.Value().(*treelist.Tree[int64])
var cs *Stock
for _, s := range selectStocks {
itf, ok := cstocks.Get(s.Code)
if ok {
cs = itf.(*Stock)
total += (cs.ClosingPrice - s.ClosingPrice) / s.ClosingPrice
// log.Printf("%.2f", (cs.ClosingPrice-s.ClosingPrice)/s.ClosingPrice)
}
}
total = total / float64(len(selectStocks))
log.Printf("%.2f%%, %s", total*100, cs.Date.Local())
if total >= 0.06 {
money += total
cday = cs.Date
break
}
}
}
}
func TestCase3(t *testing.T) {
}
2022-06-20 18:17:29 +00:00
2022-06-23 09:06:00 +00:00
func TestCase2(t *testing.T) {
2022-07-03 00:38:06 +00:00
GetFromAPI() // 获取基础数据
2022-06-23 09:06:00 +00:00
}
2022-06-20 18:17:29 +00:00
func TestCase1(t *testing.T) {
main()
}