Strategy · Pine Script v6 · On the chart

PriceStep Pivot Strategy

by ivansoft

A strategy file on the price chart that detects pivot highs and pivot lows over a settable fractal length, marks them with triangles and debug labels, and plots the highest and lowest prices over the same length as step lines.

View on GitHub

Repaints

GitHub stars1on the repository
Pine versionv6current
Lines of code119
Settings4inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago1 Mar 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

119 lines of Pine Script v6 from vstrategy/price_step_strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

price_step_strategy.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ivansoft

//@version=6
strategy("PriceStep Pivot Strategy", "price_step_strategy", overlay = true, max_labels_count = 100)

var int fractal = input.int(100, "fractal bars")
var float step1p = input.float(0.5, "step x1 %", inline = "01") / 100  // 0.5 %
var float step10p = input.float(5, "step x10 %", inline = "01") / 100  // 5 %
var bool bgfill = input.bool(true, "Backgroung Fill")

var divisor = array.from(
     0.0001, 0.0002, 0.0005,
     0.001, 0.002, 0.005,
     0.01, 0.02, 0.05,
     0.1, 0.2, 0.5,
     1, 2, 5,
     10, 20, 50,
     100, 200, 500,
     1000, 2000, 5000,
     10000, 20000, 50000)

debugLabelUp(txt,overlay=true,offset=0) =>
    label.new(bar_index+offset, high, txt, yloc = yloc.abovebar, style = label.style_arrowdown, force_overlay = overlay, textcolor = color.black)
debugLabelDn(txt,overlay=true,offset=0) =>
    label.new(bar_index+offset, low, txt, yloc = yloc.belowbar, style = label.style_arrowup, force_overlay = overlay, textcolor = color.black)

var displayMode = display.data_window + display.pane
var displayOverlayMode = display.pane

var allpvh = array.new<float>()
var allpvl = array.new<float>()

// fractal = 100
// step1 = 1.0
// step10 = 5.0

pvh = ta.pivothigh(fractal,fractal)
pvl = ta.pivotlow(fractal,fractal)

plotshape(pvh, "pivoth", shape.triangledown, location.abovebar, color.new(color.gray,50), -fractal, size = size.tiny)
plotshape(pvl, "pivotl", shape.triangleup, location.belowbar, color.new(color.gray,50), -fractal, size = size.tiny)

if not na(pvh)
    debugLabelUp(str.format("{0}",pvh),offset=-fractal)
if not na(pvl)
    debugLabelDn(str.format("{0}",pvl),offset=-fractal)

if not na(pvh)
    allpvh.push(pvh-int(pvh))
if not na(pvl)
    allpvl.push(pvl-int(pvl))

pvh_bars = ta.barssince(not na(pvh))+fractal
pvl_bars = ta.barssince(not na(pvl))+fractal

if barstate.islastconfirmedhistory
    // debugLabelUp(str.format( "h {2}\nl {1}\nm {0}",allpvh.median(),allpvh.min(),allpvh.max() ))
    // debugLabelDn(str.format( "m {0}\nl {1}\nh {2}",allpvl.median(),allpvl.min(),allpvl.max() ))
    // debugLabelDn(str.format("{0}\n{1}",pvh_bars,pvl_bars))
    na

th = ta.highest(fractal)
tl = ta.lowest(fractal)
// th_bars = ta.highestbars(fractal)
// tl_bars = ta.lowestbars(fractal)

// var float highest = 0
// var float lowest = 99999999999
// if th > highest
//     highest := th
// if tl < lowest 
//     lowest := tl

plot(th, "highest", style = plot.style_steplinebr, color = color.new(color.gray,50))
plot(tl, "lowest", style = plot.style_steplinebr, color = color.new(color.gray,50))

bullish = close > open
bearish = close < open

dividx1 = divisor.binary_search_rightmost(math.ceil(close * step1p))
step1 = divisor.get(dividx1)
dividx10 = divisor.binary_search_rightmost(math.ceil(close * step10p))
step10 = divisor.get(dividx10)

