1 Star 0 Fork 39

air_cn / hf_go_ctp

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

hf_go_ctp

介绍

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

软件架构

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

使用说明

linux 使用
  • 编译goctp 具体操作见goctp/readme.md

使用了sync.waitGroup判断登录是否成功:

  1. 基础查询完成
  2. OnRtnOrder旧数据完成
  3. 响应中相比CTP,增加了OnCancel,以处理委托被撤单.

测试代码

package main

import (
	"encoding/json"
	"fmt"
	"hf_go_ctp/src/go_ctp"
	"os"
	"os/signal"
	"syscall"
)

func exit() {
	signals := make(chan os.Signal, 1)
	done := make(chan bool, 1)
	// `signal.Notify` registers the given channel to
	// receive notifications of the specified signals.
	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
	// This goroutine executes a blocking receive for
	// signals. When it gets one it'll print it out
	// and then notify the program that it can finish.
	go func() {
		sig := <-signals
		fmt.Println()
		fmt.Println(sig)
		done <- true
	}()
	// The program will wait here until it gets the
	// expected signal (as indicated by the goroutine
	// above sending a value on `done`) and then exit.
	<-done
	fmt.Println("exiting")
}

func testTrade() *go_ctp.Trade {
	fmt.Println("connected to trade...")
	t := go_ctp.NewTrade()
	t.RegOnFrontConnected(func() {
		fmt.Println("connected")
		t.ReqLogin("008105", "1", "9999", "simnow_client_test", "0000000000000000")
	})
	t.RegOnRspUserLogin(func(login *go_ctp.RspUserLoginField, info *go_ctp.RspInfoField) {
		fmt.Println(info)
		fmt.Printf("login info: %#v\n", *login)
		//id := t.ReqOrderInsertMarket("rb2001", go_ctp.DirectionBuy, go_ctp.OffsetFlagOpen, 1)
		id := t.ReqOrderInsert("rb2001", go_ctp.DirectionBuy, go_ctp.OffsetFlagOpen, 3000, 1)
		fmt.Println(id)
	})
	t.RegOnRtnOrder(func(field *go_ctp.OrderField) {
		fmt.Printf("%#v\n", field)
	})
	t.RegOnErrRtnOrder(func(field *go_ctp.OrderField, info *go_ctp.RspInfoField) {
		fmt.Printf("%#v\n", info)
	})
	t.ReqConnect("tcp://180.168.146.187:10101")
	return t
}

func testQuote() {
	q := go_ctp.NewQuote()
	// 自动关闭
	defer q.Release()
	q.RegOnFrontConnected(func() {
		fmt.Println("connected")
		q.ReqLogin("008107", "1", "9999")
	})
	q.RegOnRspUserLogin(func(login *go_ctp.RspUserLoginField, info *go_ctp.RspInfoField) {
		fmt.Println(info)
		q.ReqSubscript("rb2001")
	})
	q.RegOnTick(func(data go_ctp.TickField) {
		bs, _err := json.Marshal(data)
		if _err == nil {
			println(string(bs))
		}
	})
	q.ReqConnect("tcp://180.168.146.187:10111")
}

func main() {
	go func() { testTrade() }()
	exit()
}
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 x86/x64 linux 64) 展开 收起
Go
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

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

搜索帮助