diff --git a/main_test.go b/main_test.go index e1f976d..e07f4cb 100644 --- a/main_test.go +++ b/main_test.go @@ -19,48 +19,12 @@ func GetDate(date string) time.Time { 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) - // } + log.Println("重新从", start.Local().Format("2006-01-02"), cday.Local().Format("2006-01-02"), "策略选股") // istartStock, _ := DateStocks.Get(start.Unix()) siter := DateStocks.Iterator() @@ -69,17 +33,17 @@ func GetStocksByCondition(cday time.Time) []*Stock { { ssiter := siter.Value().(*treelist.Tree[int64]).Iterator() ssiter.SeekToFirst() - ss := ssiter.Value().(*Stock) - log.Printf("%s", ss.Date.Local().Format("2006-01-02")) + // 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()) - + // log.Println(iter.SeekLE(cday.Unix()), iter.Valid()) + iter.SeekLE(cday.Unix()) endStock := iter.Value().(*treelist.Tree[int64]) - log.Println(DateStocks.Size(), endStock.Size()) + // log.Println(DateStocks.Size(), endStock.Size()) endStock.Traverse(func(s *treelist.Slice[int64]) bool { var ok bool @@ -131,16 +95,17 @@ func GetStocksByCondition(cday time.Time) []*Stock { func TestMoney(t *testing.T) { - var money float64 = 0.0 - cday := GetDate("2017-05-15") + var money float64 = 1000000.0 + cday := GetDate("2017-04-01") - for money < 1.0 { +ALL_LOOP: + for money < 1000000*3 { selectStocks := GetStocksByCondition(cday) - log.Println(len(selectStocks)) + // 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.Printf("%.4f%% %s %s %f", s.UpsDownsRatio*100.0, s.Date.Local().Format("2006-01-02"), s.Name, s.ClosingPrice) // log.Println(s.UpsDownsRatio, s) } @@ -148,20 +113,26 @@ func TestMoney(t *testing.T) { start := (len(selectStocks) - 10) / 2 selectStocks = selectStocks[start : start+10] - for _, s := range selectStocks { - log.Printf("%.4f%% %s", s.UpsDownsRatio*100.0, s.Name) - } + // for _, s := range selectStocks { + // log.Printf("%.4f%% %s", s.UpsDownsRatio*100.0, s.Name) + // } // TODO: 测试 收益 iter := DateStocks.Iterator() iter.SeekGT(cday.Unix()) + if !iter.Valid() { + break + } for { var total float64 = 0.0 iter.Next() + + if !iter.Valid() { + break ALL_LOOP + } cstocks := iter.Value().(*treelist.Tree[int64]) var cs *Stock - for _, s := range selectStocks { itf, ok := cstocks.Get(s.Code) if ok { @@ -171,20 +142,78 @@ func TestMoney(t *testing.T) { } } + csdate := cs.Date.Local().Format("2006-01-02") + if csdate == "2017-12-07" { + for _, s := range selectStocks { + log.Printf("%.4f%% %s %s %f %f %f", s.UpsDownsRatio*100.0, s.Date.Local().Format("2006-01-02"), s.Name, s.ClosingPrice, s.MinPrice, s.MaxPrice) + // log.Println(s.UpsDownsRatio, s) + } + log.Println() + } + total = total / float64(len(selectStocks)) - log.Printf("%.2f%%, %s", total*100, cs.Date.Local()) - if total >= 0.06 { - money += total + log.Printf("%s 总收益:%.2f%%", csdate, total*100) + if total >= 0.10 || total <= -0.20 { + money = money + money*total cday = cs.Date + log.Printf("最后总财富 1000000 -> money: %f", money) break } } } + + log.Printf("最后总财富 1000000 -> money: %f", money) + } func TestCase3(t *testing.T) { + cday := GetDate("2017-04-01") + citer := DateStocks.Iterator() + citer.SeekToFirst() + citer.SeekGE(cday.Unix()) + for citer.Valid() { + + stocks := citer.Value().(*treelist.Tree[int64]) + + todayIter := stocks.Iterator() + + cmpiter := citer.Clone() + + var i = 0 + var step = 1 + for ; i < 2; i++ { + cmpiter.Prev() + } + + for cmpiter.Valid() { + + todayIter.SeekToFirst() + + cmpday := cmpiter.Value().(*treelist.Tree[int64]) + + for todayIter.Valid() { + + s := todayIter.Value().(*Stock) + + ic, ok := cmpday.Get(s.Code) + if ok { + c := ic.(*Stock) + // TODO 存这个属性 + (c.ClosingPrice - s.ClosingPrice/c.ClosingPrice) + } + + todayIter.Next() + } + + step = step << 1 + for i := 0; i < step; i++ { + cmpiter.Prev() + } + } + citer.Next() + } } func TestCase2(t *testing.T) {