// highint1 = switch
//     bullish => step1 * math.ceil(high / step1)
//     bearish => step1 * math.ceil(high / step1)
//     => step1 * math.round(high / step1)
highint1 = step1 * math.ceil(high / step1)
lowint1 = step1 * math.floor(low / step1)
highint10 = step10 * math.round(high / step10)
lowint10 = step10 * math.round(low / step10)

// if lowint1 > high
//     lowint1 := lowint1[1]
// if highint1 < low
//     highint1 := highint1[1]

var lbllast_up = debugLabelUp("")
var lbllast_dn = debugLabelDn("")
if barstate.islast
    label.set_text(lbllast_up, str.format("{1}\n{0}",th,close))
    label.set_text(lbllast_dn, str.format("{0}\n{1}",tl,close))
    label.set_x(lbllast_up, highint1 > highint1[1] ? bar_index : bar_index-5000)
    label.set_x(lbllast_dn, lowint1 <= lowint1[1] ? bar_index : bar_index-5000)


// plot(highint10, color = color.new(color.blue,50), style = plot.style_steplinebr)
// plot(lowint10, color = color.new(color.blue,50), style = plot.style_steplinebr)
ph1 = plot(highint1, "high", color = color.new(color.green,50), style = plot.style_steplinebr, linewidth = 3, display = displayOverlayMode)
pl1 = plot(lowint1, "low", color = color.new(color.red,50), style = plot.style_steplinebr, linewidth = 2, display = displayOverlayMode)

fillcolor = switch
    not bgfill => na
    bullish => color.new(color.green,90)
    bearish => color.new(color.red,90)
    => color.new(color.gray,50)
fill(ph1, pl1, color = fillcolor)

Overview

PriceStep Pivot Strategy also rounds highs and lows to price steps of one percent-scale and ten percent-scale sizes. The visible code shows no entry or exit orders, so it works as a pivot and price level study. Settings are fractal bars, the two step sizes and background fill.

Details

Published by

ivansoft on GitHub

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Topics

pinescripttradingvew

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
fractal barsint100fractal
step x1 %float0.5step1p
step x10 %float5step10p
Backgroung Fillbooltruebgfill
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This trend-following strategy executes a classic Donchian channel breakout system designed to cross-validate backtest results against external framework benchmarks. It triggers a long entry when the closing price breaks above the highest high of the prior entr

Strategy v6 BreakoutTrend
Updated 2 days ago · AGPL-3.0 GitHub repository of Forven S01509 - Simple Donchian 20/10 Breakout
330 stars

This strategy divides a fixed price range between an upper and a lower limit into equally spaced grid levels and plots them on the price chart. It marks a buy when price crosses down through a grid and a sell when price crosses up through a grid, using either

Strategy v5 Support & resistanceMean reversion Repaints
Updated 2 months ago · None stated GitHub repository of Grid Bot Simulator [Alorse]
257 stars

This strategy calculates dynamic support and resistance levels using pivot highs and lows alongside a custom smoothed trend filter and a 3-period EMA. It identifies entry opportunities when price breaks out relative to the trend filter and short-term EMA cross

Strategy v5 TrendSupport & resistance Repaints
Updated 7 weeks ago · None stated GitHub repository of 30s - ES 1m Strategy Candle Bar

STRG-HOLP

by harryguiacorn
220 stars

STRG-HOLP is a trading strategy that identifies High of Low Period (HOLP) and Low of High Period (LOHP) breakout setups. It tracks the lowest low or highest high over a user-defined lookback window. A long trade opens when the close breaks above the high of th

Strategy v5 BreakoutSupport & resistance
Updated 1 year ago · None stated GitHub repository of STRG-HOLP
106 stars

This script monitors opening gap conditions and breakout behavior relative to the first candle's range across up to twenty configurable stock tickers. It detects whether following bars remain inside the opening range before breaking out, displaying the status

Strategy v4 Screeners & dashboardsBreakout Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 1St Bar GAP+BkOut Screener v2 by RM

More Support & resistance scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers