ds_huobi.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package tick
  3. // 本文件实现bityuan数据源接口, 实时数据和历史数据的获取和保存
  4. import (
  5. "log"
  6. "time"
  7. "tickserver/markinfo"
  8. "tickserver/server/market"
  9. _ "github.com/go-sql-driver/mysql"
  10. socketio "github.com/googollee/go-socket.io"
  11. )
  12. type HBMarketOverview HBLastTimeLine
  13. type HBTradeDetail HBLastTimeLine
  14. type HBMarketDetail HBLastTimeLine
  15. type HBMarketDepthTopDiff HBLastTimeLine
  16. type HBMarketDepthDiff struct {
  17. SymbolId string `json:"symbolId"`
  18. PushType string `json:"pushType"`
  19. Percent string `json:"percent"`
  20. }
  21. type HBLastKLine struct {
  22. SymbolId string `json:"symbolId"`
  23. PushType string `json:"pushType"`
  24. Period string `json:"period"`
  25. }
  26. type HBLastTimeLine struct {
  27. SymbolId string `json:"symbolId"`
  28. PushType string `json:"pushType"`
  29. }
  30. type HBSymbolList struct {
  31. //LastTimeLine []HBLastTimeLine `json:"lastTimeLine"`
  32. //LastKLine []HBLastKLine `json:"lastKLine"`
  33. //MarketDepthDiff []HBMarketDepthDiff `json:"marketDepthDiff"`
  34. //MarketDepthTopDiff []HBMarketDepthTopDiff `json:"marketDepthTopDiff"`
  35. //MarketDetail []HBMarketDetail `json:"marketDetail"`
  36. //TradeDetail []HBTradeDetail `json:"tradeDetail"`
  37. MarketOverview []HBMarketOverview `json:"marketOverview"`
  38. }
  39. type HBCmd struct {
  40. SymbolList HBSymbolList `json:"symbolList"`
  41. Version int `json:"version"`
  42. MsgType string `json:"msgType"`
  43. RequestIndex int `json:"requestIndex"`
  44. }
  45. type MarketOverviewPL struct {
  46. SymbolId string `json:"symbolId"`
  47. PriceNew float64 `json:"priceNew"`
  48. PriceHigh float64 `json:"priceHigh"`
  49. TotalVolume float64 `json:"totalVolume"`
  50. TotalAmount float64 `json:"totalAmount"`
  51. PriceOpen float64 `json:"priceOpen"`
  52. PriceLow float64 `json:"priceLow"`
  53. PriceAsk float64 `json:"priceAsk"`
  54. PriceBid float64 `json:"priceBid"`
  55. }
  56. type HBResp struct {
  57. Version int `json:"version"`
  58. MsgType string `json:"msgType"`
  59. SymbolId string `json:"symbolId"`
  60. PayLoad MarketOverviewPL `json:"payload"`
  61. }
  62. // HuobiDS实现了dataSource接口, 并对bityuan的历史数据和实时数据保存
  63. type HuobiDS struct {
  64. *DSBase
  65. conf *DsConf
  66. //client *socketio.Client
  67. }
  68. var lastVolume float64
  69. var huobiInss = []int{
  70. markinfo.BTCCNY,
  71. markinfo.LTCCNY,
  72. }
  73. func init() {
  74. drivers[Huobi] = newHuobiDS
  75. }
  76. func (hds *HuobiDS) connectHuobi(count int) (*socketio.Client, error) {
  77. var err error
  78. var client *socketio.Client
  79. for i := 0; i < count; i++ {
  80. client, err = socketio.Dial("http://hq.huobi.com:80")
  81. if err != nil {
  82. log.Println("newHuobiDS", err)
  83. time.Sleep(time.Second * 5)
  84. } else {
  85. log.Println("success")
  86. break
  87. }
  88. }
  89. return client, err
  90. }
  91. func subscribeHuobi(ns *socketio.NameSpace) error {
  92. var err error
  93. var hbcmd HBCmd
  94. hbcmd.MsgType = "reqMsgSubscribe"
  95. hbcmd.RequestIndex = 1404103038520
  96. hbcmd.Version = 1
  97. hbcmd.SymbolList = HBSymbolList{
  98. /*LastTimeLine: []HBLastTimeLine{{SymbolId: "btccny", PushType: "pushLong"}},
  99. LastKLine: []HBLastKLine{
  100. {SymbolId: "btccny", PushType: "pushLong", Period: "1min"},
  101. {SymbolId: "btccny", PushType: "pushLong", Period: "5min"},
  102. {SymbolId: "btccny", PushType: "pushLong", Period: "15min"},
  103. {SymbolId: "btccny", PushType: "pushLong", Period: "30min"},
  104. {SymbolId: "btccny", PushType: "pushLong", Period: "60min"},
  105. {SymbolId: "btccny", PushType: "pushLong", Period: "1day"},
  106. {SymbolId: "btccny", PushType: "pushLong", Period: "1week"},
  107. {SymbolId: "btccny", PushType: "pushLong", Period: "1mon"},
  108. {SymbolId: "btccny", PushType: "pushLong", Period: "1year"},
  109. },
  110. MarketDepthDiff: []HBMarketDepthDiff{
  111. {SymbolId: "btccny", PushType: "pushLong", Percent: "10"},
  112. {SymbolId: "btccny", PushType: "pushLong", Percent: "20"},
  113. {SymbolId: "btccny", PushType: "pushLong", Percent: "50"},
  114. {SymbolId: "btccny", PushType: "pushLong", Percent: "80"},
  115. {SymbolId: "btccny", PushType: "pushLong", Percent: "100"},
  116. },
  117. MarketDepthTopDiff: []HBMarketDepthTopDiff{{SymbolId: "btccny", PushType: "pushLong"}},
  118. MarketDetail: []HBMarketDetail{{SymbolId: "btccny", PushType: "pushLong"}},
  119. TradeDetail: []HBTradeDetail{{SymbolId: "btccny", PushType: "pushLong"}},*/
  120. MarketOverview: []HBMarketOverview{{SymbolId: "btccny", PushType: "pushLong"}},
  121. }
  122. err = ns.Emit("request", &hbcmd)
  123. if err != nil {
  124. return err
  125. }
  126. /*hbcmd.RequestIndex = 1414103038520
  127. hbcmd.SymbolList = HBSymbolList{
  128. MarketOverview: []HBMarketOverview{{SymbolId: "ltccny", PushType: "pushLong"}},
  129. }
  130. err = ns.Emit("request", &hbcmd)
  131. if err != nil {
  132. return err
  133. }*/
  134. return nil
  135. }
  136. func newHuobiDS(conf *DsConf) (DataSource, error) {
  137. hds := &HuobiDS{
  138. DSBase: NewDsBase(conf),
  139. conf: conf,
  140. }
  141. hds.insMap = huobiInsMap()
  142. return hds, nil
  143. }
  144. func (hds *HuobiDS) getData() {
  145. client, err := hds.connectHuobi(120)
  146. if err != nil {
  147. return
  148. }
  149. client.On("connect", func(ns *socketio.NameSpace) {
  150. log.Println("connected")
  151. err = subscribeHuobi(ns)
  152. if err != nil {
  153. log.Println("connect failed:", err)
  154. }
  155. })
  156. client.On("disconnect", func(ns *socketio.NameSpace) {
  157. log.Println("disconnect")
  158. })
  159. client.On("reconnect", func(ns *socketio.NameSpace) {
  160. log.Println("reconnect")
  161. })
  162. client.On("message", func(ns *socketio.NameSpace, message interface{}) {
  163. //log.Println("message")
  164. //log.Println(message)
  165. m1, ok1 := message.(map[string]interface{})
  166. if ok1 {
  167. m2, ok2 := m1["payload"]
  168. if ok2 {
  169. m3, ok3 := m2.(map[string]interface{})
  170. if ok3 {
  171. symbolId, ok4 := m3["symbolId"]
  172. if ok4 {
  173. symbol := symbolId.(string)
  174. priceNew := m3["priceNew"].(float64)
  175. priceHigh := m3["priceHigh"].(float64)
  176. totalVolume := m3["totalVolume"].(float64)
  177. totalAmount := m3["totalAmount"].(float64)
  178. priceOpen := m3["priceOpen"].(float64)
  179. priceLow := m3["priceLow"].(float64)
  180. priceAsk := m3["priceAsk"].(float64)
  181. priceBid := m3["priceBid"].(float64)
  182. mk := &Market{}
  183. mk.Type = IntHuobi
  184. if symbol == "btccny" {
  185. mk.InsId = markinfo.BTCCNY
  186. }
  187. if symbol == "ltccny" {
  188. mk.InsId = markinfo.LTCCNY
  189. }
  190. mk.Open = priceOpen
  191. mk.Low = priceLow
  192. mk.High = priceHigh
  193. mk.LastPrice = priceNew
  194. if totalVolume > lastVolume {
  195. mk.LastVolume = totalVolume - lastVolume
  196. } else {
  197. mk.LastVolume = totalVolume
  198. }
  199. mk.AllAmount = totalAmount
  200. mk.AllVolume = totalVolume
  201. var ask, bid PP
  202. ask[0] = priceAsk
  203. ask[1] = mk.LastVolume
  204. bid[0] = priceBid
  205. bid[1] = mk.LastVolume
  206. mk.Asks = append(mk.Asks, ask)
  207. mk.Bids = append(mk.Bids, bid)
  208. mk.Timestamp = time.Now().UnixNano() / (1000 * 1000)
  209. hds.Save(mk)
  210. }
  211. }
  212. }
  213. }
  214. })
  215. client.On("request", func(ns *socketio.NameSpace, request interface{}) {
  216. log.Println("request", request)
  217. })
  218. client.Run()
  219. }
  220. func (hds *HuobiDS) Name() string {
  221. return Huobi
  222. }
  223. func (hds *HuobiDS) Run() {
  224. for {
  225. log.Println("HuobiDS.Run")
  226. hds.getData()
  227. time.Sleep(5 * time.Second)
  228. log.Println("HuobiDS.End Run")
  229. }
  230. }
  231. func huobiInsMap() map[int64]*Instrument {
  232. insMap := make(map[int64]*Instrument)
  233. for _, id := range huobiInss {
  234. x, _ := markinfo.SymbolName(id)
  235. u, _ := markinfo.SymbolUint(x)
  236. ins := &Instrument{
  237. Id: int64(id),
  238. Name: x,
  239. ExId: Huobi,
  240. Type: market.Btcs,
  241. PriceInc: u,
  242. StartTime: time.Now().Unix() * 1000,
  243. }
  244. insMap[int64(id)] = ins
  245. }
  246. return insMap
  247. }