.NET Conditional Callback on a type
I have a stock price which changes by nature all the time. And, there will be many users who wants to buy that stock.
Let's say that the stock price is started at 10 and let's say, 30 people bid for 10.98, 20 people bid for 7.45, 100 people bid for 8.99.
During the day, the stock price can touch any of these values, and if that happens, I want to execute all the orders for users who quoted that price.
Techni开发者_StackOverflow社区cally, I am storing in a List. Whenever the price changes, I am checking against all the values in the list and executing those that satisfy the quoted price.
Class Bids
{
string stockname;
double quote;
}
Is there any better alternative way to callback the satisfied items in the list rather than checking all the items whenever there is a change??
If storing in a list is not right way of doing it, let me know the best way.
I don't see any better way to do it, except that I'd probably have a dictionary of stocks, stored by stock name. For each stock, I'd have a collection of orders, perhaps sorted by price. For each price, I'd have a list of callbacks.
What you really want, then, is a way to quickly determine if a move in the stock price requires you to execute any buy orders. Typically, a buy order would be executed if the price is at or below the stated price. So if somebody bids $10 and the latest quoted price is $8.99, then you would execute that order. The buyer gets his shares at $8.99 instead of at $10.
If you don't want to check every buy order every time the stock price changes, then you need to keep the orders in some kind of sorted collection. If you want to execute orders when the quoted price is exactly the price that the buyer requested, then you can do a binary search on the list to see if there is one or more orders at that exact price.
If you want to execute orders using the typical behavior of "at or below the stated price," then all you have to do is check the first item in the sorted list to see if it is at or below the stated price, and then execute all orders from the beginning of the list that meet the criteria.
You can do this with a List<T>
, although you'll have to re-sort the list whenever you add something, and removing items from the list is somewhat inefficient because other items have to move up to fill the holes left by the removed items.
The System.Collections.Generic
namespace has a number of sorted collection types that would allow you to do this, but none of them accept multiple items that have the same key. So your key couldn't be just the stock price, but would have to be multi-part key that includes the stock price and something else. Still, it wouldn't be terribly difficult to construct a key type and a comparison function that would let you keep things in order so that you could quickly insert, remove, and search.
Or you could build a min heap, which lets you quickly insert items and find the lowest-priced buy order. Finding and removing an item that's not the lowest priced one is more complicated and just as inefficient as using a list.
The real question you have to ask yourself is whether a List
is efficient enough for your purposes. How many items are in the list? How often do you add or remove items? How often do you have to check to see if buy orders should be executed? If you only have a handful of items that you check once a minute, then a sequential list is going to perform just fine and there's no reason to expend effort on improving it. If you have 10,000 pending orders and you have to check them every second (or several times per second), with orders coming in continually, then you're going to need a much more involved plan that includes an ordered data structure that's quick to search and update.
精彩评论