Difficulty coding & coloring Inside Candle formations

Aragorn

New member
Plus
Hello all, I've lurked for quite some and have found the forums very enlightening, but have only just joined.

I am not good at coding, in fact I'm very poor. I want to make an indicator that colors bar forming a pattern of mother candle > inside candle > candle up/down. The final candle has to close higher or lower than the body of the mother candle but I don't care about the wicks whatsoever.

The only criteria for the inside candle is that its closing price is inside the body of the mother candle. I do not require the entire body inside like a Harami, and I do not care about the wicks of any candle - only the body. Then I want to color all the bars in the formation.

Ideally I would like to be able to include more than 1 consecutive inside candle (a minimum of 1 up to a max of n), and I want to be able to scan for this pattern. Below is the code I have:

Code:
def Initial = BodyHeight();
def Inside = close < close[1] && close > open[1] ;
def IsUp = close > open && close > close[2]; 
plot PatternPlot =
    Initial[2] and
    Inside[1] and
    IsUp[0];

PatternPlot.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
PatternPlot.SetDefaultColor(GetColor(0));

I am stuck on coloring the bars of the pattern because SetPaintingStrategy does not color the bars themselves and AssignPriceColor gives me an error when I try to use it. I'm also stuck on how I would include multiple inside bars in the pattern.

Any help is appreciated!
 
Solution
Hello all, I've lurked for quite some and have found the forums very enlightening, but have only just joined.

I am not good at coding, in fact I'm very poor. I want to make an indicator that colors bar forming a pattern of mother candle > inside candle > candle up/down. The final candle has to close higher or lower than the body of the mother candle but I don't care about the wicks whatsoever.

The only criteria for the inside candle is that its closing price is inside the body of the mother candle. I do not require the entire body inside like a Harami, and I do not care about the wicks of any candle - only the body. Then I want to color all the bars in the formation.

Ideally I would like to be able to include more than 1...
Hello all, I've lurked for quite some and have found the forums very enlightening, but have only just joined.

I am not good at coding, in fact I'm very poor. I want to make an indicator that colors bar forming a pattern of mother candle > inside candle > candle up/down. The final candle has to close higher or lower than the body of the mother candle but I don't care about the wicks whatsoever.

The only criteria for the inside candle is that its closing price is inside the body of the mother candle. I do not require the entire body inside like a Harami, and I do not care about the wicks of any candle - only the body. Then I want to color all the bars in the formation.

Ideally I would like to be able to include more than 1 consecutive inside candle (a minimum of 1 up to a max of n), and I want to be able to scan for this pattern. Below is the code I have:

Code:
def Initial = BodyHeight();
def Inside = close < close[1] && close > open[1] ;
def IsUp = close > open && close > close[2];
plot PatternPlot =
    Initial[2] and
    Inside[1] and
    IsUp[0];

PatternPlot.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
PatternPlot.SetDefaultColor(GetColor(0));

I am stuck on coloring the bars of the pattern because SetPaintingStrategy does not color the bars themselves and AssignPriceColor gives me an error when I try to use it. I'm also stuck on how I would include multiple inside bars in the pattern.

Any help is appreciated!

i have some ideas for this, but it will be later when i get to my computer.

can you post your code so others can look it over and give you some pointers.

you can put an if then within this, to change bar color.
AssignPriceColor( if a == 2 then color.green else color.current)

i think the tricky part for you is finding multiple inside bars. you need a way to look for them.

what i have done,
look for an inside mother bar,
...use a fold loop to look at future bars and count how many have smaller bodies (or whatever condition)
from the mother bar, start a counter, from the qty down to 0.
as long as the counter is >0 then there are inside bars, so color them.
 

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

Hello all, I've lurked for quite some and have found the forums very enlightening, but have only just joined.

I am not good at coding, in fact I'm very poor. I want to make an indicator that colors bar forming a pattern of mother candle > inside candle > candle up/down. The final candle has to close higher or lower than the body of the mother candle but I don't care about the wicks whatsoever.

