PROTECTED SOURCE SCRIPT
Updated Opening Range NY - OR, SL, TP editable

Opening Range NY – Configurable OR (5/15/30), %SL, 1:2/1:3 RR, Daily Loss Limit, Fixed USD Position Size
Description (for your script):
This strategy implements a simple, configurable Opening Range breakout system around the New York cash open. It is designed for backtesting intraday breakout logic with strict risk control and a fixed position size in USD.
Core idea
Define an Opening Range (OR) starting at a specific time (e.g. 15:30 Europe/Madrid = NY cash open).
Wait until the OR has fully formed (5 / 15 / 30 minutes).
Trade only breakouts that close beyond the OR high/low, with a configurable % stop loss and fixed R:R take profit.
Stop trading for the day after a maximum number of consecutive losing trades.
How it works
Opening Range definition
Inputs:
Session start time (hour & minute) – default 15:30 (NY cash open in Madrid time).
OR duration in minutes – choose between 5, 15, or 30 minutes.
The script tracks the highest high and lowest low between:
OR Start = selected hour:minute
OR End = OR Start + OR duration
After the OR window ends (time >= OR End), the OR is considered “defined” and trading can start.
Entry logic (after the OR only)
Long breakout:
Condition:
OR is defined (we are after OR end),
No open position,
Daily loss limit not reached,
Current bar close > OR High.
When this is true on a bar, the strategy issues a long entry (assumes fill on the next bar open in backtest).
Short breakout:
Condition:
OR is defined,
No open position,
Daily loss limit not reached,
Current bar close < OR Low.
When this is true, the strategy issues a short entry.
Important:
The strategy never opens positions before the OR has fully completed.
Example: OR = 30 minutes starting at 15:30 → OR is 15:30–16:00.
First possible entries start from 16:00 onward.
Stop Loss and Take Profit
Input: Stop Loss (% of entry price) – fully editable, e.g. 0.5%, 1%, etc.
Input: Take Profit (R:R) – choose between 1:2 or 1:3.
For longs:
Entry ≈ close of breakout bar (simulated fill on next bar open).
SL = entryPrice * (1 – SL%)
TP = entryPrice * (1 + SL% × RR)
For shorts:
SL = entryPrice * (1 + SL%)
TP = entryPrice * (1 – SL% × RR)
Position sizing – fixed USD notional
Input: Tamaño de posición total (USD)/Position size total (USD) – e.g. 10,000.
The script computes:
qty = positionSizeUsd / close
So every trade controls approximately positionSizeUsd worth of the underlying, regardless of price.
Recommended: in the strategy properties, set Initial capital equal to your positionSizeUsd if you want the performance percentages in the Strategy Tester to align with “% of position”.
Daily risk control – max consecutive losses per day
Input: Máx. pérdidas consecutivas por día – default 2.
The script tracks:
Number of closed trades for the day.
If a trade closes with loss, it increments a counter.
If a trade closes with profit or break-even, it resets the counter to 0.
When the counter reaches the configured max (e.g. 2 consecutive losses):
No new trades are allowed for the rest of that day.
Optional daily session close (hard flat at a given time)
Input: boolean Forzar cierre a hora fija / “Force close at fixed time”.
Inputs: Hora cierre and Minuto cierre – e.g. 21:00.
For each day:
At the first bar that crosses the configured close time:
The strategy calls strategy.close_all(), closing any open position.
After that time, no new trades are opened on that day.
This is useful if you want all positions flat before a specific local time.
Backtest period options
Input: Periodo de backtest –
All
Last 12 months
Last 6 months
Last 3 months
Last 1 month
The script computes a startTime based on the choice and only trades bars with time >= startTime.
How to use
Apply the strategy on the instrument and timeframe you want to test (e.g. M5/M15 on indices, metals, oil, etc.).
Set:
The OR start (NY open for your chart’s timezone).
OR duration (5 / 15 / 30 min).
SL% (e.g. 0.5% or 1%).
RR (1:2 or 1:3).
Position size in USD (total notional per trade).
Optional: session close time (e.g. 21:00).
Backtest period (e.g. Last 12 months).
Then compare:
Different OR lengths (5 vs 15 vs 30 minutes).
Different SL% and RR combinations.
Results across assets (indices, metals, energies, etc.).
Notes / assumptions
Entries are evaluated on candle close: breakout requires the bar’s close to be outside the OR.
Backtests in Pine assume fills at the next bar’s open for market orders.
No retest/pullback logic is included in this version — it’s a straightforward breakout of the opening range.
All risk percentages in the Strategy Tester are best interpreted if Initial capital equals your configured position size in USD.
Description (for your script):
This strategy implements a simple, configurable Opening Range breakout system around the New York cash open. It is designed for backtesting intraday breakout logic with strict risk control and a fixed position size in USD.
Core idea
Define an Opening Range (OR) starting at a specific time (e.g. 15:30 Europe/Madrid = NY cash open).
Wait until the OR has fully formed (5 / 15 / 30 minutes).
Trade only breakouts that close beyond the OR high/low, with a configurable % stop loss and fixed R:R take profit.
Stop trading for the day after a maximum number of consecutive losing trades.
How it works
Opening Range definition
Inputs:
Session start time (hour & minute) – default 15:30 (NY cash open in Madrid time).
OR duration in minutes – choose between 5, 15, or 30 minutes.
The script tracks the highest high and lowest low between:
OR Start = selected hour:minute
OR End = OR Start + OR duration
After the OR window ends (time >= OR End), the OR is considered “defined” and trading can start.
Entry logic (after the OR only)
Long breakout:
Condition:
OR is defined (we are after OR end),
No open position,
Daily loss limit not reached,
Current bar close > OR High.
When this is true on a bar, the strategy issues a long entry (assumes fill on the next bar open in backtest).
Short breakout:
Condition:
OR is defined,
No open position,
Daily loss limit not reached,
Current bar close < OR Low.
When this is true, the strategy issues a short entry.
Important:
The strategy never opens positions before the OR has fully completed.
Example: OR = 30 minutes starting at 15:30 → OR is 15:30–16:00.
First possible entries start from 16:00 onward.
Stop Loss and Take Profit
Input: Stop Loss (% of entry price) – fully editable, e.g. 0.5%, 1%, etc.
Input: Take Profit (R:R) – choose between 1:2 or 1:3.
For longs:
Entry ≈ close of breakout bar (simulated fill on next bar open).
SL = entryPrice * (1 – SL%)
TP = entryPrice * (1 + SL% × RR)
For shorts:
SL = entryPrice * (1 + SL%)
TP = entryPrice * (1 – SL% × RR)
Position sizing – fixed USD notional
Input: Tamaño de posición total (USD)/Position size total (USD) – e.g. 10,000.
The script computes:
qty = positionSizeUsd / close
So every trade controls approximately positionSizeUsd worth of the underlying, regardless of price.
Recommended: in the strategy properties, set Initial capital equal to your positionSizeUsd if you want the performance percentages in the Strategy Tester to align with “% of position”.
Daily risk control – max consecutive losses per day
Input: Máx. pérdidas consecutivas por día – default 2.
The script tracks:
Number of closed trades for the day.
If a trade closes with loss, it increments a counter.
If a trade closes with profit or break-even, it resets the counter to 0.
When the counter reaches the configured max (e.g. 2 consecutive losses):
No new trades are allowed for the rest of that day.
Optional daily session close (hard flat at a given time)
Input: boolean Forzar cierre a hora fija / “Force close at fixed time”.
Inputs: Hora cierre and Minuto cierre – e.g. 21:00.
For each day:
At the first bar that crosses the configured close time:
The strategy calls strategy.close_all(), closing any open position.
After that time, no new trades are opened on that day.
This is useful if you want all positions flat before a specific local time.
Backtest period options
Input: Periodo de backtest –
All
Last 12 months
Last 6 months
Last 3 months
Last 1 month
The script computes a startTime based on the choice and only trades bars with time >= startTime.
How to use
Apply the strategy on the instrument and timeframe you want to test (e.g. M5/M15 on indices, metals, oil, etc.).
Set:
The OR start (NY open for your chart’s timezone).
OR duration (5 / 15 / 30 min).
SL% (e.g. 0.5% or 1%).
RR (1:2 or 1:3).
Position size in USD (total notional per trade).
Optional: session close time (e.g. 21:00).
Backtest period (e.g. Last 12 months).
Then compare:
Different OR lengths (5 vs 15 vs 30 minutes).
Different SL% and RR combinations.
Results across assets (indices, metals, energies, etc.).
Notes / assumptions
Entries are evaluated on candle close: breakout requires the bar’s close to be outside the OR.
Backtests in Pine assume fills at the next bar’s open for market orders.
No retest/pullback logic is included in this version — it’s a straightforward breakout of the opening range.
All risk percentages in the Strategy Tester are best interpreted if Initial capital equals your configured position size in USD.
Release Notes
Latest UpdatesThis version adds two important improvements:
Time window for new entries (max entry time)
ATR-based trailing stop (on top of the base %SL and fixed R:R TP)
Both are designed to make the strategy more realistic for intraday trading and prop-firm style risk rules.
1. Maximum time for opening new trades
What it does
You can now define a latest time of day after which the strategy will not open any new positions, even if valid breakouts occur.
Important:
The strategy still waits for the Opening Range (OR) to complete first.
New trades are only allowed between:
OR end time → max entry time
And, if enabled, before the session close time.
Inputs
Limitar hora máxima de NUEVAS entradas / Limit max time for NEW entries (bool)
Hora máxima de nuevas entradas / Max entry hour
Minuto máximo de nuevas entradas / Max entry minute
Logic
If usarHoraMaxEntrada is ON, then:
canOpenByTime is true only while time < entryMaxTime.
Combined with the old rules:
canTrade = orDefined AND inDateRange AND allowNewTrades AND canOpenByTime (AND before session close if enabled).
Practical effect
You can avoid the strategy opening fresh trades late in the session (e.g. when you are away from the screen or close to market close).
Example:
OR 30m from 15:30–16:00.
Max entry time: 20:00.
→ The strategy can open new trades only from 16:00 to 20:00 (and only if daily loss limit not reached and, optionally, before the forced close time).
2. ATR-based trailing stop (in addition to %SL and fixed R:R TP)
What it does
Previously, exits were based on:
A fixed % Stop Loss (e.g. 0.5%, 1%)
A fixed Take Profit at 1:2 or 1:3 R:R
(And a basic trailing tied to the initial % risk.)
Now, the trailing stop is based on ATR, which adapts to daily volatility:
The base % SL is still used as a minimum (to respect prop firm DD rules).
The ATR trailing can move the stop closer to price as the trade goes in your favor, but never further away than the initial % SL.
Inputs
Usar Trailing Stop basado en ATR / Use ATR-based Trailing Stop (bool)
ATR length (trailing) – e.g. 14
ATR multiplicador trailing – e.g. 2.0 (2 × ATR)
How the ATR trailing works (LONG trades)
Base SL from % of entry price:
baseLongSL = entryPrice * (1 – slPct)
(this is your minimum stop distance, tied to the max daily/DD rules).
Fixed TP at R:R:
longTP = entryPrice * (1 + slPct × tpRR)
ATR trailing:
atrValue = ta.atr(atrLength)
offset = atrValue * atrMult
Track the highest high since entry:
highestSinceEntry
Trailing candidate:
trailCandidate = highestSinceEntry – offset
Final SL for the current bar:
longSL = max(baseLongSL, trailCandidate)
This means:
As price makes new highs, highestSinceEntry increases.
The trailing stop follows at offset (e.g. 2×ATR) below that highest high.
But it will never go below the original baseLongSL (it only tightens, never loosens).
For SHORT trades
Mirror logic:
baseShortSL = entryPrice * (1 + slPct)
shortTP = entryPrice * (1 – slPct × tpRR)
Track lowestSinceEntry
trailCandidate = lowestSinceEntry + offset
shortSL = min(baseShortSL, trailCandidate)
So the stop trails above the price by offset (ATR × multiplier), but never above the original base stop.
Practical interpretation (intraday)
You still define your risk per trade via %SL (e.g. 0.5% or 1%), aligned with prop-firm daily drawdown rules.
The ATR trailing:
Adjusts the trailing distance dynamically to volatility:
High volatility → wider trailing stop.
Low volatility → tighter trailing stop.
Lets winners run more naturally while locking in profits as price moves in your favor.
Exits happen at whichever comes first:
Price hits the TP (1:2 or 1:3 R:R), OR
Price hits the ATR-based trailing stop, OR
(If enabled) the strategy closes the trade at the configured session close time.
These two updates make the system:
More controllable in terms of when trades can open during the session.
More realistic and adaptive in terms of how trades are managed once open, via an ATR-based trailing stop layered on top of a strict %SL and fixed R:R target.
Protected script
This script is published as closed-source. However, you can use it freely and without any limitations – learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Protected script
This script is published as closed-source. However, you can use it freely and without any limitations – learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.