Strategy · Pine Script v4 · On the chart

Higher High Lower Low Strategy Bot

by infinityhao

This strategy detects pivot highs and pivot lows with a left-bar and right-bar window, then filters those swings into higher highs and lower lows.

View on GitHub

GitHub stars3on the repository
Pine versionv4older, still runs
Lines of code114
Settings2inputs
LicenceNone statedfile: MPL-2.0
Last update5 years ago25 Mar 2021

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

114 lines of Pine Script v4 from Higher High Lower Low Strategy Bot.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Higher High Lower Low Strategy Bot.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © infinityhao

//@version=4
// This script finds pivot highs and pivot lows then calculates higher highs & lower lows.
// And also it calculates support/resistance by using HH-HL-LL-LH points.
strategy("Higher High Lower Low Strategy Bot", overlay =true)

// 1 left bar, 1 middle bar and 1 right bar to form the triangle
lb = input(1, title = "Left Bars", minval = 1)
rb = input(1, title = "Right Bars", minval = 1)

// mb is margin of bars
mb = lb + rb

// Pivot High - to find the highest bar and trigger at second highest bar
// iff function - iff(condition, result1, result2), condition true = result1 else result2
// na is a generic value of no particular type
// if current high price of mb is not na, then go into another sub iff (next line), else ph = na
// if highestbars of current high price of mb is true then ph = -lb, else ph = high[lb]
// iff function - https://www.tradingview.com/pine-script-docs/en/v4/language/Operators.html
ph = iff(not na(high[mb]), iff(highestbars(high, mb) == -lb, high[lb], na), na)

// Pivot Low - to find the lowest bar and trigger at second lowest bar
// iff function - iff(condition, result1, result2), condition true = result1 else result2
// if current low price of mb is not na, then go into another sub iff (next line), else pl = na
// if lowestbars of current low price of mb is true then pl = -lb, else pl = low[lb]
// iff function - https://www.tradingview.com/pine-script-docs/en/v4/language/Operators.html
pl = iff(not na(low[mb]), iff(lowestbars(low, mb) == -lb, low[lb], na), na)

// Determine Trend Direction, hl is highest or lowest
int hl = na
// if ph is true then hl = 1 else go into another sub iff (next line)
// if pl is true then hl = -1 else hl = na
// hl positive number means uptrend, negative means downtrend
// := history referencing operator - to refer to the historical values of any variable of a series type (values which the variable had on the previous bars)
hl := iff(ph, 1, iff(pl, -1, na))

// zz is zigzag pattern with multiple highs or lows
float zz = na
// if ph is true then zz = ph else go into another sub iff (next line)
// if pl is true zz = pl else zz = na)
zz := iff(ph, ph, iff(pl, pl, na))

// valuewhen(condition, source, occurrence) → series[integer]
// get value of hl/zz variable on second cross occurrence, this is because after confirmed the first highest or lowest bar
// and the second bar did not exceed the first bar
// valuewhen - https://www.tradingview.com/pine-script-reference/#fun_valuewhen
// if pl and h1 == -1 is to double confirm pl == -lb (lowest bar) and h1 is -1 (downtrend) and zz lesser than zz(second) to prove lower low
// if ph and h1 == 1 is to double confirm ph == -lb (highest bar) and h1 is 1 (uptrend) and zz greater than zz(second) to prove higher high
zz := iff(pl and hl == -1 and valuewhen(hl, hl, 1) == -1 and zz > valuewhen (zz, zz, 1), na, zz)
zz := iff(ph and hl == 1 and valuewhen(hl, hl, 1) == 1 and zz < valuewhen(zz, zz, 1), na, zz)

hl := iff(hl == -1 and valuewhen(hl, hl, 1) == 1 and zz > valuewhen(zz, zz, 1), na, hl)
hl := iff(hl == 1 and valuewhen(hl, hl, 1) == -1 and zz < valuewhen(zz, zz, 1), na, hl)
zz := iff(na(hl), na, zz)

