Malav Parekh
Malav Parekh

Reputation: 13

offset in atr in tradingview - pine script

Need to have offset in ATR function in pine script

Background: The indicator script below is based on the hypothesis that this period's range will be mostly within [last period high + atr(14)] and [last period low - atr(14)]. I want to sell the high call option and low put option and enjoy the premium at the end of the period (week, month).

I have created a pine script that will calculate this period range based on [last period high + atr(14)] and [last period low - atr(14)].

However, because atr(14) applies to current period as well, it plots the dots that change with the current price.

I need to have an atr(14) days till the last period and not considering this current period. Could you please advise how to achieve that.

//@version=3
study(title="High and Low Levels", shorttitle="HL Levels", overlay = true)

Width = input(2,  minval=1)
SelectPeriod = input("W", defval="W", type=string)
LookBack = input(1,  minval=1)

xHigh = high[LookBack]
xHigh := xHigh + (atr(14))
xLow = low[LookBack] - atr(14)

vS1 = xHigh
vR1 = xLow

plot(vS1, color=#ff0000, title="S1", style = circles, linewidth = Width)

plot(vR1, color=#009600, title="R1", style = circles, linewidth = Width)

Expected: the dots plotted should be plotted based on last period high + last period atr(14) and last period low - last period atr(14)

Actual: the dots plotted based on last week high + atr(14) till the current period and last week low - atr(14) till the current period. This is changing the dots based on the current price movement.

Upvotes: 1

Views: 2433

Answers (2)

Mehul B. Shah
Mehul B. Shah

Reputation: 1

This should help....

plot(vS1[1], color=#ff0000, title="S1", style = circles, linewidth = Width)

plot(vR1[1], color=#009600, title="R1", style = circles, linewidth = Width)

Upvotes: 0

Michel_T.
Michel_T.

Reputation: 2821

Maybe, I got it wrong, but I think what you want is to take the previous value of atr(14). So it looks like that:

xLow = low[LookBack] - atr(14)[1]

I think, you've got my idea.

Upvotes: 1

Related Questions