Demo strategies: QuotesDelta Indicator

Custom indicators, trading strategies, data export and recording and more...
TraderCG
Posts: 5
Joined: Fri Sep 21, 2018 7:43 am

Demo strategies: QuotesDelta Indicator

Post by TraderCG » Sat Nov 03, 2018 6:52 am

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.
Chris G

Tags:

Serg
Posts: 106
Joined: Mon Jun 11, 2018 12:40 pm
Has thanked: 17 times
Been thanked: 28 times

Re: Demo strategies: QuotesDelta Indicator

Post by Serg » Sat Nov 03, 2018 5:06 pm

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 2447 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 2445 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

TraderCG
Posts: 5
Joined: Fri Sep 21, 2018 7:43 am

Re: Demo strategies: QuotesDelta Indicator

Post by TraderCG » Sun Nov 04, 2018 1:46 am

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.
Chris G

SuperDriveGuy
Posts: 66
Joined: Thu Nov 01, 2018 1:50 pm
Has thanked: 37 times
Been thanked: 9 times

Re: Demo strategies: QuotesDelta Indicator

Post by SuperDriveGuy » Wed Nov 21, 2018 2:12 pm

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
Attachments
Annotation 2018-11-21 150519.jpg
Annotation 2018-11-21 150519.jpg (178.2 KiB) Viewed 2375 times

SuperDriveGuy
Posts: 66
Joined: Thu Nov 01, 2018 1:50 pm
Has thanked: 37 times
Been thanked: 9 times

Re: Demo strategies: QuotesDelta Indicator

Post by SuperDriveGuy » Wed Nov 28, 2018 3:32 pm

Anyone?

SuperDriveGuy
Posts: 66
Joined: Thu Nov 01, 2018 1:50 pm
Has thanked: 37 times
Been thanked: 9 times

Re: Demo strategies: QuotesDelta Indicator

Post by SuperDriveGuy » Thu Nov 29, 2018 3:31 pm

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

Serg
Posts: 106
Joined: Mon Jun 11, 2018 12:40 pm
Has thanked: 17 times
Been thanked: 28 times

Re: Demo strategies: QuotesDelta Indicator

Post by Serg » Thu Nov 29, 2018 4:18 pm

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.

SuperDriveGuy
Posts: 66
Joined: Thu Nov 01, 2018 1:50 pm
Has thanked: 37 times
Been thanked: 9 times

Re: Demo strategies: QuotesDelta Indicator

Post by SuperDriveGuy » Thu Nov 29, 2018 5:46 pm

Thanks Serg. much appreciated!

Please make the necessary changes to the code and GUI controls

Serg
Posts: 106
Joined: Mon Jun 11, 2018 12:40 pm
Has thanked: 17 times
Been thanked: 28 times

Re: Demo strategies: QuotesDelta Indicator

Post by Serg » Thu Nov 29, 2018 6:21 pm

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 2322 times

SuperDriveGuy
Posts: 66
Joined: Thu Nov 01, 2018 1:50 pm
Has thanked: 37 times
Been thanked: 9 times

Re: Demo strategies: QuotesDelta Indicator

Post by SuperDriveGuy » Thu Nov 29, 2018 6:45 pm

Thanks.

When I try to load the Quotes Delta 4 jar in Bookmap, I get the following error
Attachments
Annotation 2018-11-29 194355.jpg
Annotation 2018-11-29 194355.jpg (28.26 KiB) Viewed 2320 times

Post Reply