当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 1 Fork 5

华夏禹少/hf_ctp_cs_proxy
暂停

forked from 海风/hf_ctp_cs_proxy
关闭
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

hf_ctp_cs_proxy

上期技术期货交易api之C#封装,实现接口调用。支持winform数据绑定即时更新。

有问题反馈

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流

接口使用代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace HaiFeng
{
	public class TestQuote
	{
		CTPQuote _q = null;
		string _investor = "008105", _broker = "9999";
		public TestQuote()
		{
			_q = new CTPQuote();

			_q.OnFrontConnected += _q_OnFrontConnected;
			_q.OnRspUserLogin += _q_OnRspUserLogin;
			_q.OnRspUserLogout += _q_OnRspUserLogout;
			_q.OnRtnTick += _q_OnRtnTick;
			_q.OnRtnError += _q_OnRtnError;
		}

		public void Release()
		{
			_q.ReqUserLogout();
		}

		public void Run()
		{
			_q.ReqConnect("tcp://180.168.146.187:10010");
		}

		void Log(string pMsg)
		{
			Console.WriteLine(DateTime.Now.TimeOfDay + "\t" + pMsg);
		}

		private void _q_OnFrontConnected(object sender, EventArgs e)
		{
			Log("connected");
			_q.ReqUserLogin(_investor, "12", _broker);
		}

		private void _q_OnRspUserLogin(object sender, IntEventArgs e)
		{
			if (e.Value == 0)
			{
				Log($"登录成功:{_investor}");
				_q.ReqSubscribeMarketData("rb1705", "cu1705");
			}
			else
			{
				Log($"登录错误:{e.Value}");
				_q.ReqUserLogout();
			}
		}

		private void _q_OnRtnTick(object sender, TickEventArgs e)
		{
			Log($"{e.Tick.InstrumentID}\t{e.Tick.LastPrice}");
		}

		private void _q_OnRspUserLogout(object sender, IntEventArgs e)
		{
			Log($"quote logout: {e.Value}");
		}

		private void _q_OnRtnError(object sender, ErrorEventArgs e)
		{
			Log(e.ErrorMsg);
		}
	}

	class TestTrade
	{
		CTPTrade _t = null;
		string _broker = "9999", _ivnestor = "008105";

		public TestTrade()
		{
			_t = new CTPTrade();
		}

		public void Release()
		{
			_t.ReqUserLogout();
		}

		void Log(string pMsg)
		{
			Console.WriteLine(DateTime.Now.TimeOfDay + "\t" + pMsg);
		}

		public void Run()
		{
			_t.OnFrontConnected += _t_OnFrontConnected;
			_t.OnRspUserLogin += _t_OnRspUserLogin;
			_t.OnRtnOrder += _t_OnRtnOrder;
			_t.OnRtnTrade += _t_OnRtnTrade;
			_t.OnRtnCancel += _t_OnRtnCancel;
			_t.ReqConnect("tcp://180.168.146.187:10000");
		}

		private void _t_OnRtnCancel(object sender, OrderArgs e)
		{
			Log($"{e.Value.StatusMsg}\t{e.Value.InstrumentID}\t{e.Value.Direction}\t{e.Value.Offset}\t{e.Value.LimitPrice}\t{e.Value.Volume}");
		}

		private void _t_OnRtnTrade(object sender, TradeArgs e)
		{
			Log($"{e.Value.InstrumentID}\t{e.Value.Direction}\t{e.Value.Offset}\t{e.Value.Price}\t{e.Value.Volume}");
		}

		private void _t_OnRtnOrder(object sender, OrderArgs e)
		{
			Log($"{e.Value.InstrumentID}\t{e.Value.Direction}\t{e.Value.Offset}\t{e.Value.LimitPrice}\t{e.Value.Volume}");

			if (e.Value.IsLocal)
				_t.ReqOrderAction(e.Value.OrderID);
		}

		private void _t_OnFrontConnected(object sender, EventArgs e)
		{
			_t.ReqUserLogin(_ivnestor, "1", _broker);
		}

		private void _t_OnRspUserLogin(object sender, IntEventArgs e)
		{
			if (e.Value == 0)
			{
				Log("登录成功");
				_t.ReqOrderInsert("rb1705", DirectionType.Sell, OffsetType.Open, 3200, 1, 1000);
			}
			else
			{
				Log($"登录错误:{e.Value}");
			}
		}

		private void OnRtnInstrumentStatus(ref CThostFtdcInstrumentStatusField pInstrumentStatus)
		{
			Log($"{pInstrumentStatus.InstrumentID}:{pInstrumentStatus.InstrumentStatus}");
		}

		private void OnFrontDisconnected(int nReason)
		{
			Log("连接已断开");
		}
	}

}

空文件

简介

上期技术期货交易api之C#封装,实现接口调用。支持winform数据绑定即时更新。 展开 收起
C#
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/oyzhiquan/hf_ctp_cs_proxy.git
git@gitee.com:oyzhiquan/hf_ctp_cs_proxy.git
oyzhiquan
hf_ctp_cs_proxy
hf_ctp_cs_proxy
master

搜索帮助