Strategy · Pine Script v6 · On the chart

Basic Long Entry System

by Tim1l

This strategy evaluates higher-timeframe four-hour ADX momentum and RSI conditions to generate long trades on lower timeframes like 15 minutes.

View on GitHub

Repaints

GitHub stars6on the repository
Pine versionv6current
Lines of code128
Settings6inputs
LicenceMIT
Last update1 year ago23 Jul 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

128 lines of Pine Script v6 from strategy1.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

strategy1.pine
//@version=6
strategy("Basic Long Entry System", overlay=true)

// Parameters
lenA      = input.int(14, "Length A")
lenB      = input.int(14, "Length B")
threshA   = input.float(30.0, "Threshold A", step=1.0)
lenC      = input.int(16, "Length C")
threshB   = input.float(30.0, "Threshold B", step=1.0)
maxBars = input.int(32, "Max Bars to Hold (1-8 hours)", minval=4, maxval=32)

// --- Functions for calculation ---
func1(h, l, prev_close) =>
    math.max(h - l, math.max(math.abs(h - prev_close), math.abs(l - prev_close)))

func2(h, l, period) =>
    dm_plus = h - h[1]
    dm_minus = l[1] - l
    dm_plus := dm_plus < 0 ? 0 : dm_plus
    dm_minus := dm_minus < 0 ? 0 : dm_minus
    dm_plus := dm_plus <= dm_minus ? 0 : dm_plus
    tr = func1(h, l, h[1])
    atr = ta.ema(tr, period)
    dm_plus_ema = ta.ema(dm_plus, period)
    di_plus = atr != 0 ? 100 * dm_plus_ema / atr : 0
    di_plus

func3(h, l, period) =>
    dm_plus = h - h[1]
    dm_minus = l[1] - l
    dm_plus := dm_plus < 0 ? 0 : dm_plus
    dm_minus := dm_minus < 0 ? 0 : dm_minus
    dm_minus := dm_minus <= dm_plus ? 0 : dm_minus
    tr = func1(h, l, h[1])
    atr = ta.ema(tr, period)
    dm_minus_ema = ta.ema(dm_minus, period)
    di_minus = atr != 0 ? 100 * dm_minus_ema / atr : 0
    di_minus

func4(di_plus, di_minus, period) =>
    di_plus_prev = di_plus[1]
    di_minus_prev = di_minus[1]
    sum_di = di_plus_prev + di_minus_prev
    dx = sum_di != 0 ? 100 * math.abs(di_plus_prev - di_minus_prev) / sum_di : 0
    adx = ta.ema(dx, period)
    adx

func5() =>
    val1 = request.security(syminfo.tickerid, "240", high, barmerge.gaps_off, barmerge.lookahead_on)
    val2 = request.security(syminfo.tickerid, "240", low, barmerge.gaps_off, barmerge.lookahead_on)
    val3 = request.security(syminfo.tickerid, "240", close, barmerge.gaps_off, barmerge.lookahead_on)
    di_plus = func2(val1, val2, lenA)
    di_minus = func3(val1, val2, lenA)
    adx_val = func4(di_plus, di_minus, lenA)
    adx_prev = adx_val
    adx_prev2 = adx_val[1]
    ema_val = request.security(syminfo.tickerid, "240", ta.ema(close, lenB)[1], barmerge.gaps_off, barmerge.lookahead_on)
    close_val = request.security(syminfo.tickerid, "240", close[1], barmerge.gaps_off, barmerge.lookahead_on)
    moment = 0
    if adx_prev > threshA and adx_prev > adx_prev2
        moment := 0
    else if adx_prev > threshA and adx_prev < adx_prev2
        moment := 0
    else if adx_prev < threshA and adx_prev > adx_prev2
        moment := 0
    else
        moment := 4
    moment

// Calculation for H4
h4Val = func5()

// Fix H4 value at the start of a new 4-hour bar
var float fixedVal = na
changedVal = ta.change(time("240"))
if changedVal != 0
    fixedVal := request.security(syminfo.tickerid, "240", h4Val, barmerge.gaps_off, barmerge.lookahead_on)

// Select value for display
displayVal = timeframe.period == "15" ? fixedVal : h4Val


// Get technical indicators from H4 (fix values at the start of a new 4-hour bar)
var float fixed_rsi_42 = na
var float prev_fixed_rsi_42 = na
rsi_42 = request.security(syminfo.tickerid, "240", ta.rsi(close, 42), barmerge.gaps_off, barmerge.lookahead_on)
if changedVal != 0
    prev_fixed_rsi_42 := fixed_rsi_42
    fixed_rsi_42 := rsi_42


