Detecting and Visualizing Trend Changes with Fractals in ProBuilder

30 Mar 2024
0 comment
0 attachment

This ProBuilder script is designed to detect and visualize changes in market trends using fractals. It identifies higher highs and lower lows, marking the beginning of new trends with arrows and segments on a trading chart.

defparam calculateonlastbars=10000
cp = 3
once lastpoint = 0

if high[cp] >= highest[2*cp+1](high) then
    PEAK = 1
else
    PEAK = 0
endif

if low[cp] <= lowest[2*cp+1](low) then
    TROUGH = -1
else
    TROUGH = 0
endif

if PEAK = 1 then
    TOPy = high[cp]
    TOPx = barindex[cp]
endif

if TROUGH = -1 then
    BOTy = low[cp]
    BOTx = barindex[cp]
endif

if PEAK>0 and (lastpoint=-1 or lastpoint=0) then
    DRAWSEGMENT(lastX,lastY,TOPx,TOPy)
    COLOURED(200,0,0,255)
    DRAWTEXT("■",TOPx,TOPy,Dialog,Bold,20)
    coloured(200,0,0,255)
    lastpoint = 1
    lastX = TOPx
    lastY = TOPy
endif

if TROUGH<0 and (lastpoint=1 or lastpoint=0) then
    DRAWSEGMENT(lastX,lastY,BOTx,BOTy)
    COLOURED(0,200,0,255)
    DRAWTEXT("■",BOTx,BOTy,Dialog,Bold,20)
    coloured(0,200,0,255)
    lastpoint = -1
    lastX = BOTx
    lastY = BOTy
endif

//TREND ATTEMPT
atr=AverageTrueRange[14](close)
if TOPy > TOPy[1] and topy<>lasttop then
    drawarrowup(barindex,low-atr/2)
    coloured(0,200,0)
    lasttop=topy
    trendup = 1
else
    trendup = 0
endif

RETURN TOPy as "TOPy", BOTY as "BOTy", trendup as "trendup"

This script performs several key operations to detect and visualize trend changes:

  • Fractal Detection: It checks for fractal peaks and troughs based on the highest and lowest values within a specified window. A peak is identified if the current high is the highest in the window, and similarly, a trough is identified if the current low is the lowest.
  • Visualization: When a new peak or trough is identified, the script draws a segment from the last point to the current point and places a colored square at the current point. This helps in visualizing the trend changes.
  • Trend Change Detection: The script also checks if the current peak is higher than the previous peak. If true, it draws an upward arrow indicating a potential uptrend start.
  • Output Variables: It returns the values of the latest peak and trough, and a flag indicating if an uptrend has been detected.

This approach helps traders visually identify significant trend changes, potentially aiding in decision-making processes.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/use-lower-higher-low-high-as-trend-signal/#post-148483

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.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
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) [...]
Logo Logo
Loading...