Vendrel
Vendrel

Reputation: 987

How to differentiate an expression?

Here's the script of the basic Bollinger Bands Width indicator (plus my commented explanation):

//@version=5
indicator(title="Bollinger Bands Width", shorttitle="BBW", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
bbw = (upper-lower)/basis
// Explanation of what I seek:
// bbwd = bbw minus previousbbw
plot(bbwd, "Bollinger Bands Width Differenctial", color=#138484)

There's a commented line starting "Example of what I need". I would like to make a variable by subtracting the previous value of Width from the current value of Width. To measure speed.

Upvotes: 0

Views: 347

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8789

This shows two different ways to achieve your goal. The precision was increased to 6 because the values are smallish:

//@version=5
indicator(title="Bollinger Bands Width", shorttitle="BBW", format=format.price, precision=6, timeframe="", timeframe_gaps=true)
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
bbw = (upper-lower)/basis

// Explanation of what I seek:
// bbwd = bbw minus previousbbw
bbwd1 = ta.change(bbw)
bbwd2 = bbw - bbw[1]
plot(bbwd1, "Bollinger Bands Width Differenctial", color=#13848450, linewidth=6)
plot(bbwd2, "Bollinger Bands Width Differenctial", color=#138484)

This is the refman entry for ta.change(). This is the usrman section that explains the [] history-referencing operator.

Upvotes: 1

Related Questions