// main.go package main import ( "flag" "fmt" "io" "log" "os" "sort" "time" "tickserver/server/market" ) type byTime []market.Candle func (a byTime) Len() int { return len(a) } func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a byTime) Less(i, j int) bool { return a[i].Timestamp < a[j].Timestamp } func main() { flag.Parse() n := flag.NArg() if n != 2 { log.Println("must input current and next instrument id", n) return } insIdCurrent := flag.Arg(0) insIdNext := flag.Arg(1) log.Println("current:", insIdCurrent, "next", insIdNext) fnameCurrent := fmt.Sprintf("./fzmnew/ctp/%s/D1.gz", insIdCurrent) fnameNext := fmt.Sprintf("./fzmnew/ctp/%s/D1.gz", insIdNext) candlesCurrent, err := market.ReadCandleFile(fnameCurrent) if err != nil { log.Println(fnameCurrent, err) return } log.Println(fnameCurrent, len(candlesCurrent)) candlesNext, err := market.ReadCandleFile(fnameNext) if err != nil { log.Println(fnameNext, err) return } log.Println(fnameNext, len(candlesNext)) candlesNextMap := make(map[int64]market.Candle) for _, v := range candlesNext { candlesNextMap[v.Timestamp] = v } var candlesSelected []market.Candle var bCurrent bool for i := len(candlesCurrent) - 1; i >= 0; i-- { if fileExist(candlesCurrent[i].Timestamp) { break } candleNext, ok := candlesNextMap[candlesCurrent[i].Timestamp] if ok { if candlesCurrent[i].TickVolums >= candleNext.TickVolums { bCurrent = true } else { bCurrent = false } } else { bCurrent = true } var insIdSelected string if bCurrent { insIdSelected = insIdCurrent candlesSelected = append(candlesSelected, candlesCurrent[i]) } else { insIdSelected = insIdNext candlesSelected = append(candlesSelected, candleNext) } log.Println("insId Selected:", insIdSelected, candlesCurrent[i].TickVolums, candleNext.TickVolums, ok) fileCopy(insIdSelected, "tk", candlesCurrent[i].Timestamp) fileCopy(insIdSelected, "M1", candlesCurrent[i].Timestamp) fileCopy(insIdSelected, "M5", candlesCurrent[i].Timestamp) fileCopy(insIdSelected, "H1", candlesCurrent[i].Timestamp) } fnameD1 := "./fzmnew/ctp/ctp_IFMI/D1.gz" log.Println("fuck", len(candlesSelected), candlesSelected) candles, _ := combinEx(fnameD1, candlesSelected) _, err = market.SaveCandlesEx("./fzmnew/ctp", "ctp_IFMI", candles, market.D1, true) if err != nil { log.Println(err) return } } func fileExist(t int64) bool { tm := time.Unix(t/1000, 0) fname := fmt.Sprintf("fzmnew/ctp/ctp_IFMI/%d/%04d%02d%02d.M1.gz", tm.Year(), tm.Year(), tm.Month(), tm.Day()) if _, err := os.Stat(fname); !os.IsNotExist(err) { return true } else { log.Println("not exist:", fname) return false } } func fileCopy(insIdSour, period string, t int64) { tm := time.Unix(t/1000, 0) dataDir := fmt.Sprintf("./fzmnew/ctp/ctp_IFMI/%d", tm.Year()) os.MkdirAll(dataDir, 0777) fileNameDest := fmt.Sprintf("./fzmnew/ctp/ctp_IFMI/%d/%04d%02d%02d.%s.gz", tm.Year(), tm.Year(), tm.Month(), tm.Day(), period) fileNameSour := fmt.Sprintf("./fzmnew/ctp/%s/%d/%04d%02d%02d.%s.gz", insIdSour, tm.Year(), tm.Year(), tm.Month(), tm.Day(), period) fileSour, err := os.Open(fileNameSour) if err != nil { log.Println("os.Open", fileNameDest, err) return } defer fileSour.Close() fileDest, err := os.Create(fileNameDest) if err != nil { log.Println("os.Create", fileNameDest, err) return } defer fileDest.Close() _, err = io.Copy(fileDest, fileSour) if err != nil { log.Println(fileNameDest, err) return } } func combinEx(filename string, candles []market.Candle) ([]market.Candle, error) { buf, err := market.ReadCandleFile(filename) if err != nil { return candles, err } candles = append(buf, candles[:]...) sort.Sort(byTime(candles)) return candles, nil }