private bool SendTrust(_Truster truster, double price) { bool bRet = true; lock (__oLock) { double dPriceScale = this.Bars.Info.PriceScale; double[] dLimitPrices = GetLimitPrice(dPriceScale); List<TradeOrder> cOrders = truster.CreateTrusts(Bars.CurrentBar, price, this.TrustCount, this.LotsPerTrust, dLimitPrices[0], dLimitPrices[1], dPriceScale); if (cOrders == null) { bRet = false; } else { foreach (TradeOrder cOrder in cOrders) { if (!cOrder.IsCancel) { if (cOrder.Ticket == null) { if (bRet) { bRet = base.Send(cOrder.Action, cOrder.Category, cOrder.Price, cOrder.Contracts, cOrder.IsReverse, 0, cOrder.Name, false); } truster.SendCompleted(cOrder, bRet); } else { cOrder.IsCancel = this.SendTrust(cOrder, true); truster.SendCompleted(cOrder, cOrder.IsCancel); } } } } } return bRet; }
/// <summary> /// 傳送下單命令 /// </summary> /// <param name="action">下單進出場動作</param> /// <param name="category">下單類型</param> /// <param name="limitPrice">限價價格(市價=0)</param> /// <param name="lots">下單數量</param> /// <param name="isReverse">是否需反轉倉位</param> /// <param name="touchPrice">觸發或停損價格</param> /// <param name="name">下單註解</param> /// <param name="openNextBar">是否開倉在下一根 Bars</param> public override bool Send(EOrderAction action, OrderCategory category, double limitPrice, int lots, bool isReverse, double touchPrice = 0, string name = null, bool openNextBar = false) { if (this.Bars.IsLastBars) { double dPrice = Math.Round((limitPrice == 0) ? ((action == EOrderAction.Buy || action == EOrderAction.BuyToCover) ? Bars.DOM.Ask[0].Price : Bars.DOM.Bid[0].Price) : limitPrice, 2); //檢查是否為平倉單(如果是平倉單, 會把倉部位內所有的委託新倉單全部取消之後才會下平倉單) if (action == EOrderAction.Sell || action == EOrderAction.BuyToCover) { int iCancelCount = 0; lock (__cTrusts) { foreach (_Truster cTemp in __cTrusts.Values) { if (!cTemp.IsClosed) { TradeOrder[] cOrders = cTemp.GetTrustTickets(); if (cOrders != null) { cTemp.Cancel(); foreach (TradeOrder cOrder in cOrders) { if (cOrder.IsSended || cOrder.IsTrusted) { if (!cOrder.IsCancel && cOrder.Contracts > 0) { cOrder.IsCancel = this.SendTrust(cOrder, true); } ++iCancelCount; } } } } } } if (iCancelCount > 0) { return false; } __bClosedFlag = true; } if (__bClosedFlag && (action == EOrderAction.Buy || action == EOrderAction.SellShort)) { return false; } _Truster cTruster = null; lock (__cTrusts) { if (!__cTrusts.TryGetValue(name, out cTruster)) { TradeOrder cOrder = new TradeOrder(); cOrder.Action = action; cOrder.BarNumber = Bars.CurrentBar; cOrder.Category = category; cOrder.Contracts = lots; cOrder.IsReverse = false; cOrder.Name = name; cTruster = new _Truster(cOrder); __cTrusts.Add(name, cTruster); } } return SendTrust(cTruster, dPrice); } return false; }
/// <summary> /// 傳送下單命令 /// </summary> /// <param name="trust">交易訂單資訊</param> /// <param name="isCancel">是否要取消此交易訂單(成功委託才能取消訂單)</param> /// <returns>返回值: true=成功, false=失敗</returns> bool IOrderSender.Send(TradeOrder trust, bool isCancel) { if (isCancel) { string[] sNames = trust.Name.Split('|'); _Truster cTruster = null; lock (__cTrusts) { __cTrusts.TryGetValue(sNames[0], out cTruster); } if (cTruster != null && !cTruster.IsCancel) { cTruster.Cancel(); } return base.SendTrust(trust, true); } else { return base.SendTrust(trust, false); } }
private void GridOrderService_onResponse(object sender, ResponseEvent e) { if (e.ResponseType == ResponseType.Update) { //回報類型為 Update 都忽略(此為盈利更新通知) return; } TradeOrder cOrder = e.TradeOrder as TradeOrder; string[] sNames = cOrder.Name.Split('|'); _Truster cTruster = null; lock (__cTrusts) { __cTrusts.TryGetValue(sNames[0], out cTruster); } if (cTruster != null) { switch (e.ResponseType) { case ResponseType.Cancel: case ResponseType.Deal: if (e.ResponseType == ResponseType.Cancel) { cTruster.CheckCancel(cOrder); } else { cTruster.CheckDeal(cOrder); } if (cTruster.IsEmpty) { //如果已經交易完畢 lock (__cTrusts) { __cTrusts.Remove(sNames[0]); } if (cTruster.IsClosed && __bClosedFlag) { __bClosedFlag = false; } } break; case ResponseType.Trust: cTruster.CheckTrust(cOrder); break; } } }