Files
quantconnect--lean/Tests/Common/Securities/FakeOrderProcessor.cs
Martin Molinero ee2dc32710 Subscriptions will track Universe requests
- The class `Subscription` will internally track each `Universe`
`SubscriptionRequest` added or removed
- Adding regression test in which two different `Universe` request the
same `SubscriptionDataConfig` and one of them removes/adds it in a
toggle fashion (fails on current master)
- `UniverseSelection` pending removals will also be tracked by
`Universe`
- `UniverseDecorator` will overwrite the `Universe` member of
`SubscriptionsRequests` at `GetSubscriptionRequests()`. This is due to
`this != this,Universe`
- Adding `Subscription` unit tests covering expected behavior
- Extracting pending removals logic from `UniverseSelection` class into
a new helper class `PendingRemovalsManager`. This new class will keep
track of the `universes` requesting to remove a security. Adding unit tests
2018-11-08 13:02:11 -03:00

101 lines
3.5 KiB
C#

/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Orders;
using QuantConnect.Securities;
namespace QuantConnect.Tests.Common.Securities
{
/// <summary>
/// Provides a fake implementation of <see cref="IOrderProcessor"/> for tests
/// </summary>
public class FakeOrderProcessor : IOrderProcessor
{
private readonly ConcurrentDictionary<int, Order> _orders = new ConcurrentDictionary<int, Order>();
private readonly ConcurrentDictionary<int, OrderTicket> _tickets = new ConcurrentDictionary<int, OrderTicket>();
public readonly ConcurrentDictionary<int, OrderRequest> ProcessedOrdersRequests = new ConcurrentDictionary<int, OrderRequest>();
public void AddOrder(Order order)
{
_orders[order.Id] = order;
}
public void AddTicket(OrderTicket ticket)
{
_tickets[ticket.OrderId] = ticket;
}
public int OrdersCount { get; private set; }
public Order GetOrderById(int orderId)
{
Order order;
_orders.TryGetValue(orderId, out order);
return order;
}
public Order GetOrderByBrokerageId(string brokerageId)
{
return _orders.Values.FirstOrDefault(x => x.BrokerId.Contains(brokerageId));
}
public IEnumerable<OrderTicket> GetOrderTickets(Func<OrderTicket, bool> filter = null)
{
return _tickets.Values.Where(filter ?? (x => true));
}
public IEnumerable<OrderTicket> GetOpenOrderTickets(Func<OrderTicket, bool> filter = null)
{
return _tickets.Values.Where(x => x.Status.IsOpen() && (filter == null || filter(x)));
}
public OrderTicket GetOrderTicket(int orderId)
{
OrderTicket ticket;
_tickets.TryGetValue(orderId, out ticket);
return ticket;
}
public IEnumerable<Order> GetOrders(Func<Order, bool> filter = null)
{
return _orders.Values.Where(filter ?? (x => true));
}
public List<Order> GetOpenOrders(Func<Order, bool> filter = null)
{
return _orders.Values.Where(x => x.Status.IsOpen() && (filter == null || filter(x))).ToList();
}
public OrderTicket Process(OrderRequest request)
{
ProcessedOrdersRequests.TryAdd(request.OrderId, request);
switch (request.OrderRequestType)
{
case OrderRequestType.Submit:
return new OrderTicket(null, (SubmitOrderRequest) request);
default:
throw new NotImplementedException();
}
}
public void Clear()
{
_orders.Clear();
_tickets.Clear();
ProcessedOrdersRequests.Clear();
}
}
}