Skip to content

Instantly share code, notes, and snippets.

@codereflection
Created November 18, 2010 22:51
Show Gist options
  • Select an option

  • Save codereflection/705836 to your computer and use it in GitHub Desktop.

Select an option

Save codereflection/705836 to your computer and use it in GitHub Desktop.
public class LocationClassifier : ILocationClassifier
{
private readonly IFixMessageRepository fixRepo;
private readonly IExecutingBrokerFactory executingBrokerFactory;
public LocationClassifier(IFixMessageRepository fixRepo, IExecutingBrokerFactory executingBrokerFactory)
{
this.fixRepo = fixRepo;
this.executingBrokerFactory = executingBrokerFactory;
}
public IEnumerable<OrderReport> Classify(IEnumerable<OrderReport> orderReports)
{
var fixMessages = fixRepo.GetBy(orderReports.FirstOrDefault().Order.TradeDate);
var executingBrokers = executingBrokerFactory.Build();
orderReports.Each(orderReport =>
{
var clientOrderIds =
orderReport.Order.History.Select(orderAction => orderAction.ClientOrderId).Distinct();
var messageExecutingBrokerCodes =
fixMessages.Where(fixMessage => clientOrderIds.Contains(fixMessage.ClientOrderId.ToString()))
.Select(fixMessage => fixMessage.ExecutingBroker).Distinct();
var orderExecutingBrokers =
executingBrokers.Where(
broker => messageExecutingBrokerCodes.Contains(broker.Code));
orderReport.Order.LocationClassification =
SelectLocation(orderExecutingBrokers);
});
return orderReports;
}
internal class LocationMatch
{
public Func<IEnumerable<ExecutingBrokerClassification>, bool> IsMatch;
public LocationClassification Classification { get; set; }
}
private readonly LocationMatch[] LocationMatches = {
new LocationMatch
{
IsMatch = (x) => x.Count(y => y == ExecutingBrokerClassification.Unclassified) > 0,
Classification = LocationClassification.ExecutingBrokersUnclassified
},
new LocationMatch
{
IsMatch = (x) => x.Count() > 0 && x.All(y => y == ExecutingBrokerClassification.Us),
Classification = LocationClassification.Us
},
new LocationMatch
{
IsMatch = (x) => x.Count() > 0 && x.All(y => y == ExecutingBrokerClassification.NonUs),
Classification = LocationClassification.NonUs
}
};
private LocationClassification SelectLocation(IEnumerable<ExecutingBroker> orderExecutingBrokers)
{
var result = LocationMatches.Where(x => x.IsMatch(orderExecutingBrokers.Select(y => y.Classification))).FirstOrDefault();
return result == null ? LocationClassification.Unknown : result.Classification;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment