tags:

views:

172

answers:

3

the following code compares two lists which are sorted in descending order to find the removed entries.

fullorderbook.asks is the previous list of orders which may contain the removed orders.

orderbook.asks is the currect list of orders which contains the current state.

So the algorithm simply loops on the fullorderbook.asks and compares the price with the corresponding order price in the orderbook.asks to find if it already exist or deleted or simply shifted down.

My question is, How can I rewrite the code using LINQ?

for (int i = 1; i <= fulllength; i++)
        {
            withinrange = false;
            //for each level in the ask book check if the price still
            //exists or it is deleted or just shift down
            while (askindex <= asklength)
            {
                //removed ask 
                if (orderbook.asks[askindex].price > fullorderbook.asks[i].price)
                {
                    changes.Add(new CapturedLevel(i, fullorderbook.asks[i].price, -1 * fullorderbook.asks[i].volume));
                    withinrange = true;
                    break;
                }
                else if (orderbook.asks[askindex].price < fullorderbook.asks[i].price)
                {
                    //update the ask pointer
                    askindex++;
                }
                else
                {
                    withinrange = true;
                    askindex++;
                    break;
                }
            }

            if (!withinrange)
            {
                //removed ask
                if (askindex >= asklength && asklength < OBK_SIZE)
                {
                    changes.Add(new CapturedLevel(i, fullorderbook.asks[i].price, -1 * fullorderbook.asks[i].volume));
                }
                else if (askindex >= asklength)
                {
                    shiftedorders.Add(orderbook.asks[i]);
                }
            }
        }
        fullorderbook.asks.Clear();
        fullorderbook.asks.AddRange(orderbook.asks.ToList<PriceLevel>());
        fullorderbook.asks.AddRange(shiftedorders);

P.S:

The aim of the algorithm is to find the totally removed orders and the shifted down orders (the order position is greater than the orderbook size OBK_SIZE).

So I the usage of the IEnumerable.Except extension method will not give the solution as it will return difference without knowing what is cause of this difference (shifting down or removal). Because if the order is shifted down I have to keep it in the fullorderbook.asks in the right order.

+1  A: 

I would say use the Except Operator.

var deleted = fullorderbook.asks.Except(orderbook.asks);

If you don't want the Except to use the Object.Equals method you could provide a IEqualityComparer<T>. For instance if you still only want to compare by price.

Davy Landman
I think you did not read the algorithm, it does not simply extract the difference and consider it as deleted!!
Ahmed Said
I found your algorithm not that easy to read, and I used your textual explanation. I figured that sometimes when you want to fix a problem you keep on coding till it works, and that your current algorithm is a result of that. You might try to explain the logic better, and the 2 lists.
Davy Landman
A: 

Apologies I don't have time to give a properly considered response... this might get you started:

//checks for deliverableNo's existence in sheetDataSource
if(!(sheetDataSource.Any(item => item.DeliverableNo == varItem.DeliverableNo)))
{
sheetDataSource.Add(varItem);
}
nailitdown
A: 

I think this is the solution

        double highestprice = orderbook.asks[asklength].price;
        List<PriceLevel> difflist = new List<PriceLevel>(fullorderbook.asks.Except(orderbook.asks));
        fullorderbook.asks.Clear();
        fullorderbook.asks.AddRange(orderbook.asks);
        //fill the shifted orders
        fullorderbook.asks.AddRange(
            (difflist.FindAll((x) =>
        {
            //shifted order
            return x.price > highestprice;
        }
            )));

        //fill the deleted orders
        changes.AddRange(
            (difflist.FindAll((x) =>
        {
            //deleted order
            return x.price < highestprice;
        }).ConvertAll((y)=>

        {
            return new CapturedLevel(0, y.price, -1*y.volume);
        }
        )));
Ahmed Said