My task is to compute average half spread in percentages on a stream of market. This is part of the coding assignment that I have received.
namespace CodingAssignment
{
public interface IMarketDataFeed
{
string StockName { get; }
event Action<MarketDataUpdate> Update;
}
public interface IIndicator
{
event Action<double> IndicatorUpdate;
}
/// <summary>
/// Computes the average half spread (ie bid to mid) in percentage on a stream of market data updates
/// </summary>
public class AverageHalfSpreadIndicator
{
private List<MarketDataUpdate> _lastUpdates;
public AverageHalfSpreadIndicator(IMarketDataFeed marketDataFeed)
{
marketDataFeed.Update += OnMarketDataUpdate;
}
private void OnMarketDataUpdate(MarketDataUpdate updates)
{
_lastUpdates.Add(updates);
if (_lastUpdates.Count == 20)
{
_lastUpdates.RemoveAt(0);
}
var start = DateTime.Now;
var spreads = _lastUpdates.Select(x => 100 * (x.Mid - x.Bid) / x.Bid);
Console.WriteLine("Time to compute spreads: "+(DateTime.Now - start).TotalMilliseconds+" ms");
var averageSpread = spreads.Average();
Console.WriteLine("Total time:" + (DateTime.Now - start).TotalMilliseconds + " ms");
IndicatorUpdate(averageSpread);
}
public event Action<double> IndicatorUpdate;
}
public class MarketDataUpdate
{
public double Bid { get; set; }
public double Ask { get; set; }
public double Last { get; set; }
public double Mid
{
get { return (Bid + Ask) / 2; }
}
}
}
C# is a fairly new language for me, so there might be some best practises that I may have missed. I am also wondering how this would behave in a multi-threaded environment.