findprevious()=>    // finds previous three points (b, c, d, e)
    ehl = iff(hl == 1, -1, 1) // First cycle start
    loc1 = 0.0, loc2 = 0.0, loc3 = 0.0, loc4 = 0.0  // to initialise loc
    xx = 0  // initial counter
    for x = 1 to 1000
        if hl[x] == ehl and not na(zz[x])
            loc1 := zz[x]
            xx := x + 1
            break
    ehl := hl   // First cycle peak
    for x = xx to 1000
        if hl[x] == ehl and not na(zz[x])
            loc2 := zz[x]
            xx := x + 1
            break
    ehl := iff(hl == 1, -1, 1) // Second cycle start
    for x = xx to 1000
        if hl[x] == ehl and not na(zz[x])
            loc3 := zz[x]
            xx := x + 1
            break
    ehl := hl  // Second cycle peak
    for x = xx to 1000
        if hl[x] == ehl and not na(zz[x])
            loc4 := zz[x]
            xx := x + 1
            break
    [loc1, loc2, loc3, loc4]    // group arrays together

float a = na, float b = na, float c = na, float d = na, float e = na
if not na(hl)
    [loc1, loc2, loc3, loc4] = findprevious()
    // Passing loc values to a, b, c, d, e
    a := zz
    b := loc1
    c := loc2
    d := loc3
    e := loc4

// AI section
higherHigh = zz and (a > b and a > c and c > b and c > d)
higherLow = zz and ((a >= c and (b > c and b > d and d > c and d > e)) or (a < b and a > c and b < d))
lowerHigh = zz and ((a <= c and (b < c and b < d and d < c and d < e)) or (a > b and a < c and b > d))
lowerLow = zz and (a < b and a < c and c < b and c < d)

// For testing of actual orders whether aligning with indicator or not
//plotshape(higherHigh, text="HH", title="Higher High", style=shape.labeldown, color=color.lime, textcolor=color.black, location=location.abovebar, transp=0, offset = -lb)
//plotshape(higherLow, text="HL", title="Higher Low", style=shape.labelup, color=color.lime, textcolor=color.black, location=location.belowbar, transp=0, offset = -lb)
//plotshape(lowerHigh, text="LH", title="Lower High", style=shape.labeldown, color=color.red, textcolor=color.white, location=location.abovebar, transp=0, offset = -lb)
//plotshape(lowerLow, text="LL", title="Lower Low", style=shape.labelup, color=color.red, textcolor=color.white, location=location.belowbar, transp=0, offset = -lb)

// Order Executer
if higherHigh and (not na(higherHigh)) or (lowerHigh and (not na(lowerHigh)))
    strategy.entry("buy", strategy.long, 130, when=strategy.position_size <= 0)

if higherHigh and (not na(higherHigh)) or (lowerLow and (not na(lowerLow)))
    strategy.entry("sell", strategy.short, 130, when=strategy.position_size > 0)

Overview

Higher High Lower Low Strategy Bot also derives support and resistance from higher-high, higher-low, lower-low and lower-high points and draws on the price chart. Left Bars and Right Bars change how many bars confirm each pivot. No stop loss is set by default, and the provided code does not show take-profit or position-sizing rules.

Details

Published by

infinityhao on GitHub

Type

Strategy, Pine Script v4

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
Left Barsint1lb
Right Barsint1rb
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

Omar Edited WF

by Alorse
330 stars

Omar Edited WF is a trading strategy overlay built around Williams Fractals and multiple Exponential Moving Averages. It detects high and low fractal pivot points over configurable lookback periods alongside three adjustable EMA trendlines. Long entries are ma

Strategy v5 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of Omar Edited WF
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
106 stars

This overlay strategy plots a two-timeframe Ichimoku setup on the price chart, with Tenkan, Kijun, Chikou, and Kumo from a base higher timeframe and an optional highest-timeframe cloud. It allows two entries in the same direction and uses ATR-based trailing st

Strategy v4 TrendVolatility & bands
Updated 2 years ago · GPL-3.0 GitHub repository of 2Tier Ichimoku Pyramiding [Takazudo]
104 stars

This strategy combines three Exponential Moving Averages with Williams Fractals to execute trend-following or mean-reverting trades. It analyzes price structure using short, medium, and long-term EMAs along with fractal highs and lows. Entries trigger when fra

Strategy v5 TrendMoving averages
Updated 3 years ago · MIT GitHub repository of 3MA and Fractals Strategy

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