Changes to Pointers not made permanent
struct Limit
{
float Price;
int size;
int Volume;
struct Limit *Parent;
struct Limit *ltChild;
struct Limit *rtChild;
struct list_head* Order;
};
typedef struct Limit Limit;
struct Order
{
double idNum;
bool BuySell;
int shares;
float limitPrice;
char* entryTime;
char* eventTime;
struct list_head ord_Queue;
};
typedef struct Order Order;
void AddOrder(Order* newOrder,Limit* Limit,HXmap* OrderMap)
{
list_add_tail(&newOrder->ord_Queue,Limit->Order);
HXmap_add(OrderMap,&newOrder->idNum,newOrder);
Limit->size++;
Limit->Volume +=newOrder->shares;
}
void ModifyOrder(fl开发者_如何学Pythonoat oLimit, int oShares,float nLimit,int nShares,HXmap* LimitMap,HXmap* OrderMap, oBook* OrderBook)
{
Limit* ParentLimit = (Limit*)HXmap_get(LimitMap,&oLimit);
if(ParentLimit==NULL)
{
printf("ERRONEOUS ORDER\n");
return;
}
struct list_head *pos;
pos = ParentLimit->Order->next;
Order* Ord= NULL;
while(pos!=ParentLimit->Order)
{
Ord = list_entry((pos),Order,ord_Queue);
if(Ord->shares==oShares)
break; //found the order
else pos = pos->next;
}
if(pos==ParentLimit->Order && Ord->shares!=oShares)
{
printf("ORDER NOT FOUND\n");
return;
}
if(oLimit==nLimit)
{
ParentLimit->Volume = (ParentLimit->Volume + nShares)-oShares;
Ord->shares = nShares;
}
else
{
//delete from list
Ord->ord_Queue.next->prev = Ord->ord_Queue.prev;
Ord->ord_Queue.prev->next = Ord->ord_Queue.next;
ParentLimit->size--;
HXmap_del(OrderMap,&Ord->idNum);
if(ParentLimit->Volume==Ord->shares)
{
if(Ord->BuySell==1)
OrderBook->buyTree = RemoveLimit(OrderBook->buyTree,ParentLimit,LimitMap);
else
OrderBook->sellTree = RemoveLimit(OrderBook->sellTree,ParentLimit,LimitMap);
}
else
ParentLimit->Volume-=Ord->shares;
Ord->limitPrice = nLimit;
Ord->shares = nShares;
INIT_LIST_HEAD(&Ord->ord_Queue);
ParentLimit = HXmap_get(LimitMap,&nLimit);
if(ParentLimit==NULL)
{
ParentLimit = Limit_init(nLimit);
if(Ord->BuySell==1)
OrderBook->buyTree= AddLimit(OrderBook->buyTree,ParentLimit,LimitMap);
else
OrderBook->sellTree = AddLimit(OrderBook->sellTree,ParentLimit,LimitMap);
}
AddOrder(Ord,ParentLimit,OrderMap);
}
}
Okay, its a longish code, but much of it is purely intuitive. [It uses list.h Kernel Linked List and its associate functions.More info about KLL can be found here ]The idea, is when a message to modify a preexisting order at a particular price(its a financial code) arrives, it removes the order from the previous "queue" of its old Price (ParentLimit in ModifyOrder()
) finds the address of new limit price structure by querying the map, if it doesnt exist, creates a new limit and add the order else it simply adds it.
Now, I enter same message of modifying orders in a particular limit price. Configuration before passing the messages:
Limit Price : 181.25, two orders of 250 shares each.
When I pass, the first modify message, to modify the first order of 250 shares from 181.25 to 181.35(no previous limit exists so it will create a new limit and add it to the tree), the control eventually flows to AddOrder()
hence, adding the orders. Definition of AddOrder()
function is attached, though its very simplistic and calls list_add_tail()
to add it to list.
After the first modification(and addition of order), the DDD gives me this situation:
Address of ParentLimit: 0x804f1d0
Address of ->Order: 804f710
Contents of ->next: 804dec4
Contents of ->prev: 804dec4
Address of Order->ord_Queue (just inserted): 0x804dec4
Contents of Order->ord_Queue->prev: 0x804f710
Contents of Order->ord_Queue->next: 0x804f710
This shows the addition of the order to the queue has taken place successfully.
When I pass the second message to modify another order at the same old price (181.25 to 181.35) and query the map to find me the address of the new Limit Price,
The situation is:
Address of ParentLimit: 0x804f1d0
Address of ->Order: 804f710
Contents of ->next: 804f710
Contents of ->prev: 804f710
Which means, that somehow the change made was not made permanent. I dont know why this is happeneing.
This is the behavior you expect, given your description of where you are querying. These lines will remove the order from your linked list before your query the map to get a ParentLimit for the new price:
Ord->ord_Queue.next->prev = Ord->ord_Queue.prev;
Ord->ord_Queue.prev->next = Ord->ord_Queue.next;
To clarify: Limit.Order
is a pointer. It will follow your list head even when you move it to other lists. Thus, deleting from from your old list ends up following a pointer to your new list if you only have one order. You will either need to make it an embedded struct or keep a dummy head that is empty for both new and old lists.
精彩评论