private string GetPositionKey(string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType PositionDate) { return(string.Format("{0}:{1}:{2}:{3}", InstrumentID, PosiDirection, HedgeFlag, PositionDate)); }
//开仓时调用 public bool InsertOrReplaceForOpen( string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType PositionDate, int volume) { lock (this) { //冲突的可能性大一些,所以要先Update后Insert DataRow[] rows = Select(InstrumentID, PosiDirection, HedgeFlag, PositionDate); if (rows.Count() == 1) { rows[0][Position] = volume + (int)rows[0][Position]; } else { try { dtInvestorPosition.Rows.Add( InstrumentID, PosiDirection, HedgeFlag, PositionDate, volume); } catch { return(false); } } return(true); } }
public void GetPositions( string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, out int YdPosition, out int TodayPosition) { YdPosition = 0; TodayPosition = 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]; TThostFtdcPositionDateType PositionDate1 = (TThostFtdcPositionDateType)dr[PositionDate]; if (TThostFtdcPositionDateType.Today == PositionDate1) { TodayPosition += vol; } else { YdPosition += vol; } } }
//只有在平今或平昨时才会调用 public int ReplaceForClose( string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType PositionDate, int volume) { lock (this) { //冲突的可能性大一些,所以要先Update后Insert DataRow[] rows = Select(InstrumentID, PosiDirection, HedgeFlag, PositionDate); int restVol = volume; foreach (DataRow dr in rows) { if (restVol > 0) { int vol = (int)dr[Position]; if (restVol - vol >= 0) { //不够,得多设置几条 dr[Position] = 0; restVol -= vol; } else { //足够了,只设置当前即可 dr[Position] = vol - restVol; restVol = 0; } } } return(restVol); } }
//构造平仓单:对手价限价单(OrderOffsetFlag 平今平昨) public OrderField ClosePositionOrder(string InstrumentID, int VolumeToClose, TThostFtdcPosiDirectionType OrderDirection, TThostFtdcOffsetFlagType OrderOffsetFlag, TThostFtdcOrderPriceTypeType OrderPriceType) { DepthMarketData dmd; this._dicStrategyDmds.TryGetValue(InstrumentID, out dmd); OrderField of = new OrderField(); of.InstrumentID = InstrumentID; of.OrderPriceType = OrderPriceType; of.CombOffsetFlag = new String((char)OrderOffsetFlag, 1); if (OrderDirection == TThostFtdcPosiDirectionType.THOST_FTDC_PD_Long) { of.LimitPrice = dmd.BidPrice1; of.Direction = TThostFtdcDirectionType.THOST_FTDC_D_Sell; } else { of.LimitPrice = dmd.AskPrice1; of.Direction = TThostFtdcDirectionType.THOST_FTDC_D_Buy; } of.VolumeTotalOriginal = VolumeToClose;//确保是正数 return(of); }
public DataRow[] Select(string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType PositionDate) { return(dtInvestorPosition.Select( string.Format("InstrumentID='{0}' and PosiDirection={1} and HedgeFlag={2} and PositionDate={3}", InstrumentID, (int)PosiDirection, (int)HedgeFlag, (int)PositionDate))); }
/// <summary> /// TThostFtdcPosiDirectionType枚举型转为Direction枚举型 /// </summary> /// <param name="tfdt">TThostFtdcPosiDirectionType枚举型</param> /// <returns></returns> public static Direction TThostFtdcPosiDirectionType_To_Direction(TThostFtdcPosiDirectionType tfdt) { Direction dir = Direction.Buy; switch (tfdt) { case TThostFtdcPosiDirectionType.THOST_FTDC_PD_Net: throw new Exception("Direction枚举型中没有Net选项。"); case TThostFtdcPosiDirectionType.THOST_FTDC_PD_Long: break; case TThostFtdcPosiDirectionType.THOST_FTDC_PD_Short: dir = Direction.Sell; break; default: break; } return(dir); }
//只有在TThostFtdcOffsetFlagType.Close时才调用这一函数 public int ReplaceForClose( string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, int volume) { lock (this) { DataView view = dtInvestorPosition.DefaultView; view.RowFilter = string.Format("InstrumentID='{0}' and PosiDirection={1} and HedgeFlag={2}", InstrumentID, (int)PosiDirection, (int)HedgeFlag); view.Sort = string.Format("PositionDate DESC");//将历史的排前面 int restVol = volume; foreach (DataRowView dr in view) { if (restVol > 0) { int vol = (int)dr[Position]; if (restVol - vol >= 0) { //不够,得多设置几条 dr[Position] = 0; restVol -= vol; } else { //足够了,只设置当前即可 dr[Position] = vol - restVol; restVol = 0; } } } return(restVol); } }
//private int x = 0; //查询持仓后调用此函数 public bool InsertOrReplace( string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType PositionDate, int volume) { lock(this) { //冲突的可能性大一些,所以要先Update后Insert DataRow[] rows = Select(InstrumentID, PosiDirection, HedgeFlag, PositionDate); if (rows.Count() == 1) { rows[0][Position] = volume; } else { try { dtInvestorPosition.Rows.Add( InstrumentID, PosiDirection, HedgeFlag, PositionDate, volume); } catch { return false; } } return true; } }
//只有在TThostFtdcOffsetFlagType.Close时才调用这一函数 public int ReplaceForClose( string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, int volume) { lock(this) { DataView view = dtInvestorPosition.DefaultView; view.RowFilter = string.Format("InstrumentID='{0}' and PosiDirection={1} and HedgeFlag={2}", InstrumentID, (int)PosiDirection, (int)HedgeFlag); view.Sort = "PositionDate DESC";//将历史的排前面 int restVol = volume; foreach (DataRowView dr in view) { if (restVol > 0) { int vol = (int)dr[Position]; if (restVol - vol >= 0) { //不够,得多设置几条 dr[Position] = 0; restVol -= vol; } else { //足够了,只设置当前即可 dr[Position] = vol - restVol; restVol = 0; } } } return restVol; } }
//只有在平今或平昨时才会调用 public int ReplaceForClose( string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType PositionDate, int volume) { lock(this) { //冲突的可能性大一些,所以要先Update后Insert DataRow[] rows = Select(InstrumentID, PosiDirection, HedgeFlag, PositionDate); int restVol = volume; foreach (DataRow dr in rows) { if (restVol > 0) { int vol = (int)dr[Position]; if (restVol - vol >= 0) { //不够,得多设置几条 dr[Position] = 0; restVol -= vol; } else { //足够了,只设置当前即可 dr[Position] = vol - restVol; restVol = 0; } } } return restVol; } }
//只收到成交信息时调用 public bool InsertOrReplaceForTrade( string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcDirectionType Direction, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType 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 == TThostFtdcDirectionType.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) { BuyingPower = m_TradingAccount.Available }; Type t = typeof(CThostFtdcTradingAccountField); 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]; TThostFtdcPosiDirectionType PosiDirection = (TThostFtdcPosiDirectionType)dr[DbInMemInvestorPosition.PosiDirection]; if (TThostFtdcPosiDirectionType.Long == PosiDirection) { brokerPosition.LongQty = pos; } else if (TThostFtdcPosiDirectionType.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, ((TThostFtdcHedgeFlagType)dr[DbInMemInvestorPosition.HedgeFlag]).ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.PositionDate, ((TThostFtdcPositionDateType)dr[DbInMemInvestorPosition.PositionDate]).ToString()); brokerAccount.AddPosition(brokerPosition); } brokerInfo.Accounts.Add(brokerAccount); } return(brokerInfo); }
private string GetPositionKey(string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType PositionDate) { return string.Format("{0}:{1}:{2}:{3}", InstrumentID, PosiDirection, HedgeFlag, PositionDate); }
public DataRow[] Select(string InstrumentID, TThostFtdcPosiDirectionType PosiDirection, TThostFtdcHedgeFlagType HedgeFlag, TThostFtdcPositionDateType 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 UpdateByTrade(CThostFtdcTradeField pTrade) { /* * Q:向非上海市场发送平今指今,系统能自动处理成平今,接到的回报是平今还是平仓? * A:上海市场的昨仓用Close和CloseYesterday都能平,报单回报不会改变,而成交回报会修改成CloseYesterday * * Q:是否不同市场收到的成交回报都分为了今天与历史。非上海市场不时用Close时,返回数据能区分CloseToday和CloseYesterday吗?还是只回Close * * Q:上海强平今仓与昨仓会收到什么结果? * * Q:非上海市场,一手今,一手昨,同时平,收到的回报会如何? * */ //如果是开仓,查找唯一的那条记录,不存在就插入 //如果是平今,只查找今天的记录,直接修改 //如果是平昨,只处理昨天 //如果是平仓,从历史开始处理 lock (this) { TThostFtdcPosiDirectionType PosiDirection = TThostFtdcPosiDirectionType.Short; TThostFtdcPositionDateType PositionDate = TThostFtdcPositionDateType.Today; if (TThostFtdcOffsetFlagType.Open == pTrade.OffsetFlag) { if (TThostFtdcDirectionType.Buy == pTrade.Direction) { PosiDirection = TThostFtdcPosiDirectionType.Long; } return(InsertOrReplaceForOpen(pTrade.InstrumentID, PosiDirection, pTrade.HedgeFlag, PositionDate, pTrade.Volume)); } else { if (TThostFtdcDirectionType.Sell == pTrade.Direction) { PosiDirection = TThostFtdcPosiDirectionType.Long; } if (TThostFtdcOffsetFlagType.CloseToday == pTrade.OffsetFlag) { return(ReplaceForClose(pTrade.InstrumentID, PosiDirection, pTrade.HedgeFlag, PositionDate, pTrade.Volume) == 0); } else if (TThostFtdcOffsetFlagType.CloseYesterday == pTrade.OffsetFlag) { PositionDate = TThostFtdcPositionDateType.History; return(ReplaceForClose(pTrade.InstrumentID, PosiDirection, pTrade.HedgeFlag, PositionDate, pTrade.Volume) == 0); } else if (TThostFtdcOffsetFlagType.Close == pTrade.OffsetFlag) { return(ReplaceForClose(pTrade.InstrumentID, PosiDirection, pTrade.HedgeFlag, pTrade.Volume) == 0); } else { //无法计算的开平类型,要求直接发送查询请求 return(false); } } } }
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; brokerAccount.AddField("Available", m_TradingAccount.Available.ToString()); brokerAccount.AddField("Balance", m_TradingAccount.Balance.ToString()); brokerAccount.AddField("CashIn", m_TradingAccount.CashIn.ToString()); brokerAccount.AddField("CloseProfit", m_TradingAccount.CloseProfit.ToString()); brokerAccount.AddField("Commission", m_TradingAccount.Commission.ToString()); brokerAccount.AddField("Credit", m_TradingAccount.Credit.ToString()); brokerAccount.AddField("CurrMargin", m_TradingAccount.CurrMargin.ToString()); brokerAccount.AddField("DeliveryMargin", m_TradingAccount.DeliveryMargin.ToString()); brokerAccount.AddField("Deposit", m_TradingAccount.Deposit.ToString()); brokerAccount.AddField("ExchangeDeliveryMargin", m_TradingAccount.ExchangeDeliveryMargin.ToString()); brokerAccount.AddField("ExchangeMargin", m_TradingAccount.ExchangeMargin.ToString()); brokerAccount.AddField("FrozenCash", m_TradingAccount.FrozenCash.ToString()); brokerAccount.AddField("FrozenCommission", m_TradingAccount.FrozenCommission.ToString()); brokerAccount.AddField("FrozenMargin", m_TradingAccount.FrozenMargin.ToString()); brokerAccount.AddField("Interest", m_TradingAccount.Interest.ToString()); brokerAccount.AddField("InterestBase", m_TradingAccount.InterestBase.ToString()); brokerAccount.AddField("Mortgage", m_TradingAccount.Mortgage.ToString()); brokerAccount.AddField("PositionProfit", m_TradingAccount.PositionProfit.ToString()); brokerAccount.AddField("PreBalance", m_TradingAccount.PreBalance.ToString()); brokerAccount.AddField("PreCredit", m_TradingAccount.PreCredit.ToString()); brokerAccount.AddField("PreDeposit", m_TradingAccount.PreDeposit.ToString()); brokerAccount.AddField("PreMargin", m_TradingAccount.PreMargin.ToString()); brokerAccount.AddField("PreMortgage", m_TradingAccount.PreMortgage.ToString()); brokerAccount.AddField("Reserve", m_TradingAccount.Reserve.ToString()); brokerAccount.AddField("SettlementID", m_TradingAccount.SettlementID.ToString()); brokerAccount.AddField("Withdraw", m_TradingAccount.Withdraw.ToString()); brokerAccount.AddField("WithdrawQuota", m_TradingAccount.WithdrawQuota.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]; TThostFtdcPosiDirectionType PosiDirection = (TThostFtdcPosiDirectionType)dr[DbInMemInvestorPosition.PosiDirection]; if (TThostFtdcPosiDirectionType.Long == PosiDirection) { brokerPosition.LongQty = pos; } else if (TThostFtdcPosiDirectionType.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, ((TThostFtdcHedgeFlagType)dr[DbInMemInvestorPosition.HedgeFlag]).ToString()); brokerPosition.AddCustomField(DbInMemInvestorPosition.PositionDate, ((TThostFtdcPositionDateType)dr[DbInMemInvestorPosition.PositionDate]).ToString()); brokerAccount.AddPosition(brokerPosition); } brokerInfo.Accounts.Add(brokerAccount); } return(brokerInfo); }