Ejemplo n.º 1
0
		/// <summary>
		/// Рассчитать комиссию.
		/// </summary>
		/// <param name="message">Сообщение, содержащее информацию по заявке или собственной сделке.</param>
		/// <returns>Комиссия. Если комиссию рассчитать невозможно, то будет возвращено <see langword="null"/>.</returns>
		protected override decimal? OnProcessExecution(ExecutionMessage message)
		{
			if (message.ExecutionType != ExecutionTypes.Trade)
				return null;

			_currentTurnOver += message.GetTradePrice() * message.GetVolume();

			if (_currentTurnOver < TurnOver)
				return null;

			return (decimal)Value;
		}
		private bool TryApplyTrades(ExecutionMessage item)
		{
			if (_matchingOrder == null)
				return false;

			try
			{
				var volume = _matchingOrder.GetVolume();

				if (item != null && _matchingOrder.OrderId == item.OrderId)
					volume -= item.GetVolume();

				// если заявка была вся отменена. например, по причине http://forum.rts.ru/viewtopic.asp?t=24197
				if (volume == 0)
					return false;

				var removingQuotes = new List<Tuple<QuoteChange, decimal>>();

				foreach (var quote in (_matchingOrder.Side == Sides.Buy ? _depth.Asks : _depth.Bids))
				{
					if ((_matchingOrder.Side == Sides.Buy && _matchingOrder.Price < quote.Price) || (_matchingOrder.Side == Sides.Sell && _matchingOrder.Price > quote.Price))
						break;

					if (volume >= quote.Volume)
					{
						removingQuotes.Add(Tuple.Create(quote, quote.Volume));

						volume -= quote.Volume;

						if (volume == 0)
							break;
					}
					else
					{
						removingQuotes.Add(Tuple.Create(quote, volume));

						volume = 0;
						break;
					}
				}

				// в текущей момент Плаза не транслирует признак MatchOrCancel через ОЛ, поэтому сделано на будущее
				if (_matchingOrder.TimeInForce == TimeInForce.MatchOrCancel && volume > 0)
					return false;

				foreach (var removingQuote in removingQuotes)
				{
					var quotes = (_matchingOrder.Side.Invert() == Sides.Buy ? _bids : _asks);

					var quote = quotes.TryGetValue(removingQuote.Item1.Price);

					if (quote != null)
					{
						quote.Volume -= removingQuote.Item2;

						if (quote.Volume <= 0)
						{
							quotes.Remove(removingQuote.Item1.Price);
						}
					}
				}
				
				if (volume > 0)
				{
					if (_matchingOrder.TimeInForce == TimeInForce.PutInQueue)
					{
						var quote = new QuoteChange
						{
							Side = _matchingOrder.Side,
							Price = _matchingOrder.Price,
							Volume = volume,
						};

						(quote.Side == Sides.Buy ? _bids : _asks).Add(quote.Price, quote);
					}
				}

				_depth.Bids = GetArray(_bids);
				_depth.Asks = GetArray(_asks);

				return true;
			}
			finally
			{
				_matchingOrder = null;
			}
		}
		// mika
		// debug code for check build algo

		//private readonly Dictionary<long, decimal> _pendingMatch = new Dictionary<long, decimal>();
		//private readonly Dictionary<long, Tuple<Sides, decimal, decimal>> _activeOrders = new Dictionary<long, Tuple<Sides, decimal, decimal>>();

		//private QuoteChangeMessage BuildDepth()
		//{
		//	var bids = new SortedDictionary<decimal, QuoteChange>(new BackwardComparer<decimal>());
		//	var asks = new SortedDictionary<decimal, QuoteChange>();

		//	foreach (var pair in _activeOrders)
		//	{
		//		var quotes = pair.Value.Item1 == Sides.Buy ? bids : asks;

		//		var quote = quotes.TryGetValue(pair.Value.Item2);

		//		if (quote == null)
		//		{
		//			quote = new QuoteChange(pair.Value.Item1, pair.Value.Item2, pair.Value.Item3);
		//			quotes.Add(pair.Value.Item2, quote);
		//		}
		//		else
		//			quote.Volume += pair.Value.Item3;
		//	}

		//	return new QuoteChangeMessage
		//	{
		//		Bids = bids.Values.ToArray(),
		//		Asks = asks.Values.ToArray()
		//	};
		//}

		/// <summary>
		/// Добавить новую строчку из лога заявок к стакану.
		/// </summary>
		/// <param name="item">Строчка лога заявок.</param>
		/// <returns>Был ли изменен стакан.</returns>
		public bool Update(ExecutionMessage item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			if (item.ExecutionType != ExecutionTypes.OrderLog)
				throw new ArgumentException("item");

			// mika
			// debug code for check build algo

			//var orderId = item.OrderId.Value;
			//var orderVol = item.Volume.Value;

			//if (item.IsOrderLogRegistered())
			//{
			//	var vol = _pendingMatch.TryGetValue2(orderId);

			//	if (vol != null)
			//		vol -= orderVol;
			//	else
			//		vol = orderVol;

			//	if (vol > 0)
			//		_activeOrders.Add(orderId, Tuple.Create(item.Side, item.Price, vol.Value));
			//}
			//else if (item.IsOrderLogMatched())
			//{
			//	var t = _activeOrders.TryGetValue(orderId);

			//	if (t != null)
			//	{
			//		var vol = t.Item3;

			//		vol -= orderVol;

			//		if (vol < 0)
			//			throw new Exception();

			//		if (vol == 0)
			//			_activeOrders.Remove(orderId);
			//		else
			//			_activeOrders[orderId] = Tuple.Create(item.Side, item.Price, vol);
			//	}
			//	else
			//	{
			//		var vol = _pendingMatch.TryGetValue2(orderId);

			//		if (vol == null)
			//			vol = orderVol;
			//		else
			//			vol += orderVol;

			//		_pendingMatch[orderId] = vol.Value;
			//	}
			//}
			//else if (item.IsOrderLogCanceled())
			//	_activeOrders.Remove(orderId);

			var volume = item.GetVolume();

			var changed = false;

			try
			{
				// Очистить стакан в вечерний клиринг
				if (item.ServerTime.TimeOfDay >= _clearingBeginTime)
				{
					// Garic - переделал
					// Очищаем только в рабочие дни поскольку в субботу/воскресенье допустима отмена заявок

					if (_lastUpdateTime != null && _lastUpdateTime.Value.TimeOfDay < _clearingBeginTime && _exchange.WorkingTime.IsTradeDate(item.ServerTime.LocalDateTime, true))
					{
						_depth.ServerTime = item.ServerTime;
						_depth.Bids = Enumerable.Empty<QuoteChange>();
						_depth.Asks = Enumerable.Empty<QuoteChange>();

						_matchingOrder = null;
						changed = true;
					}
				}

				_lastUpdateTime = item.ServerTime.LocalDateTime;

				if (item.IsSystem == false || item.TradePrice != null || item.Price == 0 /* нулевая цена может появится при поставке опционов */)
					return changed;

				if (item.IsOrderLogRegistered())
				{
					changed = TryApplyTrades(null);

					if	(
							(item.Side == Sides.Buy && (_depth.Asks.IsEmpty() || item.Price < _depth.Asks.First().Price)) ||
							(item.Side == Sides.Sell && (_depth.Bids.IsEmpty() || item.Price > _depth.Bids.First().Price))
						)
					{
						if (item.TimeInForce == TimeInForce.PutInQueue)
						{
							var quotes = (item.Side == Sides.Buy ? _bids : _asks);
							var quote = quotes.TryGetValue(item.Price);

							if (quote == null)
							{
								quote = new QuoteChange
								{
									Side = item.Side,
									Price = item.Price,
									Volume = volume,
								};

								quotes.Add(item.Price, quote);

								if (item.Side == Sides.Buy)
									_depth.Bids = GetArray(quotes);
								else
									_depth.Asks = GetArray(quotes);
							}
							else
								quote.Volume += volume;

							changed = true;
						}
					}
					else
					{
						_matchingOrder = (ExecutionMessage)item.Clone();

						// mika
						// из-за того, что могут быть кросс-сделки, матчинг только по заявкам невозможен
						// (сначала идет регистрация вглубь стакана, затем отмена по причине кросс-сделки)
						// http://forum.rts.ru/viewtopic.asp?t=24197
						// 
					}
				}
				else if (item.IsOrderLogCanceled())
				{
					var isSame = _matchingOrder != null && _matchingOrder.OrderId == item.OrderId;

					changed = TryApplyTrades(item);

					if (!isSame && item.TimeInForce == TimeInForce.PutInQueue)
					{
						// http://forum.rts.ru/viewtopic.asp?t=24197
						if (item.GetOrderLogCancelReason() != OrderLogCancelReasons.CrossTrade)
						{
							var quotes = (item.Side == Sides.Buy ? _bids : _asks);
							var quote = quotes.TryGetValue(item.Price);

							if (quote != null)
							{
								quote.Volume -= volume;

								if (quote.Volume <= 0)
								{
									quotes.Remove(item.Price);

									if (item.Side == Sides.Buy)
										_depth.Bids = GetArray(quotes);
									else
										_depth.Asks = GetArray(quotes);
								}
							}
						}

						changed = true;
					}
				}
				else
				{
					throw new ArgumentException(LocalizedStrings.Str943Params.Put(item), "item");

					// для одной сделки соответствуют две строчки в ОЛ
					//_trades[item.Trade.Id] = item;
				}
			}
			finally
			{
				if (changed)
					_depth.ServerTime = item.ServerTime;
			}

			return changed;
		}
