internal bool SendTrust(TradeOrder trust, bool isCancel = false) { EOrderAction cAction = trust.Action; bool bClose = cAction == EOrderAction.Sell || cAction == EOrderAction.BuyToCover; StringBuilder cBuilder = new StringBuilder(128); cBuilder.Append(__sLocalPipe).Append(",") .Append(trust.SymbolId).Append(",") .Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")).Append(",") .Append((cAction == EOrderAction.Buy || cAction == EOrderAction.BuyToCover) ? 'B' : (cAction == EOrderAction.Sell || cAction == EOrderAction.SellShort) ? 'S' : ' ').Append(",") .Append(trust.Contracts).Append(",") .Append(trust.Price).Append(",") .Append((isCancel) ? -1 : (bClose) ? 0 : 1).Append(",") .Append(Math.Round(Bars.Close.Value, __iDecimalPoint)).Append(",") .Append((isCancel) ? trust.Ticket : (trust.IsReverse) ? "1" : "0"); string sCommand = cBuilder.ToString(); bool bSuccess = __cPipeStream.Send(__sApiPipe, sCommand); //發送委託單命令給下單機 if (logger.IsInfoEnabled) logger.InfoFormat("[RealOrderService.Send] {0}", sCommand); if (bSuccess) { //如果傳送成功 trust.IsSended = true; //設定已經傳送完畢 } else { __cEntrusts.Remove(trust.Ticket); //移除此筆尚未委託的委託單 } return bSuccess; }
private void CalculatePositions() { while (__cDeals.Count > 0) { TradeOrder cDeal = null; lock (__cDeals) { cDeal = __cDeals.Dequeue(); } string sTrustID = cDeal.Ticket; cDeal.Ticket = GetDealID(); //填入成交單號(自動編號) if (logger.IsInfoEnabled) logger.InfoFormat("[Deal] #{0} {1} {2} {3} at {4} {5} @{6}", cDeal.Ticket, cDeal.SymbolId, cDeal.Action, cDeal.Contracts, cDeal.Price, cDeal.Name, cDeal.Time.ToString("yyyy-MM-dd HH:mm:ss")); bool bClosed = __cCurrentPosition.CheckPosition(cDeal); //檢查是否已經平倉完畢 int iLatestHistoryCount = __cCurrentPosition.LatestHistoryCount; //取得最新新增的平倉歷史明細個數 if (bClosed) { //如果已經平倉完畢 __cCurrentPosition = new MarketPosition(16); //重新建立新的留倉部位 __cCurrentPosition.SetBigPointValue(Bars.Info.BigPointValue); //設定每一大點的交易金額 ++__cPositions.Current; //移動目前留倉部位陣列的目前索引值 __cPositions.Value = __cCurrentPosition; //指定新的留倉部位至留倉陣列 } if (cDeal.IsDealed) { //檢查是否戳合完成 __cEntrusts.Remove(sTrustID); } OnResponse(cDeal, cDeal.SymbolId, ResponseType.Deal, (bClosed) ? null : __cCurrentPosition, (bClosed) ? __cPositions[1].ClosedTrades : __cCurrentPosition.ClosedTrades, iLatestHistoryCount); } Interlocked.Exchange(ref __iUsedClosedTempLots, 0); //處理成交單完畢之後將暫存平倉量歸0(這樣平倉單才能在下單, [開倉量 - 暫存平倉量 >= 下單平倉量] 此單才會被接受並成交) if (__cCurrentPosition.OpenLots > 0) { //檢查是否有開倉(有開倉就要發送這個更新事件, 這樣損益才會被更新) OnResponse(null, Bars.Request.Symbol, ResponseType.Update); } }