// 15-minute indicators (M15)
rsi_16 = ta.rsi(close, lenC)
rsi_16_prev = rsi_16[1]

long_cond = (fixedVal == 4) and rsi_16 < fixed_rsi_42 and rsi_16_prev > prev_fixed_rsi_42
// TP and SL
atr_val = ta.atr(96)  // Local ATR on M15
tp_dist = 5 * atr_val
sl_dist = 5 * atr_val

// Variables to track entry bar and TP/SL levels
var int entry_idx = na
var float entry_pr = na
var float tp_pr = na
var float sl_pr = na

// Entry on current candle
if long_cond and strategy.position_size == 0  // Check that no position exists
    strategy.entry("Long", strategy.long)
    entry_idx := bar_index
    entry_pr := close
    tp_pr := entry_pr + tp_dist
    sl_pr := entry_pr - sl_dist

// Update TP and SL for open position
if strategy.position_size > 0
    tp_pr := entry_pr + tp_dist  // Floating TP
    sl_pr := entry_pr - sl_dist  // Floating SL
    strategy.exit("TP/SL", "Long", limit=tp_pr, stop=sl_pr, comment="Floating TP/SL")

// Close position by time (if max_hold_bars passed)
if strategy.position_size > 0 and bar_index - entry_idx >= maxBars
    strategy.close("Long", comment="Time Exit")
    entry_idx := na
    entry_pr := na
    tp_pr := na
    sl_pr := na

Overview

Entries trigger when 15-minute RSI drops relative to four-hour RSI under specific directional strength conditions. Positional exit rules incorporate an ATR-based calculation and a configurable maximum bar holding duration. Drawn directly on the main price chart, the script relies on security requests with lookahead enabled, causing backtest lookahead bias.

Details

Published by

Tim1l on GitHub

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend, Oscillators & momentum, Volatility & bands

Markets

Crypto

Timeframes

M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • Reads higher-timeframe data with lookahead on, so the history can show signals that were not there in real time

Settings

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

SettingTypeDefaultVariable
Length Aint14lenA
Length Bint14lenB
Threshold Afloat30.0threshA
Length Cint16lenC
Threshold Bfloat30.0threshB
Max Bars to Hold (1-8 hours)int32maxBars
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

106 stars

This trading strategy combines three Exponential Moving Averages (8, 14, and 50 periods by default) with Stochastic RSI and Average True Range (ATR). It enters a long position when EMA 1 is above EMA 2, EMA 2 is above EMA 3, and the Stochastic RSI %K line cros

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 3 x EMA + Stochastic RSI + ATR

Momentum Rider DAILY FINAL

by greenpeas007
58 stars

This strategy combines exponential moving averages (EMA 9, 21, 50), RSI, MACD, and Supertrend indicators to enter long positions. It triggers trades using six dynamic setups based on momentum breakouts, EMA crossovers, and pullbacks. Position sizing is compute

Strategy v6 TrendOscillators & momentum Repaints
Updated 4 months ago · Custom licence GitHub repository of Momentum Rider DAILY FINAL
57 stars

This strategy identifies trend reversals through market structure changes (CHoCH) filtered by MACD momentum. It initiates long positions on bullish structure breaks when MACD is positive and above its signal line, and short positions on bearish breaks when MAC

Strategy v6 SMC / ICT / price actionOscillators & momentum Repaints
Updated 1 year ago · Apache-2.0 GitHub repository of Market Structure MACD 4H + Trailing

CombinedBinHAndClucV6

by viktors1982
36 stars

CombinedBinHAndClucV6 is a trading strategy based on the popular BinHV45 and ClucMay72018 algorithms. It calculates dual Bollinger Bands, Exponential Moving Averages, Relative Strength Index, Average True Range, and volume metrics across primary and one-hour h

Strategy v4 Volatility & bandsOscillators & momentum Repaints
Updated 2 years ago · None stated GitHub repository of CombinedBinHAndClucV6
27 stars

LuxAlgo Hybrid Gold Scalper is a price-chart strategy aimed at gold. It stacks a fast, slow, trend and 200-period bias EMA to mark trend direction, then filters strength with RSI, ADX and an intraday VWAP. Swing highs and lows are marked from pivot length. Set

Strategy v5 TrendOscillators & momentum
Updated 5 months ago · None stated GitHub repository of LuxAlgo Hybrid Gold Scalper

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