Detecting Trend Reversals with Dual Thresholds in ProBuilder

05 Jun 2021
0 comment
0 attachment

This ProBuilder script is designed to identify potential trend reversals by analyzing patterns where a bullish bar closes above two preceding bearish bars and vice versa. The code uses loops, conditional statements, and logical operators to determine the trading signals.

DEFPARAM CalculateOnLastBars = 1000
Bullish = close > open
Bearish = close < open

//
// DOWN trend detection
//
Count1 = 0
i = 0
WHILE Count1 < 2 AND i < BarIndex
    i = i + 1
    IF Count1 = 0 THEN
        IF Bullish[i] THEN
            Count1 = 1
            MyLO1 = low[i]
        ENDIF
    ELSE
        IF Bullish[i] THEN
            Count2 = 2
            MyLO2 = low[i]
            d1 = MyLO1 > MyLO2
            MyLO2 = min(MyLO1,MyLO2)
            Break
        ENDIF
    ENDIF
WEND
d2 = Bearish
d3 = close < MyLO2
Dcond = d1 AND d2 AND d3

//
// UP trend detection
//
Count2 = 0
j = 0
WHILE Count2 < 2 AND j < BarIndex
    j = j + 1
    IF Count2 = 0 THEN
        IF Bearish[j] THEN
            Count2 = 1
            MyHI1 = high[j]
        ENDIF
    ELSE
        IF Bearish[j] THEN
            Count2 = 2
            MyHI2 = high[j]
            u1 = MyHI1 < MyHI2
            MyHI2 = max(MyHI1,MyHI2)
            Break
        ENDIF
    ENDIF
WEND
u2 = Bullish
u3 = close > MyHI2
Ucond = u1 AND u2 AND u3

// Cond = 0
IF Ucond THEN
    Cond = 1
ELSIF Dcond THEN
    Cond = -1
ENDIF

RETURN Cond AS "Signal"

The code snippet above is structured into two main parts: detecting a downward trend reversal and an upward trend reversal. Here’s a breakdown of how each part works:

  • Initialization: Sets the number of bars to calculate and defines bullish and bearish conditions based on the closing and opening prices.
  • Downward Trend Detection: Uses a WHILE loop to find two consecutive bullish bars where the second bar’s low is lower than the first. This suggests a potential bearish reversal if followed by a bearish bar closing below the lowest of these two bullish bars.
  • Upward Trend Detection: Similarly, another WHILE loop searches for two consecutive bearish bars where the second bar’s high is higher than the first, indicating a potential bullish reversal if followed by a bullish bar closing above the highest of these bearish bars.
  • Signal Output: The script outputs a signal (1 for bullish reversal, -1 for bearish reversal, and 0 otherwise) based on the conditions met in the loops.

This approach helps in identifying key reversal points, which are crucial for making informed trading decisions in various market conditions.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/bullish-bar-closing-above-2-bearish-bar-and-vice-versa/#post-139440

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
ROBERTO New
This author is like an anonymous function, present but not directly identifiable. More details on this code architect as soon as they exit 'incognito' mode.
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
33
indicator
132
strategy
171

Recent Snippets

How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...