Beispiel #1
0
        /// <summary>
        /// TThostFtdcHedgeFlagType枚举型转为HedgeFlag枚举型
        /// </summary>
        /// <param name="tfhft">TThostFtdcHedgeFlagType枚举型</param>
        /// <returns></returns>
        public static HedgeFlag TThostFtdcHedgeFlagType_To_HedgeFlag(TThostFtdcHedgeFlagType tfhft)
        {
            HedgeFlag hf = HedgeFlag.Speculation;

            switch (tfhft)
            {
            case TThostFtdcHedgeFlagType.THOST_FTDC_HF_Speculation:
                break;

            case TThostFtdcHedgeFlagType.THOST_FTDC_HF_Arbitrage:
                hf = HedgeFlag.Arbitrage;
                break;

            case TThostFtdcHedgeFlagType.THOST_FTDC_HF_Hedge:
                hf = HedgeFlag.Hedge;
                break;

            case TThostFtdcHedgeFlagType.THOST_FTDC_HF_MarketMaker:
                hf = HedgeFlag.MarketMaker;
                break;

            default:
                break;
            }
            return(hf);
        }
Beispiel #2
0
        //开仓时调用
        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);
            }
        }
Beispiel #3
0
        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 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;
                }
            }
        }
Beispiel #5
0
        //只有在平今或平昨时才会调用
        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);
            }
        }
Beispiel #6
0
        public int SendQuote(
            int QuoteRef,
            string szInstrument,
            double AskPrice,
            double BidPrice,
            int AskVolume,
            int BidVolume,
            TThostFtdcOffsetFlagType AskOffsetFlag,
            TThostFtdcOffsetFlagType BidOffsetFlag,
            TThostFtdcHedgeFlagType AskHedgeFlag,
            TThostFtdcHedgeFlagType BidHedgeFlag)
        {
            if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
            {
                return(0);
            }

            return(TraderApi.TD_SendQuote(
                       IntPtrKey,
                       QuoteRef,
                       szInstrument,
                       AskPrice,
                       BidPrice,
                       AskVolume,
                       BidVolume,
                       AskOffsetFlag,
                       BidOffsetFlag,
                       AskHedgeFlag,
                       BidHedgeFlag));
        }
 private string GetPositionKey(string InstrumentID,
                               TThostFtdcPosiDirectionType PosiDirection,
                               TThostFtdcHedgeFlagType HedgeFlag,
                               TThostFtdcPositionDateType PositionDate)
 {
     return(string.Format("{0}:{1}:{2}:{3}", InstrumentID, PosiDirection, HedgeFlag, PositionDate));
 }
Beispiel #8
0
 public void ReqQryInstrumentMarginRate(string szInstrument, TThostFtdcHedgeFlagType HedgeFlag)
 {
     if (null == m_pTdApi || IntPtr.Zero == m_pTdApi)
     {
         return;
     }
     TraderApi.TD_ReqQryInstrumentMarginRate(m_pTdApi, szInstrument, HedgeFlag);
 }
Beispiel #9
0
 public static extern int TD_SendQuote(
     IntPtr pTraderApi,
     int QuoteRef,
     string szInstrument,
     double AskPrice,
     double BidPrice,
     int AskVolume,
     int BidVolume,
     TThostFtdcOffsetFlagType AskOffsetFlag,
     TThostFtdcOffsetFlagType BidOffsetFlag,
     TThostFtdcHedgeFlagType AskHedgeFlag,
     TThostFtdcHedgeFlagType BidHedgeFlag);
Beispiel #10
0
        public void ReqQryInstrumentMarginRate(string instrument, TThostFtdcHedgeFlagType HedgeFlag)
        {
            if (null != MarginRates)
            {
                CThostFtdcInstrumentMarginRateField value;
                if (MarginRates.TryGetValue(instrument, out value))
                {
                    FireOnRspQryInstrumentMarginRate(value);
                    return;
                }
            }

            if (!string.IsNullOrEmpty(instrument) &&
                null != m_pTdApi &&
                IntPtr.Zero != m_pTdApi)
            {
                TraderApi.TD_ReqQryInstrumentMarginRate(m_pTdApi, instrument, HedgeFlag);
            }
        }
