Identifying and Tracking the Last Three Higher Highs and Lower Lows in a Price Series

27 Jun 2021
0 comment
0 attachment

This ProBuilder code snippet is designed to identify and track the last three higher highs (HH) and lower lows (LL) in a price series, which are key concepts in technical analysis for determining trends. The code also includes a basic trend determination logic based on the sequence of these highs and lows.

DEFPARAM DrawOnLastBarOnly = TRUE
//DEFPARAM CalculateOnLastBars = 1000

//ONCE LB = 5 //5 LookBack periods
LB = max(1,min(999,LB))

// ONCE HH1 = 0 //previous Higher High
ONCE HHbar1 = 0
ONCE HH2 = 0 //previous Higher High
ONCE HHbar2 = 0
ONCE HH3 = 0 //current Higher High
ONCE HHbar3 = 0

// ONCE LL1 = 0 //previous Lower Low
ONCE LLbar1 = 0
ONCE LL2 = 0 //previous Lower Low
ONCE LLbar2 = 0
ONCE LL3 = 0 //current Lower Low
ONCE LLbar3 = 0

// ONCE MyTrend = 0
Bullish = close > open
Bearish = close < open

// 
IF summation[2](high = highest[LB](high)) AND Bullish[1] AND Bearish THEN
    HH1 = HH2
    HHbar1 = HHbar2
    HH2 = HH3
    HHbar2 = HHbar3
    HH3 = max(high,high[1])
    HHbar3 = BarIndex
ELSIF summation[2](low = lowest[LB](low)) AND Bearish[1] AND Bullish THEN
    LL1 = LL2
    LLbar1 = LLbar2
    LL2 = LL3
    LLbar2 = LLbar3
    LL3 = min(low,low[1])
    LLbar3 = BarIndex
ENDIF

//
MyTrend = 0
IF ((HH1 < HH2) AND (HH2 < HH3)) AND ((LL1 < LL2) AND (LL2 < LL3)) THEN
    MyTrend = 1
ELSIF ((HH1 < HH2) AND (HH2 < HH3)) AND ((LL1 > LL2) OR (LL2 > LL3) OR (LL1 > LL3)) THEN
    MyTrend = 0
ELSIF ((LL1 > LL2) AND (LL2 > LL3)) AND ((HH1 > HH2) AND (HH2 > HH3)) THEN
    MyTrend = -1
ELSIF ((LL1 > LL2) AND (LL2 > LL3)) AND ((HH1 < HH2) OR (HH2 < HH3) OR (HH1 < HH3)) THEN
    MyTrend = 0
ENDIF

//
DrawText("HH1",HHbar1,HH1 * 1.001)
DrawText("HH2",HHbar2,HH2 * 1.001)
DrawText("HH3",HHbar3,HH3 * 1.001)

//
DrawText("LL1",LLbar1,LL1 * 0.999)
DrawText("LL2",LLbar2,LL2 * 0.999)
DrawText("LL3",LLbar3,LL3 * 0.999)

//
RETURN MyTrend AS "Trend",0 AS "0"

Explanation of the Code:

  • The code starts by setting parameters to only draw on the last bar and defines a lookback period (LB) which is used to determine the highest and lowest points within that period.
  • Variables for tracking the last three higher highs (HH) and lower lows (LL) along with their corresponding bar indices are initialized.
  • The code checks for new HH and LL based on the conditions that there must be a reversal pattern indicated by a bullish candle followed by a bearish candle for HH, and vice versa for LL.
  • When a new HH or LL is identified, the values are updated, and the oldest HH or LL is dropped from the tracking.
  • The trend determination logic checks the sequence of HH and LL to decide if the trend is upward, downward, or if there is no clear trend.
  • Finally, the code uses DrawText functions to visually display the HH and LL values on the chart for the last three occurrences.

This snippet is useful for traders or analysts who want to automate the tracking of key trend reversal points within a given timeframe.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/way-to-tell-if-market-is-bull-or-bear/#post-172002

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.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
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...