Translating a recursive function from Thinkscript
Author: rmandel00
Creation Date: 10/8/2010 8:12 PM
profile picture

rmandel00

#1
The Thinkscript manual describes compoundValue as:
compoundValue(int length, IDataHolder visible data, IDataHolder historical data);

The default value of the length parameter is 1.
Description: Calculates a compound value according to following rule: if a bar number is bigger than length then the visible data value is returned, otherwise the historical data value is returned. This function is used to initialize studies with recursion.

The are several lines of code that I am working with and they are of the form:
rec prevLowSMI = compoundValue(1, if(isLow[1], SMIData[1], prevLowSMI[1]), 0);

Any help translating this would be most appreciated
Thanks,
Rich Man
profile picture

Eugene

#2
Kindly explain the following:

"rec" is the equivalent of returning a "float" value,
"if(isLow[1], ...)" appears to return a mix of boolean variables.

What does it mean? In other words, what exactly should the line starting with "if(isLow..." return if placed outside of this context?
profile picture

rmandel00

#3
With regard to the if statement:
if(isLow[1], SMIData[1], prevLowSMI[1])
This if Statement uses the result of a Boolean condition (isLow[bar-1] which if true returns SMIData[bar-1] and if false returns preLowSMI[bar-1].
I think the prevLowSMI is recursive through the rec statement.
They state:

Rec Variables
Rec variables are used to store floating point data. Values of such variables are calculated one after another in a chronological manner. When you assign a value to the rec variable you can use its values calculated for the previous bars. If you use data related to the period prior to the beginning of the time period then the rec variable is also calculated for the period. Before the calculation, the rec value is set to 0 for all moments of time.

rec x = x[1] + (random() - 0.5);
plot RandomWalk = x;
Shows random walk.

declare lower;
rec x = x[1] + x[10] * 0 + 1;
plot line = x;

This script takes data for the previous 10 bars. Thus, the rec calculation starts 10 bars prior to the beginning of the time period. As a result, the first drawn value of the plot line is 11.
---------------------
To give an example using compound value they state:
declare lower;
rec x = compoundValue(2, x[1] + x[2], 1);
plot FibonacciNumbers = x;

The example calculates the Fibonacci sequence. Starting from the third bar each following number is calculated as the sum of the previous two numbers while the numbers for the first two bars are equal to one. As a result you will get a plot containing the 1 1 2 3 5 etc values.

I hope that clarifies the problem.
Thanks,
Rich Man
profile picture

Cone

#4
This isn't really recursion, but most other TA apps "hide" or imply looping through the data series. So...
CODE:
Please log in to see this code.
Note that this implies that isLow is a boolean array that has the same number of elements as bars in the chart (Bars.Count), and SMIData is also a previously-declared DataSeries.

The same argument applies to the rest of your examples. Rec Variables are DataSeries.
profile picture

rmandel00

#5
Eugene and Cone,
Thanks for your help. I have another of the same series but with the last term nonzero that I don't know what to do with.

rec prevHigh = compoundValue(1, if(isHigh[1], high, prevHigh[1]), high);

Where does the last term high fit into the C# code?
Thanks,
Rich Man


profile picture

Eugene

#6
See Bars.High in the QuickRef, if this is what I'm thinking.
profile picture

rmandel00

#7
Hi,
I still don't have a solution to my problem or don't understand what is proposed. I found the following.

The compound value function is used to initialize a recursive function. In the case of the first two lines, it is simply telling the recursive function to initialize at zero. In other words, the function and arguments read this way...

compoundValue(length, value to be used after initialization, value at initialization)
The 4 lines at question are:
rec prevLowSMI = compoundValue(1, if(isLow[1], SMIData[1], prevLowSMI[1]), 0);
rec prevHighSMI = compoundValue(1, if(isHigh[1], SMIData[1], prevHighSMI[1]), 0);
rec prevLow = compoundValue(1, if(isLow[1], low, prevLow[1]), low);
rec prevHigh = compoundValue(1, if(isHigh[1], high, prevHigh[1]), high);

In the case of the second two lines, the recursive function will initialize at the value of the LOW and HIGH of the first bar of the chart respectively whereas the first two lines initialize at zero.
Thanks.
Rich Man
profile picture

Eugene

#8
Have you looked my hint in the QuickRef? Have you found how to initalize a double value?
CODE:
Please log in to see this code.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).