main.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "gocctp"
  4. "log"
  5. "time"
  6. )
  7. type MyMdSpi struct {
  8. gocctp.SpiBase
  9. }
  10. func (spi *MyMdSpi) OnRspUserLogin(errMsg string) {
  11. log.Println("OnRspUserLogin return:", errMsg)
  12. }
  13. func (spi *MyMdSpi) OnRtnDepthMarketData(pDepthMarketData *gocctp.CThostFtdcDepthMarketDataField) {
  14. if pDepthMarketData == nil {
  15. return
  16. }
  17. log.Println("OnRtnDepthMarketData return:", pDepthMarketData.InstrumentID, pDepthMarketData.AskPrice1, pDepthMarketData.BidPrice1)
  18. }
  19. type MyTdSpi struct {
  20. gocctp.SpiBase
  21. insMap map[string]*gocctp.CThostFtdcInstrumentField
  22. tdapi *gocctp.TdApi
  23. mdapi *gocctp.MdApi
  24. exOkCh chan bool
  25. }
  26. func (spi *MyTdSpi) OnRspUserLogin(errMsg string) {
  27. log.Println("OnRspUserLogin return:", errMsg)
  28. spi.tdapi.QryInstrument("")
  29. }
  30. func (spi *MyTdSpi) OnRspQryExchange(field *gocctp.CThostFtdcExchangeField, errMsg string, isLast bool) {
  31. log.Println(field.ExchangeID, errMsg, isLast)
  32. if isLast {
  33. insIds := []string{}
  34. for insId, _ := range spi.insMap {
  35. insIds = append(insIds, insId)
  36. }
  37. // spi.mdapi.SubscribeMarketData(insIds)
  38. }
  39. }
  40. func (spi *MyTdSpi) OnRspQryInstrument(field *gocctp.CThostFtdcInstrumentField, errMsg string, isLast bool) {
  41. log.Println(field, errMsg, isLast)
  42. sid := field.InstrumentID.String()
  43. spi.insMap[sid] = field
  44. if isLast {
  45. time.Sleep(time.Millisecond * 500)
  46. go spi.tdapi.QryExchange("")
  47. }
  48. }
  49. func main() {
  50. mdspi := &MyMdSpi{}
  51. mdApi := gocctp.NewMdApi(mdspi)
  52. mdApi.Login("tcp://ctp1-md5.citicsf.com:41213", "66666", "1011000", "317496")
  53. log.Println("md login")
  54. tdspi := &MyTdSpi{insMap: make(map[string]*gocctp.CThostFtdcInstrumentField), exOkCh: make(chan bool), mdapi: mdApi}
  55. tdApi := gocctp.NewTdApi(tdspi)
  56. tdspi.tdapi = tdApi
  57. tdApi.Login("tcp://ctp1-front5.citicsf.com:41205", "66666", "1011000", "317496", "", "")
  58. log.Println("td login")
  59. for {
  60. time.Sleep(time.Minute)
  61. }
  62. }