Exemplo n.º 1
0
 public static extern int TD_SendOrder(
     IntPtr pTraderApi,
     int localOrderID,
     string szInstrument,
     DFITCBuySellTypeType sBuySellType,
     DFITCOpenCloseTypeType sOpenCloseType,
     DFITCSpeculatorType sSpeculator,
     int lAmount,
     double dbPrice,
     DFITCOrderTypeType orderType,
     DFITCOrderPropertyType orderProperty,
     DFITCInstrumentTypeType nInstrumentType,
     DFITCInsertType insertType);
        private void Send(NewOrderSingle order)
        {
            if (order == null)
            {
                return;
            }

            if (!_bTdConnected)
            {
                EmitError(-1, -1, "交易服务器没有连接,无法报单");
                tdlog.Error("交易服务器没有连接,无法报单");
                return;
            }

            Instrument inst        = InstrumentManager.Instruments[order.Symbol];
            string     altSymbol   = inst.GetSymbol(Name);
            string     altExchange = inst.GetSecurityExchange(Name);
            double     tickSize    = inst.TickSize;
            string     type        = inst.SecurityType;

            //    CThostFtdcInstrumentField _Instrument;
            //    if (_dictInstruments.TryGetValue(altSymbol, out _Instrument))
            //    {
            //        //从合约列表中取交易所名与tickSize,不再依赖用户手工设置的参数了
            //        tickSize = _Instrument.PriceTick;
            //        altExchange = _Instrument.ExchangeID;
            //    }

            //最小变动价格修正
            double price = order.Price;

            //市价修正,如果不连接行情,此修正不执行,得策略层处理
            DFITCDepthMarketDataField DepthMarket;

            //如果取出来了,并且为有效的,涨跌停价将不为0
            _dictDepthMarketData.TryGetValue(altSymbol, out DepthMarket);

            // 市价单模拟
            if (OrdType.Market == order.OrdType)
            {
                //按买卖调整价格
                if (order.Side == Side.Buy)
                {
                    price = DepthMarket.LastPrice + LastPricePlusNTicks * tickSize;
                }
                else
                {
                    price = DepthMarket.LastPrice - LastPricePlusNTicks * tickSize;
                }
            }

            //没有设置就直接用
            if (tickSize > 0)
            {
                decimal remainder = ((decimal)price % (decimal)tickSize);
                if (remainder != 0)
                {
                    if (order.Side == Side.Buy)
                    {
                        price = Math.Ceiling(price / tickSize) * tickSize;
                    }
                    else
                    {
                        price = Math.Floor(price / tickSize) * tickSize;
                    }
                }
                else
                {
                    //正好能整除,不操作
                }
            }

            if (0 == DepthMarket.UpperLimitPrice &&
                0 == DepthMarket.LowerLimitPrice)
            {
                //涨跌停无效
            }
            else
            {
                //防止价格超过涨跌停
                if (price >= DepthMarket.UpperLimitPrice)
                {
                    price = DepthMarket.UpperLimitPrice;
                }
                else if (price <= DepthMarket.LowerLimitPrice)
                {
                    price = DepthMarket.LowerLimitPrice;
                }
            }


            DFITCOpenCloseTypeType szCombOffsetFlag;

            //根据 梦翔 与 马不停蹄 的提示,新加在Text域中指定开平标志的功能
            if (order.Text.StartsWith(OpenPrefix))
            {
                szCombOffsetFlag = DFITCOpenCloseTypeType.OPEN;
            }
            else if (order.Text.StartsWith(ClosePrefix))
            {
                szCombOffsetFlag = DFITCOpenCloseTypeType.CLOSE;
            }
            else if (order.Text.StartsWith(CloseTodayPrefix))
            {
                szCombOffsetFlag = DFITCOpenCloseTypeType.CLOSETODAY;
            }
            else if (order.Text.StartsWith(CloseYesterdayPrefix))
            {
                szCombOffsetFlag = DFITCOpenCloseTypeType.CLOSE;
            }
            else if (order.Text.StartsWith(ExecutePrefix))
            {
                szCombOffsetFlag = DFITCOpenCloseTypeType.EXECUTE;
            }
            else
            {
                szCombOffsetFlag = DFITCOpenCloseTypeType.OPEN;
            }

            DFITCInsertType insertType = DFITCInsertType.BASIC_ORDER;

            if (order.Text.Length >= 3)
            {
                if (order.Text[2] == '*')
                {
                    insertType = DFITCInsertType.AUTO_ORDER;
                }
            }

            int leave = (int)order.OrderQty;

            DFITCSpeculatorType szCombHedgeFlag = SpeculatorType;

            bool bSupportMarketOrder = SupportMarketOrder.Contains(altExchange);

            tdlog.Info("Side:{0},Price:{1},LastPrice:{2},Qty:{3},Text:{4}",
                       order.Side, order.Price, DepthMarket.LastPrice, order.OrderQty, order.Text);

            DFITCBuySellTypeType    sBuySellType    = order.Side == Side.Buy ? DFITCBuySellTypeType.BUY : DFITCBuySellTypeType.SELL;
            DFITCOrderTypeType      orderType       = DFITCOrderTypeType.LIMITORDER;
            DFITCOrderPropertyType  orderProperty   = DFITCOrderPropertyType.NON;
            DFITCInstrumentTypeType nInstrumentType = (FIXSecurityType.FutureOption == type) ? DFITCInstrumentTypeType.OPT_TYPE : DFITCInstrumentTypeType.COMM_TYPE;

            switch (order.TimeInForce)
            {
            case TimeInForce.IOC:
                orderProperty = DFITCOrderPropertyType.FAK;
                break;

            case TimeInForce.FOK:
                orderProperty = DFITCOrderPropertyType.FOK;
                break;

            default:
                break;
            }

            int nRet = 0;

            switch (order.OrdType)
            {
            case OrdType.Limit:
                break;

            case OrdType.Market:
                if (SwitchMakertOrderToLimitOrder || !bSupportMarketOrder)
                {
                }
                else
                {
                    //price = 0;
                    orderType = DFITCOrderTypeType.MKORDER;
                }
                break;

            default:
                tdlog.Warn("没有实现{0}", order.OrdType);
                return;
            }

            nRet = TraderApi.TD_SendOrder(m_pTdApi, -1,
                                          altSymbol,
                                          sBuySellType,
                                          szCombOffsetFlag,
                                          szCombHedgeFlag,
                                          leave,
                                          price,
                                          orderType,
                                          orderProperty,
                                          nInstrumentType,
                                          insertType);

            if (nRet > 0)
            {
                _OrderRef2Order[string.Format("{0}:{1}", _RspUserLogin.sessionID, nRet)] = order as SingleOrder;
            }
        }