Page 1 of 4

Demo strategies: QuotesDelta Indicator

Posted: Sat Nov 03, 2018 6:52 am
by TraderCG
The API demo-strategy examples are a great way of learning and show how powerful the API is. In the QuotesDelta strategy the indicator plots an orange line in the bottom panel which has a continuously changing value, but from the code it seems that it should plot as a constant value. So I obviously am misunderstanding something within the code and am writing to get clarification on how the QuotesDelta strategy works as follows:

The QuotesDelta onDepth method has the calculation (on line 56) delta = size – prevsize. The diagram below shows my interpretation of how the size value trickles on down through the methods and then returned to prevsize. If this diagram is correct, then size would equal prevsize and delta will always be 0.

Image

My question: on line-14 of OrderBookBase class: should sizePrevious = book.put(price,size) ? Clarifying this may be the key to what I am misunderstanding.

It seems to me that to get the previous TreeMap value, line-14 should be: Integer sizePrevious = book.get(price);

Any help will be much appreciated.

Re: Demo strategies: QuotesDelta Indicator

Posted: Sat Nov 03, 2018 5:06 pm
by Serg
TraderCG wrote:
Sat Nov 03, 2018 6:52 am
My question: on line-14 of OrderBookBase class: should sizePrevious = book.put(price,size) ?
It seems to me that to get the previous TreeMap value, line-14 should be: Integer sizePrevious = book.get(price);
Hello,

It works correctly, I just tried to make the code shorter and more elegant. A straight forward implementation as you expected would look like this:
refactoring.png
refactoring.png (100.34 KiB) Viewed 21984 times
Notice that both Map#put() and Map#remove() return the previous value associated with the key (in our case price is the key and size is the value).
put.png
put.png (72.24 KiB) Viewed 21982 times
Also, since they both return a value, it allows usage of ternary operation

Code: Select all

 value = <condition> ? <this if true> : <this if false>
, i.e.

Code: Select all

Integer sizePrevious = (size == 0) ? book.remove(price) : book.put(price, size);
and from the indicator:

Code: Select all

int prevSize = book.onDepth(isBid, price, size);
So, in one line we update the order book, and also get the previous value, which in some cases like this can be useful.

Remark: Here prevSize can be 'int' instead of 'Integer' because it was already checked for null.

Code: Select all

 Integer prevSize = book.onDepth(isBid, price, size);
I already updated this.

Best,
S

Re: Demo strategies: QuotesDelta Indicator

Posted: Sun Nov 04, 2018 1:46 am
by TraderCG
Notice that both Map#put() and Map#remove() return the previous value associated with the key (in our case price is the key and size is the value).
@Serg, thanks for your answer and clarifying that. I have mistakenly assumed that it returned the new value associated with the key (not sure now why I didn't check that assumption - as you have shown, the information is easy to find).
Thanks again for the examples... they are a great way of learning the API.

Re: Demo strategies: QuotesDelta Indicator

Posted: Wed Nov 21, 2018 2:12 pm
by SuperDriveGuy
Hi Serg,
What if I wanted to limited the number of levels used in the calculations of the QuotesDelta, similar to the column COB(Current Order Book) or the Quotes Delta column? Where would one place the limit on the LEVELS counted?

Thanks,
SDG

Re: Demo strategies: QuotesDelta Indicator

Posted: Wed Nov 28, 2018 3:32 pm
by SuperDriveGuy
Anyone?

Re: Demo strategies: QuotesDelta Indicator

Posted: Thu Nov 29, 2018 3:31 pm
by SuperDriveGuy
The reason I showed a screenshot of the Quotes Delta column is that, it already includes a NUMBER OF LEVELS(set maximal depth) setting. So its already been done.

Just not sure how to go about it, in the QuotesDelta Indicator example

Re: Demo strategies: QuotesDelta Indicator

Posted: Thu Nov 29, 2018 4:18 pm
by Serg
Hi. Sorry for long delay in response. If you wish to limit number of levels that affect Quotes Delta I think you should modify the onDepth() method like this. I can't test it right now. But if this is what you need, I can add it tomorrow and add necessary GUI controls.

Code: Select all

	@Override
	public void onDepth(boolean isBid, int price, int size) {
		int maxPriceLevels = 10;
		TreeMap<Integer, Integer> halfBook = isBid ? book.bids : book.asks;
		int bestPrice = halfBook.isEmpty() ? price : halfBook.firstKey();
		int priceLevel = isBid ? bestPrice - price : price - bestPrice;
		int prevSize = book.onDepth(isBid, price, size);
		if (priceLevel > maxPriceLevels) {
			return;
		}
		if (snapshotCompleted) {
			int delta = size - prevSize;
			if (isBid) {
				bidDelta += delta;
			} else {
				askDelta += delta;
			}
		}
	}
Notice that we calculate the price level before applying the update on the order book.

Re: Demo strategies: QuotesDelta Indicator

Posted: Thu Nov 29, 2018 5:46 pm
by SuperDriveGuy
Thanks Serg. much appreciated!

Please make the necessary changes to the code and GUI controls

Re: Demo strategies: QuotesDelta Indicator

Posted: Thu Nov 29, 2018 6:21 pm
by Serg
I already did some changes. I'll update it on github once we fix some API bug. If you wish, use this jar file. It contains a single indicator called Quotes Delta 4, see image attached. I also added Quotes Count Delta, which counts number of quote updates (regardless of their size)

Code: Select all

	@Override
	public void onDepth(boolean isBid, int price, int size) {
		TreeMap<Integer, Integer> halfBook = isBid ? book.bids : book.asks;
		int bestPrice = halfBook.isEmpty() ? price : halfBook.firstKey();
		int priceLevel = isBid ? bestPrice - price : price - bestPrice;

		int prevSize = book.onDepth(isBid, price, size);

		if (limitNumPriceLevels && priceLevel > maxPriceLevels) {
			return;
		}

		if (snapshotCompleted) {
			int delta = size - prevSize;
			if (isBid) {
				bidDeltaSize += delta;
				bidDeltaCount++;
			} else {
				askDeltaSize += delta;
				askDeltaCount++;
			}
		}
	}
quotes-delta.png
quotes-delta.png (452.66 KiB) Viewed 21860 times

Re: Demo strategies: QuotesDelta Indicator

Posted: Thu Nov 29, 2018 6:45 pm
by SuperDriveGuy
Thanks.

When I try to load the Quotes Delta 4 jar in Bookmap, I get the following error