Level 2 Order Book Add-Ons

Level 2 Order Book

The code on this page is meant to be pasted into the premium level II book NinjaScript here. It will add extra functionality to the core strategy. It is provided separately as to keep the core strategy clean and simple.

Weighted Levels

The following code will weight each level in the order book — the further out the level, the less weight. In this example level 0 (current price) is 100%, level 1 is 90%, level 2 is 80% etc.

//paste this above the "Entry Logic" region in the core strategy
double weightedBid0 = bidRows[0].Volume;
double weightedBid1 = bidRows[1].Volume * .9;
double weightedBid2 = bidRows[2].Volume * .8;
double weightedBid3 = bidRows[3].Volume * .7;
double weightedBid4 = bidRows[4].Volume * .6;
double weightedBid5 = bidRows[5].Volume * .5;
double weightedBid6 = bidRows[6].Volume * .4;
double weightedBid7 = bidRows[7].Volume * .3;
double weightedBid8 = bidRows[8].Volume * .2;
double weightedBid9 = bidRows[9].Volume * .1;

double weightedAsk0 = askRows[0].Volume;
double weightedAsk1 = askRows[1].Volume * .9;
double weightedAsk2 = askRows[2].Volume * .8;
double weightedAsk3 = askRows[3].Volume * .7;
double weightedAsk4 = askRows[4].Volume * .6;
double weightedAsk5 = askRows[5].Volume * .5;
double weightedAsk6 = askRows[6].Volume * .4;
double weightedAsk7 = askRows[7].Volume * .3;
double weightedAsk8 = askRows[8].Volume * .2;
double weightedAsk9 = askRows[9].Volume * .1; 

/*
Use case: If you want to go long when the volume of the first three levels of the weighted BID book 
is greater than the volume of the first three levels of the weighted ASK book you would use:

if ((weightedBid0 + weightedBid1 + weightedBid 2) > (weightedAsk0 + weightedAsk1 + weightedAsk 2))
EnterLong("long"); 
*/

With this pasted into the core strategy, say if you wanted to access the weighted value of the bid volume 8 levels out, you would reference the weightedBid8 variable.