Convert thinkScript CompoundValue() to EasyLanguage


Hey Pete,

Thanks a bunch for your help in the past with thinkscript!  I recently switched to Tradestation and was wondering if you had a way to convert a CompoundValue code from thinkscript into Easy Language (see below):

def Date = GetYYYYMMDD();
def DayRoll = CompoundValue(1, Date != Date[1], yes);

if (DayRoll) {
volumeSum = volume;
volumeVWAPsum = volume * vwap;
volumeVWAP2sum = volume * Sqr(vwap);
} else {
volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
volumeVWAPsum = CompoundValue(1, volumeVWAPsum[1] + volume * vwap, volume * vwap);
volumeVWAP2sum = CompoundValue(1, volumeVWAP2sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));

This is a component of thinkscript’s VWAP study that I’m trying to incorporate into a strategy on tradestation that rolls VWAP on a 1min chart to show new statistics off of the opening of each trading session.

Thanks in advance for your help!


Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on May 10, 2020 6:03 pm
Private answer

I can show you how to convert one as an example. Let's take this line of code from the section of code you provided:

volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);

We can get all the details about how this function works here:

All it really does is to initialize the variable with a value at the bar number listed in the first parameter. The second parameter is the value for every bar after that. The third parameter is the value used to initialize at the bar number in the first parameter. Confused? You should be. But in plain English, CompoundValue() is merely the tool thinkScript uses to initialize a recursive variable. In practice, you will find it is rarely needed. You can create a recursive variable without it. And in 99.9% of cases it will work just fine. Experience tells you when that 0.1% comes into play.

But for those that work with more standardized computer language the EasyLanguage version will actually make things much more clear. All we do is create an if/then/else statement. First checking if the BarNumber is the first bar on the chart. Then we initialize the variable with a value of our choosing. In the else section of that statement we assign the value we want to use for every bar after BarNumber 1.

Here is is in EasyLanguage:

double volumeSum(0.0);
If BarNumber() == 1 Then Begin
volumeSum = volume;
Else Begin
volumeSum = volumeSum[1] + volume;

Once you get used to the thinkScript method of initializing recursive variables you realize how efficient that structure is, compared to it's equivalent in other languages. Try writing that EasyLanguage form in a single line of code. It can be done but it's a real mess.

Marked as spam
Posted by (Questions: 38, Answers: 3345)
Answered on May 10, 2020 6:38 pm
You're the man - thanks Pete!
( at May 10, 2020 7:28 pm)