0

I'm trying to write a code where if a following bar of an inside bar didn't break the high or low or both of the previous bar of the inside bar, then that bar should also be marked as an inside bar with different bar colour

(https://i.stack.imgur.com/AMMow.jpg)

For e.g. in the above image after the large red bar there are 3 bars that did not break the high or the low of the red bar, I want to mark them as inside bars

As I'm a beginner at pine script and coding altogether I wrote a simple code to identify different bars and it worked except for inside bars. I tried using if function, for loop, custom functions, etc. Either I got an error or some different value altogether.

`

high_bar    = high >= high[1] and low >= low[1]
low_bar     = high <=high[1] and low <= low[1]
inside_bar  = high < high[1] and low > low[1]
outside_bar = high > high[1] and low < low[1]

` I wrote this simple code for other bars but as you can guess it only identifies only first inside bar but not the following bars. If anyone can help me with this problem it'll be much appreciated. Thank you!

2 Answers2

0

You need to store the high and low of that bar in a var variable.

var is the keyword used for assigning and one-time initializing of the variable.

So, if that red bar you are talking about is an inside_bar, you would store its high and low like below.

var float inside_bar_high = na
var float inside_bar_low = na

if (inside_bar)    // If there is a new inside bar, update the high and low price
    inside_bar_high := high
    inside_bar_low := low

Then for the upcoming bars, use inside_bar_high and inside_bar_low for comparison.

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • No, that large red bar is a low_bar as it broke the previous bar's low but not high. The next 3 bars after that large red bar are inside bars as they are within the range of the red bar and did not break high or low of the red bar. So I will ignore the values of those inside bars and then the bar on 24th Dec becomes a high bar as it broke the high of the red bar. – Sanket Kolte Nov 15 '22 at 15:27
0

Modifying the above suggestion to include outside bars.

var float inside_bar_high = high
var float inside_bar_low = low
var float outside_bar_high = high
var float outside_bar_low = low

inside_bar = high<= outside_bar_high and low>= outside_bar_low
if (inside_bar)    // If there is a new inside bar, update the high and low price
    inside_bar_high := high
    inside_bar_low := low

if not(inside_bar)
    outside_bar_high := high
    outside_bar_low := low
John Baron
  • 291
  • 2
  • 8