1 Star 0 Fork 38

anhuazhang / goctp

forked from 海风 / goctp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

goctp

介绍

CTP 封装之 golang 版,支持 Windows Linux x64. 采用二次封装,将 C++封装成 C,并 export. win lnx 封装逻辑相同: trade.go quote.go

运行

环境变量

"tradeFront": "tcp://180.168.146.187:10130",
"quoteFront": "tcp://180.168.146.187:10131",
"loginInfo": "9999/008107/1/simnow_client_test/0000000000000000"

示例

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"time"

	"gitee.com/haifengat/goctp"
	// ctp "gitee.com/haifengat/goctp/lnx" // linx
	ctp "gitee.com/haifengat/goctp/win"  // windows
)

var (
	investorID, password, brokerID, appID, authCode string
	tradeFront, quoteFront, loginInfo               string
)

var t = ctp.NewTrade()
var q = ctp.NewQuote()

func init() {
	tradeFront = os.Getenv("tradeFront")
	quoteFront = os.Getenv("quoteFront")
	loginInfo = os.Getenv("loginInfo")
	fs := strings.Split(loginInfo, "/")
	brokerID, investorID, password, appID, authCode = fs[0], fs[1], fs[2], fs[3], fs[4]
	fmt.Println("tradeFront: ", tradeFront)
	fmt.Println("quoteFront: ", quoteFront)
	fmt.Printf("brokerID:%s\ninvestorID:%s\npassword:%s\nappID:%s\nauthCode:%s\n", brokerID, investorID, password, appID, authCode)
}

func testQuote() {
	q.RegOnFrontConnected(func() {
		fmt.Println("quote connected")
		q.ReqLogin(investorID, password, brokerID)
	})
	q.RegOnRspUserLogin(func(login *goctp.RspUserLoginField, info *goctp.RspInfoField) {
		fmt.Println("quote login:", info)
	})
	q.RegOnTick(func(tick *goctp.TickField) {
		bs, _ := json.Marshal(tick)
		fmt.Println(string(bs))
	})
	fmt.Println("quote connecting " + quoteFront)
	q.ReqConnect(quoteFront)
}

func testTrade() {
	t.RegOnFrontConnected(func() {
		fmt.Println("trade connected")
		go t.ReqLogin(investorID, password, brokerID, appID, authCode)
	})
	t.RegOnRspUserLogin(func(login *goctp.RspUserLoginField, info *goctp.RspInfoField) {
		fmt.Println(info)
		bs, _ := json.Marshal(login)
		fmt.Println("login: ", string(bs))
	})

	t.RegOnRtnOrder(func(field *goctp.OrderField) {
		bs, _ := json.Marshal(field)
		fmt.Println("OnRtnOrder:", string(bs))
	})
	t.RegOnRtnCancel(func(field *goctp.OrderField) {
		bs, _ := json.Marshal(field)
		fmt.Println("OnRtnCancel: ", string(bs))
	})
	t.RegOnErrRtnOrder(func(field *goctp.OrderField, info *goctp.RspInfoField) {
		bs, _ := json.Marshal(info)
		fmt.Println("OnErrRtnOrder: ", string(bs))
	})
	// 交易状态
	t.RegOnRtnInstrumentStatus(func(field *goctp.InstrumentStatus) {
		// fmt.Println(field)
	})
	// 断开
	t.RegOnFrontDisConnected(func(reason int) {
		fmt.Println("disconntcted: ", reason)
	})
	fmt.Println("connecting to trade " + tradeFront)
	t.ReqConnect(tradeFront)
}

func main() {
	go testQuote()
	go testTrade()
	for !t.IsLogin {
		time.Sleep(1 * time.Second)
	}

	time.Sleep(3 * time.Second)
	// 委托测试
	if true {
		t.ReqOrderInsert("rb2205", goctp.DirectionBuy, goctp.OffsetFlagOpen, 4000, 1)
	}
	// 合约
	if true {
		cnt := 0
		t.Instruments.Range(func(k, v interface{}) bool {
			cnt++
			return true
		})
		fmt.Println("instrument count:", cnt)
	}
	// 权益
	if true {
		bs, _ := json.Marshal(t.Account)
		fmt.Println(string(bs))
	}
	// 委托信息
	if true {
		t.Orders.Range(func(key, value interface{}) bool {
			fmt.Printf("%s: %v\n", key, value)
			return true
		})
	}
	// 成交信息
	if true {
		t.Trades.Range(func(key, value interface{}) bool {
			fmt.Printf("%s: %v\n", key, value)
			return true
		})
	}
	// 持仓
	if true {
		t.Positions.Range(func(key, value interface{}) bool {
			p := value.(*goctp.PositionField)
			fmt.Printf("%s: %s: 昨:%d,今:%d,总: %d, 可: %d\n", key, p.InstrumentID, p.YdPosition, p.TodayPosition, p.Position, p.Position-p.ShortFrozen)
			return true
		})
		time.Sleep(500 * time.Millisecond)
	}
	// 入金
	if false {
		t.RegOnRtnFromFutureToBank(func(field *goctp.TransferField) {
			fmt.Println(field)
		})
		t.ReqFutureToBank("", "", 30)
	}
	// 订阅合约
	if true {
		q.ReqSubscript("rb2205")
	}

	fmt.Scanf("exit: ")
	t.Release()
	q.Release()
}

版本切换

复制 6.5.1 版本以上官方库文件覆盖到 lnx win 下同名文件即可。

QA

operator delete(void*, unsigned long)@CXXABI_1.3.9’未定义的引用

不同系统,不同版本的底层依赖不同 重新编译即可

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 ..
MIT License Copyright (c) 2019 海风 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

CTP封装之golang版(支持windows linux x64) 展开 收起
Go
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/anhuazhang/goctp.git
git@gitee.com:anhuazhang/goctp.git
anhuazhang
goctp
goctp
master

搜索帮助