FINterpreter
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: January 12th, 2022
Contact: TradingView Profile

Identifying Higher Lows/Lower Highs

Hey Pine Scripters,
I'm really excited with this journey so far!
I appreciate your consideration in reading this.

What I'm Trying To Accomplish:
I have a partially back-tested strategy (250+ trades the old fashioned way lol) that I eventually want to code for ease of testing and use, but for now I'm just wondering how I would go about identifying/targeting certain conditions in price and on this indicator!
*Full code for indicator below

*Pic of valid setups for clarity on what I'm describing:

Image

My Entry Conditions

1. Only LONG entries above 200EMA. Only SHORTS below 200EMA. (No help needed with this)

*For simplicity's sake, let's define the last two conditions in context of a LONG.

2. My Long entry is defined as a Green Triangle of a 'Momentum Wave" (faster blue/pink oscillator - called "WTO ONE/wt_1" in the script) that is SMALLER than the previous, larger "Momentum Wave". That is to say the oscillator did not go as low into negative integers as it did the time before - AND the oscillator has to cross the "0 line" to the opposite side between the two waves occurring. A Smaller Wave with a Buy Triangle after having NOT crossed the 0 line between it's accompanied Larger Wave does not constitute a valid set-up.

*Green Triangles are triggered by the (invisible) "cross" or change of direction in WTO ONE, in case it's helpful.

3. While the Momentum Wave is making that "Higher-Low" (Smaller after Larger), price-action ALSO has to make a higher low - or simply be higher than it was at the time of the Large Wave. If price is lower at the time of the Small-Wave "Buy" triangle than it was at the Large-Wave "Buy" triangle, the setup is invalid.

In summary, "Higher Lows" on both price-action and momentum waves (with retracement across 0 line between momentum waves) with "Buy" on small momentum wave, + above 200EMA = LONG. Inverse for a short.

I need guidance with:
How do you go about identifying price being higher on what could be a varying period? It's clearly not as cut and dry as a look-back period, so I'm somewhat at a loss.
How do you go about identifying the fact that the current oscillator wave is higher at the time of posting a triangle than the last one was, and that it crossed into positive territory at some point since the last one?
My fear is that this is outside of the limitations of Pine Script - that said - I know like 5% of Pine, and certainly less about what's achievable with it. So I humbly ask you internet friends for guidance.

I sincerely thank you!

Code: "WTO ONE" is the oscillator in question. WT2 can be ignored entirely.

Code: Select all

//@version=5
indicator(title="+ WAVE", timeframe="")

src = input(defval=hlc3, title="Source")

//WTO PLOT ONE
channel_len = input(defval=10, title="Channel Length", group="Primary Wavetrend Inputs")
average_len = input(defval=12, title="Average Length", group="Primary Wavetrend Inputs")
wt_1_ma_len = input(defval=3, title="Moving Average Length", group="Primary Wavetrend Inputs")

esa = ta.ema(src, channel_len)
d = ta.ema(math.abs(src - esa), channel_len)
ci = (src - esa) / (0.015 * d)
tci = ta.ema(ci, average_len)
wt_1 = tci
wt_1_ma = ta.sma(wt_1, wt_1_ma_len)

