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