OPEN-SOURCE SCRIPT

Long-Only Opening Range Breakout (ORB) with Pivot Points

Intraday Trading Strategy: Long-Only Opening Range Breakout (ORB) with Pivot Points

Background:

Opening Range Breakout (ORB) is a popular long-only trading strategy that capitalizes on the early morning volatility in financial markets. It's based on the idea that the initial price movements during the first few minutes or hours of the trading day can set the tone for the rest of the session. The strategy involves identifying a price range within which the asset trades during the opening period and then taking long positions when the price breaks out to the upside of this range.

Pivot Points are a widely used technical indicator in trading. They represent potential support and resistance levels based on the previous day's price action. Pivot points are calculated using the previous day's high, low, and close prices and can help traders identify key price levels for making trading decisions.

How to Use the Script:

Initialization: This script is written in Pine Script, a domain-specific language for trading strategies on the TradingView platform. To use this script, you need to have access to TradingView.

Apply the Script: You can do this by adding it to your favorites, then selecting the script in the indicators list under favorites or by searching for it by name under community scripts.

Customize Settings: The script allows you to customize various settings through the TradingView interface. These settings include:
  • Opening Session: You can set the time frame for the opening session.
  • Max Trades per Day: Specify the maximum number of long trades allowed per trading day.
  • Initial Stop Loss Type: Choose between using a percentage-based stop loss or the previous candles low for stop loss calculations.
  • Stop Loss Percentage: If you select the percentage-based stop loss, specify the percentage of the entry price for the stop loss.
  • Backtesting Start and End Time: Set the time frame for backtesting the strategy.


Strategy Signals:

  • The script will display pivot points in blue (R1, R2, R3, R4, R5) and half-pivot points in gray (R0.5, R1.5, R2.5, R3.5, R4.5) on your chart.
  • The green line represents the opening range.
  • The script generates long (buy) signals based on specific conditions:
  • ---The open price is below the opening range high (h).
  • ---The current high price is above the opening range high.
  • ---Pivot point R1 is above the opening range high.
  • ---It's a long-only strategy designed to capture upside breakouts.
  • ---It also respects the maximum number of long trades per day.
  • The script manages long positions, calculates stop losses, and adjusts long positions according to the defined rules.


Trailing Stop Mechanism

The script incorporates a dynamic trailing stop mechanism designed to protect and maximize profits for long positions. Here's how it works:

1. Initialization:
  • The script allows you to choose between two types of initial stop loss:
  • ---Percentage-based: This option sets the initial stop loss as a percentage of the entry price.
  • ---Previous day's low: This option sets the initial stop loss at the previous day's low.


2. Setting the Initial Stop Loss (`sl_long0`):
  • The initial stop loss (`sl_long0`) is calculated based on the chosen method:
  • ---If "Percentage" is selected, it calculates the stop loss as a percentage of the entry price.
  • ---If "Previous Low" is selected, it sets the stop loss at the previous day's low.


3. Dynamic Trailing Stop (`trail_long`):
  • The script then monitors price movements and uses a dynamic trailing stop mechanism (`trail_long`) to adjust the stop loss level for long positions.
  • If the current high price rises above certain pivot point levels, the trailing stop is adjusted upwards to lock in profits.
  • The trailing stop levels are calculated based on pivot points (`r1`, `r2`, `r3`, etc.) and half-pivot points (`r0.5`, `r1.5`, `r2.5`, etc.).
  • The script checks if the high price surpasses these levels and, if so, updates the trailing stop accordingly.
  • This dynamic trailing stop allows traders to secure profits while giving the position room to potentially capture additional gains.


4. Final Stop Loss (`sl_long`):
  • The script calculates the final stop loss level (`sl_long`) based on the following logic:
  • ---If no position is open (`pos == 0`), the stop loss is set to zero, indicating there is no active stop loss.
  • ---If a position is open (`pos == 1`), the script calculates the maximum of the initial stop loss (`sl_long0`) and the dynamic trailing stop (`trail_long`).
  • ---This ensures that the stop loss is always set to the more conservative of the two values to protect profits.


5. Plotting the Stop Loss:
  • The script plots the stop loss level on the chart using the `plot` function.
  • It will only display the stop loss level if there is an open position (`pos == 1`) and it's not a new trading day (`not newday`).
  • The stop loss level is shown in red on the chart.


By combining an initial stop loss with a dynamic trailing stop based on pivot points and half-pivot points, the script aims to provide a comprehensive risk management mechanism for long positions. This allows traders to lock in profits as the price moves in their favor while maintaining a safeguard against adverse price movements.

End of Day (EOD) Exit:

The script includes an "End of Day" (EOD) exit mechanism to automatically close any open positions at the end of the trading day. This feature is designed to manage and control positions when the trading day comes to a close. Here's how it works:

1. Initialization:
  • At the beginning of each trading day, the script identifies a new trading day using the `is_newbar('D')` condition.
  • When a new trading day begins, the `newday` variable becomes `true`, indicating the start of a new trading session.


2. Plotting the "End of Day" Signal:
  • The script includes a plot on the chart to visually represent the "End of Day" signal. This is done using the `plot` function.
  • The plot is labeled "DayEnd" and is displayed as a comment on the chart. It signifies the EOD point.


3. EOD Exit Condition:
  • When the script detects that a new trading day has started (`newday == true`), it triggers the EOD exit condition.
  • At this point, the script proceeds to close all open positions that may have been active during the trading day.


4. Closing Open Positions:
  • The `strategy.close_all` function is used to close all open positions when the EOD exit condition is met.
  • This function ensures that any remaining long positions are exited, regardless of their current profit or loss.
  • The function also includes an `alert_message`, which can be customized to send an alert or notification when positions are closed at EOD.


Purpose of EOD Exit

The "End of Day" exit mechanism serves several essential purposes in the trading strategy:

  • Risk Management: It helps manage risk by ensuring that positions are not left open overnight when markets can experience increased volatility.
  • Capital Preservation: Closing positions at EOD can help preserve trading capital by avoiding potential adverse overnight price movements.
  • Rule-Based Exit: The EOD exit is rule-based and automatic, ensuring that it is consistently applied without emotions or manual intervention.
  • Scalability: It allows the strategy to be applied to various markets and timeframes where EOD exits may be appropriate.


By incorporating an EOD exit mechanism, the script provides a comprehensive approach to managing positions, taking profits, and minimizing risk as each trading day concludes. This can be especially important in volatile markets like cryptocurrencies, where overnight price swings can be significant.

Backtesting: The script includes a backtesting feature that allows you to test the strategy's performance over historical data. Set the start and end times for backtesting to see how the long-only strategy would have performed in the past.

Trade Execution: If you choose to use this script for live trading, make sure you understand the risks involved. It's essential to set up proper risk management, including position sizing and stop loss orders.

Monitoring: Monitor the long-only strategy's performance over time and be prepared to make adjustments as market conditions change.

Disclaimer: Trading carries a risk of capital loss. This script is provided for educational purposes and as a starting point for your own long-only strategy development. Always do your own research and consider seeking advice from a qualified financial professional before making trading decisions.
longsetupopeningrangebreakoutPivot points and levelspivotstrategystoplosstrailingstop

Script de código aberto

No verdadeiro espírito do TradingView, o autor desse script o publicou como código aberto, para que os traders possam compreendê-lo e analisá-lo. Parabéns ao autor! Você pode usá-lo gratuitamente, mas a reutilização desse código em publicações é regida pelas Regras da Casa. Você pode favoritá-lo para usá-lo em um gráfico.

Quer usar esse script no gráfico?

Aviso legal