Beispiel #11
0
        public void ReqQryInstrumentMarginRate(string instrument, TThostFtdcHedgeFlagType HedgeFlag)
        {
            if (null != MarginRates)
            {
                CThostFtdcInstrumentMarginRateField value;
                if (MarginRates.TryGetValue(instrument, out value))
                {
                    FireOnRspQryInstrumentMarginRate(value);
                    return;
                }
            }

            if (!string.IsNullOrEmpty(instrument)
                && null != m_pTdApi
                && IntPtr.Zero != m_pTdApi)
            {
                TraderApi.TD_ReqQryInstrumentMarginRate(m_pTdApi, instrument, HedgeFlag);
            }
        }        
        private void Send(QuoteOrderItem item)
        {
            if (item == null)
            {
                return;
            }

            SingleOrder AskOrder = item.Sell.Order;
            SingleOrder BidOrder = item.Buy.Order;

            string symbol = item.Buy.Order.Symbol;

            double AskPrice  = AskOrder.Price;
            double BidPrice  = BidOrder.Price;
            int    AskVolume = (int)AskOrder.OrderQty;
            int    BidVolume = (int)BidOrder.OrderQty;

            TThostFtdcOffsetFlagType AskOffsetFlag = CTPAPI.ToCTP(item.Sell.OpenClose);
            TThostFtdcOffsetFlagType BidOffsetFlag = CTPAPI.ToCTP(item.Buy.OpenClose);

            TThostFtdcHedgeFlagType AskHedgeFlag = HedgeFlagType;
            TThostFtdcHedgeFlagType BidHedgeFlag = HedgeFlagType;

            int nRet = 0;

#if CTP
            nRet = TraderApi.TD_SendQuote(m_pTdApi,
                                          -1,
                                          symbol,
                                          AskPrice,
                                          BidPrice,
                                          AskVolume,
                                          BidVolume,
                                          AskOffsetFlag,
                                          BidOffsetFlag,
                                          AskHedgeFlag,
                                          BidHedgeFlag);
#endif
            if (nRet > 0)
            {
                orderMap.CreateNewOrder(string.Format("{0}:{1}:{2}", _RspUserLogin.FrontID, _RspUserLogin.SessionID, nRet), item);
            }
        }
Beispiel #13
0
        //只有在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;
            }
        }
Beispiel #15
0
		/// <summary>
		/// 请求查询合约保证金率:能为null;每次只能查一个合约
		/// </summary>
		/// <param name="hedgeFlag">投机/套保</param>
		/// <param name="instrumentID">合约代码:不填-查所有</param>
		public int QryInstrumentMarginRate(string instrumentID,TThostFtdcHedgeFlagType hedgeFlag = TThostFtdcHedgeFlagType.Speculation)
		{ 
			return reqQryInstrumentMarginRate(this.BrokerID, this.InvestorID, instrumentID, hedgeFlag); 
		}
 private string GetPositionKey(string InstrumentID,
     TThostFtdcPosiDirectionType PosiDirection,
     TThostFtdcHedgeFlagType HedgeFlag,
     TThostFtdcPositionDateType PositionDate)
 {
     return string.Format("{0}:{1}:{2}:{3}", InstrumentID, PosiDirection, HedgeFlag, PositionDate);
 }
Beispiel #17
0
 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 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));
 }
Beispiel #19
0
 public void ReqQryInstrumentMarginRate(string szInstrument, TThostFtdcHedgeFlagType HedgeFlag)
 {
     m_Api.ReqQryInstrumentMarginRate(szInstrument, HedgeFlag);
 }
 public void ReqQryInstrumentMarginRate(string szInstrument, TThostFtdcHedgeFlagType HedgeFlag)
 {
     m_Api.ReqQryInstrumentMarginRate(szInstrument, HedgeFlag);
 }
