public DataRow[] Select(string InstrumentID, TZQThostFtdcPosiDirectionType PosiDirection, TZQThostFtdcHedgeFlagType HedgeFlag, TZQThostFtdcPositionDateType PositionDate) { return(dtInvestorPosition.Select( string.Format("InstrumentID='{0}' and PosiDirection={1} and HedgeFlag={2} and PositionDate={3}", InstrumentID, (int)PosiDirection, (int)HedgeFlag, (int)PositionDate))); }
//只收到成交信息时调用 public bool InsertOrReplaceForTrade( string InstrumentID, TZQThostFtdcPosiDirectionType PosiDirection, TZQThostFtdcDirectionType Direction, TZQThostFtdcHedgeFlagType HedgeFlag, TZQThostFtdcPositionDateType PositionDate, int volume) { lock (this) { // 今天的买入要先冻结 //冲突的可能性大一些,所以要先Update后Insert DataRow[] rows = Select(InstrumentID, PosiDirection, HedgeFlag, PositionDate); if (rows.Count() == 1) { int vol = (int)rows[0][Position]; rows[0][Position] = vol - volume; } else { //假设是新添数据 try { if (Direction == TZQThostFtdcDirectionType.Buy) { dtInvestorPosition.Rows.Add( InstrumentID, PosiDirection, HedgeFlag, PositionDate, 0, volume, 0); } else { dtInvestorPosition.Rows.Add( InstrumentID, PosiDirection, HedgeFlag, PositionDate, 0, 0, volume); } } catch { return(false); } } return(true); } }
public bool UpdateByTrade(CZQThostFtdcTradeField pTrade) { lock (this) { TZQThostFtdcPosiDirectionType PosiDirection = TZQThostFtdcPosiDirectionType.Net; TZQThostFtdcPositionDateType PositionDate = TZQThostFtdcPositionDateType.Today; TZQThostFtdcHedgeFlagType HedgeFlag = TZQThostFtdcHedgeFlagType.Speculation; return(InsertOrReplaceForTrade( pTrade.InstrumentID, PosiDirection, pTrade.Direction, HedgeFlag, PositionDate, pTrade.Volume)); } }
//private int x = 0; //查询持仓后调用此函数 public bool InsertOrReplace( string InstrumentID, TZQThostFtdcPosiDirectionType PosiDirection, TZQThostFtdcHedgeFlagType HedgeFlag, TZQThostFtdcPositionDateType PositionDate, int volume, int nLongFrozen, int nShortFrozen) { lock (this) { //冲突的可能性大一些,所以要先Update后Insert DataRow[] rows = Select(InstrumentID, PosiDirection, HedgeFlag, PositionDate); if (rows.Count() == 1) { rows[0][Position] = volume; rows[0][LongFrozen] = nLongFrozen; rows[0][ShortFrozen] = nShortFrozen; } else { try { dtInvestorPosition.Rows.Add( InstrumentID, PosiDirection, HedgeFlag, PositionDate, volume, nLongFrozen, nShortFrozen); } catch { return(false); } } return(true); } }
//private int x = 0; //查询持仓后调用此函数 public bool InsertOrReplace( string InstrumentID, TZQThostFtdcPosiDirectionType PosiDirection, TZQThostFtdcHedgeFlagType HedgeFlag, TZQThostFtdcPositionDateType PositionDate, int volume, int nLongFrozen, int nShortFrozen) { lock(this) { //冲突的可能性大一些,所以要先Update后Insert DataRow[] rows = Select(InstrumentID, PosiDirection, HedgeFlag, PositionDate); if (rows.Count() == 1) { rows[0][Position] = volume; rows[0][LongFrozen] = nLongFrozen; rows[0][ShortFrozen] = nShortFrozen; } else { try { dtInvestorPosition.Rows.Add( InstrumentID, PosiDirection, HedgeFlag, PositionDate, volume, nLongFrozen, nShortFrozen); } catch { return false; } } return true; } }
public void GetPositions( string InstrumentID, TZQThostFtdcPosiDirectionType PosiDirection, TZQThostFtdcHedgeFlagType HedgeFlag, out int YdPosition, out int TodayPosition, out int nLongFrozen, out int nShortFrozen) { YdPosition = 0; TodayPosition = 0; nLongFrozen = 0; nShortFrozen = 0; DataView view = dtInvestorPosition.DefaultView; view.RowFilter = string.Format("InstrumentID='{0}' and PosiDirection={1} and HedgeFlag={2}", InstrumentID, (int)PosiDirection, (int)HedgeFlag); foreach (DataRowView dr in view) { int vol = (int)dr[Position]; int iLongFrozen = (int)dr[LongFrozen]; int iShortFrozen = (int)dr[ShortFrozen]; TZQThostFtdcPositionDateType PositionDate1 = (TZQThostFtdcPositionDateType)dr[PositionDate]; if (TZQThostFtdcPositionDateType.Today == PositionDate1) { TodayPosition += vol; nLongFrozen += iLongFrozen; nShortFrozen += iShortFrozen; } else { YdPosition += vol; nLongFrozen += iLongFrozen; nShortFrozen += iShortFrozen; } } }
public DataRow[] Select(string InstrumentID, TZQThostFtdcPosiDirectionType PosiDirection, TZQThostFtdcHedgeFlagType HedgeFlag, TZQThostFtdcPositionDateType PositionDate) { return dtInvestorPosition.Select( string.Format("InstrumentID='{0}' and PosiDirection={1} and HedgeFlag={2} and PositionDate={3}", InstrumentID, (int)PosiDirection, (int)HedgeFlag, (int)PositionDate)); }
//只收到成交信息时调用 public bool InsertOrReplaceForTrade( string InstrumentID, TZQThostFtdcPosiDirectionType PosiDirection, TZQThostFtdcDirectionType Direction, TZQThostFtdcHedgeFlagType HedgeFlag, TZQThostFtdcPositionDateType PositionDate, int volume) { lock(this) { // 今天的买入要先冻结 //冲突的可能性大一些,所以要先Update后Insert DataRow[] rows = Select(InstrumentID, PosiDirection, HedgeFlag, PositionDate); if (rows.Count() == 1) { int vol = (int)rows[0][Position]; rows[0][Position] = vol - volume; } else { //假设是新添数据 try { if (Direction == TZQThostFtdcDirectionType.Buy) { dtInvestorPosition.Rows.Add( InstrumentID, PosiDirection, HedgeFlag, PositionDate, 0, volume, 0); } else { dtInvestorPosition.Rows.Add( InstrumentID, PosiDirection, HedgeFlag, PositionDate, 0, 0, volume); } } catch { return false; } } return true; } }
public BrokerInfo GetBrokerInfo() { BrokerInfo brokerInfo = new BrokerInfo(); if (IsConnected) { if (_bTdConnected) { //tdlog.Info("GetBrokerInfo"); } else { //if (nGetBrokerInfoCount < 5) //{ // tdlog.Info("GetBrokerInfo,交易没有连接,查询无效,5次后将不显示"); // ++nGetBrokerInfoCount; //} return(null); } BrokerAccount brokerAccount = new BrokerAccount(m_TradingAccount.AccountID); // account fields brokerAccount.BuyingPower = m_TradingAccount.Available; Type t = typeof(CZQThostFtdcTradingAccountField); FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo field in fields) { brokerAccount.AddField(field.Name, field.GetValue(m_TradingAccount).ToString()); } DataRow[] rows = _dbInMemInvestorPosition.SelectAll(); foreach (DataRow dr in rows) { BrokerPosition brokerPosition = new BrokerPosition { Symbol = dr[DbInMemInvestorPosition.InstrumentID].ToString() }; int pos = (int)dr[DbInMemInvestorPosition.Position]; TZQThostFtdcPosiDirectionType PosiDirection = (TZQThostFtdcPosiDirectionType)dr[DbInMemInvestorPosition.PosiDirection]; if (TZQThostFtdcPosiDirectionType.Long == PosiDirection) { brokerPosition.LongQty = pos; } else if (TZQThostFtdcPosiDirectionType.Short == PosiDirection) { brokerPosition.ShortQty = pos; } else { if (pos >= 0)//净NET这个概念是什么情况? { brokerPosition.LongQty = pos; } else { brokerPosition.ShortQty = -pos; } } brokerPosition.Qty = brokerPosition.LongQty - brokerPosition.ShortQty; brokerPosition.AddCustomField(DbInMemInvestorPosition.PosiDirection, PosiDirection.ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.HedgeFlag, ((TZQThostFtdcHedgeFlagType)dr[DbInMemInvestorPosition.HedgeFlag]).ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.PositionDate, ((TZQThostFtdcPositionDateType)dr[DbInMemInvestorPosition.PositionDate]).ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.LongFrozen, dr[DbInMemInvestorPosition.LongFrozen].ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.ShortFrozen, dr[DbInMemInvestorPosition.ShortFrozen].ToString()); brokerAccount.AddPosition(brokerPosition); } brokerInfo.Accounts.Add(brokerAccount); } return(brokerInfo); }
public BrokerInfo GetBrokerInfo() { BrokerInfo brokerInfo = new BrokerInfo(); if (IsConnected) { Console.WriteLine(string.Format("GetBrokerInfo:{0}", Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"))); //TraderApi.TD_ReqQryTradingAccount(m_pTdApi); //TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, null); //timerAccount.Enabled = false; //timerAccount.Enabled = true; //timerPonstion.Enabled = false; //timerPonstion.Enabled = true; BrokerAccount brokerAccount = new BrokerAccount(m_TradingAccount.AccountID); // account fields brokerAccount.BuyingPower = m_TradingAccount.Available; Type t = typeof(CZQThostFtdcTradingAccountField); FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo field in fields) { brokerAccount.AddField(field.Name, field.GetValue(m_TradingAccount).ToString()); } DataRow[] rows = _dbInMemInvestorPosition.SelectAll(); foreach (DataRow dr in rows) { BrokerPosition brokerPosition = new BrokerPosition { Symbol = dr[DbInMemInvestorPosition.InstrumentID].ToString() }; int pos = (int)dr[DbInMemInvestorPosition.Position]; TZQThostFtdcPosiDirectionType PosiDirection = (TZQThostFtdcPosiDirectionType)dr[DbInMemInvestorPosition.PosiDirection]; if (TZQThostFtdcPosiDirectionType.Long == PosiDirection) { brokerPosition.LongQty = pos; } else if (TZQThostFtdcPosiDirectionType.Short == PosiDirection) { brokerPosition.ShortQty = pos; } else { if (pos >= 0)//净NET这个概念是什么情况? { brokerPosition.LongQty = pos; } else { brokerPosition.ShortQty = -pos; } } brokerPosition.Qty = brokerPosition.LongQty - brokerPosition.ShortQty; brokerPosition.AddCustomField(DbInMemInvestorPosition.PosiDirection, PosiDirection.ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.HedgeFlag, ((TZQThostFtdcHedgeFlagType)dr[DbInMemInvestorPosition.HedgeFlag]).ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.PositionDate, ((TZQThostFtdcPositionDateType)dr[DbInMemInvestorPosition.PositionDate]).ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.LongFrozen, dr[DbInMemInvestorPosition.LongFrozen].ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.ShortFrozen, dr[DbInMemInvestorPosition.ShortFrozen].ToString()); brokerAccount.AddPosition(brokerPosition); } brokerInfo.Accounts.Add(brokerAccount); } return(brokerInfo); }