我正在构建一个使用市场深度数据的交易应用程序。每个市场深度更新包含多个价格级别,并且每个订单 - 这些都构成订单。我需要订单类尽可能小,因为它们会有数千个实例 - 最好包含有关价格和大小的信息。原因是给定订单中有很多订单,我的应用程序在各种交易所上使用来自各种资产对的许多不同订单。
在运行时,我从交换API查询订单并在内部构建订单。因此,每个订单都与给定的订单相关联(对于集对名称和交换)。因此,订单簿是从单个订单元素中构建,但在顶部还有一些额外的信息。
应用的某些方法需要从订单的给定实例中访问交换名称或配对名称。由于每个订单都与订单“捆绑”,我希望orderBook保存此信息,而不是所有需要复制此订单的订单。如果我不希望每个订单由于性能(特别是内存需求)而保留这些信息,那么实现此目的的最佳方法是什么?
思想:
我正在寻找
EDIT1 根据评论:我会测试性能。我不知道如何 - 鉴于我还不知道如何以任何方式实现该方法。其次,一旦我这样做,测试CPU和内存需求的工具是什么?我从来没有这样做,基本上我只是定时测试。我是在Visual Studio中寻找一些东西还是应该使用其他工具?
最小例子:
public class A
{
List<B> instancesOfB;
public string PropertyOfInterest { get; set; }
public A(B someB, B anotherB, string propertyString)
{
PropertyOfInterest = propertyString;
instancesOfB = new List<B>();
instancesOfB.Add(someB);
instancesOfB.Add(anotherB);
}
}
public class B
{
public string GetPropertyFromA()
{
// TODO: how?
// fire an event that class A will subscribe to when B is added to it?
// any other way?
throw new NotFiniteNumberException();
}
}
public class ExecutionClass
{
public void Execute()
{
B b1 = new B();
B b2 = new B();
A a = new A(b1, b2, "texOfInterest");
string someText = b1.GetPropertyFromA(); // Should equal to "textOfInterest"
}
}
示例:
public class TestOrder
{
public decimal Price { get; set; }
public decimal Size { get; set; }
public DateTime TimeStamp { get; set; }
public MarketSide Side { get; set; }
public TestOrder(decimal price, decimal size, DateTime timeStamp, MarketSide side)
{
Price = Price;
Size = size;
TimeStamp = timeStamp;
Side = side;
}
public string GetPairName()
{
//TODO: how?
throw new NotImplementedException();
}
public ExchangeName GetExchangeName()
{
// TODO: how?
throw new NotImplementedException();
}
public decimal GetTickSize()
{
// TODO how?
throw new NotImplementedException();
}
public void PrintSomeOrderInfo()
{
Console.WriteLine("{0}, {1}, {2}", this.GetExchangeName().ToString(), this.Size, this.GetTickSize());
}
}
public class Pricelevel
{
List<TestOrder> orders;
public Pricelevel() { }
public void AddOrder(TestOrder order)
{
orders.Add(order);
}
}
public class Assetpair
{
public string Name { get; set; }
public ExchangeName ExchangeName { get; set; }
public decimal TickSize { get; set; }
public Assetpair(string name, ExchangeName exchange, decimal tickSize)
{
Name = name;
ExchangeName = exchange;
TickSize = tickSize;
}
}
public class Orderbook
{
Dictionary<MarketSide, Dictionary< decimal, PriceLevel>> OrderBookDict; // side is either asks or bids
public ExchangeName ExchangeName { get; set; }
public AssetPair PairInfo { get; set; }
public Orderbook(Assetpair pairInfo, List<Pricelevel> asks, List<Pricelevel> bids)
{
}
public void AddOrder(TestOrder order)
{
// TODO: search the nested dictionary according to price property of the order
// Add to given pricelevel and update the pricelevel in the orderbook
}
public void AddPriceLevel(PriceLevel level)
{
// TODO: Search for given pricelevel according to price - add on the correct position
}
}
public class ExecutionClass
{
public void Execute()
{
TestOrder order1 = new TestOrder(price: 11.2m, size: 100.23m, timeStamp: DateTime.Now, side: MarketSide.MarketAsk);
Pricelevel level1 = new Pricelevel();
level1.AddOrder(order1);
List<Pricelevel> asks = new List<Pricelevel> { level1/*, level2, ...*/ };
List<Pricelevel> bids = new List<Pricelevel> { /*level3, level4, ...*/};
Assetpair assetPair = new Assetpair("pairOne", ExchangeName.None, 0.01m);
Orderbook book1 = new Orderbook(assetPair, asks, bids);
// print info contained in the book1 where order1 belongs
// trivial example - need to do more advanced stuff
order1.PrintSomeOrderInfo();
}
}