//COLOR INPUTS
wt_1_up = input(color.new(#00e1ff, 0), title="High", inline="wt 1 color", group="Primary Wavetrend Inputs")
wt_1_down = input(color.new(#ff00ff, 0), title="Low", inline="wt 1 color", group="Primary Wavetrend Inputs")
wt_1_color = color.from_gradient(wt_1, -80, 80, wt_1_down, wt_1_up)

plot(wt_1, color=wt_1_color, title="Primary Wavetrend", linewidth=2)



//WTO PLOT TWO
channel_len_2 = input(defval=32, title="Channel Length", group="Secondary Wavetrend Inputs")
average_len_2 = input(defval=50, title="Average Length", group="Secondary Wavetrend Inputs")

esa2 = ta.ema(src, channel_len_2)
d2 = ta.ema(math.abs(src - esa2), channel_len_2)
ci2 = (src - esa2) / (0.015 * d2)
tci2 = ta.ema(ci2, average_len_2)
wt_2 = tci2

//COLOR INPUTS
wt_2_up = input(color.new(#09ff00, 40), title="High", inline="wt 2 color", group="Secondary Wavetrend Inputs")
wt_2_down = input(color.new(#ff1919, 40), title="Low", inline="wt 2 color", group="Secondary Wavetrend Inputs")
wt_2_color = color.from_gradient(wt_2, -60, 60, wt_2_down, wt_2_up)

plot(wt_2, color=wt_2_color, title="Secondary Wavetrend")


//MOVING AVERAGE CROSS SIGNALS ON PRIMARY WAVETREND
plotshape(ta.crossover(wt_1, wt_1_ma), title="Maybe Buy", style=shape.triangleup, color=color.green, size=size.tiny, location=location.bottom)
plotshape(ta.crossunder(wt_1, wt_1_ma), title="Maybe Sell", style=shape.triangledown, color=color.red, size=size.tiny, location=location.top)


////HORIZONTAL LINES & OB/OS REGIONS
wto_2_overbought = hline(80, color=#ff1919, linestyle=hline.style_dotted, title="80")
wto_1_overbought = hline(60, color=#ff1919, linestyle=hline.style_dotted, title="60")
uline40 = hline(25, color=#787b86, linestyle=hline.style_dotted, title="25")
uline20 = hline(15, color=#787b86, linestyle=hline.style_dotted, title="15")
median = hline(0, color=color.fuchsia, linestyle=hline.style_solid, title="Median", linewidth=3)
dline20 = hline(-15, color=#787b86, linestyle=hline.style_dotted, title="-15")
dline40 = hline(-25, color=#787b86, linestyle=hline.style_dotted, title="-25")
wto_1_oversold = hline(-60, color=#56ff38, linestyle=hline.style_dotted, title="-60")
wto_2_oversold = hline(-80, color=#4bff2b, linestyle=hline.style_dotted, title="-80")

fill_col_upper = input(color.new(#ff5252, 85), title="Overbought Fill Color", group="Oscillator Extremes")
fill_col_lower = input(color.new(#47a637, 85), title="Oversold Fill Color", group="Oscillator Extremes")
fill(wto_2_overbought, wto_1_overbought, color=fill_col_upper, title="Overbought Region")
fill(wto_2_oversold, wto_1_oversold, color=fill_col_lower, title="Oversold Region")



G9X
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: February 1st, 2022

Re: Identifying Higher Lows/Lower Highs

Helpful

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Identifying Higher Lows/Lower Highs

Hey,

Well i am sure what you are asking can be achieved. Its just that your point 2 and 3 need more explanation.

Your questions identifying price being higher on what could be a varying period and on tracking it crossing 0 line before next buy also can be achieved using variables and if functions using AND logic.

If you can further explain just the LONG strategy , i would be able to help you out. At present i am getting confused on the provided details.

FINterpreter
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: January 12th, 2022
Contact: TradingView Profile

Re: Identifying Higher Lows/Lower Highs

Hi!
Thank you so much for getting back to me.
I will try my best to frame this in a different way - forgive me for not providing more pictures etc, as I am currently on mobile.

To clarify - when the blue/pink oscillator dips below that pink 0 line, it eventually rounds off and posts a green triangle as it switches direction back upward. Let’s call that oscillation a “wave”. So, my long entry must come from a green triangle that posted when the “wave” was below 0, not above it.

The wave I would enter on must come directly after a “larger” wave to the downside. See in the pic: the wave I mark yellow first (before the one noted as “long”) dips significantly lower than the wave I entered “long” on. This is what I mean by “larger” waves.
So, point 2 means I enter at a green triangle on a wave that is “smaller” than it’s previous wave - as shown in the pic. If the wave I went “long” on dipped lower than it’s previous wave did, the setup would be invalid. Because the oscillator didn’t go as low before crossing up as it did on the wave prior, this is a “smaller” and “valid” entry wave.

Also, you’ll notice the first-marked “large” wave immediately retraces above the 0 line after it’s green triangle, before oscillating back down to form the smaller wave I entered on. If the oscillator did not manage to get above “0” between the large and small wave, this setup would be invalid.

Finally, to point 3 - price simply has to be higher at the time the triangle posts on the small wave than it was when the large wave posted. Again, referring to the picture, price was around $50.6k when the large wave posted a triangle. Price was higher than that - just over $51k - when the small wave posted a triangle, making this setup valid.

If we had a large wave, and then a small wave, but price was lower at the small wave than it was at the large wave, this setup would be invalid. That would be some kind of momentum divergence, which is not what this specific strategy is targeting.

I sincerely hope this provides more clarity - but I admit I’m having a hard time expressing these conditions in a different way while remaining clear.
If you’re still unsure as to what I mean - please, please feel free to pick me apart asking “what, specifically, does THAT mean?!” Haha.
If I can understand what you understand about the strategy so far, I can attempt to fill in the gaps concisely and sufficiently for you to provide me some ideas in coding this.

I truly, truly appreciate you taking the time to help me with this. More than I can express.
Thank you.🍻

FINterpreter
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: January 12th, 2022
Contact: TradingView Profile

Re: Identifying Higher Lows/Lower Highs

I will record and link a video explaining this more clearly if you’d prefer.

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Identifying Higher Lows/Lower Highs

Hey,

Just to make sure we are sailing in the same direction. I am attaching an example chart with numbered points and their understanding of mine. Just let me know if i am wrong anywhere. (I am ignoring the 200 ema point in this scenario)

Image

1. This is the last green in the first big wave that we start checking from. (NOT LONG)

2.
  • This wave comes after crossing the 0 line and is smaller than previous at 1.
  • Its price is higher than price at 1.
  • This is marked as LONG
3.
  • This does not cross 0 line so ignored and NOT LONG
  • Used as marker for next wave like 1.
4.This is LONG like point 2.

5. DO we use this as next marker or do we use number 4 as the marker. I am assuming we use 5.

6. If 5 was the marker. 6 is smaller number 5 but price is lower so NOT LONG (slightly lower)

7. This is LONG as price is above, passes 0 line and also shorter than 6.

8. This is NOT LONG as bigger than 7

FINterpreter
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: January 12th, 2022
Contact: TradingView Profile

Re: Identifying Higher Lows/Lower Highs

For simplicity’s sake:
Large wave = “Anchor Wave”
Small entry wave = “Trigger Wave”

1 - correct - although, I would use “total” or “max” height (“inverted height” in the LONG case) of the anchor wave - not just the height of the last triangle as our height limit for a future potential trigger wave. However, I’m not married to that principle and if it makes more sense in coding to target from the height of last triangle only, I’m ok with that.


2 - correct - perfect trigger wave.

3 - correct!

4 - correct!

5 - your assumption is correct - this overtook 4 in terms of height, making 5 the next potential anchor wave

6 - correct!

7 - correct!

8 - correct!

You’re fully picking up what I’m putting down 👍
Thank you again!

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Identifying Higher Lows/Lower Highs

Great, i will see what i can do on this, the tricky part is the oscillating waves and keeping tabs on them. For point 1 , i will change it to max height of wave itself.

FINterpreter
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: January 12th, 2022
Contact: TradingView Profile

Re: Identifying Higher Lows/Lower Highs

I greatly appreciate anything you’re able to come up with - this is going to do a lot in helping me learn as well.
Can’t wait to hear back,
Thank you!

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Identifying Higher Lows/Lower Highs

Hey,

Have a look at it and let me know if you have any questions on it.

I have coded it to mark with purple pointer LONG on following conditions :
  1. Your crossover of ta.crossover(wt_1, wt_1_ma)
  2. Wt_1 being below 0
  3. Source value must be higher than last wave highest point value
  4. This is shorter than highest point in previous wave
  5. Source value is above 200 ema

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds

//@version=5
indicator(title="+ WAVE v2.0", timeframe="")

src = input(defval=close, title="Source")


// Dip1 = Anchor Wave
// Dip2 = Trigger Wave
var dip1_wt1 = 0.
var dip1_price = 0.
var dip2_wt1 = 0.
var dip2_price = 0.
isLong = false

// Original Code for WT1 and WT2 calculations {
// WTO PLOT ONE 
channel_len = input(defval=10, title="Channel Length", group="Primary Wavetrend Inputs")
average_len = input(defval=12, title="Average Length", group="Primary Wavetrend Inputs")
wt_1_ma_len = input(defval=3, title="Moving Average Length", group="Primary Wavetrend Inputs")

esa = ta.ema(src, channel_len)
d = ta.ema(math.abs(src - esa), channel_len)
ci = (src - esa) / (0.015 * d)
tci = ta.ema(ci, average_len)
wt_1 = tci
wt_1_ma = ta.sma(wt_1, wt_1_ma_len)

//COLOR INPUTS
wt_1_up = input(color.new(#00e1ff, 0), title="High", inline="wt 1 color", group="Primary Wavetrend Inputs")
wt_1_down = input(color.new(#ff00ff, 0), title="Low", inline="wt 1 color", group="Primary Wavetrend Inputs")
wt_1_color = color.from_gradient(wt_1, -80, 80, wt_1_down, wt_1_up)

plot(wt_1, color=wt_1_color, title="Primary Wavetrend", linewidth=2)


//WTO PLOT TWO
channel_len_2 = input(defval=32, title="Channel Length", group="Secondary Wavetrend Inputs")
average_len_2 = input(defval=50, title="Average Length", group="Secondary Wavetrend Inputs")

esa2 = ta.ema(src, channel_len_2)
d2 = ta.ema(math.abs(src - esa2), channel_len_2)
ci2 = (src - esa2) / (0.015 * d2)
tci2 = ta.ema(ci2, average_len_2)
wt_2 = tci2

//COLOR INPUTS
wt_2_up = input(color.new(#09ff00, 40), title="High", inline="wt 2 color", group="Secondary Wavetrend Inputs")
wt_2_down = input(color.new(#ff1919, 40), title="Low", inline="wt 2 color", group="Secondary Wavetrend Inputs")
wt_2_color = color.from_gradient(wt_2, -60, 60, wt_2_down, wt_2_up)

plot(wt_2, color=wt_2_color, title="Secondary Wavetrend")


//MOVING AVERAGE CROSS SIGNALS ON PRIMARY WAVETREND

plotshape(ta.crossover(wt_1, wt_1_ma), title="Maybe Buy", style=shape.triangleup, color=color.green, size=size.tiny, location=location.bottom)
plotshape(ta.crossunder(wt_1, wt_1_ma), title="Maybe Sell", style=shape.triangledown, color=color.red, size=size.tiny, location=location.top)

// ----- }


if (ta.crossover(wt_1, 0.0) and not dip2_wt1 == 0.0)
    dip1_wt1   := dip2_wt1
    dip1_price := dip2_price
    dip2_wt1   := 0.
    dip2_price := 0.
if (wt_1 < 0 and wt_1 < dip2_wt1)
    dip2_wt1   := wt_1
    dip2_price := src
isLong  := (ta.crossover(wt_1, wt_1_ma) and wt_1 < 0 
     and src > dip1_price and wt_1 > dip1_wt1 
     and src > ta.ema(src, 200))
plotshape(isLong, title="LONG", style=shape.labelup, color=color.purple, size=size.normal, location=location.belowbar)


////HORIZONTAL LINES & OB/OS REGIONS
wto_2_overbought = hline(80, color=#ff1919, linestyle=hline.style_dotted, title="80")
wto_1_overbought = hline(60, color=#ff1919, linestyle=hline.style_dotted, title="60")
uline40 = hline(25, color=#787b86, linestyle=hline.style_dotted, title="25")
uline20 = hline(15, color=#787b86, linestyle=hline.style_dotted, title="15")
median = hline(0, color=color.fuchsia, linestyle=hline.style_solid, title="Median", linewidth=3)
dline20 = hline(-15, color=#787b86, linestyle=hline.style_dotted, title="-15")
dline40 = hline(-25, color=#787b86, linestyle=hline.style_dotted, title="-25")
wto_1_oversold = hline(-60, color=#56ff38, linestyle=hline.style_dotted, title="-60")
wto_2_oversold = hline(-80, color=#4bff2b, linestyle=hline.style_dotted, title="-80")

fill_col_upper = input(color.new(#ff5252, 85), title="Overbought Fill Color", group="Oscillator Extremes")
fill_col_lower = input(color.new(#47a637, 85), title="Oversold Fill Color", group="Oscillator Extremes")
fill(wto_2_overbought, wto_1_overbought, color=fill_col_upper, title="Overbought Region")
fill(wto_2_oversold, wto_1_oversold, color=fill_col_lower, title="Oversold Region")


Return to “Pine Script Q&A”