public CommonOrderItem Add(SingleOrder order, TextRequest t) { if(item == null || item.IsDone()) { item = new CommonOrderItem(); } item.Add(order, t); if (item.IsDone()) { return item; } return null; }
public CommonOrderItem Add(SingleOrder order, TextRequest t) { if (item == null || item.IsDone()) { item = new CommonOrderItem(); } item.Add(order, t); if (item.IsDone()) { return(item); } return(null); }
private void Send(NewOrderSingle order) { if (!_bTdConnected) { EmitError(-1, -1, "交易服务器没有连接,无法报单"); tdlog.Error("交易服务器没有连接,无法报单"); return; } // 表示特殊的Json格式 if (order.Text.StartsWith("{") && order.Text.EndsWith("}")) { TextParameter parameter = JsonConvert.DeserializeObject <TextParameter>(order.Text); switch (parameter.Type) { case EnumGroupType.COMMON: { TextCommon t = JsonConvert.DeserializeObject <TextCommon>(order.Text); CommonOrderItem item = CommonOrderCombiner.Add(order as SingleOrder, t); Send(item); } break; case EnumGroupType.QUOTE: { TextQuote t = JsonConvert.DeserializeObject <TextQuote>(order.Text); QuoteOrderItem item = QuoteOrderCombiner.Add(order as SingleOrder, t); } break; case EnumGroupType.SP: { TextSP t = JsonConvert.DeserializeObject <TextSP>(order.Text); SPOrderItem item = SPOrderCombiner.Add(order as SingleOrder, t); Send(item); } break; case EnumGroupType.SPC: { TextSPC t = JsonConvert.DeserializeObject <TextSPC>(order.Text); SPCOrderItem item = SPCOrderCombiner.Add(order as SingleOrder, t); Send(item); } break; case EnumGroupType.SPD: { TextSPD t = JsonConvert.DeserializeObject <TextSPD>(order.Text); SPDOrderItem item = SPDOrderCombiner.Add(order as SingleOrder, t); Send(item); } break; } } else { // 无法识别的格式,直接发送报单,只开仓 TextCommon t = new TextCommon() { Type = EnumGroupType.COMMON, OpenClose = EnumOpenClose.OPEN }; CommonOrderItem item = CommonOrderCombiner.Add(order as SingleOrder, t); Send(item); } }
private void Send(CommonOrderItem item) { if (item == null) { return; } SingleOrder order = item.Leg.Order; string apiSymbol; string apiExchange; double apiTickSize; string altSymbol; #if CTP GetInstrumentInfoForCTP(order.Instrument, out apiSymbol, out apiExchange, out apiTickSize); altSymbol = apiSymbol; #elif CTPZQ GetInstrumentInfoForCTPZQ(order.Instrument, out apiSymbol, out apiExchange, out apiTickSize, out altSymbol); #endif double price = order.Price; int qty = (int)order.OrderQty; //市价修正,如果不连接行情,此修正不执行,得策略层处理 CThostFtdcDepthMarketDataField DepthMarket; //如果取出来了,并且为有效的,涨跌停价将不为0 _dictDepthMarketData.TryGetValue(altSymbol, out DepthMarket); //市价单模拟 if (OrdType.Market == order.OrdType) { //按买卖调整价格 if (order.Side == Side.Buy) { price = DepthMarket.LastPrice + LastPricePlusNTicks * apiTickSize; } else { price = DepthMarket.LastPrice - LastPricePlusNTicks * apiTickSize; } } price = FixPrice(price, order.Side, apiTickSize, DepthMarket.LowerLimitPrice, DepthMarket.UpperLimitPrice); // 是否要做价格调整? byte[] bytes = { (byte)CTPAPI.ToCTP(item.Leg.OpenClose) }; string szCombOffsetFlag = System.Text.Encoding.Default.GetString(bytes, 0, bytes.Length); byte[] bytes2 = { (byte)HedgeFlagType }; string szCombHedgeFlag = System.Text.Encoding.Default.GetString(bytes2, 0, bytes2.Length); TThostFtdcDirectionType Direction = order.Side == Side.Buy ? TThostFtdcDirectionType.Buy : TThostFtdcDirectionType.Sell; TThostFtdcOrderPriceTypeType OrderPriceType = TThostFtdcOrderPriceTypeType.LimitPrice; TThostFtdcTimeConditionType TimeCondition = TThostFtdcTimeConditionType.GFD; TThostFtdcContingentConditionType ContingentCondition = TThostFtdcContingentConditionType.Immediately; TThostFtdcVolumeConditionType VolumeCondition = TThostFtdcVolumeConditionType.AV; #if CTP bool bSupportMarketOrder = SupportMarketOrder.Contains(apiExchange); #elif CTPZQ bool bSupportMarketOrder = true; #endif switch (order.TimeInForce) { case TimeInForce.IOC: TimeCondition = TThostFtdcTimeConditionType.IOC; VolumeCondition = TThostFtdcVolumeConditionType.AV; break; case TimeInForce.FOK: TimeCondition = TThostFtdcTimeConditionType.IOC; VolumeCondition = TThostFtdcVolumeConditionType.CV; break; default: break; } int nRet = 0; switch (order.OrdType) { case OrdType.Limit: break; case OrdType.Market: if (SwitchMakertOrderToLimitOrder || !bSupportMarketOrder) { } else { price = 0; OrderPriceType = TThostFtdcOrderPriceTypeType.AnyPrice; TimeCondition = TThostFtdcTimeConditionType.IOC; } break; default: tdlog.Warn("没有实现{0}", order.OrdType); return; } #if CTP nRet = TraderApi.TD_SendOrder(m_pTdApi, apiSymbol, Direction, szCombOffsetFlag, szCombHedgeFlag, qty, price, OrderPriceType, TimeCondition, ContingentCondition, order.StopPx, VolumeCondition); #elif CTPZQ nRet = TraderApi.TD_SendOrder(m_pTdApi, apiSymbol, apiExchange, Direction, szCombOffsetFlag, szCombHedgeFlag, qty, string.Format("{0}", price), OrderPriceType, TimeCondition, ContingentCondition, order.StopPx, VolumeCondition); #endif if (nRet > 0) { orderMap.CreateNewOrder(string.Format("{0}:{1}:{2}", _RspUserLogin.FrontID, _RspUserLogin.SessionID, nRet), item); } }