Strategy · Pine Script v4 · On the chart

Highs and lows

by hasnocool· code credited to Coded by Eduardo Mattje

Calculates dynamic entry and exit price levels based on rolling lowest and highest bars with optional Average True Range (ATR) offsets.

View on GitHub

GitHub stars106on the repository
Pine versionv4older, still runs
Lines of code28
Settings8inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

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

28 lines of Pine Script v4 from 2HL.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

2HL.pine
//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Coded by Eduardo Mattje
strategy("Highs and lows", "HsLs", true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=50000, process_orders_on_close=true)
// Inputs
var I_ENTRY_LENGTH = input(2, "Entry length", minval=1, inline="Length", group="Basic settings")
var I_EXIT_LENGTH = input(2, "Exit length", minval=1, inline="Length", group="Basic settings")
var I_CLOSE_AFTER = input(5, "Close after X candles", tooltip="Leave 0 if you want this to be disabled.", group="Basic settings")
var I_ATR_ENTRY_MULT = input(0.0, "ATR entry multiplier", step=0.1, inline="ATR settings", group="ATR settings")
var I_ATR_EXIT_MULT = input(0.0, "ATR exit multiplier", step=0.1, inline="ATR settings", group="ATR settings")
var I_ATR_PERIOD = input(20, "ATR period", minval=2, inline="ATR settings", group="ATR settings")
var I_INITIAL_DATE = input(0, "Initial date", input.time, tooltip="Leave this value as it is to disable.", inline="Date selection", group="Date selection")
var I_FINAL_DATE = input(0, "Final date", input.time, tooltip="Leave this value as it is to disable.", inline="Date selection", group="Date selection")
// Calculations
_lowest = lowest(I_ENTRY_LENGTH) - atr(I_ENTRY_LENGTH) * I_ATR_ENTRY_MULT
_highest = highest(I_EXIT_LENGTH) + atr(I_EXIT_LENGTH) * I_ATR_EXIT_MULT
inMarket = strategy.opentrades != 0
opened = strategy.opentrades[0] > strategy.opentrades[1]
plot(not inMarket or not inMarket[1] ? _lowest[1] : na, "Entry price", color.red, 2, plot.style_linebr, offset=0)
plot(inMarket or inMarket[1] ? _highest[1] : na, "Exit price", color.teal, 2, plot.style_linebr, offset=0)
// Range check
inRange = (I_INITIAL_DATE == 0 ? true : time >= I_INITIAL_DATE) and (I_FINAL_DATE == 0 ? true : time <= I_FINAL_DATE)
// Orders
strategy.entry("Long", true, limit=_lowest, comment="Entry long", when=inRange)
strategy.exit("Long", "Long", limit=_highest, comment="Exit long")
if I_CLOSE_AFTER and barssince(opened) >= I_CLOSE_AFTER
    strategy.close_all(comment="It has been " + tostring(I_CLOSE_AFTER) + " candle" + (I_CLOSE_AFTER > 1 ? "s" : "")  + " since you entered")
Expand (42 lines)

Overview

Highs and lows places limit orders to enter long positions at the lowest price level and exits at the highest price level. An optional time-based exit closes all open positions after a user-defined number of bars. The script plots the target entry and exit levels directly on the price chart. There are no stop-loss orders configured.

Details

Published by

hasnocool on GitHub

Code credit

Coded by Eduardo Mattje, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Mean reversion, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

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

SettingTypeDefaultVariable
Entry lengthint2I_ENTRY_LENGTH
Exit lengthint2I_EXIT_LENGTH
Close after X candlesint5I_CLOSE_AFTER
ATR entry multiplierfloat0.0I_ATR_ENTRY_MULT
ATR exit multiplierfloat0.0I_ATR_EXIT_MULT
ATR periodint20I_ATR_PERIOD
Initial datetime0I_INITIAL_DATE
Final datetime0I_FINAL_DATE
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

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]

This strategy builds a spot grid between user-set high and low prices and draws it on the price chart. It splits that range into ten levels and places up to five long limit orders at the lower levels, sizing each from current equity. Orders already below price

Strategy v6 Support & resistanceMean reversion Repaints
Updated 6 weeks ago · None stated GitHub repository of Grid Spot Trading Algorithm [The Quant Science]

FairValueGap E003

by ruckard
13 stars

Calculates the distance between the current price and the opposite end of the latest fair value gap, entering trades on the expectation that price will partially fill the gap. It incorporates risk management with customizable stop loss, take profit based on ri

Strategy v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · None stated GitHub repository of FairValueGap E003

Wyckoff Phase

by muammeryldrm42
7 stars

This strategy identifies Wyckoff consolidation ranges directly on the price chart to trade potential spring and upthrust events. It calculates highest highs and lowest lows over a customizable lookback period, filtering setups by average volume spikes and ATR

Strategy v5 SMC / ICT / price actionSupport & resistance
Updated 3 months ago · MIT GitHub repository of Wyckoff Phase
5 stars

This strategy enters one long trade when the bar low comes within a set point tolerance of the prior day's low and the close finishes above that level. Entries are allowed only inside a UTC session and are blocked after a Friday cutoff hour. It stays flat unti

Strategy v5 Support & resistanceMean reversion Repaints
Updated 4 months ago · MIT GitHub repository of PDL — Prior Day Level Retest (Bull)

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers