ds_sina_future.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package tick
  2. import "strings"
  3. import "errors"
  4. import "strconv"
  5. import "time"
  6. import "log"
  7. type SinaFutureDS struct {
  8. *DSBase
  9. }
  10. var sinaFutureSymbols []string
  11. func init() {
  12. drivers[SinaFuture] = newSinaFutureDS
  13. }
  14. func newSinaFutureDS(conf *DsConf) (DataSource, error) {
  15. return &SinaFutureDS{
  16. DSBase: NewDsBase(conf), // lmax 自己下载历史数据, 所以参数db==nil
  17. }, nil
  18. }
  19. func (lds *SinaFutureDS) Name() string {
  20. return SinaFuture
  21. }
  22. func (lds *SinaFutureDS) Run() {
  23. if !lds.conf.Run {
  24. log.Println("SinaFutureDS.run config NOT run")
  25. return
  26. }
  27. log.Println("SinaFutureDS.run")
  28. for i, _ := range SinaFutureSymbols {
  29. sinaFutureSymbols = append(sinaFutureSymbols, i)
  30. }
  31. fn := func() {
  32. url := getfetchurl()
  33. lastMarket = make(map[string]string)
  34. datach := make(chan []string, 20)
  35. // 登录产生新的Session
  36. go func() {
  37. for {
  38. fetchurl(IntSinaFuture, url, datach)
  39. time.Sleep(time.Millisecond * 200)
  40. }
  41. }()
  42. for {
  43. item := <-datach
  44. //log.Println("sinafuture parse begin")
  45. newdata := getnewdata(IntSinaFuture, item)
  46. for i := 0; i < len(newdata); i++ {
  47. //log.Println("sinafuture save begin")
  48. lds.Save(newdata[i])
  49. }
  50. }
  51. }
  52. fn()
  53. }
  54. func getfetchurl() string {
  55. baseurl := "http://hq.sinajs.cn/list="
  56. query := strings.Join(sinaFutureSymbols, ",")
  57. return baseurl + query
  58. }
  59. func parsemarketfuture(name string, data string) (*Market, error) {
  60. data = strings.Trim(data, "\";\r\n")
  61. datas := strings.Split(data, ",")
  62. if len(datas) < 27 {
  63. return nil, errors.New("makret data error")
  64. }
  65. var market Market
  66. var err error
  67. market.Open, err = strconv.ParseFloat(datas[2], 64)
  68. if err != nil {
  69. return nil, err
  70. }
  71. market.LastPrice, err = strconv.ParseFloat(datas[8], 64)
  72. if err != nil {
  73. return nil, err
  74. }
  75. market.Close, err = strconv.ParseFloat(datas[5], 64)
  76. if err != nil {
  77. return nil, err
  78. }
  79. market.High, err = strconv.ParseFloat(datas[3], 64)
  80. if err != nil {
  81. return nil, err
  82. }
  83. market.Low, err = strconv.ParseFloat(datas[4], 64)
  84. if err != nil {
  85. return nil, err
  86. }
  87. //market.AllVolume, err = strconv.ParseFloat(datas[8], 64)
  88. //if err != nil {
  89. //return nil, err
  90. //}
  91. //market.AllAmount, err = strconv.ParseFloat(datas[9], 64)
  92. //if err != nil {
  93. //return nil, err
  94. //}
  95. bids := make([]PP, 5)
  96. //bids[0][1], err = strconv.ParseFloat(datas[10], 64)
  97. //if err != nil {
  98. //return nil, err
  99. //}
  100. bids[0][0], err = strconv.ParseFloat(datas[7], 64)
  101. if err != nil {
  102. return nil, err
  103. }
  104. //bids[1][1], err = strconv.ParseFloat(datas[12], 64)
  105. //if err != nil {
  106. //return nil, err
  107. //}
  108. //bids[1][0], err = strconv.ParseFloat(datas[13], 64)
  109. //if err != nil {
  110. //return nil, err
  111. //}
  112. //bids[2][1], err = strconv.ParseFloat(datas[14], 64)
  113. //if err != nil {
  114. //return nil, err
  115. //}
  116. //bids[2][0], err = strconv.ParseFloat(datas[15], 64)
  117. //if err != nil {
  118. //return nil, err
  119. //}
  120. //bids[3][1], err = strconv.ParseFloat(datas[16], 64)
  121. //if err != nil {
  122. //return nil, err
  123. //}
  124. //bids[3][0], err = strconv.ParseFloat(datas[17], 64)
  125. //if err != nil {
  126. //return nil, err
  127. //}
  128. //bids[4][1], err = strconv.ParseFloat(datas[18], 64)
  129. //if err != nil {
  130. //return nil, err
  131. //}
  132. //bids[4][0], err = strconv.ParseFloat(datas[19], 64)
  133. //if err != nil {
  134. //return nil, err
  135. //}
  136. market.Bids = bids
  137. asks := make([]PP, 5)
  138. //asks[0][1], err = strconv.ParseFloat(datas[6], 64)
  139. //if err != nil {
  140. //return nil, err
  141. //}
  142. asks[0][0], err = strconv.ParseFloat(datas[6], 64)
  143. if err != nil {
  144. return nil, err
  145. }
  146. //asks[1][1], err = strconv.ParseFloat(datas[22], 64)
  147. //if err != nil {
  148. //return nil, err
  149. //}
  150. //asks[1][0], err = strconv.ParseFloat(datas[23], 64)
  151. //if err != nil {
  152. //return nil, err
  153. //}
  154. //asks[2][1], err = strconv.ParseFloat(datas[24], 64)
  155. //if err != nil {
  156. //return nil, err
  157. //}
  158. //asks[2][0], err = strconv.ParseFloat(datas[25], 64)
  159. //if err != nil {
  160. //return nil, err
  161. //}
  162. //asks[3][1], err = strconv.ParseFloat(datas[26], 64)
  163. //if err != nil {
  164. //return nil, err
  165. //}
  166. //asks[3][0], err = strconv.ParseFloat(datas[27], 64)
  167. //if err != nil {
  168. //return nil, err
  169. //}
  170. //asks[4][1], err = strconv.ParseFloat(datas[28], 64)
  171. //if err != nil {
  172. //return nil, err
  173. //}
  174. //asks[4][0], err = strconv.ParseFloat(datas[29], 64)
  175. //if err != nil {
  176. //return nil, err
  177. //}
  178. market.Asks = asks
  179. //t, err := time.Parse(time.RFC3339, datas[30]+"T"+datas[31]+"+08:00")
  180. //if err != nil {
  181. //return nil, err
  182. //}
  183. //market.Timestamp = t.Unix() * 1000
  184. market.Timestamp = time.Now().Unix() * 1000
  185. //name = name[2:]
  186. //market.InsId, err = strconv.ParseInt(name, 10, 64)
  187. //if err != nil {
  188. //return nil, err
  189. //}
  190. var id int
  191. var ok bool
  192. if id, ok = SinaFutureSymbols[name]; !ok {
  193. return nil, err
  194. }
  195. market.InsId = 1000000 + int64(id)
  196. market.Type = IntSinaFuture
  197. return &market, nil
  198. }