public string Test(string IP, short Port, string Version, short YybID, string AccountNo, string TradeAccount, string JyPassword, string TxPassword) { try { StringBuilder Result = new StringBuilder(1024 * 1024); StringBuilder ErrInfo = new StringBuilder(256); int ClientID = TdxApi.Logon(IP, Port, Version, YybID, AccountNo, TradeAccount, JyPassword, TxPassword, ErrInfo); if (ClientID == -1) { return(string.Format("{0}|{1}", ClientID, ErrInfo)); } TdxApi.QueryData(ClientID, 5, Result, ErrInfo); TdxApi.Logoff(ClientID); return(string.Format("{0}|{1}", Result, ErrInfo)); } catch (Exception ex) { return(ex.Message); } }
/// <summary> /// /// </summary> /// <param name="dataType">表示查询信息的种类,0资金 1股份 2当日委托 3当日成交 4可撤单 5股东代码 6融资余额 7融券余额 8可融证券</param> /// <returns></returns> internal void QueryAccountData(int dataType, GroupClient.GroupService.Queryinfo QueryResult) { StringBuilder result = new StringBuilder(1024 * 1024); StringBuilder errInfo = new StringBuilder(256); try { if (this.ClientID == -1) { this.ClientID = TdxApi.Logon(this.IP, this.Port, this.版本号, this.营业部代码, this.登录帐号, this.交易帐号, Cryptor.MD5Decrypt(this.交易密码), Cryptor.MD5Decrypt(this.通讯密码), errInfo); isLogonInterim = this.ClientID > -1; } if (this.ClientID > -1) { TdxApi.QueryData(this.ClientID, dataType, result, errInfo); } } catch (Exception ex) { errInfo.AppendFormat("查询接口异常:{0}", ex.Message); } QueryResult.Error = errInfo.ToString(); QueryResult.Result = result.ToString(); if (isLogonInterim) { isLogonInterim = false; } }
private void QueryCJMain() { StringBuilder resultCJ = new StringBuilder(1024 * 1024); StringBuilder errInfoCJ = new StringBuilder(256); while (true) { DateTime dt = DateTime.Now; if ((DateTime.Parse(CommonUtils.GetConfig("开始查询时间", "8:15")) <= DateTime.Now && DateTime.Now <= DateTime.Parse(CommonUtils.GetConfig("结束查询时间", "15:30")))) { try { TdxApi.QueryData(ClientID, 3, resultCJ, errInfoCJ); queueCj.Enqueue(resultCJ.ToString()); QueryInfo.SearchTradeErrInfo = errInfoCJ.ToString(); } catch (Exception ex) { CommonUtils.Log("成交查询异常", ex); } Thread.Sleep(查询间隔时间); } else { Thread.Sleep(10000); } } }
public GroupService.Queryinfo SendOrderHK(int Category, int PriceType, string Gddm, string Zqdm, float Price, int Quantity, StringBuilder result, StringBuilder errInfo) { GroupClient.GroupService.Queryinfo response = new GroupService.Queryinfo(); try { TdxApi.QueryHKData(ClientID, 2, result, errInfo); var 单前ErrInfo = errInfo.ToString(); if (单前ErrInfo != string.Empty) { response.Result = result.ToString(); response.Error = errInfo.ToString(); } else { response.Other = result.ToString(); TdxApi.SendHKOrder(ClientID, Category, 2, 0, Gddm, Zqdm, Price.ToString(), Quantity.ToString(), result, errInfo); } } catch (Exception ex) { response.Error += "下单异常:" + ex.Message; } return(response); }
public bool Test(string IP, short Port, string Version, short YybID, string AccountNo, string TradeAccount, string JyPassword, string TxPassword) { try { StringBuilder Result = new StringBuilder(1024 * 1024); StringBuilder ErrInfo = new StringBuilder(256); int ClientID = TdxApi.Logon(IP, Port, Version, YybID, AccountNo, TradeAccount, JyPassword, TxPassword, ErrInfo); if (ClientID == -1) { MessageBox.Show("登录测试失败:" + ErrInfo.ToString()); return(false); } TdxApi.QueryData(ClientID, 5, Result, ErrInfo); TdxApi.Logoff(ClientID); return(true); } catch (Exception ex) { CommonUtils.Log("组合号添加异常:" + ex.Message); return(false); } }
public string Logon() { StringBuilder ErrInfo = new StringBuilder(256); if (IsIMSAccount) { string Msg; ImsApi.Init(ImsNotify); bool isLogin = ImsApi.Login(this.交易帐号, Cryptor.MD5Decrypt(this.交易密码), this.版本号, out Msg); if (isLogin) { this.ClientID = 0; return("登录成功"); } else { return(Msg); } } else { try { this.ClientID = TdxApi.Logon(this.IP, this.Port, this.版本号, this.营业部代码, this.登录帐号, this.交易帐号, Cryptor.MD5Decrypt(this.交易密码), Cryptor.MD5Decrypt(this.通讯密码), ErrInfo); } catch (Exception ex) { CommonUtils.Log("组合号登录异常", ex); } string strError = ErrInfo.ToString(); if (!string.IsNullOrEmpty(strError)) { //交易中心(-10016):您使用的版本太低已因为安全原因停止使用,请到官网上下载最新版本重新安装。 if (strError.Contains("密码") || strError.Contains("您使用的版本太低已因为安全原因停止使用")) { //ErrInfo.Append("登录异常,将自动禁用,请检查组合号信息!"); CommonUtils.Log("登录密码错误或版本过低导致登录失败,将自动停用,错误信息:{0}", strError); this.启用 = false; } return("登录失败:" + ErrInfo.ToString()); } else { return("登录成功"); } } }
public void Logoff() { try { if (this.IsIMSAccount) { ImsApi.ImsPbClient_Disconnect(); } else { TdxApi.Logoff(this.ClientID); } } catch { } this.ClientID = -1; }
private void QueryFilterData() { StringBuilder resultWt = new StringBuilder(1024 * 1024); StringBuilder errInfoWt = new StringBuilder(256); DateTime dt1 = DateTime.Now; TdxApi.QueryData(this.ClientID, 2, resultWt, errInfoWt); var span = DateTime.Now - dt1; var costsSeconds = span.TotalSeconds; //CommonUtils.Log("{0}委托查询单次耗时{1}", this.名称, costsSeconds.ToString()); queueWt.Enqueue(resultWt.ToString()); QueryInfo.SearchOperatorError = errInfoWt.ToString(); Thread.Sleep(查询间隔时间); #region 其他线程 if (QeuryCJThread == null) { QeuryCJThread = new Thread(new ThreadStart(QueryCJMain)) { IsBackground = true }; QeuryCJThread.Start(); } if (FilteWtDataThread == null) { FilteWtDataThread = new Thread(new ThreadStart(FilterWtDataMain)) { IsBackground = true }; FilteWtDataThread.Start(); } if (FilteCjDataThread == null) { FilteCjDataThread = new Thread(new ThreadStart(FilterCjDataMain)) { IsBackground = true }; FilteCjDataThread.Start(); } #endregion }
void worker_DoWork(object sender, DoWorkEventArgs e) { CommonUtils.Log("启动服务中…"); TdxApi.OpenTdx(); InitService(); adapter.Start(); this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { this.dgState.ItemsSource = Adapter.GroupLogonList; })); while (!this.worker.CancellationPending) { //try //{ // this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => // { // this.dgState.Items.Refresh(); // })); //} //catch (Exception) { } Thread.Sleep(1000); } CommonUtils.Log("服务关闭中…"); adapter.Stop(); TdxApi.CloseTdx(); ImsApi.Close(); if (this.ServiceHost1.State != CommunicationState.Faulted) { this.ServiceHost1.Close(); } CommonUtils.Log("服务已关闭!"); }
public void CancelHKOrder(string hth, StringBuilder resultCancel, StringBuilder errCancelInfo) { if (this.ClientID > -1) { try { StringBuilder result = new StringBuilder(1024 * 1024); StringBuilder errInfo = new StringBuilder(256); TdxApi.QueryHKData(ClientID, 0, result, errInfo); result.Clear(); errInfo.Clear(); TdxApi.CancelHKOrder(ClientID, hth, result, errInfo); } catch (Exception ex) { CommonUtils.Log("CancelHKOrder Exception", ex); } } }
private void QueryData() { if (QueryInfo == null) { QueryInfo = new GroupService.QueryDataObj(); } if (this.IsIMSAccount) { QueryImsData(); return; } if (this.Multithreading == true) { QueryFilterData(); } else { StringBuilder result = new StringBuilder(1024 * 1024); StringBuilder errInfo = new StringBuilder(256); TdxApi.QueryData(this.ClientID, 3, result, errInfo); QueryInfo.SearchTradeResult = result.ToString(); QueryInfo.SearchTradeErrInfo = errInfo.ToString(); Thread.Sleep(查询间隔时间 / 2); TdxApi.QueryData(this.ClientID, 2, result, errInfo); QueryInfo.SearchOperatorResult = result.ToString(); QueryInfo.SearchOperatorError = errInfo.ToString(); if (QueryInfo.SearchTradeErrInfo.Contains("请尝试其它交易服务器") && QueryInfo.SearchOperatorError.Contains("请尝试其它交易服务器")) { CommonUtils.Log("检测到{0}连接交易服务器失败信息,尝试注销登录后重新登录", this.称); this.Logoff(); this.Logon(); } Thread.Sleep(查询间隔时间 / 2); } }
public void CancelOrder(string ExchangeID, string hth, StringBuilder resultCancel, StringBuilder errCancelInfo) { if (this.ClientID > -1) { DateTime st = DateTime.Now; try { //TdxApi.QueryData(ClientID, 5, resultCancel, errCancelInfo); TdxApi.CancelOrder(ClientID, ExchangeID, hth, resultCancel, errCancelInfo); CommonUtils.Log("{0}执行撤单完成, Result:{1}, Error:{2}, 耗时{3}", this.称, resultCancel.ToString(), errCancelInfo.ToString(), (DateTime.Now - st).TotalSeconds.ToString()); } catch (Exception ex) { CommonUtils.Log(string.Format("{0}执行撤单异常,订单号{1}, 耗时{2}, Exception{3}", this.称, hth, (DateTime.Now - st).TotalSeconds.ToString(), ex.Message)); } } else { errCancelInfo.AppendFormat("撤单失败,组合号{0} ClientID为-1", this.称); } }
public void Repay(string amount, StringBuilder Result, StringBuilder ErrInfo) { try { if (this.ClientID == -1) { this.ClientID = TdxApi.Logon(this.IP, this.Port, this.版本号, this.营业部代码, this.登录帐号, this.交易帐号, Cryptor.MD5Decrypt(this.交易密码), Cryptor.MD5Decrypt(this.通讯密码), ErrInfo); isLogonInterim = this.ClientID > -1; } if (this.ClientID > -1) { TdxApi.Repay(this.ClientID, amount, Result, ErrInfo); } } catch (Exception ex) { ErrInfo.AppendFormat("Repay异常:{0}", ex.Message); } if (isLogonInterim) { isLogonInterim = false; } }
/// <summary> /// A股下单逻辑 /// </summary> public GroupService.Queryinfo SendOrderNormal(int Category, int PriceType, string Gddm, string Zqdm, float Price, int Quantity, string mac, StringBuilder result, StringBuilder errInfo) { DateTime st = DateTime.Now; StringBuilder sbResult = new StringBuilder(1024 * 1024); StringBuilder sbError = new StringBuilder(256); GroupService.Queryinfo response = new GroupService.Queryinfo(); try { sbResult.Append(this.strTitleWt + "\n" + string.Join("\n", dictWt.Values)); sbError.Append(this.QueryInfo.SearchOperatorError); var 单前ErrInfo = sbError.ToString(); if (单前ErrInfo != string.Empty) { response.Result = sbResult.ToString(); response.Error = sbError.ToString(); CommonUtils.Log("下单前查询异常,组合号{0},证券代码{1},错误信息{2},耗时{3}", this.称, Zqdm, response.Error, (DateTime.Now - st).TotalSeconds.ToString()); } else { response.Other = result.ToString(); TdxApi.SendOrder(ClientID, Category, PriceType, Gddm, Zqdm, Price, Quantity, sbResult, sbError); string ErrInfo = sbError.ToString(); if (ErrInfo == string.Empty) { DataTable DataTable1 = CommonUtils.ChangeDataStringToTable(sbResult.ToString()); string id = null; if (DataTable1.Columns.Contains("委托编号")) { id = DataTable1.Rows[0]["委托编号"] as string; } else if (DataTable1.Columns.Contains("合同编号")) { id = DataTable1.Rows[0]["合同编号"] as string; } if (id != null && !lstOrderID.Contains(id)) { lstOrderID.Add(id); dictOrderSuccessTime[id] = DateTime.Now; } } else { CommonUtils.Log("下单异常,组合号{0},证券代码{1},错误信息{2}", this.称, Zqdm, ErrInfo); if (ErrInfo.Contains("尝试其他交易服务器") || ErrInfo.Contains("超时")) { CommonUtils.Log("开始记录可能存在漏单数据"); var mayLostOrder = new OrderInfoCache() { Category = Category, GroupName = this.称, SendPrice = Price, SendQty = Quantity, StockID = Zqdm, SendTime = DateTime.Now, SendMac = mac, }; CommonUtils.Log("可能存在漏单数据,请求数据{0}", mayLostOrder.ToJson()); var table = Tool.ChangeDataStringToTable(this.QueryInfo.SearchOperatorResult); if (table != null && table.Rows.Count > 0) { mayLostOrder.CheckedIndex = table.Rows.Count; } queueMayLostOrder.Enqueue(mayLostOrder); } } } } catch (Exception ex) { CommonUtils.Log("下单异常,组合号{0}, 股票代码{1},异常信息{2}", this.称, Zqdm, ex.Message); } response.Result = sbResult.ToString(); response.Error = sbError.ToString(); return(response); }