main.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. //
  3. //
  4. package main
  5. import (
  6. "bufio"
  7. "compress/gzip"
  8. "encoding/binary"
  9. "flag"
  10. "log"
  11. "os"
  12. "strings"
  13. "tickserver/server/market"
  14. )
  15. // var file = flag.String("", "", "open .TK or .Mn file")
  16. func main() {
  17. flag.Parse()
  18. n := flag.NArg()
  19. if n != 1 {
  20. log.Println("must input file name", n)
  21. return
  22. }
  23. file := flag.Arg(0)
  24. log.Println(file)
  25. filelow := strings.ToLower(file)
  26. if strings.Contains(filelow, ".tk") {
  27. log.Fatal(simplifyData(file))
  28. } else {
  29. log.Println("%s maybe not a tick file", file)
  30. }
  31. }
  32. func simplifyData(fname string) error {
  33. ticks, err := market.ReadTickFile(fname)
  34. if err != nil {
  35. return err
  36. }
  37. fnameSimplify := fname + ".simplify"
  38. fps, err := os.Create(fnameSimplify)
  39. if err != nil {
  40. return err
  41. }
  42. gw := gzip.NewWriter(fps)
  43. bw := bufio.NewWriter(gw)
  44. var lasttick market.Tick
  45. for _, x := range ticks {
  46. if x.Price == lasttick.Price && x.Ask[0] == lasttick.Ask[0] && x.Bid[0] == lasttick.Bid[0] {
  47. continue
  48. } else {
  49. binary.Write(bw, binary.LittleEndian, x)
  50. lasttick = x
  51. }
  52. }
  53. bw.Flush()
  54. gw.Close()
  55. fps.Close()
  56. os.Remove(fname)
  57. os.Rename(fnameSimplify, fname)
  58. return nil
  59. }