main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // main.go
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "io"
  7. "log"
  8. "os"
  9. "sort"
  10. "time"
  11. "tickserver/server/market"
  12. )
  13. type byTime []market.Candle
  14. func (a byTime) Len() int { return len(a) }
  15. func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  16. func (a byTime) Less(i, j int) bool { return a[i].Timestamp < a[j].Timestamp }
  17. func main() {
  18. flag.Parse()
  19. n := flag.NArg()
  20. if n != 2 {
  21. log.Println("must input current and next instrument id", n)
  22. return
  23. }
  24. insIdCurrent := flag.Arg(0)
  25. insIdNext := flag.Arg(1)
  26. log.Println("current:", insIdCurrent, "next", insIdNext)
  27. fnameCurrent := fmt.Sprintf("./fzmnew/ctp/%s/D1.gz", insIdCurrent)
  28. fnameNext := fmt.Sprintf("./fzmnew/ctp/%s/D1.gz", insIdNext)
  29. candlesCurrent, err := market.ReadCandleFile(fnameCurrent)
  30. if err != nil {
  31. log.Println(fnameCurrent, err)
  32. return
  33. }
  34. log.Println(fnameCurrent, len(candlesCurrent))
  35. candlesNext, err := market.ReadCandleFile(fnameNext)
  36. if err != nil {
  37. log.Println(fnameNext, err)
  38. return
  39. }
  40. log.Println(fnameNext, len(candlesNext))
  41. candlesNextMap := make(map[int64]market.Candle)
  42. for _, v := range candlesNext {
  43. candlesNextMap[v.Timestamp] = v
  44. }
  45. var candlesSelected []market.Candle
  46. var bCurrent bool
  47. for i := len(candlesCurrent) - 1; i >= 0; i-- {
  48. if fileExist(candlesCurrent[i].Timestamp) {
  49. break
  50. }
  51. candleNext, ok := candlesNextMap[candlesCurrent[i].Timestamp]
  52. if ok {
  53. if candlesCurrent[i].TickVolums >= candleNext.TickVolums {
  54. bCurrent = true
  55. } else {
  56. bCurrent = false
  57. }
  58. } else {
  59. bCurrent = true
  60. }
  61. var insIdSelected string
  62. if bCurrent {
  63. insIdSelected = insIdCurrent
  64. candlesSelected = append(candlesSelected, candlesCurrent[i])
  65. } else {
  66. insIdSelected = insIdNext
  67. candlesSelected = append(candlesSelected, candleNext)
  68. }
  69. log.Println("insId Selected:", insIdSelected, candlesCurrent[i].TickVolums, candleNext.TickVolums, ok)
  70. fileCopy(insIdSelected, "tk", candlesCurrent[i].Timestamp)
  71. fileCopy(insIdSelected, "M1", candlesCurrent[i].Timestamp)
  72. fileCopy(insIdSelected, "M5", candlesCurrent[i].Timestamp)
  73. fileCopy(insIdSelected, "H1", candlesCurrent[i].Timestamp)
  74. }
  75. fnameD1 := "./fzmnew/ctp/ctp_IFMI/D1.gz"
  76. log.Println("fuck", len(candlesSelected), candlesSelected)
  77. candles, _ := combinEx(fnameD1, candlesSelected)
  78. _, err = market.SaveCandlesEx("./fzmnew/ctp", "ctp_IFMI", candles, market.D1, true)
  79. if err != nil {
  80. log.Println(err)
  81. return
  82. }
  83. }
  84. func fileExist(t int64) bool {
  85. tm := time.Unix(t/1000, 0)
  86. fname := fmt.Sprintf("fzmnew/ctp/ctp_IFMI/%d/%04d%02d%02d.M1.gz", tm.Year(), tm.Year(), tm.Month(), tm.Day())
  87. if _, err := os.Stat(fname); !os.IsNotExist(err) {
  88. return true
  89. } else {
  90. log.Println("not exist:", fname)
  91. return false
  92. }
  93. }
  94. func fileCopy(insIdSour, period string, t int64) {
  95. tm := time.Unix(t/1000, 0)
  96. dataDir := fmt.Sprintf("./fzmnew/ctp/ctp_IFMI/%d", tm.Year())
  97. os.MkdirAll(dataDir, 0777)
  98. fileNameDest := fmt.Sprintf("./fzmnew/ctp/ctp_IFMI/%d/%04d%02d%02d.%s.gz", tm.Year(), tm.Year(), tm.Month(), tm.Day(), period)
  99. fileNameSour := fmt.Sprintf("./fzmnew/ctp/%s/%d/%04d%02d%02d.%s.gz", insIdSour, tm.Year(), tm.Year(), tm.Month(), tm.Day(), period)
  100. fileSour, err := os.Open(fileNameSour)
  101. if err != nil {
  102. log.Println("os.Open", fileNameDest, err)
  103. return
  104. }
  105. defer fileSour.Close()
  106. fileDest, err := os.Create(fileNameDest)
  107. if err != nil {
  108. log.Println("os.Create", fileNameDest, err)
  109. return
  110. }
  111. defer fileDest.Close()
  112. _, err = io.Copy(fileDest, fileSour)
  113. if err != nil {
  114. log.Println(fileNameDest, err)
  115. return
  116. }
  117. }
  118. func combinEx(filename string, candles []market.Candle) ([]market.Candle, error) {
  119. buf, err := market.ReadCandleFile(filename)
  120. if err != nil {
  121. return candles, err
  122. }
  123. candles = append(buf, candles[:]...)
  124. sort.Sort(byTime(candles))
  125. return candles, nil
  126. }