market_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package market
  3. // 本文件测试martket相关函数
  4. import (
  5. // "compress/gzip"
  6. "io"
  7. "log"
  8. // "market/lmax"
  9. "os"
  10. "testing"
  11. )
  12. func TestReadCtpCandleFile(t *testing.T) {
  13. fname := "candle2.M1"
  14. r, err := os.Open(fname)
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. defer r.Close()
  19. for {
  20. c, err := ReadCandleBinary(r)
  21. if err != nil {
  22. if err != io.EOF {
  23. t.Fatal(err)
  24. }
  25. break
  26. }
  27. log.Println(c, getTime(c.Timestamp))
  28. // candles = append(candles, *c)
  29. }
  30. }
  31. func TestReadCandleFile(t *testing.T) {
  32. // use gzip compress
  33. fname := "candle.M1"
  34. // r, err := os.Open(fname)
  35. // if err != nil {
  36. // t.Fatal(err)
  37. // }
  38. // defer r.Close()
  39. // fname = "testdata/candle.M1.zip"
  40. // w, err := os.Create(fname)
  41. // if err != nil {
  42. // t.Fatal(err)
  43. // }
  44. // defer w.Close()
  45. // zw := gzip.NewWriter(w)
  46. // _, err = io.Copy(zw, r)
  47. // if err != nil {
  48. // t.Fatal(err)
  49. // }
  50. // // zw.Flush()
  51. // zw.Close()
  52. testReadCandleFile(fname, t)
  53. }
  54. func testReadCandleFile(fname string, t *testing.T) {
  55. candles, err := readCandleFile(fname)
  56. log.Println("@@@@@@@:", len(candles))
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. log.Println("@@@@@@:", len(candles))
  61. for _, c := range candles {
  62. log.Println(getTime(c.Timestamp), c)
  63. }
  64. }
  65. func TestToH1(t *testing.T) {
  66. fnames := []string{
  67. // "testdata/2014-07-01-21-00-00-000-4001-ask-minute-aggregation.M1",
  68. "testdata/2014-07-02-21-00-00-000-4001-ask-minute-aggregation.M1",
  69. // "testdata/2014-07-03-21-00-00-000-4001-ask-minute-aggregation.M1",
  70. }
  71. h1file := "testdata/candle.H1"
  72. for _, fname := range fnames {
  73. candles, err := readCandleFile(fname)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. r := &CandleBuf{candles: candles}
  78. buf, err := convPeriod(r, H1)
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. candles, err = readCandleFile(h1file)
  83. if err != nil {
  84. log.Println(err)
  85. }
  86. candles = append(candles, buf...)
  87. w, err := os.Create(h1file)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. err = zipCBuf(w, candles)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. w.Close()
  96. }
  97. candles, err := readCandleFile(h1file)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. for _, c := range candles {
  102. log.Println(getTime(c.Timestamp))
  103. }
  104. }