Strategy based on Study with both 'Calculate' and 'CalculateValues'

Spin

Well-known member
Joined
May 22, 2019
Posts
506
Likes
212
Dear fellow-coders,


Another day, another coding issue. ❓❓

I am building a Strategy, based on VWAP. I have built quite a few Strategies before, but they were all based on a Study that only contained 'CalculateValues'.
VWAP is different: it contains both 'Calculate' and 'CalculateValues'.

I altered the (standard) MW VWAP Study lightly and it runs fine by itself in its new form on any chart I drop it on: the VWAP gets drawn and updated on each incoming Tick.

However, from the moment I build a Strategy, based on VWAP, and drop in onto a chart, the underlying VWAP simply freezes. Even when I add it to a 'skeleton'-Strategy (containing nothing more than 'Initialize') the VWAP fails to do its job. :unsure:
486

A little hint perhaps: when I click the 'pencil' icon, to call the Strategy's menu and close it by selecting 'update' or 'apply', the VWAP gets calculated up until the last bar at that moment. This is the behavior I would like to see, but without passing through the menu, obviously.

I believe this has to do with the VWAP 'Calculate' (and 'onTick ?) function no longer getting called. By using a bunch of 'debug'-lines, I established that 'Calculate' only gets executed upon exiting the menu or the very first tick after the Strategy is dropped on a chart. The same seems to be true for 'onTick'.

Why is this ?
Has anyone experienced similar behavior with the same or another Study ?
Did you get it fixed ?
How ?

All help / input welcome !

Thanks in advance !! 😁
 
Last edited:
Ok, so I investigated a little deeper and noticed the ticks continue to be treated when I use only the Study, but 'treatment' of incoming Ticks stops when I remove the Study and attach the Strategy (that is of course based on the exact same Study) to a chart.

I am puzzled now: what part of my (skeleton-) Strategy would 'break' the treatment / calculation of incoming Ticks ? :unsure::unsure:
 
Replay Mode is funny when it comes to Ticks:
1653465046204.png

So here is the question: does your browser support tick-based historical data ? If not, MW will 'construct' Ticks.

The question after that would be then: how are these constructed Ticks treated ? I'm afraid only MW staff can answer this ...
My original post in this thread is pretty old already, but I believe I had 'debug-listeners' in both 'onTick' and 'Calculate' to figure out what was going on behind the curtains.

YMMV :)
 
Hi @Spin I know this is a very old post but did you manage to figure out what was going on? I am also having issues running the Optimizer with a Strategy derived from the VWAP Study. I get NullPointers on all but on of the Optimizer runs and I need to figure out how to
 
Hi there @pandalover

The issue I faced in the original post of this thread (VWAP no longer updating) was indeed solved by adding a line to the Study's header.
I describe that (very simple) solution in post # 3: follow this link

Is that what you are referring to ? :)
 
Hi @Spin thanks for getting back to me. The Strategy already has "requiresBarUpdates" set to "true". If only it were that simple :)

I am attempting to use the Optimizer with a Strategy subclassed from the VWAP Study. Unfortunately, though the backtester works, subsequent runs are returning NullPointer exceptions when trying to pull Values.VWAP, Values.TOP1 and Values.BOTTOM1.

Any ideas would be much appreciated.
 
TBH: I only rarely worked with the optimizer, and on all those occasions it performed as expected. (On a sidenote: I guess that's a coder's fate: always writing Studies & Strategies for others, never getting to optimize your own 😌)

My suggestion would be to lavishly add debug-statements at key points in your Study, so you know which steps are handled correctly and which ones aren't.
Also: try adding 'barIndexNumbers' to every bar and add those in the debug statements. They will help you at which moment in 'time' the calculations go AWOL. I use this snippet often:

Java:
for (int i = 0; i < series.getEndIndex() + 1; i++) // iterate over all bars since beginning
        {
            series.setInt(i, Values.index, i); // easier to debug !
        }

(don't forget to export that Value to the RunTimeDescriptor, so it show up in a Cursor Data Window)
Java:
desc.exportValue(new ValueDescriptor(Values.index, Enums.ValueType.INTEGER, get("Index"), null));

I have had a couple 'watchlist'-issues before, and there I even added code to see how many bars are loaded/treated by the MW-engine at any given moment. You would simply subtract the number of the first bar in the series from the last bar in the series. I noticed that the Watchlist only goes back a (very) limited amount of bars. Perhaps the Optimizer shows similar behaviour ?

Finally, feel free to post your Study Log here, so we can all play debug-detective :cool:
 
Thanks for your thoughtful reply @Spin. I'm going to try and provide a skeletal example. That way, others will be able to benefit when we (hopefully) figure this out.
 
Top