goctp

package module
v1.16.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2022 License: MIT Imports: 7 Imported by: 2

README

goctp

介绍

CTP 封装之 golang 版,支持 Windows Linux x64. 采用二次封装,将 C++封装成 C,并 export 所用函数.

软件架构

代码与 C#/PYTHON 版本逻辑相同

使用说明

源码测试

需卸载已安装 `go clean -i github.com/haifengat/goctp'

安装
go get github.com/haifengat/goctp
github 提交
sed -i "s#gitee.com#github.com#g" go.mod
sed -i "s#gitee.com#github.com#g" lnx/*.go
sed -i "s#gitee.com#github.com#g" win/*.go
sed -i "s#gitee.com#github.com#g" demo/*.go
# 提取最后一次提交的说明
export comment=$(git log --pretty=%s -1) ver=v0.2.2
git commit -am $comment
git tag -a $ver -m $comment
git push github && git push github $ver
# 恢复为gitee状态
sed -i "s#github.com#gitee.com#g" go.mod
sed -i "s#github.com#gitee.com#g" lnx/*.go
sed -i "s#github.com#gitee.com#g" win/*.go
sed -i "s#github.com#gitee.com#g" demo/*.go
示例

https://github.com/haifengat/goctp/raw/master/demo/main.go

编译
linux

复制所有 so 文件到系统 lib 下,或把当前路径加入 LD_LIBRARY_PATH 中.

windows

编译后复制 dll 到应用程序目录下即可

QA

linux 中 quote 与 trade 不能同时载入的问题

经测试,在 trade 创建子目录 test_quote 并载入 quote 测试代码可行。(test_quote 下放 quote 代码,test_trade 下放 trade 代码,亦报错)

解决

从 python 项目中复制 quote.h quote.cpp 过来,修改所有函数和回调函数,增加前缀 q. 重新编译 ctp_quote.so

cd lnx && g++ -shared -fPIC -Wl,-rpath . -o ./libctp_quote.so ../generate/quote.cpp  thostmduserapi_se.so && cd ..
cd lnx && g++ -shared -fPIC -Wl,-rpath . -o ./libctp_trade.so ../generate/trade.cpp  thosttraderapi_se.so && cd ..
VSCode launch.json 配置
{
  // 使用 IntelliSense 了解相关属性。
  // 悬停以查看现有属性的描述。
  // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${workspaceFolder}/demo/",
      "cwd": "${workspaceFolder}",
      "env": { "LD_LIBRARY_PATH": "${workspaceFolder}/go_ctp/lib64/" },
      "args": []
    }
  ]
}

Documentation

Index

Constants

View Source
const (
	// 今日持仓
	PositionDateToday = '1'
	// 历史持仓
	PositionDateHistory = '2'
)

Variables

This section is empty.

Functions

func Bytes2String

func Bytes2String(t []byte) string

func CheckErr

func CheckErr(err error)

func Contain

func Contain(obj interface{}, target interface{}) (bool, error)

判断obj是否在target中,target支持的类型 arrary, slice, map

func TemplateMap

func TemplateMap(templateString string, mapContent map[string]string) string

Types

type AccountField

type AccountField struct {
	// 上次质押金额
	PreMortgage float64
	// 上次存款额
	PreDeposit float64
	// 上次结算准备金
	PreBalance float64
	// 上次占用的保证金
	PreMargin float64
	// 利息基数
	InterestBase float64
	// 利息收入
	Interest float64
	// 入金金额
	Deposit float64
	// 出金金额
	Withdraw float64
	// 冻结的保证金
	FrozenMargin float64
	// 冻结的资金
	FrozenCash float64
	// 冻结的手续费
	FrozenCommission float64
	// 当前保证金总额
	CurrMargin float64
	// 资金差额
	CashIn float64
	// 手续费
	Commission float64
	// 平仓盈亏
	CloseProfit float64
	// 持仓盈亏
	PositionProfit float64
	// 期货结算准备金
	Balance float64
	// 可用资金
	Available float64
	// 可取资金
	WithdrawQuota float64
	// 基本准备金
	Reserve float64
	// 信用额度
	Credit float64
	// 质押金额
	Mortgage float64
	// 交易所保证金
	ExchangeMargin float64
	// 投资者交割保证金
	DeliveryMargin float64
	// 交易所交割保证金
	ExchangeDeliveryMargin float64
	// 保底期货结算准备金
	ReserveBalance float64
	// 币种代码
	CurrencyID string
	// 上次货币质入金额
	PreFundMortgageIn float64
	// 上次货币质出金额
	PreFundMortgageOut float64
	// 货币质入金额
	FundMortgageIn float64
	// 货币质出金额
	FundMortgageOut float64
	// 货币质押余额
	FundMortgageAvailable float64
	// 可质押货币金额
	MortgageableFund float64
}

资金账户

type BaseQuote

type BaseQuote interface {
	ReqConnect(addr string)
	ReqLogin(investor, pwd, broker string)
	Release()
	ReqSubscript(instrument string)

	RegOnFrontConnected(on OnFrontConnectedType)
	RegOnRspUserLogin(on OnRspUserLoginType)
	RegOnTick(on OnTickType)
}

type BaseTrade

type BaseTrade interface {
	ReqConnect(addr string)
	ReqLogin(investor, pwd, broker string)
	Release()

	ReqOrderInsert(instrument string, buySell DirectionType, openClose OffsetFlagType, price float64, volume int) string
	ReqOrderInsertMarket(instrument string, buySell DirectionType, openClose OffsetFlagType, price float64, volume int) string
	ReqOrderInsertFOK(instrument string, buySell DirectionType, openClose OffsetFlagType, price float64, volume int) string
	ReqOrderInsertFAK(instrument string, buySell DirectionType, openClose OffsetFlagType, price float64, volume int) string

	RegOnFrontConnected(on OnFrontConnectedType)
	RegOnFrontDisConnected(on OnFrontDisConnectedType)
	RegOnRspUserLogin(on OnRspUserLoginType)
	RegOnRtnOrder(on OnRtnOrderType)
	RegOnErrRtnOrder(on OnRtnErrOrderType)
	RegOnErrAction(on OnRtnOrderType)
	RegOnRtnCancel(on OnRtnOrderType)
	RegOnRtnTrade(on OnRtnTradeType)
	RegOnRtnInstrumentStatus(on OnRtnInstrumentStatusType)
}

type CombinationTypeType

type CombinationTypeType byte

组合类型

const (
	// 期货组合
	CombinationTypeFuture CombinationTypeType = '0'
	// 垂直价差BUL
	CombinationTypeBUL CombinationTypeType = '1'
	// 垂直价差BER
	CombinationTypeBER CombinationTypeType = '2'
	// 跨式组合
	CombinationTypeSTD CombinationTypeType = '3'
	// 宽跨式组合
	CombinationTypeSTG CombinationTypeType = '4'
	// 备兑组合
	CombinationTypePRT CombinationTypeType = '5'
	// 时间价差组合
	CombinationTypeCLD CombinationTypeType = '6'
)

type DirectionType

type DirectionType byte

买卖方向类型

const (
	// 买
	DirectionBuy DirectionType = '0'
	// 卖
	DirectionSell DirectionType = '1'
)

type HedgeFlagType

type HedgeFlagType byte

投机套保标志类型

const (
	// 投机
	HedgeFlagSpeculation HedgeFlagType = '1'
	// 套利
	HedgeFlagArbitrage HedgeFlagType = '2'
	// 套保
	HedgeFlagHedge HedgeFlagType = '3'
	// 做市商
	HedgeFlagMarketMaker HedgeFlagType = '5'
	// 第一腿投机第二腿套保 大商所专用
	HedgeFlagSpecHedge HedgeFlagType = '6'
	// 第一腿套保第二腿投机  大商所专用
	HedgeFlagHedgeSpec HedgeFlagType = '7'
)

type InstrumentField

type InstrumentField struct {
	// 合约代码
	InstrumentID string
	// 交易所代码
	ExchangeID string
	// 产品代码
	ProductID string
	// 产品类型
	ProductClass ProductClassType
	// 市价单最大下单量
	MaxMarketOrderVolume int
	// 市价单最小下单量
	MinMarketOrderVolume int
	// 限价单最大下单量
	MaxLimitOrderVolume int
	// 限价单最小下单量
	MinLimitOrderVolume int
	// 合约数量乘数
	VolumeMultiple int
	// 最小变动价位
	PriceTick float64
	// 持仓类型
	PositionType PositionTypeType
	// 是否使用大额单边保证金算法
	UseMaxMarginSideAlgorithm bool
	// 基础商品代码
	UnderlyingInstrID string
	// 执行价
	StrikePrice float64
	// 期权类型
	OptionsType OptionsTypeType
	// 合约基础商品乘数
	UnderlyingMultiple float64
	// 组合类型
	CombinationType CombinationTypeType
}

合约

type InstrumentStatus

type InstrumentStatus struct {
	// 交易所代码
	ExchangeID string
	// 合约代码
	InstrumentID string
	// 合约交易状态
	InstrumentStatus InstrumentStatusType
	// 进入本状态时间
	EnterTime string
}

InstrumentStatus 合约状态

type InstrumentStatusType

type InstrumentStatusType byte

合约交易状态类型

const (
	// 开盘前
	InstrumentStatusBeforeTrading InstrumentStatusType = '0'
	// 非交易
	InstrumentStatusNoTrading InstrumentStatusType = '1'
	// 连续交易
	InstrumentStatusContinous InstrumentStatusType = '2'
	// 集合竞价报单
	InstrumentStatusAuctionOrdering InstrumentStatusType = '3'
	// 集合竞价价格平衡
	InstrumentStatusAuctionBalance InstrumentStatusType = '4'
	// 集合竞价撮合
	InstrumentStatusAuctionMatch InstrumentStatusType = '5'
	// 收盘
	InstrumentStatusClosed InstrumentStatusType = '6'
)

type OffsetFlagType

type OffsetFlagType byte

开平标志类型

const (
	// 开仓
	OffsetFlagOpen OffsetFlagType = '0'
	// 平仓
	OffsetFlagClose OffsetFlagType = '1'
	// 强平
	OffsetFlagForceClose OffsetFlagType = '2'
	// 平今
	OffsetFlagCloseToday OffsetFlagType = '3'
	// 平昨
	OffsetFlagCloseYesterday OffsetFlagType = '4'
	// 强减
	OffsetFlagForceOff OffsetFlagType = '5'
	// 本地强平
	OffsetFlagLocalForceClose OffsetFlagType = '6'
)

type OnFrontConnectedType

type OnFrontConnectedType func()

公共-连接

type OnFrontDisConnectedType

type OnFrontDisConnectedType func(reason int)

公共-断开

type OnRspUserLoginType

type OnRspUserLoginType func(loginField *RspUserLoginField, info *RspInfoField)

公共-登录

type OnRtnAccountType added in v1.12.6

type OnRtnAccountType func(field *AccountField)

交易-账户权益变化

type OnRtnErrActionType

type OnRtnErrActionType func(orderID string, info *RspInfoField)

交易-错误撤单

type OnRtnErrOrderType

type OnRtnErrOrderType func(field *OrderField, info *RspInfoField)

交易-错误委托

type OnRtnFromBankToFutureByFuture

type OnRtnFromBankToFutureByFuture func(field *TransferField)

银转-银行->期货

type OnRtnFromFutureToBankByFuture

type OnRtnFromFutureToBankByFuture func(field *TransferField)

银转-期货->银行

type OnRtnInstrumentStatusType

type OnRtnInstrumentStatusType func(field *InstrumentStatus)

交易-合约状态响应

type OnRtnInstrumentType added in v1.15.0

type OnRtnInstrumentType func(field *InstrumentField)

交易-合约信息

type OnRtnOrderType

type OnRtnOrderType func(field *OrderField)

交易-委托响应

type OnRtnPositionType added in v1.9.11

type OnRtnPositionType func(field *PositionField)

交易-持仓变化

type OnRtnTradeType

type OnRtnTradeType func(field *TradeField)

交易-成交响应

type OnTickType

type OnTickType func(tick *TickField)

行情

type OptionsTypeType

type OptionsTypeType byte

期权类型

const (
	// 看涨
	OptionsTypeCallOptions OptionsTypeType = '1'
	// 看跌
	OptionsTypePutOptions OptionsTypeType = '2'
)

type OrderField

type OrderField struct {
	// 合约代码
	InstrumentID string
	// 报单引用
	OrderRef string
	// 买卖方向
	Direction DirectionType
	// 组合开平标志
	OffsetFlag OffsetFlagType
	// 组合投机套保标志
	HedgeFlag HedgeFlagType
	// 价格
	LimitPrice float64
	// 数量
	VolumeTotalOriginal int
	// 交易所代码
	ExchangeID string
	// 请求ID
	RequestID int
	// 报单编号
	OrderSysID string
	// 报单状态
	OrderStatus OrderStatusType
	// 今成交数量
	VolumeTraded int
	// 剩余数量
	VolumeLeft int
	// 报单日期
	InsertDate string
	// 委托时间
	InsertTime string
	// 撤销时间
	CancelTime string
	// 前置编号
	FrontID int
	// 会话编号
	SessionID int
	// 状态信息
	StatusMsg string
	// 是否本次登录后的委托
	IsLocal bool
	// 成交时间(有OnTrade更新)
	LastTradeTime string
}

报单

type OrderStatusType

type OrderStatusType byte

报单状态类型

const (
	// 全部成交
	OrderStatusAllTraded OrderStatusType = '0'
	// 部分成交还在队列中
	OrderStatusPartTradedQueueing OrderStatusType = '1'
	// 部分成交不在队列中
	OrderStatusPartTradedNotQueueing OrderStatusType = '2'
	// 未成交还在队列中
	OrderStatusNoTradeQueueing OrderStatusType = '3'
	// 未成交不在队列中
	OrderStatusNoTradeNotQueueing OrderStatusType = '4'
	// 撤单
	OrderStatusCanceled OrderStatusType = '5'
	// 未知
	OrderStatusUnknown OrderStatusType = 'a'
	// 尚未触发
	OrderStatusNotTouched OrderStatusType = 'b'
	// 已触发
	OrderStatusTouched OrderStatusType = 'c'
)

type PosiDirectionType

type PosiDirectionType byte

持仓多空方向类型

const (
	// 净
	PosiDirectionNet PosiDirectionType = '1'
	// 多头
	PosiDirectionLong PosiDirectionType = '2'
	// 空头
	PosiDirectionShort PosiDirectionType = '3'
)

type PositionDateType added in v1.9.10

type PositionDateType byte

持仓日期类型

type PositionField

type PositionField struct {
	// 合约代码
	InstrumentID string
	// 持仓多空方向
	PositionDirection PosiDirectionType
	// 投机套保标志
	HedgeFlag HedgeFlagType
	// 持仓日期类型
	PositionDate PositionDateType
	// 上日持仓
	YdPosition int
	// 今日持仓
	Position int
	// 多头冻结
	LongFrozen int
	// 空头冻结
	ShortFrozen int
	// 开仓冻结金额
	LongFrozenAmount float64
	// 开仓冻结金额
	ShortFrozenAmount float64
	// 开仓量
	OpenVolume int
	// 平仓量
	CloseVolume int
	// 开仓金额
	OpenAmount float64
	// 平仓金额
	CloseAmount float64
	// 持仓成本
	PositionCost float64
	// 上次占用的保证金
	PreMargin float64
	// 占用的保证金
	UseMargin float64
	// 冻结的保证金
	FrozenMargin float64
	// 冻结的资金
	FrozenCash float64
	// 冻结的手续费
	FrozenCommission float64
	// 资金差额
	CashIn float64
	// 手续费
	Commission float64
	// 平仓盈亏
	CloseProfit float64
	// 持仓盈亏
	PositionProfit float64
	// 上次结算价
	PreSettlementPrice float64
	// 本次结算价
	SettlementPrice float64
	// 开仓成本
	OpenCost float64
	// 交易所保证金
	ExchangeMargin float64
	// 组合成交形成的持仓
	CombPosition int
	// 组合多头冻结
	CombLongFrozen int
	// 组合空头冻结
	CombShortFrozen int
	// 逐日盯市平仓盈亏
	CloseProfitByDate float64
	// 逐笔对冲平仓盈亏
	CloseProfitByTrade float64
	// 今日持仓
	TodayPosition int
	// 执行冻结
	StrikeFrozen int
	// 执行冻结金额
	StrikeFrozenAmount float64
	// 放弃执行冻结
	AbandonFrozen int
	// 交易所代码
	ExchangeID string
	// 执行冻结的昨仓
	YdStrikeFrozen int
	// 大商所持仓成本差值,只有大商所使用
	PositionCostOffset float64
}

投资者持仓

type PositionTypeType

type PositionTypeType byte

持仓类型类型

const (
	// 净持仓
	PositionTypeNet PositionTypeType = '1'
	// 综合持仓
	PositionTypeGross PositionTypeType = '2'
)

type ProductClassType

type ProductClassType byte

产品类型类型

const (
	// 期货
	ProductClassFutures ProductClassType = '1'
	// 期货期权
	ProductClassOptions ProductClassType = '2'
	// 组合
	ProductClassCombinationProductClassType = '3'
	// 即期
	ProductClassSpot ProductClassType = '4'
	// 期转现
	ProductClassEFP ProductClassType = '5'
	// 现货期权
	ProductClassSpotOption ProductClassType = '6'
)

type RspInfoField

type RspInfoField struct {
	// 错误代码
	ErrorID int
	// 错误信息
	ErrorMsg string
}

响应信息

type RspUserLoginField

type RspUserLoginField struct {
	// 交易日
	TradingDay string
	// 登录成功时间
	LoginTime string
	// 经纪公司代码
	BrokerID string
	// 用户代码
	UserID string
	// 交易系统名称
	//SystemName string
	// 前置编号
	FrontID int
	// 会话编号
	SessionID int
	// 最大报单引用
	MaxOrderRef string
}

用户登录应答

type TickField

type TickField struct {
	// 交易日
	TradingDay string
	// 合约代码
	InstrumentID string
	// 交易所代码
	ExchangeID string
	// 合约在交易所的代码
	//ExchangeInstID string
	// 最新价
	LastPrice float64
	// 上次结算价
	//PreSettlementPrice float64
	// 昨收盘
	//PreClosePrice float64
	// 昨持仓量
	//PreOpenInterest float64
	// 今开盘
	OpenPrice float64
	// 最高价
	HighestPrice float64
	// 最低价
	LowestPrice float64
	// 数量
	Volume int
	// 成交金额
	Turnover float64
	// 持仓量
	OpenInterest float64
	// 今收盘
	ClosePrice float64
	// 本次结算价
	SettlementPrice float64
	// 涨停板价
	UpperLimitPrice float64
	// 跌停板价
	LowerLimitPrice float64
	// 昨虚实度
	//PreDelta float64
	// 今虚实度
	CurrDelta float64
	// 最后修改时间
	UpdateTime string
	// 最后修改毫秒
	UpdateMillisec int
	// 申买价一
	BidPrice1 float64
	// 申买量一
	BidVolume1 int
	// 申卖价一
	AskPrice1 float64
	// 申卖量一
	AskVolume1 int
	// 申买价二
	BidPrice2 float64
	// 申买量二
	BidVolume2 int
	// 申卖价二
	AskPrice2 float64
	// 申卖量二
	AskVolume2 int
	// 申买价三
	BidPrice3 float64
	// 申买量三
	BidVolume3 int
	// 申卖价三
	AskPrice3 float64
	// 申卖量三
	AskVolume3 int
	// 申买价四
	BidPrice4 float64
	// 申买量四
	BidVolume4 int
	// 申卖价四
	AskPrice4 float64
	// 申卖量四
	AskVolume4 int
	// 申买价五
	BidPrice5 float64
	// 申买量五
	BidVolume5 int
	// 申卖价五
	AskPrice5 float64
	// 申卖量五
	AskVolume5 int
	// 当日均价
	AveragePrice float64
	// 业务日期
	ActionDay string
}

行情响应

type TradeField

type TradeField struct {
	// 报单引用
	OrderRef string
	// 合约代码
	InstrumentID string
	// 交易所代码
	ExchangeID string
	// 成交编号
	TradeID string
	// 买卖方向
	Direction DirectionType
	// 报单编号
	OrderSysID string
	// 开平标志
	OffsetFlag OffsetFlagType
	// 投机套保标志
	HedgeFlag HedgeFlagType
	// 价格
	Price float64
	// 数量
	Volume int
	// 成交时期
	TradeDate string
	// 成交时间
	TradeTime string
	// 交易日
	TradingDay string
}

成交

type TransferField

type TransferField struct {
	Time       string  // 时间
	CurrencyID string  // 币种
	Amout      float64 // 金额
	ErrorID    int     // 错误码
	ErrorMsg   string  // 错误描述
}

TransferField 银转响应

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL