Indicator for Triangles

armybender

Active member
Hi,

I've tried my hand at creating something to do this, but can't seem to get it quite right. I'm wondering if someone can help. The idea is that congestion is building, so I want to know when both sides (buyers and sellers) have entered expecting a breakout.

I'm hoping to create an indicator that will put either a down arrow or up arrow above or below a bar when a triangle (nested set of highs or lows) forms and is triggered by the nested high or low being confirmed on the close of a bar.

I've included a picture below, but here are the rules that need to be coded. I'll use the rules that match the picture, but the opposite direction also needs to be considered (i.e. a triangle that is triggered by a bear bar following a set of nested highs / lows).

  1. There must first be a high - it can be a single bar, and does not need to be considered a swing (i.e. doesn't need to be a specific distance from anything)
  2. There must then be a low - same rules about not needing to be a swing.
  3. Next, a subsequent lower high and higher low must form.
  4. When the higher low is "locked in" by the closing of the bar and the price ticking at least one tick above, there is a triangle formed and it would trigger an alert or draw an arrow underneath the nested low.

Here's a picture of how it would work. Any help is appreciated!!!


AwG380F.jpg
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@armybender
Still have some bugs to work out and still have to code the plots but here is a rough start.
Ruby:
def bn = if BarNumber()>0 then BarNumber() else 1;

def LastLow = if low[-1]>low then low else 0;
def LastLowBN = if low[-1]>low then bn else 0;

def FirstLow = if !isnan(LastLow)
    then fold i = 1 to bn
    while GetValue(low,i)>low
    do if GetValue(low,i+2)>GetValue(low,i+1)
    then GetValue(low,i+1) else 0 else 0;
def FirstLowBN = if !isnan(LastLow)
    then fold j = 1 to bn
    while GetValue(low,j)>low
    do if GetValue(low,j+2)>GetValue(low,j+1)
    then GetValue(bn,j+1) else 0 else 0;

def LowBNDiff = AbsValue(LastLowBN-FirstLowBN);

def LastHigh = if !isnan(LastLow) and !isnan(LowBNDiff)
    then fold y = 0 to LowBNDiff
    with intyh = high
    do if GetValue(high,y)>intyh and GetValue(low,y)>FirstLow and GetValue(low,y)>LastLow
    then GetValue(high,y) else intyh else 0;
def LastHighBN = if !isnan(LastLow) and !isnan(LowBNDiff)
    then fold z = 0 to LowBNDiff
    with intzh = high
    do if GetValue(high,z)>intzh and GetValue(low,z)>FirstLow and GetValue(low,z)>LastLow
    then GetValue(bn,z) else intzh else 0;

def LastHighBNDiff = AbsValue(FirstLowBN-LastHighBN);

def FirstHigh = if !isnan(LastLow)
    then fold w = 1 to bn
    with intwh = LastHigh
    while GetValue(high,w+LastHighBNDiff)<intwh
    do if GetValue(high,w+LastHighBNDiff+2)<GetValue(high,w+LastHighBNDiff+1)
    then GetValue(high,w+LastHighBNDiff+1) else 0 else 0;

def FirstHighBN = if !isnan(LastLow)
    then fold x = 1 to bn
    with intxh = LastHigh
    while GetValue(high,x+LastHighBNDiff)<intxh
    do if GetValue(high,x+LastHighBNDiff+2)<GetValue(high,x+LastHighBNDiff+1)
    then GetValue(bn,x+LastHighBNDiff+1) else 0 else 0;

def condition = LastLow and LastLowBN and FirstLow and FirstLowBN and LastHigh and LastHighBN and FirstHigh and FirstHighBN;
addchartbubble(condition,low,"First High BN "+FirstHighBN+"\nFirst High "+FirstHigh+"\nFirst Low BN "+FirstLowBN+"\nFirst Low "+FirstLow+"\nLast High BN "+LastHighBN+"\nLast High "+LastHigh+"\nLast Low BN "+LastLowBN+"\nLast Low "+LastLow,color.white,no);

Example of pattern it is identifying:


I may end up only being able to plot the most recent occurrence. Since there is no look back period, the triangles can overlap as a smaller triangle pattern can be engulfed by a larger triangle pattern.
 
@armybender
Still have some bugs to work out and still have to code the plots but here is a rough start.
Ruby:
def bn = if BarNumber()>0 then BarNumber() else 1;

def LastLow = if low[-1]>low then low else 0;
def LastLowBN = if low[-1]>low then bn else 0;

def FirstLow = if !isnan(LastLow)
    then fold i = 1 to bn
    while GetValue(low,i)>low
    do if GetValue(low,i+2)>GetValue(low,i+1)
    then GetValue(low,i+1) else 0 else 0;
def FirstLowBN = if !isnan(LastLow)
    then fold j = 1 to bn
    while GetValue(low,j)>low
    do if GetValue(low,j+2)>GetValue(low,j+1)
    then GetValue(bn,j+1) else 0 else 0;

def LowBNDiff = AbsValue(LastLowBN-FirstLowBN);

def LastHigh = if !isnan(LastLow) and !isnan(LowBNDiff)
    then fold y = 0 to LowBNDiff
    with intyh = high
    do if GetValue(high,y)>intyh and GetValue(low,y)>FirstLow and GetValue(low,y)>LastLow
    then GetValue(high,y) else intyh else 0;
def LastHighBN = if !isnan(LastLow) and !isnan(LowBNDiff)
    then fold z = 0 to LowBNDiff
    with intzh = high
    do if GetValue(high,z)>intzh and GetValue(low,z)>FirstLow and GetValue(low,z)>LastLow
    then GetValue(bn,z) else intzh else 0;

def LastHighBNDiff = AbsValue(FirstLowBN-LastHighBN);

def FirstHigh = if !isnan(LastLow)
    then fold w = 1 to bn
    with intwh = LastHigh
    while GetValue(high,w+LastHighBNDiff)<intwh
    do if GetValue(high,w+LastHighBNDiff+2)<GetValue(high,w+LastHighBNDiff+1)
    then GetValue(high,w+LastHighBNDiff+1) else 0 else 0;

def FirstHighBN = if !isnan(LastLow)
    then fold x = 1 to bn
    with intxh = LastHigh
    while GetValue(high,x+LastHighBNDiff)<intxh
    do if GetValue(high,x+LastHighBNDiff+2)<GetValue(high,x+LastHighBNDiff+1)
    then GetValue(bn,x+LastHighBNDiff+1) else 0 else 0;

def condition = LastLow and LastLowBN and FirstLow and FirstLowBN and LastHigh and LastHighBN and FirstHigh and FirstHighBN;
addchartbubble(condition,low,"First High BN "+FirstHighBN+"\nFirst High "+FirstHigh+"\nFirst Low BN "+FirstLowBN+"\nFirst Low "+FirstLow+"\nLast High BN "+LastHighBN+"\nLast High "+LastHigh+"\nLast Low BN "+LastLowBN+"\nLast Low "+LastLow,color.white,no);

Example of pattern it is identifying:


I may end up only being able to plot the most recent occurrence. Since there is no look back period, the triangles can overlap as a smaller triangle pattern can be engulfed by a larger triangle pattern.


Thanks. I suspected it would require a fold process but was hoping it could be avoided. Ultimately, as long as it identifies the most recent occurrence then it will be useful in real time.

I'll try it out and let you know if I have any issues, or if I end up modifying the code at all. Looks good at first glance though.
 
Hi,

I've tried my hand at creating something to do this, but can't seem to get it quite right. I'm wondering if someone can help. The idea is that congestion is building, so I want to know when both sides (buyers and sellers) have entered expecting a breakout.

I'm hoping to create an indicator that will put either a down arrow or up arrow above or below a bar when a triangle (nested set of highs or lows) forms and is triggered by the nested high or low being confirmed on the close of a bar.

I've included a picture below, but here are the rules that need to be coded. I'll use the rules that match the picture, but the opposite direction also needs to be considered (i.e. a triangle that is triggered by a bear bar following a set of nested highs / lows).

  1. There must first be a high - it can be a single bar, and does not need to be considered a swing (i.e. doesn't need to be a specific distance from anything)
  2. There must then be a low - same rules about not needing to be a swing.
  3. Next, a subsequent lower high and higher low must form.
  4. When the higher low is "locked in" by the closing of the bar and the price ticking at least one tick above, there is a triangle formed and it would trigger an alert or draw an arrow underneath the nested low.

Here's a picture of how it would work. Any help is appreciated!!!

here is my version, to find a 4 bar pattern , of 2 peaks and 2 valleys
https://usethinkscript.com/threads/find-4-bar-wedge-pattern-2-peaks-2-valleys.14413/
.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
379 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top