12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package main
- import (
- "gocctp"
- "log"
- "time"
- )
- type MyMdSpi struct {
- gocctp.SpiBase
- }
- func (spi *MyMdSpi) OnRspUserLogin(errMsg string) {
- log.Println("OnRspUserLogin return:", errMsg)
- }
- func (spi *MyMdSpi) OnRtnDepthMarketData(pDepthMarketData *gocctp.CThostFtdcDepthMarketDataField) {
- if pDepthMarketData == nil {
- return
- }
- log.Println("OnRtnDepthMarketData return:", pDepthMarketData.InstrumentID, pDepthMarketData.AskPrice1, pDepthMarketData.BidPrice1)
- }
- type MyTdSpi struct {
- gocctp.SpiBase
- insMap map[string]*gocctp.CThostFtdcInstrumentField
- tdapi *gocctp.TdApi
- mdapi *gocctp.MdApi
- exOkCh chan bool
- }
- func (spi *MyTdSpi) OnRspUserLogin(errMsg string) {
- log.Println("OnRspUserLogin return:", errMsg)
- spi.tdapi.QryInstrument("")
- }
- func (spi *MyTdSpi) OnRspQryExchange(field *gocctp.CThostFtdcExchangeField, errMsg string, isLast bool) {
- log.Println(field.ExchangeID, errMsg, isLast)
- if isLast {
- insIds := []string{}
- for insId, _ := range spi.insMap {
- insIds = append(insIds, insId)
- }
- // spi.mdapi.SubscribeMarketData(insIds)
- }
- }
- func (spi *MyTdSpi) OnRspQryInstrument(field *gocctp.CThostFtdcInstrumentField, errMsg string, isLast bool) {
- log.Println(field, errMsg, isLast)
- sid := field.InstrumentID.String()
- spi.insMap[sid] = field
- if isLast {
- time.Sleep(time.Millisecond * 500)
- go spi.tdapi.QryExchange("")
- }
- }
- func main() {
- mdspi := &MyMdSpi{}
- mdApi := gocctp.NewMdApi(mdspi)
- mdApi.Login("tcp://ctp1-md5.citicsf.com:41213", "66666", "1011000", "317496")
- log.Println("md login")
- tdspi := &MyTdSpi{insMap: make(map[string]*gocctp.CThostFtdcInstrumentField), exOkCh: make(chan bool), mdapi: mdApi}
- tdApi := gocctp.NewTdApi(tdspi)
- tdspi.tdapi = tdApi
- tdApi.Login("tcp://ctp1-front5.citicsf.com:41205", "66666", "1011000", "317496", "", "")
- log.Println("td login")
- for {
- time.Sleep(time.Minute)
- }
- }
|