The only criteria for the inside candle is that its closing price is inside the body of the mother candle. I do not require the entire body inside like a Harami, and I do not care about the wicks of any candle - only the body. Then I want to color all the bars in the formation.

Ideally I would like to be able to include more than 1 consecutive inside candle (a minimum of 1 up to a max of n), and I want to be able to scan for this pattern. Below is the code I have:

Code:
def Initial = BodyHeight();
def Inside = close < close[1] && close > open[1] ;
def IsUp = close > open && close > close[2];
plot PatternPlot =
    Initial[2] and
    Inside[1] and
    IsUp[0];

PatternPlot.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
PatternPlot.SetDefaultColor(GetColor(0));

I am stuck on coloring the bars of the pattern because SetPaintingStrategy does not color the bars themselves and AssignPriceColor gives me an error when I try to use it. I'm also stuck on how I would include multiple inside bars in the pattern.

Any help is appreciated!


on each bar, determine if it is a mother bar,
by looking at future bars, to see if each close is within the body of the mother bar.
if so, then count the sequencial ones and color them as yellow.

can pick a minimum (1) and maximum (100) quantity of bars.
can draw a rectangle around the mother bar.


Code:
#inside_bar_color_c

#https://usethinkscript.com/threads/difficulty-coding-coloring-inside-candle-formations.15820/
#The only criteria for the inside candle is that its closing price is inside the body of the mother candle.

#------------------
# halcyonguy
# 23-06-29
# find when following close's are within the mother bar body
# -----------------


def bn = BarNumber();
def na = Double.NaN;
def o = open;
def h = high;
def l = low;
def c = close;
#def barup = (c > o);
#def bardwn = (c < o);

DefineGlobalColor("c1", Color.YELLOW);
# GlobalColor("c1")

input minimum_inside_bars = 1;
input maximum_inside_bars = 100;

input show_inside_count_bubbles = yes;
input show_inside_count_values_above = no;
input draw_a_box_around_inside_bar = no;

input show_horizontal_lines = yes;
input show_inside_bars_within_larger_range = no;

input candle_levels = {"wick" , default "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
    highx = h;
    lowx = l;
case "body":
    highx = Max(o, c);
    lowx = Min(o, c);
}


# def loop1 = maximum_inside_bars;
#  count how many future bars are smaller than the current bar
def incnt1 = fold k = 1 to maximum_inside_bars
  with p
# compare future body levels to a prev body
#  while (highx >= GetValue(highx, -k) and lowx <= GetValue(lowx, -k))
# compare future close prices to a prev, mother bar body levels
  while (highx >= GetValue(close, -k) and lowx <= GetValue(close, -k))
  do p + 1;

def incnt2 = if incnt1 >= minimum_inside_bars and incnt1 <= maximum_inside_bars then incnt1 else 0;

# count down the bars during the inside range , cnt+1 down to 1
def incountdown = if bn == 1 then 0
 else if incountdown[1] <= 1 and incnt2 > 0 then incnt2 + 1
 else if incountdown[1] > 0 then (incountdown[1] - 1)
 else 0;

# ignore future mother bars, within prev inside bar range
#  if countdown > 0 then ignore
def skip_in = if incnt2 > 0 and incountdown > 0 and incountdown[1] > 1 then 1 else 0;
def skipin = (show_inside_bars_within_larger_range and skip_in);

plot zskp1 = skipin;
zskp1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
zskp1.SetDefaultColor(GlobalColor("c1"));
zskp1.SetLineWeight(2);
zskp1.HideBubble();

plot zskp2 = skipin;
zskp2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
zskp2.SetDefaultColor(GlobalColor("c1"));
zskp2.SetLineWeight(2);
zskp2.HideBubble();