Ejemplo n.º 4
0
		private void ProcessMarketOrder(List<ExecutionMessage> retVal, SortedDictionary<decimal, RefPair<List<ExecutionMessage>, QuoteChange>> quotes, ExecutionMessage tradeMessage, Sides orderSide)
		{
			// вычисляем объем заявки по рынку, который смог бы пробить текущие котировки.

			var tradePrice = tradeMessage.GetTradePrice();
			// bigOrder - это наша большая рыночная заявка, которая способствовала появлению tradeMessage
			var bigOrder = CreateMessage(tradeMessage.LocalTime, orderSide, tradePrice, 0, tif: TimeInForce.MatchOrCancel);
			var sign = orderSide == Sides.Buy ? -1 : 1;
			var hasQuotes = false;

			foreach (var pair in quotes)
			{
				var quote = pair.Value.Second;

				if (quote.Price * sign > tradeMessage.TradePrice * sign)
				{
					bigOrder.Volume += quote.Volume;
				}
				else
				{
					if (quote.Price == tradeMessage.TradePrice)
					{
						bigOrder.Volume += tradeMessage.Volume;

						//var diff = tradeMessage.Volume - quote.Volume;

						//// если объем котиовки был меньше объема сделки
						//if (diff > 0)
						//	retVal.Add(CreateMessage(tradeMessage.LocalTime, quote.Side, quote.Price, diff));
					}
					else
					{
						if ((tradePrice - quote.Price).Abs() == _securityDefinition.PriceStep)
						{
							// если на один шаг цены выше/ниже есть котировка, то не выполняем никаких действий
							// иначе добавляем новый уровень в стакан, чтобы не было большого расхождения цен.
							hasQuotes = true;
						}
					
						break;
					}

					//// если котировки с ценой сделки вообще не было в стакане
					//else if (quote.Price * sign < tradeMessage.TradePrice * sign)
					//{
					//	retVal.Add(CreateMessage(tradeMessage.LocalTime, quote.Side, tradeMessage.Price, tradeMessage.Volume));
					//}
				}
			}

			retVal.Add(bigOrder);

			// если собрали все котировки, то оставляем заявку в стакане по цене сделки
			if (!hasQuotes)
				retVal.Add(CreateMessage(tradeMessage.LocalTime, orderSide.Invert(), tradePrice, tradeMessage.GetVolume()));
		}
