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:
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.
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