I am trying to develop a multi time frame indicator using pine script. I use the following line for choosing the prices based on which I can do further analysis.
resolution = "D"
source = security(syminfo.tickerid, resolution, close, gaps=true)
...
... <indicator developed using source>
...
If I want the same indicator, but on a different time scale. I simply change the resolution
variable to be whatever I want. So if I want the same indicator on a 30 minute chart, I simply write resolution="30"
and that would change the source
and every calculations from there.
The indicator is supposed to draw a label on the last bar and the colour of the label would be the result we want.
My problem is: keeping the resolution variable constant, if I change the timeframe of the tradingview charting, the colour of the indicator also changes. Which is weird, because af far as my understanding goes, the timeframe of the chart being viewed by me has nothing to do with the indicator, since the indicator is specifically developed from the values inside source
variable which is constant irrespective of what timeframe I view the chart.
Now, I am going to some details on the working of the indicator. In case that is relevant for this debugging (although I think understanding the indicator probably is not necessary for solvig my problem). It is the "SqueezePro Indicator" developed by John Carter. If the bollinger band is within Keltner band, then there is a squeeze. Now the squeeze can be tight, standard, or loose. We give a circular label whose colour is a determined whether the squeeze is tight or standard or loose. This circular label is printed only once - that too on the last bar.
Secondly, we also show the momentum as another label. Just like squeeze label, this is also a one time label - displayed on the last bar of the chart. Its colour is based on the condition whether the momentum is positive or negative. However, for fancy purposes, this label is not circular in shape, but an up or down triangle shape.
Unlike the original Squeeze Pro indicator, I am only bothered with whether currently there is a squeeze or not and whether currently there is an upward momentum or not. I am not concerned with past squeezes or past momentums. Thats why I only need two labels - one for giving me the current momentum and one for giving me the current squeeze.
//@version=4
study(title="Squeeze Pro", shorttitle="SqueezePro", overlay=true)
resolution="D" // This resolution should theoretically dictate the indicator's colour
source = security(syminfo.tickerid, resolution, close, gaps=true) // Variable on which the indicator is based.
length = 20
ma = sma(source,length)
devBB = stdev(source,length)
devKC = sma(tr,length)
//Bollinger 2x
upBB = ma + devBB * 2
lowBB = ma - devBB * 2
//Keltner 2x
upKCWide = ma + devKC * 2
lowKCWide = ma - devKC * 2
//Keltner 1.5x
upKCNormal = ma + devKC * 1.5
lowKCNormal = ma - devKC * 1.5
//Keltner 1x
upKCNarrow = ma + devKC
lowKCNarrow = ma - devKC
sqzOnWide = (lowBB >= lowKCWide) and (upBB <= upKCWide)
sqzOnNormal = (lowBB >= lowKCNormal) and (upBB <= upKCNormal)
sqzOnNarrow = (lowBB >= lowKCNarrow) and (upBB <= upKCNarrow)
noSqz = (sqzOnWide == false) and (sqzOffWide == false)
//Momentum Oscillator
mom = linreg(source - avg(avg(highest(high, length), lowest(low, length)),sma(close,length)),length,0)
//Momentum histogram color
momentum_color = iff( mom > 0,iff( mom > nz(mom[1]), color.lime, color.green),iff( mom < nz(mom[1]), color.red, color.maroon))
momentum_style= iff( mom > 0, label.style_triangleup, label.style_triangledown)
sq_color = sqzOnNarrow ? color.orange : sqzOnNormal ? color.red : sqzOnWide ? color.gray : color.green
squeeze_size = sqzOnNarrow ? size.tiny : sqzOnNormal ? size.small : sqzOnWide ? size.normal : size.large
squeeze_text = sqzOnNarrow ? "Tight" : sqzOnNormal ? "Standard" : sqzOnWide ? "Loose" : "No"
momentum_label = label.new(bar_index, high, color=momentum_color, textcolor=color.white, style=momentum_style )
label.set_text(momentum_label, resolution_formatted+"\n"+squeeze_text)
label.set_x(momentum_label, bar_index)
label.set_y(momentum_label, high+3*tr)
squeeze_label = label.new(bar_index, high, color=sq_color, textcolor=color.white, style = label.style_circle)
label.set_x(squeeze_label, bar_index)
label.set_y(squeeze_label, high+1*tr)
label.set_size(squeeze_label, squeeze_size)
label.delete(momentum_label[1])
label.delete(squeeze_label[1])
It displays the two labels nicely on the last bar. However, my problem is : upon changing the timeframe of the tradingview, the colour of the indicator also changes. Why should that happen? As far as resolution
variable is constant, why should the indicator's colour change? What mistake am I making here ?