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.
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.Volume; double weightedBid1 = bidRows.Volume * .9; double weightedBid2 = bidRows.Volume * .8; double weightedBid3 = bidRows.Volume * .7; double weightedBid4 = bidRows.Volume * .6; double weightedBid5 = bidRows.Volume * .5; double weightedBid6 = bidRows.Volume * .4; double weightedBid7 = bidRows.Volume * .3; double weightedBid8 = bidRows.Volume * .2; double weightedBid9 = bidRows.Volume * .1; double weightedAsk0 = askRows.Volume; double weightedAsk1 = askRows.Volume * .9; double weightedAsk2 = askRows.Volume * .8; double weightedAsk3 = askRows.Volume * .7; double weightedAsk4 = askRows.Volume * .6; double weightedAsk5 = askRows.Volume * .5; double weightedAsk6 = askRows.Volume * .4; double weightedAsk7 = askRows.Volume * .3; double weightedAsk8 = askRows.Volume * .2; double weightedAsk9 = askRows.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.