def yoff = 0.001;
def y6 = h * (1 + yoff);
AddChartBubble(show_inside_count_bubbles and incountdown[1] <= 1 and incountdown > 0 , y6, incnt2 , GlobalColor("c1"), yes);

plot z = if show_inside_count_values_above and incnt2 > 0 and !skipin then incnt2 else na;
z.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
z.SetDefaultColor(GlobalColor("c1"));


# draw box around inside bar
# hollow -  open = high , close = low
def o5 = if draw_a_box_around_inside_bar and (incountdown[1] <= 1 and incountdown > 0) then h * (1 + (yoff / 1)) else na;
def c5 = if draw_a_box_around_inside_bar and (incountdown[1] <= 1 and incountdown > 0) then l * (1 - (yoff / 1)) else na;
AddChart(growcolor = GlobalColor("c1"), high = o5, low = c5, open = c5, close = o5, type = ChartType.CANDLE);


# plot horz lines during inside range
def in_hi = if incountdown[1] <= 1 and incountdown > 0 then highx
 else if incountdown > 0 then in_hi[1]
 else 0;
def in_lo = if incountdown[1] <= 1 and incountdown > 0 then lowx
 else if incountdown > 0 then in_lo[1]
 else 0;


plot zinhi = if show_horizontal_lines and in_hi > 0 then in_hi else na;
plot zinlo = if show_horizontal_lines and in_lo > 0 then in_lo else na;
zinhi.SetDefaultColor(GlobalColor("c1"));
zinhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zinhi.HideBubble();
zinlo.SetDefaultColor(GlobalColor("c1"));
zinlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zinlo.HideBubble();

#================================

#https://usethinkscript.com/threads/candle-fill-color.6691/#post-68474

input color_inside_bars = yes;

def oo1;
def ll1;
def cc1;
def hh1;
if  color_inside_bars and incountdown > 0 and incountdown[1] > 0 then {
    oo1 = if o <= c then c
          else if o > c then o
          else Double.NaN;
    hh1 = h;
    ll1 = l;
    cc1 = if o <= c then o
          else if o > c then c
          else Double.NaN;
} else {
    oo1 = Double.NaN;
    hh1 = Double.NaN;
    ll1 = Double.NaN;
    cc1 = Double.NaN;
}

AddChart(growColor = GlobalColor("c1"), fallColor = Color.GREEN, neutralColor = Color.GRAY, high = hh1, low = ll1, open = oo1, close = cc1, type = ChartType.CANDLE);
#

#--------------------------

# test stuff

AddChartBubble(0, l,
 incnt1 + "\n" +
 incnt2 + "\n" +
 incountdown,
 (if incountdown > 0 then Color.YELLOW else Color.GRAY), no);
#

TJX 15min
find a mother bar,
then color the inside bars as yellow
XWRQ1Bv.jpg
 
Solution
@halcyonguy Many, many thanks for your helpful post!

This is a perfect solution, and it also illustrates the use of "fold" for me which is something I never considered using!

i didn't mention , my answer is just for a chart.
to make a scan, we need data on the last bar. maybe another variable would be 0 or 1, depending if the current bar is an inside bar.

maybe disable all the plots and make a new one,

plot z = if incountdown > 0 then 1 else 0;
 
i didn't mention , my answer is just for a chart.
to make a scan, we need data on the last bar. maybe another variable would be 0 or 1, depending if the current bar is an inside bar.

maybe disable all the plots and make a new one,

plot z = if incountdown > 0 then 1 else 0;

Thank you again. I'll work on that and improving my code abilities!
 
i didn't mention , my answer is just for a chart.
to make a scan, we need data on the last bar. maybe another variable would be 0 or 1, depending if the current bar is an inside bar.

maybe disable all the plots and make a new one,

plot z = if incountdown > 0 then 1 else 0;
Thanks for this script.

May i request a slight tweak to scan for inside candle that has the body within the Mother Bar Body. Thanks
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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