How to code a breakout

Hi guys,

I am trying to code a breakout from resistance/support line. How do I do that? This is what I want my indicator to do:

Up arrow -> if 30 min candle goes below support within the last 5 candles and then recovers above the support
down arrow -> if 30 min candle goes above a resistance within the last 5 candles and then falls below the support

Will it be done through If statement or fold? Can someone help me write a sample code please
 
Solution
I am trying to code a breakout from resistance/support line. How do I do that? This is what I want my indicator to do:

Up arrow -> if 30 min candle goes below support within the last 5 candles and then recovers above the support
down arrow -> if 30 min candle goes above a resistance within the last 5 candles and then falls below the support


THIS IS NOT COMPLETE.
THE USER WILL HAVE TO ADD CODE TO CALCULATE WHAT DEFINES A 'SUPPORT' VALUE TO THEM.


draw arrows when,
..price crosses some level
..AND
..the opposite crossing happened within x bars ( default 5)

it finds crossings, then compares the barnumbers of them, to determine if they are close enough to each other.

for testing, this uses an input for the 'support' level. it...
I am trying to code a breakout from resistance/support line. How do I do that? This is what I want my indicator to do:

Up arrow -> if 30 min candle goes below support within the last 5 candles and then recovers above the support
down arrow -> if 30 min candle goes above a resistance within the last 5 candles and then falls below the support


THIS IS NOT COMPLETE.
THE USER WILL HAVE TO ADD CODE TO CALCULATE WHAT DEFINES A 'SUPPORT' VALUE TO THEM.


draw arrows when,
..price crosses some level
..AND
..the opposite crossing happened within x bars ( default 5)

it finds crossings, then compares the barnumbers of them, to determine if they are close enough to each other.

for testing, this uses an input for the 'support' level. it is set to 50.

can pick from 3 different crossing levels, bar to bar
..wicks - high to low, low to high
..body - open to close, close to open
..close - close to close

using wicks, will catch when just a wick crosses a level. close to close will miss it.


Ruby:
# breakout_from_support_00d

# https://usethinkscript.com/threads/how-to-code-a-breakout.10758/
# How to code a breakout

def bn = barnumber();
def na = double.nan;
input bars_back = 5;


# add in your code to determine support levels
# def support = ... some price level
input support_level = 50;



# convert constant to a var.  so  support[1] can be used
def support = if bn == 1 then support_level else support[1];

input show_ref_line = yes;
plot y = if show_ref_line then support else na;

#--------------------------------
input candle_levels = {default "wick" , "body" , "close"};

def highx;
def lowx;
switch (candle_levels) {
case "wick":
    highx = high;
    lowx = low;
case "body":
    highx = Max(open, close);
    lowx = Min(open, close);
case "close":
    highx = close;
    lowx = close;
}
#-----------------------------------

addlabel(1, "crossing levels " + candle_levels , color.yellow);
addlabel(1, "bars back " + bars_back, color.yellow);

# use wicks or body data or close
def crossdwnbn = if bn == 1 then 0 else if highx[1] > support[1] and lowx < support then bn else crossdwnbn[1];
def crossupbn = if bn == 1 then 0 else if lowx[1] < support[1] and highx > support then bn else crossupbn[1];

# dip then rise, within len bars
def up2 = if
bn > bars_back
and
( (crossupbn - crossdwnbn) <= bars_back and (crossupbn - crossdwnbn) > 0 )
and
crossupbn == bn
and
sum(up2[1], (bars_back-1)) == 0
then 1 else 0;

# rise then dip, within len bars
def down2 = if
bn > bars_back
and
( (crossdwnbn - crossupbn) <= bars_back and (crossdwnbn - crossupbn) > 0 )
and
crossdwnbn == bn
and
sum(down2[1], (bars_back-1)) == 0
then 1 else 0;

input arrow_size = 2;

plot zup = up2;
zup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
zup.SetDefaultColor(Color.green);
zup.setlineweight(arrow_size);

plot zdwn = down2;
zdwn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
zdwn.SetDefaultColor(Color.red);
zdwn.setlineweight(arrow_size);

# ---------------------------------------
#  test stuff

input test_crossing_bubbles = no;
addchartbubble(test_crossing_bubbles and crossdwnbn == bn, high, bn, color.red, no);
addchartbubble(test_crossing_bubbles and crossupbn == bn, low, bn, color.green, yes);
#

Z 30min chart , on 3/16. support set to 50
av4XLXX.jpg
 
Solution
THIS IS NOT COMPLETE.
THE USER WILL HAVE TO ADD CODE TO CALCULATE WHAT DEFINES A 'SUPPORT' VALUE TO THEM.


draw arrows when,
..price crosses some level
..AND
..the opposite crossing happened within x bars ( default 5)

it finds crossings, then compares the barnumbers of them, to determine if they are close enough to each other.

for testing, this uses an input for the 'support' level. it is set to 50.

can pick from 3 different crossing levels, bar to bar
..wicks - high to low, low to high
..body - open to close, close to open
..close - close to close

using wicks, will catch when just a wick crosses a level. close to close will miss it.


Ruby:
# breakout_from_support_00d

# https://usethinkscript.com/threads/how-to-code-a-breakout.10758/
# How to code a breakout

def bn = barnumber();
def na = double.nan;
input bars_back = 5;


# add in your code to determine support levels
# def support = ... some price level
input support_level = 50;



# convert constant to a var.  so  support[1] can be used
def support = if bn == 1 then support_level else support[1];

input show_ref_line = yes;
plot y = if show_ref_line then support else na;

#--------------------------------
input candle_levels = {default "wick" , "body" , "close"};

def highx;
def lowx;
switch (candle_levels) {
case "wick":
    highx = high;
    lowx = low;
case "body":
    highx = Max(open, close);
    lowx = Min(open, close);
case "close":
    highx = close;
    lowx = close;
}
#-----------------------------------

addlabel(1, "crossing levels " + candle_levels , color.yellow);
addlabel(1, "bars back " + bars_back, color.yellow);

# use wicks or body data or close
def crossdwnbn = if bn == 1 then 0 else if highx[1] > support[1] and lowx < support then bn else crossdwnbn[1];
def crossupbn = if bn == 1 then 0 else if lowx[1] < support[1] and highx > support then bn else crossupbn[1];

# dip then rise, within len bars
def up2 = if
bn > bars_back
and
( (crossupbn - crossdwnbn) <= bars_back and (crossupbn - crossdwnbn) > 0 )
and
crossupbn == bn
and
sum(up2[1], (bars_back-1)) == 0
then 1 else 0;

# rise then dip, within len bars
def down2 = if
bn > bars_back
and
( (crossdwnbn - crossupbn) <= bars_back and (crossdwnbn - crossupbn) > 0 )
and
crossdwnbn == bn
and
sum(down2[1], (bars_back-1)) == 0
then 1 else 0;

input arrow_size = 2;

plot zup = up2;
zup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
zup.SetDefaultColor(Color.green);
zup.setlineweight(arrow_size);

plot zdwn = down2;
zdwn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
zdwn.SetDefaultColor(Color.red);
zdwn.setlineweight(arrow_size);

# ---------------------------------------
#  test stuff

input test_crossing_bubbles = no;
addchartbubble(test_crossing_bubbles and crossdwnbn == bn, high, bn, color.red, no);
addchartbubble(test_crossing_bubbles and crossupbn == bn, low, bn, color.green, yes);
#

Z 30min chart , on 3/16. support set to 50
av4XLXX.jpg
This is wonderful, THANK YOU! I used the template to create 3 Resistances and 3 supports. There are some false breakouts that I want to reduce. I usually just trade /ES.. and say in a 30 minute chart, I want to say, the candle goes down and then goes up 15 points above the support/resistance and only then get a signal, how do I do that?

For up arrow -> 15 points above support/resistance
For down arrow -> 15 points below support/resistance

THANK YOU VERY MUCH ONCE AGAIN
 
This is wonderful, THANK YOU! I used the template to create 3 Resistances and 3 supports. There are some false breakouts that I want to reduce. I usually just trade /ES.. and say in a 30 minute chart, I want to say, the candle goes down and then goes up 15 points above the support/resistance and only then get a signal, how do I do that?

For up arrow -> 15 points above support/resistance
For down arrow -> 15 points below support/resistance

THANK YOU VERY MUCH ONCE AGAIN

maybe this will work.
try adding a number to these 2 formulas.

def y = 15;

def crossdwnbn = if bn == 1 then 0 else if highx[1] > support[1] + y and lowx < support - y then bn else crossdwnbn[1];

def crossupbn = if bn == 1 then 0 else if lowx[1] < support[1] + y and highx > support - y then bn else crossupbn[1];
 
Hello, I am coding breakouts using this template for C3_Max_Spark… here is a screen shot from tos mobile….


I am trying to understand how to modify the code to plot a single arrow when the candle crosses below the red line then crosses down to the green line then back up into above the red then pulls back to whatever line maybe an ema.

So basically I need to know how to modify this:

# rise then dip, within len bars
def down2 = if
bn > bars_back
and
( (crossdwnbn - crossupbn) <= bars_back and (crossdwnbn - crossupbn) > 0 )
and
crossdwnbn == bn
and
sum(down2[1], (bars_back-1)) == 0
then 1 else 0;

To account for more than one crossing of more than one line for a single plot. Or how you would go about accomplishing it. If you have to time to make a generic template / example or just point me in the right direction it would be greatly appreciated. I have all of the support resistance lines cross up cross down coded just not sure how to add them together in this section of the code.
Thanks!
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
326 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