// 通达信期权文档.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include using namespace std; //1.交易API均是Trade.dll文件的导出函数,包括以下函数: // int Logon(char* IP, short Port, char* Version, short YybID, char* AccountNo,char* TradeAccount, char* JyPassword, char* TxPassword, char* ErrInfo);//登录帐号 // void Logoff(int ClientID);//注销 // void QueryOptionData(int ClientID, int Category, char* StartTimeStamp, int Count, char* Result, char* ErrInfo);//查询各类交易数据 // void SendOptionOrder(int ClientID, int exchange_id, char* secu_code, int bs_flag, int offset_flag, int order_type, int order_prop, char* order_price, int order_volume, char* Result, char* ErrInfo);//下单 // void CancelOptionOrder(int ClientID, char* order_ref, char* Result, char* ErrInfo);//撤单 ///交易接口执行后,如果失败,则字符串ErrInfo保存了出错信息中文说明; ///如果成功,则字符串Result保存了结果数据,形式为表格数据,行数据之间通过\n字符分割,列数据之间通过\t分隔。 ///Result是\n,\t分隔的中文字符串,比如查询股东代码时返回的结果字符串就是 ///"股东代码\t股东名称\t帐号类别\t保留信息\n ///0000064567\t\t0\t\nA000064567\t\t1\t\n ///2000064567\t\t2\t\nB000064567\t\t3\t" ///查得此数据之后,通过分割字符串, 可以恢复为几行几列的表格形式的数据 //2.API使用流程为: 应用程序可以同时登录多个交易账户,每个交易账户称之为ClientID. //通过调用Logon获得ClientID,然后可以调用其他API函数向各个ClientID进行查询或下单; 应用程序退出时应调用Logoff注销ClientID //如果发生断线, api会报错要求重新登录,应用程序必须重新登录 //3. 各个函数功能说明 /// /// 交易账户登录 /// /// 券商交易服务器IP /// 券商交易服务器端口 /// 设置通达信客户端的版本号 /// 营业部代码,一般为8888 /// 完整的登录账号,券商一般使用资金帐户或客户号 /// 交易账号,留空串 /// 交易密码 /// 通讯密码, 没有就留空串 /// 此API执行返回后,如果出错,保存了错误信息说明。一般要分配256字节的空间。没出错时为空字符串。 /// 客户端ID,失败时返回-1 typedef int(__stdcall* LogonDelegate)(char* IP, short Port, char* Version, short YybID, char* AccountNo, char* TradeAccount, char* JyPassword, char* TxPassword, char* ErrInfo); /// /// 交易账户注销 /// /// 客户端ID typedef void(__stdcall* LogoffDelegate)(int ClientID); /// /// 查询各种交易数据 /// /// 客户端ID /// 表示查询信息的种类,0资金 1股份 2当日委托 3当日成交 4合约代码 /// 时间戳字符串, 从此时间戳的记录开始返回查询结果, 空字符串表示从最初一条数据开始返回, 查询资金和持仓时无效,通过查询当日委托获知时间戳 /// 指定返回查询结果的记录数目,查询资金和持仓时无效 /// 此API执行返回后,Result内保存了返回的查询数据, 形式为表格数据,行数据之间通过\n字符分割,列数据之间通过\t分隔。一般要分配1024*1024字节的空间。出错时为空字符串。 /// 同Logon函数的ErrInfo说明 typedef void(__stdcall* QueryOptionDataDelegate)(int ClientID, int Category, char* StartTimeStamp, int Count, char* Result, char* ErrInfo); /// /// 下单 /// /// 返回客户端ID /// 交易所代码, 0深圳 1上海 /// 合约代码 /// 买卖类别 0买 1卖 /// 开平标志 0开仓 1平仓 /// 委托类型,取值见下面表格 /// 委托属性,取值见下面表格 /// 上海市场: 限价GFD:order_type=0 order_prop=0 市价IOC剩余即撤:order_type=1 order_prop=2 市价剩余转限价GFD:order_type=1 order_prop=0 限价FOK全成或撤:order_type=0 order_prop=1 市价FOK全成或撤:order_type=1 order_prop=1 /// 深圳市场: 限价GFD:order_type=0 order_prop=0 限价FOK全成或撤:order_type=0 order_prop=1 对方最优价格:order_type=1 order_prop=0 本方最优价格:order_type=2 order_prop=0 即时成交剩撤:order_type=3 order_prop=0 五档即成剩撤:order_type=4 order_prop=0 全额成交或撤:order_type=5 order_prop=0 /// 委托价格 /// 委托数量 /// 同上 /// 同上 typedef void(__stdcall* SendOptionOrderDelegate)(int ClientID, int exchange_id, char* secu_code, int bs_flag, int offset_flag, int order_type, int order_prop, char* order_price, int order_volume, char* Result, char* ErrInfo); /// /// 撤单 /// /// 返回客户端ID /// 表示要撤的目标委托的引用号, 通过查询当日委托获知引用号 /// 同上 /// 同上 typedef void(__stdcall* CancelOptionOrderDelegate)(int ClientID, char* order_ref, char* Result, char* ErrInfo); int main() { //本软件详细信息请见网站 http://tdxjy.com/ //载入dll, 所有4个dll都要复制到debug和release目录下,必须采用多字节字符集编程设置 HMODULE HMODULE1 = LoadLibrary("trade.dll"); //获取api函数 LogonDelegate Logon = (LogonDelegate)GetProcAddress(HMODULE1, "Logon"); LogoffDelegate Logoff = (LogoffDelegate)GetProcAddress(HMODULE1, "Logoff"); QueryOptionDataDelegate QueryOptionData = (QueryOptionDataDelegate)GetProcAddress(HMODULE1, "QueryOptionData"); SendOptionOrderDelegate SendOptionOrder = (SendOptionOrderDelegate)GetProcAddress(HMODULE1, "SendOptionOrder"); CancelOptionOrderDelegate CancelOptionOrder = (CancelOptionOrderDelegate)GetProcAddress(HMODULE1, "CancelOptionOrder"); //开始交易 char* Result = new char[1024 * 1024]; char* ErrInfo = new char[256]; //登录帐号 int ClientID = Logon("61.183.150.139", 7616, "", 8888, "1234567890", "", "123456", "", ErrInfo); //登录第二个帐号 //int ClientID2 = Logon("111.111.111.111", 7708, "4.20", 8888, "33333333","", "333", "", ErrInfo); if (ClientID == -1) {//登录失败 cout << ErrInfo << endl; } else {//登录成功 QueryOptionData(ClientID, 0, "", 0, Result, ErrInfo); cout << "查询资金结果:" << Result << " " << ErrInfo<