Beispiel #21
0
 public static extern int TD_SendQuote(
     IntPtr pTraderApi,
     int QuoteRef,
     string szInstrument,
     string szExchange,
     double AskPrice,
     double BidPrice,
     int AskVolume,
     int BidVolume,
     TThostFtdcOffsetFlagType AskOffsetFlag,
     TThostFtdcOffsetFlagType BidOffsetFlag,
     TThostFtdcHedgeFlagType AskHedgeFlag,
     TThostFtdcHedgeFlagType BidHedgeFlag);
Beispiel #22
0
 public void ReqQryInstrumentMarginRate(string szInstrument, TThostFtdcHedgeFlagType HedgeFlag)
 {
     if (null == m_pTdApi || IntPtr.Zero == m_pTdApi)
     {
         return;
     }
     TraderApi.TD_ReqQryInstrumentMarginRate(m_pTdApi, szInstrument, HedgeFlag);
 }
        //只收到成交信息时调用
        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;
            }
        }
Beispiel #24
0
        public int SendQuote(
            int QuoteRef,
            string szInstrument,
            string szExchange,
            double AskPrice,
            double BidPrice,
            int AskVolume,
            int BidVolume,
            TThostFtdcOffsetFlagType AskOffsetFlag,
            TThostFtdcOffsetFlagType BidOffsetFlag,
            TThostFtdcHedgeFlagType AskHedgeFlag,
            TThostFtdcHedgeFlagType BidHedgeFlag)
        {
            if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
            {
                return 0;
            }

            return TraderApi.TD_SendQuote(
                IntPtrKey,
                QuoteRef,
                szInstrument,
                szExchange,
                AskPrice,
                BidPrice,
                AskVolume,
                BidVolume,
                AskOffsetFlag,
                BidOffsetFlag,
                AskHedgeFlag,
                BidHedgeFlag);
        }
Beispiel #25
0
 public int SendOrder(
     int OrderRef,
     string szInstrument,
     TThostFtdcDirectionType Direction,
     TThostFtdcOffsetFlagType OffsetFlag,
     TThostFtdcHedgeFlagType HedgeFlag,
     int VolumeTotalOriginal,
     double LimitPrice,
     TThostFtdcOrderPriceTypeType OrderPriceType,
     TThostFtdcTimeConditionType TimeCondition,
     TThostFtdcContingentConditionType ContingentCondition,
     double StopPrice,
     TThostFtdcVolumeConditionType VolumeCondition)
 {
     if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
     {
         return 0;
     }
     char szOffsetFlag = (char) OffsetFlag;
     char hedgeFlag = (char) HedgeFlag;
     return TraderApi.TD_SendOrder(
        IntPtrKey,
        OrderRef,
        szInstrument,
        Direction,
        szOffsetFlag.ToString(),
        hedgeFlag.ToString(),
        VolumeTotalOriginal,
        LimitPrice,
        OrderPriceType,
        TimeCondition,
        ContingentCondition,
        StopPrice,
        VolumeCondition);
 }
Beispiel #26
0
		static extern int reqQryInstrumentMarginRate(string BROKER_ID, string INVESTOR_ID, string INSTRUMENT_ID, TThostFtdcHedgeFlagType HEDGE_FLAG);
Beispiel #27
0
 public static extern void TD_ReqQryInstrumentMarginRate(IntPtr pTraderApi, string szInstrument, TThostFtdcHedgeFlagType HedgeFlag);
        //只有在平今或平昨时才会调用
        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;
            }
        }
Beispiel #29
0
 public static extern void TD_ReqQryInstrumentMarginRate(IntPtr pTraderApi, string szInstrument, TThostFtdcHedgeFlagType HedgeFlag);
        //只有在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;
            }
        }