If I wanted to apply a function to only the last bar of a chart , how should I go about that?
Announcement
Collapse
No announcement yet.
Limiting a function to the last bar only
Collapse
X
-
Using the code below, when first loaded vClose for the last bar is displayed and not any previous bars (which is what I want). However when the chart moves on to the next bar the existing value of vClose remains for what is then the second to last bar. How can I get it to only display vClose for the last bar on the chart?
Code:var vClose = Close(0); var nBarIndex; nBarIndex = getCurrentBarIndex(); if (nBarIndex == 0) { return vClose; }
-
Hi Wat Tyler,
I don't know if this is what you need but I think it might or at least a variation of it.
PHP Code:var nState = null;
function main() {
var nState;
nState = getBarState();
if (nState == BARSTATE_NEWBAR) {
var vClose = Close(0);
return vClose;
} else{
vClose = 0;
return;
}
Comment
-
Wat,
The simple answer is you can't. What you can do however is modify the value of the study as returned for previous bars.
So, using waynecd's code (which unfortnately won't quite do what you require) you would use the 'setBar' command as per this example to set the return value of the study, in this case close, to 0 as the study moves from left to right through the historical data.
Then, moving forwards, as each new bar starts on the right hand side of the chart the return value of the study for the previous bar will be set to 0.
PHP Code:var nState = null;
function main() {
nState = getBarState();
if (nState == BARSTATE_NEWBAR) {
vClose = 0;
setBar(Bar.Value, -1, 0);
}
else {
var vClose = close(0);
}
return vClose;
}
RegardsLast edited by sandpiper; 05-22-2009, 03:10 PM.
Comment
Comment