예제 #1
0
            /// <summary>
            /// Performs partial market fills once per time step
            /// </summary>
            /// <param name="asset">The security being ordered</param>
            /// <param name="order">The order</param>
            /// <returns>The order fill</returns>
            public override OrderEvent MarketFill(Security asset, MarketOrder order)
            {
                var currentUtcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);

                var ticket = _orderProvider.GetOrderTickets(x => x.OrderId == order.Id).FirstOrDefault();

                if (ticket == null)
                {
                    // if we can't find the ticket issue empty fills
                    return(new OrderEvent(order, currentUtcTime, OrderFee.Zero));
                }

                // make sure some time has passed
                var lastOrderEvent = ticket.OrderEvents.LastOrDefault();
                var increment      = TimeSpan.FromTicks(Math.Max(asset.Resolution.ToTimeSpan().Ticks, 1));

                if (lastOrderEvent != null && currentUtcTime - lastOrderEvent.UtcTime < increment)
                {
                    // wait a minute between fills
                    return(new OrderEvent(order, currentUtcTime, OrderFee.Zero));
                }

                var remaining      = (int)(ticket.Quantity - ticket.QuantityFilled);
                var fill           = base.MarketFill(asset, order);
                var filledThisTime = Math.Min(remaining, (int)(_percent * order.Quantity));

                fill.FillQuantity = filledThisTime;

                // only mark it as filled if there is zero quantity remaining
                fill.Status = remaining == filledThisTime
                    ? OrderStatus.Filled
                    : OrderStatus.PartiallyFilled;

                return(fill);
            }