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
Size:
Color:
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?
Size:
Color:
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
Size:
Color:
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.
Size:
Color:
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
Size:
Color:
See Bars.High in the QuickRef, if this is what I'm thinking.
Size:
Color:
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
Size:
Color:
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.
Size:
Color: