Demo strategies: QuotesDelta Indicator
Demo strategies: QuotesDelta Indicator
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.
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.
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.
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:
Re: Demo strategies: QuotesDelta Indicator
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:
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).
Also, since they both return a value, it allows usage of ternary operation
Code: Select all
value = <condition> ? <this if true> : <this if false>
Code: Select all
Integer sizePrevious = (size == 0) ? book.remove(price) : book.put(price, size);
Code: Select all
int prevSize = book.onDepth(isBid, price, size);
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);
Best,
S
Re: Demo strategies: QuotesDelta Indicator
@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).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).
Thanks again for the examples... they are a great way of learning the API.
Chris G
-
- Posts: 67
- Joined: Thu Nov 01, 2018 1:50 pm
- Has thanked: 38 times
- Been thanked: 9 times
Re: Demo strategies: QuotesDelta Indicator
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
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 (178.2 KiB) Viewed 21911 times
-
- Posts: 67
- Joined: Thu Nov 01, 2018 1:50 pm
- Has thanked: 38 times
- Been thanked: 9 times
-
- Posts: 67
- Joined: Thu Nov 01, 2018 1:50 pm
- Has thanked: 38 times
- Been thanked: 9 times
Re: Demo strategies: QuotesDelta Indicator
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
Just not sure how to go about it, in the QuotesDelta Indicator example
Re: Demo strategies: QuotesDelta Indicator
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.
Notice that we calculate the price level before applying the update on the order book.
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;
}
}
}
-
- Posts: 67
- Joined: Thu Nov 01, 2018 1:50 pm
- Has thanked: 38 times
- Been thanked: 9 times
Re: Demo strategies: QuotesDelta Indicator
Thanks Serg. much appreciated!
Please make the necessary changes to the code and GUI controls
Please make the necessary changes to the code and GUI controls
Re: Demo strategies: QuotesDelta Indicator
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++;
}
}
}
-
- Posts: 67
- Joined: Thu Nov 01, 2018 1:50 pm
- Has thanked: 38 times
- Been thanked: 9 times
Re: Demo strategies: QuotesDelta Indicator
Thanks.
When I try to load the Quotes Delta 4 jar in Bookmap, I get the following error
When I try to load the Quotes Delta 4 jar in Bookmap, I get the following error
- Attachments
-
- Annotation 2018-11-29 194355.jpg (28.26 KiB) Viewed 21857 times