private TradeOrder GetWaiting(string symbolId, double price) { TradeOrder cTrust = null; again: if (__iMaxTrustIndex > __iTrustIndex) { string sTrustTicket = __iTrustIndex.ToString(); cTrust = __cEntrusts.GetTrade(sTrustTicket); if (cTrust == null) { Interlocked.Increment(ref __iTrustIndex); goto again; } else { if (cTrust.SymbolId.Equals(symbolId) && cTrust.Price == price) { Interlocked.Increment(ref __iTrustIndex); __cEntrusts.Remove(sTrustTicket); } else { cTrust = null; } } } return cTrust; }
private void SendNextBars(int previousBar) { while (__cReserves.Count > 0) { string sTicket = __cReserves.Peek(); //取出以 name 當作 ticket 的委託單字串(NextBars 都先以 name 當作委託單的 ticket 號碼) TradeOrder cTrust = __cEntrusts.GetTrade(sTicket); //取得委託單資訊 if (cTrust.BarNumber > previousBar) { break; } else { __cReserves.Dequeue(); //移除保留的 name __cEntrusts.Remove(sTicket); //移除暫存的委託單 if (__cPNextBarRequires.Contains(sTicket)) { //檢查有符合條件才下單 cTrust.Ticket = GetTrustID(); //設定委託ID __cEntrusts.Add(cTrust); //加入至委託列表內 if (cTrust.IsReverse) { //如果有反轉倉單 CancelLimit(cTrust.Action); //取消所有反向之前的限價委託單 } this.SendTrust(cTrust, Bars.Open.Value); //發送委託單 } } } }
private void TradeTimer_onElapsed(object sender, ElapsedEventArgs e) { bool bBusy = false; lock (__oLock) { bBusy = __bTradeBusy; if (!bBusy) { __bTradeBusy = true; } } if (!bBusy) { bool bUpdate = __cQueue.Count > 0; while (__cQueue.Count > 0) { ResponseEvent cResponse = null; lock (__cQueue) { cResponse = __cQueue.Dequeue(); } ResponseType cType = cResponse.ResponseType; if (cType == ResponseType.Update) { continue; } string sSymbolId = cResponse.SymbolId; ITradeOrder cOrder = cResponse.TradeOrder; string sTicket = cOrder.Ticket; switch (cType) { case ResponseType.Cancel: //取消委託 case ResponseType.Trust: //委託回報 if (cType == ResponseType.Cancel) { __cTrusts.Remove(sTicket); } else { if (cOrder.Contracts == 0) { __cTrusts.Remove(sTicket); } else { __cTrusts.Add(new _TradeInfo(cOrder, sSymbolId)); } } break; case ResponseType.Deal: //成交回報 EOrderAction cAction = cOrder.Action; if (cAction == EOrderAction.Buy || cAction == EOrderAction.SellShort) { TradeList <ITrade> cOpenTrades = cResponse.OpenTrades; if (cOpenTrades != null && cOpenTrades.Count > 0) { ITrade cTrade = cOpenTrades.GetTrade(sTicket); if (cTrade != null) { __cOpens.Add(new _TradeInfo(cTrade, sSymbolId)); } } } else { List <ITrade> cCloseTrades = cResponse.CloseTrades; int iHistoryCount = cResponse.LatestHistoryCount; if (iHistoryCount > 0) { int iHistoryIndex = cResponse.LatestHistoryIndex; if (iHistoryIndex > -1) { for (int i = 0; i < iHistoryCount; i++) { ITrade cTrade = cCloseTrades[iHistoryIndex + i]; __cCloses.Add(new _TradeInfo(cTrade.EntryOrder, sSymbolId, 0)); __cCloses.Add(new _TradeInfo(cTrade.ExitOrder, sSymbolId, cTrade.Profit)); if (cTrade.IsTradeClosed) { __cOpens.Remove(cTrade.Ticket); } } } } } break; } } if (bUpdate) { __cUpdateTimer.Start(); onUpdate(this, EventArgs.Empty); } lock (__oLock) { __bTradeBusy = false; } } }