简体   繁体   中英

Pinescript - current [0] period pivot point as static line?

I'm building on a simple pivot point indicator with pine script and i wanted to have Daily current close, low and high calculated as one "static" moving line "object". Code below is behaving like a moving average now but showing like a pivot point. When you look previous periods you will see a line formed. But when you choose Bar Replay (behaving like live test) it is forming a "moving average" line. So overall I want it to be more like a "static" Pivot Point plus which moves due to current price action. So the PP would only move up or down as a one line due to price action.

//@version=2
strategy(title="Pivot", shorttitle="Pivot", overlay = true)
xHigh  = security(tickerid,"D", high[0])
xLow   = security(tickerid,"D", low[0])
xClose = security(tickerid,"D", close[0])
PP = (xHigh+xLow+xClose) / 3

width = input(1, minval=1)
plot(PP, color=#FFFFFF, title="PP", style = line, linewidth = width)

Is this possible? I think in MetaTrader4 it is like this automatically when you choose current ([0]) period.

----------------------------- update: came across of this one, but the [0] pivot line "hangs" from its starting point to the end point, not from the middle like is supposed to. Because if it hangs from the center it would move as a line due price action as wanted. Ideas?

//@version=4
study("Periodic lines", "", true)
htf = input("D", type = input.resolution)

// Returns the average number of current chart bars in the given target HTF resolution (this reflects the dataset's history).
f_avgDilationOf(_res) =>
    // _res: resolution of any TF (in "timeframe.period" string format).
    b = barssince(change(time(_res)))
    cumTotal = cum(b == 0 ? b[1] + 1 : 0)
    cumCount = cum(b == 0 ? 1 : 0)
    cumTotal / cumCount

// Period change detection.
pChange(res) =>
    change(time(res == 'Y' ? 'D' : res))

xHigh = security(syminfo.tickerid,"D", high[0])
xLow  = security(syminfo.tickerid,"D", low[0])
xClose = security(syminfo.tickerid,"D", close[0])
cvPP = (xHigh+xLow+xClose) / 3

// Get some previous value from last HTF period.
pHi = security(syminfo.tickerid, htf, cvPP, lookahead = barmerge.lookahead_on)
// Verify if current charts bars are part of the last dilation of HTF.
lastPBar = security(syminfo.tickerid, htf, barstate.islast, lookahead = barmerge.lookahead_on)
// Get avg no of chart bars in one dilation of HTF.
dilation = round(f_avgDilationOf(htf))
timeDelta = time - time[1]

var line pHiLine = na
// Holds bar index where a new line is created.
var pHiBar = 0
if pChange(htf)
    // Extend old line for the last bar before creating a new one.
    line.set_xy2(pHiLine, time, pHi[1])
    // Save bar index on transition.
    pHiBar := bar_index
    // Create new line.
    pHiLine := line.new(time, pHi, time + timeDelta, pHi, xloc.bar_time, color = color.white, width = 2)
    // Make type of the 2 `if` blocks the same.
    float(na)
else
    // We are not on a transition; prolong line until next transition.
    line.set_xy2(pHiLine, time, pHi)
    float(na)

// If we are in the last bars of the HTF resolution's dilation, project line into the future with remaining bars in average no of bars in dilation.
if lastPBar
    line.set_xy2(pHiLine, time + (timeDelta * (dilation - (bar_index - pHiBar))), pHi)

Maybe this can help:

//@version=2
strategy(title="Pivot", shorttitle="Pivot", overlay = true)
xHigh  = security(tickerid,"D", high[1])
xLow   = security(tickerid,"D", low[1])
xClose = security(tickerid,"D", close[1])
PP = (xHigh+xLow+xClose) / 3

width = input(1, minval=1)
plot(PP, color=#FFFFFF, title="PP", style = line, linewidth = width)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM