ctp_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package gocctp
  3. import (
  4. "log"
  5. "testing"
  6. "time"
  7. )
  8. type MyMdSpi struct {
  9. SpiBase
  10. }
  11. func (spi *MyMdSpi) OnRspUserLogin(errMsg string) {
  12. log.Println("OnRspUserLogin return:", errMsg)
  13. }
  14. func (spi *MyMdSpi) OnRtnDepthMarketData(pDepthMarketData *CThostFtdcDepthMarketDataField) {
  15. log.Println("OnRtnDepthMarketData return:", pDepthMarketData.InstrumentID, pDepthMarketData.AskPrice1, pDepthMarketData.BidPrice1)
  16. }
  17. type MyTdSpi struct {
  18. SpiBase
  19. insMap map[string]*CThostFtdcInstrumentField
  20. insAllDone chan bool
  21. }
  22. func (spi *MyTdSpi) OnRspUserLogin(errMsg string) {
  23. log.Println("OnRspUserLogin return:", errMsg)
  24. }
  25. func (spi *MyTdSpi) OnRspQryExchange(field *CThostFtdcExchangeField, errMsg string, isLast bool) {
  26. log.Println(field.ExchangeName, errMsg, isLast)
  27. }
  28. func (spi *MyTdSpi) OnRspQryInstrument(field *CThostFtdcInstrumentField, errMsg string, isLast bool) {
  29. log.Println(field.InstrumentName, errMsg, isLast)
  30. spi.insMap[field.InstrumentID.String()] = field
  31. if isLast {
  32. spi.insAllDone <- true
  33. }
  34. }
  35. func TestMdSubscribe(t *testing.T) {
  36. mdspi := &MyMdSpi{}
  37. mdApi := NewMdApi(mdspi)
  38. mdApi.Login("tcp://ctp1-md5.citicsf.com:41213", "66666", "1011000", "317496")
  39. tdspi := &MyTdSpi{insMap: make(map[string]*CThostFtdcInstrumentField), insAllDone: make(chan bool)}
  40. tdApi := NewTdApi(tdspi)
  41. tdApi.Login("tcp://ctp1-front5.citicsf.com:41205", "66666", "1011000", "317496", "", "")
  42. tdApi.QryExchange("")
  43. tdApi.QryInstrument("")
  44. <-tdspi.insAllDone
  45. insIds := []string{}
  46. for insId, _ := range tdspi.insMap {
  47. insIds = append(insIds, insId)
  48. }
  49. log.Println(insIds)
  50. mdApi.SubscribeMarketData(insIds)
  51. time.Sleep(time.Minute)
  52. }