代码拉取完成,页面将自动刷新
同步操作将从 海风/goctp 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package goctp
import (
"os"
"sync"
"gitee.com/haifengat/goctp/ctpdefine"
)
// HFQuote 行情接口
type HFQuote struct {
IsLogin bool
InvestorID string
BrokerID string
ReqConnect ReqConnectType
ReleaseAPI ReleaseAPIType
ReqUserLogin ReqUserLoginType
ReqSubscript ReqSubscriptType
onFrontConnected OnFrontConnectedType
onFrontDisConnected OnFrontDisConnectedType
onRspUserLogin OnRspUserLoginType
onTick OnTickType
Ticks sync.Map // 合约:TickField
}
type ReqSubscriptType func(...string)
func (q *HFQuote) Init() {
// 执行目录下创建 log目录
_, err := os.Stat("log")
if err != nil {
os.Mkdir("log", os.ModePerm)
}
}
func (q *HFQuote) Release() {
q.IsLogin = false
q.ReleaseAPI()
q.FrontDisConnected(0) // 需手动触发
}
// ReqLogin 登录
func (q *HFQuote) ReqLogin(investor, pwd, broker string) {
q.InvestorID = investor
q.BrokerID = broker
f := ctpdefine.CThostFtdcReqUserLoginField{}
copy(f.UserID[:], q.InvestorID)
copy(f.BrokerID[:], q.BrokerID)
copy(f.Password[:], pwd)
copy(f.UserProductInfo[:], "@HF")
q.ReqUserLogin(&f, 1)
}
// RegOnFrontConnected 注册前置响应
func (q *HFQuote) RegOnFrontConnected(on OnFrontConnectedType) {
q.onFrontConnected = on
}
// RegOnFrontDisConnected 注册连接响应
func (q *HFQuote) RegOnFrontDisConnected(on OnFrontDisConnectedType) {
q.onFrontDisConnected = on
}
// RegOnRspUserLogin 注册登录响应
func (q *HFQuote) RegOnRspUserLogin(on OnRspUserLoginType) {
q.onRspUserLogin = on
}
// RegOnTick 注册行情响应
func (q *HFQuote) RegOnTick(on OnTickType) {
q.onTick = on
}
func (q *HFQuote) RtnDepthMarketData(dataField *ctpdefine.CThostFtdcDepthMarketDataField) {
tick := TickField{
TradingDay: Bytes2String(dataField.TradingDay[:]),
InstrumentID: Bytes2String(dataField.InstrumentID[:]),
ExchangeID: Bytes2String(dataField.ExchangeID[:]),
LastPrice: float64(dataField.LastPrice),
OpenPrice: float64(dataField.OpenPrice),
HighestPrice: float64(dataField.HighestPrice),
LowestPrice: float64(dataField.LowestPrice),
Volume: int(dataField.Volume),
Turnover: float64(dataField.Turnover),
OpenInterest: float64(dataField.OpenInterest),
ClosePrice: float64(dataField.ClosePrice),
SettlementPrice: float64(dataField.SettlementPrice),
UpperLimitPrice: float64(dataField.UpperLimitPrice),
LowerLimitPrice: float64(dataField.LowerLimitPrice),
CurrDelta: float64(dataField.CurrDelta),
UpdateTime: Bytes2String(dataField.UpdateTime[:]),
UpdateMillisec: int(dataField.UpdateMillisec),
BidPrice1: float64(dataField.BidPrice1),
BidVolume1: int(dataField.BidVolume1),
AskPrice1: float64(dataField.AskPrice1),
AskVolume1: int(dataField.AskVolume1),
BidPrice2: float64(dataField.BidPrice2),
BidVolume2: int(dataField.BidVolume2),
AskPrice2: float64(dataField.AskPrice2),
AskVolume2: int(dataField.AskVolume2),
BidPrice3: float64(dataField.BidPrice3),
BidVolume3: int(dataField.BidVolume3),
AskPrice3: float64(dataField.AskPrice3),
AskVolume3: int(dataField.AskVolume3),
BidPrice4: float64(dataField.BidPrice4),
BidVolume4: int(dataField.BidVolume4),
AskPrice4: float64(dataField.AskPrice4),
AskVolume4: int(dataField.AskVolume4),
BidPrice5: float64(dataField.BidPrice5),
BidVolume5: int(dataField.BidVolume5),
AskPrice5: float64(dataField.AskPrice5),
AskVolume5: int(dataField.AskVolume5),
AveragePrice: float64(dataField.AveragePrice),
ActionDay: Bytes2String(dataField.ActionDay[:]),
}
q.Ticks.Store(tick.InstrumentID, &tick)
if q.onTick == nil {
return
}
q.onTick(&tick)
}
func (q *HFQuote) RspUserLogin(loginField *ctpdefine.CThostFtdcRspUserLoginField, infoField *ctpdefine.CThostFtdcRspInfoField) {
q.IsLogin = infoField.ErrorID == 0
if q.onRspUserLogin == nil {
return
}
q.onRspUserLogin(&RspUserLoginField{
TradingDay: string(loginField.TradingDay[:]),
LoginTime: string(loginField.LoginTime[:]),
BrokerID: string(loginField.BrokerID[:]),
UserID: string(loginField.UserID[:]),
FrontID: int(loginField.FrontID),
SessionID: int(loginField.SessionID),
MaxOrderRef: string(loginField.MaxOrderRef[:]),
}, &RspInfoField{
ErrorID: int(infoField.ErrorID),
ErrorMsg: Bytes2String(infoField.ErrorMsg[:]),
})
}
func (q *HFQuote) FrontConnected() {
if q.onFrontConnected != nil {
q.onFrontConnected()
}
}
func (q *HFQuote) FrontDisConnected(reason int) {
if q.onFrontDisConnected != nil {
q.onFrontDisConnected(reason)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。