Hi guys,
someone could tell me why the following code doesn’t work?
once cbidx=-1
once orsi=-1
once crsi=-1
once lrsi=-1
once hrsi=-1
curr = rsi[period](close)
if BarIndex = cbidx then
crsi = curr
if curr>hrsi then
hrsi = curr
elsif curr<lrsi then
lrsi = curr
endif
else
cbidx = BarIndex
orsi = curr
crsi = curr
hrsi = curr
lrsi = curr
endif
if crsi>orsi then
drawcandle(orsi,hrsi,lrsi,crsi) coloured(0,200,0)
else
drawcandle(orsi,hrsi,lrsi,crsi) coloured(200,0,0)
endif
RETURN 20 as "20", 80 as "80"
It was supposed to graph rsi as candlestick, instead it only graphs a single dash of current value. I didn’t expect it to work in already completed bars (because I imagine for that bars only ohlc are available), but I don’t understand why it doesn’t work in real-time bars.
Thanks,
Alberto
Before debugging your code, please have a look at this version I made months ago for another member of the forum: RSI with candlesticks
Hi Nicolas,
your code has a different logic: your ohlc values are calculated applying rsi() on different price series; instead, I always use the close price.
It seems that even if the variables are declared as ONCE their prev value is lost.
That’s right, I get the same behaviour, even with my own version:
defparam drawonlastbaronly=true
mrsi = rsi[14](close)
if lastopen<>open[0] then
lastopen=open[0]
hh=0
ll=mrsi
o=mrsi
endif
c=mrsi
hh=max(hh,mrsi)
ll=min(ll,mrsi)
if o>c then
r=0
g=255
else
r=255
g=0
endif
drawcandle(o,hh,ll,c) coloured(r,g,0)
return //lastopen// barindex//o,hh,ll,c
I really don’t understand how the code is evaluated; does the code is executed every tick?
The following code should plot a line with constant value of 1 (only on the first tick of each bar it should switch to 0), instead it’s always 0
once cbidx=-1
once rval=1
if BarIndex = cbidx then
rval = 1
else
rval = 0
cbidx = BarIndex
endif
RETURN rval
Am I missing something or could it be a bug?
IT IS POSSIBLE BARINDEX=-1?
No, it wouldn’t.
According to documentation, BarIndex is a monothonically increasing value starting from 0
AVTParticipant
Senior
once cbidx=-1
once rval=1
// beginning of condition check
if BarIndex = cbidx then // it is never -1 cause it starts with 0, so
rval = 1 // the plotted val will never be 1
else // but instead
rval = 0 // the plotted val will be 0
cbidx = BarIndex // and cbidx is set to the current BarIndex
endif // end of condition check
RETURN rval // condition examined, result found and plotted
As far as I see it, you have a condition which is checked and a result is found and plotted. There is nowhere something telling the program to repeat the condition check.
Hi AVT,
I totally agree with you during the first execution of the code; that body is from a dummy indicator I wrote to reproduce the issue. Being an indicator, I expect that body will be executed for every tick; from the second execution onward cbidx won’t never be -1, it will be most of the time equal to BarIndex (except during the first tick of a new bar). So, most of the time the result should be 1.
Being delcared as “once” (that from what I understood from doc is somewhat similar to “static” keyword in classical programming languages) the previous values of cbidx and rval should be kept.
Hi guys,
I’m sorry to bother you (again) with this issue, but really I don’t understand the code execution logic behind an indicator. Another example:
<pre class=”lang:probuilder decode:true “>once cbidx=0
once a=0
a = BarIndex-cbidx
cbidx = BarIndex
return a
once cbidx=0
once a=0
a = BarIndex-cbidx
cbidx = BarIndex
return a
The returned value, a, is always 1.
I really don’t understand how is that possible!!!! The only way for “a” to be always 1 is that my code is executed only when a new bar is formed (more specifically, on the first tick of every new bar). But looking at other indicators I can see their values changing every tick, so the indicator has to be executed every tick. I checked the official doc again but it’s quite useless.
Live calculation of an indicator for an on-going candle is at every tick, so yes a=1. The only way I would understand the question and why you think a=1 is impossible would be in case you had a different understanding for “once” of what it really does, and would be expecting a to remain =0?
If that’s the case, what “once a=0” does is to perform a=0 just once at first candle, it’s a line not read at following candles, and it doesn’t prevent either other calculations to be made for “a” in the code. It means “read this line just once at the beginning of the history”, it doesn’t mean “this is the value of a once and for all”.
If this wasn’t the problem, sorry I completely misunderstood the question…
Noobywan,
I’m sorry but english not being my language maybe I can’t explain my thoughts clearly.
I’ll try one more attempt; lets see these two lines of code:
a = BarIndex-cbidx
cbidx = BarIndex
From my understanding, BarIndex is an increasing counter counting the bars processed in the graph. If you agree with that, so the value of BarIndex will change every first tick of a new bar; said in another way, all the ticks of the same bar will have the same value of BarIndex.
If what I just said is true, so the variable “a” should always be 0, except for every first tick of a new bar.
Suppose that the code is evaluating the last tick of bar 1000; the variables are
a = 1000-1000 = 0
cbidx = 1000
The next tick will be the first tick of bar 1001; so the variables will be
a = 1001 – 1000 = 1
cbidx = 1001
Again, the second tick of bar 1001; the variables will be
a = 1001 – 1001 = 0
cbidx = 1001
Third tick of bar 1001; the variables will be
a = 1001 – 1001 = 0
cbidx = 1001
…and so on…
First tick of bar 1002; the variables will be
a = 1002 – 1001 = 1
cbidx = 1002
ok I think I see what you mean, it implies you consider cbidx would change value at first tick and keep it in memory for second tick of same bar, but what the code does is reevaluate at each tick what the latest close price mean for all variable values compared with previous bar’s values, not with previous tick values of current bar… so in other words, if cbidx is calculated on a tick which is not the last of the candle, then its calculation is “forgotten” for this tick and “remade” at next tick starting again from values at close of previous candle, and only calculation of last tick of a candle is then kept for calculations of next candle
Really? So there is no way to keep a value from tick to tick? Only from bar to bar?
Values that can change from tick to tick within the same bar are stored in platform reserved keywords, like high or low. But for a user-created variable inside a probuilder personal code, within a given timeframe, I am not aware of any way to store in memory all the diffrenent values such a variable went through at ticks before the last one inside a same candle. Even using a drawcandle command to display the range an indicator value would have to happen by calculation at last tick from price open,high,low,close of cnadle rather than intermediate-tick memory storing… Nicolas might correct me when he’s back if I’m wrong…