// ---------------------------------------------------------------------- void ExecuteSell(Transaction t) { t.Processed(); t.Operation = TradeOp.Sell; switch (t.Source.Quote) { case BaseQuote.Absolute: t.Price = t.Source.Value; break; case BaseQuote.Counter: if (MktProvider.BidPrice == 0) { PutExecutionResult(t, false, strBidError); return; } t.Price = Price.Ceil(MktProvider.BidPrice - t.Source.Value); break; case BaseQuote.Similar: if (MktProvider.AskPrice == 0) { PutExecutionResult(t, false, strAskError); return; } t.Price = Price.Ceil(MktProvider.AskPrice - t.Source.Value); break; case BaseQuote.Position: if (Position.AvgPrice == 0) { PutExecutionResult(t, false, strPosError); return; } t.Price = Price.Floor(Position.AvgPrice - t.Source.Value); break; default: return; } if (t.Source.IsStop) { AddStopOrder(t); PutExecutionResult(t, false, null); } else { switch (t.Source.QtyType) { case QtyType.Absolute: t.Volume = t.Source.Quantity; break; case QtyType.WorkSize: t.Volume = cfg.u.WorkSize * t.Source.Quantity / 100; break; case QtyType.Position: t.Volume = Math.Abs(FilledBalance) * t.Source.Quantity / 100; break; } if (t.Volume == 0) { PutExecutionResult(t, false, strVolError); return; } PutExecutionResult(t, true, trader.SendSellOrder( t.Price, t.Volume, out t.TId)); } }
// ********************************************************************** public void PutOwnTrade(OwnTrade trade) { // ------------------------------------------------------------ int nq = this.quantity + trade.Quantity; if (Math.Sign(nq) != Math.Sign(this.quantity)) { // ------------------------------------------------ if (this.quantity != 0) { // MktProvider.TradeLog.AddClose(trade.DateTime, -this.quantity, // trade.Price, trade.Price * this.quantity - this.pricesum); if (cfg.u.CancelOnClose) { tmgr.ExecAction(cancelDescr, new OwnAction(TradeOp.Cancel)); } } if (nq != 0) { // MktProvider.TradeLog.AddOpen(trade.DateTime, nq, trade.Price); this.pricesum = trade.Price * nq; } // ------------------------------------------------ if (stopLoss != null) { tmgr.CancelAction(stopDescr, stopLoss); stopLoss = null; } if (takeProfit != null) { tmgr.CancelAction(takeDescr, takeProfit); takeProfit = null; } stopActivated = false; stopDisposed = false; takeActivated = false; takeDisposed = false; // ------------------------------------------------ } else { if (Math.Sign(trade.Quantity) == Math.Sign(this.quantity)) { String ddsd = ""; } // MktProvider.TradeLog.AddOpen(trade.DateTime, trade.Quantity, trade.Price); else { // MktProvider.TradeLog.AddClose(trade.DateTime, trade.Quantity, trade.Price); } this.pricesum += trade.Price * trade.Quantity; } this.quantity = nq; // ------------------------------------------------------------ if (quantity == 0) { AvgPrice = 0; } else { // ------------------------------------------------ if (stopLoss != null) { if (stopLoss.State == Transaction.States.Disposed) { stopDisposed = true; } else { tmgr.CancelAction(stopDescr, stopLoss); } stopLoss = null; } if (takeProfit != null) { if (takeProfit.State == Transaction.States.Disposed) { takeDisposed = true; takeProfit = null; } else if (takeProfit.OId == trade.OId) { takeActivated = true; } else if (!takeActivated) { tmgr.CancelAction(takeDescr, takeProfit); takeProfit = null; } } // ------------------------------------------------ AvgPrice = pricesum / quantity; // ------------------------------------------------ if (cfg.u.AutoStopOffset != 0 && !(stopActivated || stopDisposed)) { if (quantity > 0) { if (!takeActivated) { stopLossPrice = Price.Ceil(AvgPrice - cfg.u.AutoStopOffset); } stopLoss = tmgr.ExecAction(stopDescr, new OwnAction( TradeOp.Sell, BaseQuote.Absolute, stopLossPrice, QtyType.Absolute, quantity, true, cfg.u.AutoStopSlippage, cfg.u.AutoStopTrailStart, cfg.u.AutoStopTrailOffset)); } else { if (!takeActivated) { stopLossPrice = Price.Floor(AvgPrice + cfg.u.AutoStopOffset); } stopLoss = tmgr.ExecAction(stopDescr, new OwnAction( TradeOp.Buy, BaseQuote.Absolute, stopLossPrice, QtyType.Absolute, -quantity, true, cfg.u.AutoStopSlippage, cfg.u.AutoStopTrailStart, cfg.u.AutoStopTrailOffset)); } } // ------------------------------------------------ if (cfg.u.AutoTakeOffset != 0 && !(takeActivated || takeDisposed || stopActivated)) { if (quantity > 0) { takeProfit = tmgr.ExecAction(takeDescr, new OwnAction( TradeOp.Sell, BaseQuote.Absolute, Price.Ceil(AvgPrice + cfg.u.AutoTakeOffset), QtyType.Absolute, quantity)); } else { takeProfit = tmgr.ExecAction(takeDescr, new OwnAction( TradeOp.Buy, BaseQuote.Absolute, Price.Floor(AvgPrice - cfg.u.AutoTakeOffset), QtyType.Absolute, -quantity)); } } // ------------------------------------------------ } // ------------------------------------------------------------ MktProvider.Receiver.PutPosition(quantity, AvgPrice); }
// ********************************************************************** void LastPriceHandler(int price) { if (stopOrders.Count > 0) { lock (tlist) { int i = 0; while (i < stopOrders.Count) { Transaction t = stopOrders[i]; OwnAction a = t.Source; switch (t.Operation) { case TradeOp.Buy: if (price >= t.Price) { ActivateStopOrder(t, new OwnAction(a.Operation, BaseQuote.Absolute, Price.Floor(t.Price + a.Slippage), a.QtyType, a.Quantity)); stopOrders.RemoveAt(i); } else { if (a.TrailStart > 0) { TryTrail(t, price, 1); } i++; } break; case TradeOp.Sell: if (price <= t.Price) { ActivateStopOrder(t, new OwnAction(a.Operation, BaseQuote.Absolute, Price.Ceil(t.Price - a.Slippage), a.QtyType, a.Quantity)); stopOrders.RemoveAt(i); } else { if (a.TrailStart > 0) { TryTrail(t, price, -1); } i++; } break; default: stopOrders.RemoveAt(i); MktProvider.Receiver.PutOwnOrder(new OwnOrder(t.TId, t.Price)); break; } } } } }