Ramses
Ramses

Reputation: 672

Find the highest and lowest value for a time frame in the pine editor

As an absoulutely new beginner I'm experimenting with the pine editor of Tradingview. I wrote a simple script that plots the difference between ema and dema. Additionally, I want to get the highest and lowest value in the choosen time frame.

Let's assume the highest closed value of a stock is $120,3 and the lowest closed value is $49,41 in the 6M time frame. I want to plot these two horizontal lines that represents all-time high and all-time low for a specific time frame.

//@version=4
study(title="Test")

biggest(series) =>
    max = 0.0
    max := nz(max[1], series)
    if series > max
        max := series
    max

smallest(series) =>
    min = 0.0
    min := nz(min[1], series)
    if series < min
        min := series
    min

fast = 14, slow = 50

length = input(fast, minval=1)
src = input(close, title="Source")
e1 = ema(src, length)
e2 = ema(e1, length)
dema = 2 * e1 - e2

band4 = hline(0, "Upper Band", color=#ff0000)

fastEMA = ema(close, fast)
slowEMA = ema(close, slow)
test = (dema - slowEMA)//(high1-low1)
plot(test,color=color.white)

Upvotes: 1

Views: 6394

Answers (2)

AnyDozer
AnyDozer

Reputation: 2568

//@version=4
study(title="Help (Test)")

biggest(series) =>
    max = 0.0
    max := nz(max[1], series)
    if series > max
        max := series
    max

smallest(series) =>
    min = 0.0
    min := nz(min[1], series)
    if series < min
        min := series
    min

fast = 14, slow = 50

length = input(fast, minval=1)
src = input(close, title="Source")
e1 = ema(src, length)
e2 = ema(e1, length)
dema = 2 * e1 - e2

band4 = hline(0, "Upper Band", color=#ff0000)

fastEMA = ema(close, fast)
slowEMA = ema(close, slow)
test = (dema - slowEMA)//(high1-low1)
plot(test,color=color.black)

//[ADDON]
period = input("6M", "Period hi/lo detect", input.resolution) //  Six Months

var hi = 0.0
var lo = 10e10
var br = 0
var lnhi = line.new(na, na, na , na)
var lnlo = line.new(na, na, na , na)

if change(time(period))
    hi := test
    lo := test
    br := bar_index
    lnhi := line.new(br, hi , br, hi, color=color.red, width=2)
    lnlo := line.new(br, lo , br, lo, color=color.green, width=2)
    float(na)
else
    hi := max(test, hi)
    lo := min(test, lo)

line.set_xy1(lnhi, br, hi)
line.set_xy2(lnhi, bar_index, hi)
line.set_xy1(lnlo, br, lo)
line.set_xy2(lnlo, bar_index, lo)

Your plot test with the ABSOLUTE HIGHEST and LOWEST value for six months time frames. enter image description here

Upvotes: 4

Shiraaz.M
Shiraaz.M

Reputation: 3191

You can use the highest() and lowest() functions.

So using your example you can add the highest and lowest bands as follows:

//@version=4
study(title="Test")

hiloperiod = 200 // track highest/lowest of the last 200 periods
fast = 14, slow = 50

length = input(fast, minval=1)
src = input(close, title="Source")
e1 = ema(src, length)
e2 = ema(e1, length)
dema = 2 * e1 - e2

band4 = hline(0, "Upper Band", color=#ff0000)

fastEMA = ema(close, fast)
slowEMA = ema(close, slow)
test = (dema - slowEMA)//(high1-low1)
plot(test,color=color.white)

// Plot the highest and lowest values for the last hilo period.
hi=highest(hiloperiod)
lo=lowest(hiloperiod)
plot(hi, color=color.green, linewidth=2)
plot(lo, color=color.green, linewidth=2)

Using it on the bitcoin price for example yields this graph. Notice how the green lines move to represent the highest and lowest prices for that timeframe

bitcoin price with high and low tracking

Upvotes: 0

Related Questions