Is there a way to retain values in a formula, so that (say) the value for the most recent swing high can be plotted as a horizontal line and then reset to the new level when a new swing high occurs? I've tried adapting code from existing studies like pivots, but no luck. Any ideas gratefully received!
Announcement
Collapse
No announcement yet.
retaining a value in a custom formula
Collapse
X
-
Hi,
If all you are trying to do is create a variable whos contents will survive through new iterations of main(), then define the variable outside main() and preMain().
preMain(){
.
.
.
}
// Variables defined here will keep their contents through
// iterations of main
var IwantToSaveThisInfo;
main(){
.
.
.
}Garth
-
Hi Garth
Many thanks for the response. I'm not quite sure whether I made myself clear in the original post. I'm basically trying to write a study so that when a pattern occurs, the high of the last bar of the pattern is retained (and can be plotted as a horizontal line on a chart) until the next time the pattern occurs when the value/line will change to the high of the last bar of the new pattern.
The problem I'm having is that I can write the code for the pattern OK, but obviously if I just ask it to return the high of the pattern bar, this will simply reiterate for each following bar and I end up with a study that plots the high of each bar.
Is that what you thought I meant?
Many thanks.
Andy
Comment
-
Hi,
I guess I misunderstood. What exactly is the problem you are trying to solve. Remebering the vaule through each iteration of main() is what I was thinking the problem was, but is it drawing the lines? It's still not clear to me...
GGarth
Comment
-
Comment
-
I'm trying to write a study so that every time a particular chart pattern occurs, the high of the last bar of the pattern is captured and displayed on the chart as a horizontal line. (It's the possible basis for a breakout trading system so the idea is to look for upward breaches by prices of this line.)
Each time a new instance of the pattern occurs I want to discard the high of the previous instance of the pattern and start using the high of the new instance . And so on for each subsequent instance of the pattern.
In terms of appearance, the study based on this code would look something like that of the x bar high study, though changes in the level would be rather less frequent as the pattern is obviously less common as it won't occur every x bars.
Hope this makes a bit more sense - apologies for my lack of clarity in previous posts. Many thanks.
A
Comment
-
OK, well there are two parts to this problem. One is retaining the value in the EFS, which is done as I stated previosuly. The second, is how best to create the line.
The easiest way to do this, is to just return the value you saved in the EFS. For example if it is the high():
PHP Code:var LastHigh = 0;
main(){
if (high() > LastHigh){
LastHigh = high();
}
return(LastHigh);
}
GarthGarth
Comment
-
Many thanks Garth. I've put a sample of the code below, but for some reason I keep getting a syntax error message:
"missing ; before main(){ " which doesn't make a lot of sense. Am I doing something dumb?
function preMain() {
setPriceStudy(true);
setStudyTitle("Last High");
setCursorLabelName("LH");
setPlotType(PLOTTYPE_FLATLINES);
}
var LastHigh = 0;
main(){
if (high() > LastHigh){
LastHigh = high();
}
return(LastHigh);
}
Comment
Comment