Ejemplo n.º 5
0
		private IEnumerable<ExecutionMessage> ProcessExecution(ExecutionMessage message)
		{
			var retVal = new List<ExecutionMessage>();

			var bestBid = _bids.FirstOrDefault();
			var bestAsk = _asks.FirstOrDefault();

			var tradePrice = message.GetTradePrice();
			var volume = message.GetVolume();
			var time = message.LocalTime;

			if (bestBid.Value != null && tradePrice <= bestBid.Key)
			{
				// тик попал в биды, значит была крупная заявка по рынку на продажу,
				// которая возможна исполнила наши заявки

				ProcessMarketOrder(retVal, _bids, message, Sides.Sell);

				// подтягиваем противоположные котировки и снимаем лишние заявки
				TryCreateOppositeOrder(retVal, _asks, time, tradePrice, volume, Sides.Buy);
			}
			else if (bestAsk.Value != null && tradePrice >= bestAsk.Key)
			{
				// тик попал в аски, значит была крупная заявка по рынку на покупку,
				// которая возможна исполнила наши заявки

				ProcessMarketOrder(retVal, _asks, message, Sides.Buy);

				TryCreateOppositeOrder(retVal, _bids, time, tradePrice, volume, Sides.Sell);
			}
			else if (bestBid.Value != null && bestAsk.Value != null && bestBid.Key < tradePrice && tradePrice < bestAsk.Key)
			{
				// тик попал в спред, значит в спреде до сделки была заявка.
				// создаем две лимитки с разных сторон, но одинаковой ценой.
				// если в эмуляторе есть наша заявка на этом уровне, то она исполниться.
				// если нет, то эмулятор взаимно исполнит эти заявки друг об друга

				var originSide = GetOrderSide(message);

				retVal.Add(CreateMessage(time, originSide, tradePrice, volume + (_securityDefinition.VolumeStep ?? 1 * _settings.VolumeMultiplier), tif: TimeInForce.MatchOrCancel));

				var spreadStep = _settings.SpreadSize * GetPriceStep();

				// try to fill depth gaps

				var newBestPrice = tradePrice + spreadStep;

				var depth = _settings.MaxDepth;
				while (--depth > 0)
				{
					var diff = bestAsk.Key - newBestPrice;

					if (diff > 0)
					{
						retVal.Add(CreateMessage(time, Sides.Sell, newBestPrice, 0));
						newBestPrice += spreadStep * _priceRandom.Next(1, _settings.SpreadSize);
					}
					else
						break;
				}

				newBestPrice = tradePrice - spreadStep;

				depth = _settings.MaxDepth;
				while (--depth > 0)
				{
					var diff = newBestPrice - bestBid.Key;

					if (diff > 0)
					{
						retVal.Add(CreateMessage(time, Sides.Buy, newBestPrice, 0));
						newBestPrice -= spreadStep * _priceRandom.Next(1, _settings.SpreadSize);
					}
					else
						break;
				}

				retVal.Add(CreateMessage(time, originSide.Invert(), tradePrice, volume, tif: TimeInForce.MatchOrCancel));
			}
			else
			{
				// если у нас стакан был полу пустой, то тик формирует некий ценовой уровень в стакана,
				// так как прошедщая заявка должна была обо что-то удариться. допускаем, что после
				// прохождения сделки на этом ценовом уровне остался объем равный тиковой сделки

				var hasOpposite = true;

				Sides originSide;

				// определяем направление псевдо-ранее существовавшей заявки, из которой получился тик
				if (bestBid.Value != null)
					originSide = Sides.Sell;
				else if (bestAsk.Value != null)
					originSide = Sides.Buy;
				else
				{
					originSide = GetOrderSide(message);
					hasOpposite = false;
				}

				retVal.Add(CreateMessage(time, originSide, tradePrice, volume));

				// если стакан был полностью пустой, то формируем сразу уровень с противоположной стороны
				if (!hasOpposite)
				{
					var oppositePrice = tradePrice + _settings.SpreadSize * GetPriceStep() * (originSide == Sides.Buy ? 1 : -1);

					if (oppositePrice > 0)
						retVal.Add(CreateMessage(time, originSide.Invert(), oppositePrice, volume));
				}
			}

			if (!HasDepth(time))
			{
				// если стакан слишком разросся, то удаляем его хвосты (не удаляя пользовательские заявки)
				CancelWorstQuote(retVal, time, Sides.Buy, _bids);
				CancelWorstQuote(retVal, time, Sides.Sell, _asks);	
			}

			_prevTickPrice = tradePrice;

			return retVal;
		}
Ejemplo n.º 6
0
		/// <summary>
		/// Преобразовать тиковую сделку.
		/// </summary>
		/// <param name="message">Тиковая сделка.</param>
		/// <returns>Поток <see cref="ExecutionMessage"/>.</returns>
		public IEnumerable<ExecutionMessage> ToExecutionLog(ExecutionMessage message)
		{
			if (message == null)
				throw new ArgumentNullException("message");

			if (!_stepsUpdated)
			{
				_securityDefinition.PriceStep = message.GetTradePrice().GetDecimalInfo().EffectiveScale.GetPriceStep();
				_securityDefinition.VolumeStep = message.GetVolume().GetDecimalInfo().EffectiveScale.GetPriceStep();
				_stepsUpdated = true;
			}

			//if (message.DataType != ExecutionDataTypes.Trade)
			//	throw new ArgumentOutOfRangeException("Тип данных не может быть {0}.".Put(message.DataType), "message");

			_lastTradeDate = message.LocalTime.Date;

			return ProcessExecution(message);
		}