Indicator · Pine Script v6 · On the chart

LunarTick Initial Balance

by lunarticktrading· code credited to gambcl

This indicator tracks and visualizes the initial balance range over customizable trading sessions directly on the price chart.

View on GitHub

Repaints

GitHub stars7on the repository
Pine versionv6current
Lines of code555
Settings27inputs
LicenceNone statedfile: MPL-2.0
Last update2 weeks ago10 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

555 lines of Pine Script v6 from LunarTick Initial Balance.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

LunarTick Initial Balance.txt
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gambcl

//@version=6
indicator("LunarTick Initial Balance", overlay = true, max_lines_count = 500, max_labels_count = 500)


////////////////////////////////////////////////////////////////////////////////////////////////////
// Version History
////////////////////////////////////////////////////////////////////////////////////////////////////
// v1.2.3
// - Improved inputs
// v1.2.2
// - Added limits to label offset, transparency
// v1.2.1
// - Prevent IB from extending into the next session
// - Cleanup labels when truncating IB
// v1.2.0
// - Added AVWAP
// v1.1.0
// - Improved labels
// - Highlight range
// v1.0.0
// - Initial release
////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////
// Imports
////////////////////////////////////////////////////////////////////////////////////////////////////

import gambcl/LunarTick/2


////////////////////////////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////////////////////////////

var SESSION_GROUP = "SESSION"
var DISPLAY_GROUP = "DISPLAY"
var DEVELOPER_GROUP = "DEVELOPER"

var int INITIAL_BALANCE_DURATION = 60


////////////////////////////////////////////////////////////////////////////////////////////////////
// Enums
////////////////////////////////////////////////////////////////////////////////////////////////////

enum LineStyle
    solid = "Solid"
    dashed = "Dashed"
    dotted = "Dotted"

enum ShowExtensionLevels
    none = "None"
    one = "One"
    two = "Two"
    three = "Three"

enum LabelStyle
    none = "None"
    label_up = "Label Up"
    label_down = "Label Down"
    label_left = "Label Left"
    label_right = "Label Right"
    label_lower_left = "Label Lower Left"
    label_lower_right = "Label Lower Right"
    label_upper_left = "Label Upper Left"
    label_upper_right = "Label Upper Right"
    label_center = "Label Center"
    text_outline = "Text Outline"

enum LabelSize
    auto = "Auto"
    tiny = "Tiny"
    small = "Small"
    normal = "Normal"
    large = "Large"
    huge = "Huge"

enum VersionEnum
    version = "v1.2.3"
    

////////////////////////////////////////////////////////////////////////////////////////////////////
// Inputs
////////////////////////////////////////////////////////////////////////////////////////////////////

i_session = input.session("0930-1600", "Session", group = SESSION_GROUP)
i_tz = input.string("America/New_York", "Timezone", group = SESSION_GROUP)

i_show_num_days = input.int(5, "Show Days", group = DISPLAY_GROUP, display = display.none)
i_show_ext_levels = input.enum(ShowExtensionLevels.none, "Show Extension Levels", group = DISPLAY_GROUP, display = display.none)
i_show_mid_levels = input.bool(true, "Show Mid Levels", group = DISPLAY_GROUP, display = display.none)

i_highlight_initial_balance_region = input.bool(true, "Highlight Initial Balance Region", inline = "IB Region", group = DISPLAY_GROUP, display = display.none)
i_initial_balance_region_color = input.color(color.new(color.aqua, 67), "", inline = "IB Region", group = DISPLAY_GROUP, display = display.none, active = i_highlight_initial_balance_region)

i_highlight_initial_balance_range = input.bool(false, "Highlight Initial Balance Range", inline = "IB Range", group = DISPLAY_GROUP, display = display.none)
i_initial_balance_range_color = input.color(color.new(color.aqua, 67), "", inline = "IB Range", group = DISPLAY_GROUP, display = display.none, active = i_highlight_initial_balance_range)

i_initial_balance_h_color = input.color(color.green, "Initial Balance High", inline = "IBH", group = DISPLAY_GROUP, display = display.none)
i_initial_balance_h_style = input.enum(LineStyle.solid, "", inline = "IBH", group = DISPLAY_GROUP, display = display.none)
i_initial_balance_h_width = input.int(2, "", minval = 1, maxval = 4, inline = "IBH", group = DISPLAY_GROUP, display = display.none)

i_initial_balance_m_color = input.color(color.orange, "Initial Balance Mid", inline = "IBM", group = DISPLAY_GROUP, display = display.none, active = i_show_mid_levels)
i_initial_balance_m_style = input.enum(LineStyle.dashed, "", inline = "IBM", group = DISPLAY_GROUP, display = display.none, active = i_show_mid_levels)
i_initial_balance_m_width = input.int(1, "", minval = 1, maxval = 4, inline = "IBM", group = DISPLAY_GROUP, display = display.none, active = i_show_mid_levels)

i_initial_balance_l_color = input.color(color.red, "Initial Balance Low", inline = "IBL", group = DISPLAY_GROUP, display = display.none)
i_initial_balance_l_style = input.enum(LineStyle.solid, "", inline = "IBL", group = DISPLAY_GROUP, display = display.none)
i_initial_balance_l_width = input.int(2, "", minval = 1, maxval = 4, inline = "IBL", group = DISPLAY_GROUP, display = display.none)

bool i_show_avwap = input.bool(false, "Show AVWAP", inline = "AVWAP", group = DISPLAY_GROUP, display = display.none)
color i_avwap_color = input.color(color.aqua, "", inline = "AVWAP", group = DISPLAY_GROUP, active = i_show_avwap, display = display.none)

bool i_show_labels = input.bool(true, "Show Labels", group = DISPLAY_GROUP, display = display.none)
int i_label_offset = input.int(1, "Label Offset", minval = 1, maxval = 100, group = DISPLAY_GROUP, active = i_show_labels, display = display.none)
LabelStyle i_label_style = input.enum(LabelStyle.label_left, "Label Style", group = DISPLAY_GROUP, active = i_show_labels, display = display.none)
LabelSize i_label_size = input.enum(LabelSize.normal, "Label Size", group = DISPLAY_GROUP, active = i_show_labels, display = display.none)
int i_label_bg_transparency = input.int(0, "Label Transparency", minval = 0, maxval = 100, group = DISPLAY_GROUP, active = i_show_labels, display = display.none)
color i_label_text_color = input.color(color.white, "Label Text Color", group = DISPLAY_GROUP, active = i_show_labels, display = display.none)

VersionEnum i_version = input.enum(VersionEnum.version, "Version", display = display.none, group = DEVELOPER_GROUP)


////////////////////////////////////////////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////////////////////////////////////////////

type InitialBalance
    int region_start_bar_index = na
    int region_end_bar_index = na
    float region_high = na
    float region_low = na
    bool region_closed = false
    box region_box = na
    int range_end_bar_index = na
    box range_box = na
    line high_ext3_line = na
    line high_ext3_mid_line = na
    line high_ext2_line = na
    line high_ext2_mid_line = na
    line high_ext1_line = na
    line high_ext1_mid_line = na
    line high_line = na
    line mid_line = na
    line low_line = na
    line low_ext1_mid_line = na
    line low_ext1_line = na
    line low_ext2_mid_line = na
    line low_ext2_line = na
    line low_ext3_mid_line = na
    line low_ext3_line = na
    label high_ext3_label = na
    label high_ext3_mid_label = na
    label high_ext2_label = na
    label high_ext2_mid_label = na
    label high_ext1_label = na
    label high_ext1_mid_label = na
    label high_label = na
    label mid_label = na
    label low_label = na
    label low_ext1_mid_label = na
    label low_ext1_label = na
    label low_ext2_mid_label = na
    label low_ext2_label = na
    label low_ext3_mid_label = na
    label low_ext3_label = na


////////////////////////////////////////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////////////////////////////////////////

request_security_no_repaint(timeframe, expression) =>
    request.security(symbol = syminfo.tickerid, timeframe = timeframe, expression = expression[barstate.isrealtime ? 1 : 0], gaps = barmerge.gaps_off)[barstate.isrealtime ? 0 : 1] // PineCoders best practice.

build_initial_balance_session(string session, int initial_balance_duration_minutes) =>
    string initial_balance_session = na
    int start_time = int(str.tonumber(str.substring(session, 0, 4)))
    //log.info("START_TIME: [" + str.tostring(start_time) + "]")
    int end_time = LunarTick.time_add_minutes(start_time, initial_balance_duration_minutes)
    //log.info("END_TIME: [" + str.tostring(end_time) + "]")
    initial_balance_session := LunarTick.pad_leading_zeroes(start_time, 4) + "-" + LunarTick.pad_leading_zeroes(end_time, 4)
    //log.info("IB_SESSION: [" + initial_balance_session + "]")
    initial_balance_session

get_label_style(LabelStyle lbl_style) =>
    switch lbl_style
        LabelStyle.none => label.style_label_left
        LabelStyle.label_up => label.style_label_up
        LabelStyle.label_down => label.style_label_down
        LabelStyle.label_left => label.style_label_left
        LabelStyle.label_right => label.style_label_right
        LabelStyle.label_lower_left => label.style_label_lower_left
        LabelStyle.label_lower_right => label.style_label_lower_right
        LabelStyle.label_upper_left => label.style_label_upper_left
        LabelStyle.label_upper_right => label.style_label_upper_right
        LabelStyle.label_center => label.style_label_center
        LabelStyle.text_outline => label.style_text_outline
        => label.style_none

get_label_textalign(LabelStyle lbl_style) =>
    switch lbl_style
        LabelStyle.none => text.align_left
        LabelStyle.label_left => text.align_left
        LabelStyle.label_right => text.align_right
        LabelStyle.label_lower_left => text.align_left
        LabelStyle.label_lower_right => text.align_right
        LabelStyle.label_upper_left => text.align_left
        LabelStyle.label_upper_right => text.align_right
        => text.align_center

get_label_bg_color(LabelStyle lbl_style, color lbl_color) =>
    switch lbl_style
        LabelStyle.none => na
        => color.new(lbl_color, i_label_bg_transparency)

get_label_text_color(LabelStyle lbl_style, color lbl_color) =>
    switch lbl_style
        LabelStyle.none => lbl_color
        => i_label_text_color

get_label_size(LabelSize lbl_size) =>
    switch lbl_size
        LabelSize.auto => size.auto
        LabelSize.tiny => size.tiny
        LabelSize.small => size.small
        LabelSize.normal => size.normal
        LabelSize.large => size.large
        LabelSize.huge => size.huge
        => size.auto

get_line_style(LineStyle value) =>
    switch value
        LineStyle.dashed => line.style_dashed
        LineStyle.dotted => line.style_dotted
        => line.style_solid

get_end_bar_index() =>
    int end_bar_index = bar_index
    for i = 1 to 499
        if na(time("", i_session, i_tz, -i))
            break
        end_bar_index := end_bar_index + 1
    end_bar_index


////////////////////////////////////////////////////////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////////////////////////////////////////////////////////

method initialize(InitialBalance self, int region_start_bar_index, float region_high, float region_low) =>
    self.region_start_bar_index := region_start_bar_index
    self.region_high := region_high
    self.region_low := region_low
    self.region_closed := false
    if i_highlight_initial_balance_region
        self.region_box := box.new(region_start_bar_index, region_high, region_start_bar_index, region_low, na, 0, line.style_solid, extend.none, xloc.bar_index, i_initial_balance_region_color)

method update_levels(InitialBalance self, int region_end_bar_index, float region_high, float region_low) =>
    self.region_end_bar_index := region_end_bar_index
    self.region_high := math.max(region_high, self.region_high)
    self.region_low := math.min(region_low, self.region_low)
    if not na(self.range_box)
        self.range_box.set_right(region_end_bar_index)
        self.range_box.set_top(self.region_high)
        self.range_box.set_bottom(self.region_low)
    if not na(self.region_box)
        self.region_box.set_right(region_end_bar_index)
        self.region_box.set_top(self.region_high)
        self.region_box.set_bottom(self.region_low)

method confirm_levels(InitialBalance self, int right_bar_index) =>
    if not self.region_closed
        self.range_end_bar_index := right_bar_index
        if i_highlight_initial_balance_range
            self.range_box := box.new(self.region_start_bar_index, self.region_high, right_bar_index, self.region_low, na, 0, line.style_solid, extend.none, xloc.bar_index, i_initial_balance_range_color)
        range_mid = (self.region_high + self.region_low) / 2.0
        self.high_line := line.new(self.region_start_bar_index, self.region_high, right_bar_index, self.region_high, xloc.bar_index, extend.none, i_initial_balance_h_color, get_line_style(i_initial_balance_h_style), i_initial_balance_h_width)
        self.high_label := i_show_labels ? label.new(right_bar_index + i_label_offset, self.region_high, str.tostring(self.region_high, format.mintick) + " IB High", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_h_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_h_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance High") : na
        self.mid_line := i_show_mid_levels ? line.new(self.region_start_bar_index, range_mid, right_bar_index, range_mid, xloc.bar_index, extend.none, i_initial_balance_m_color, get_line_style(i_initial_balance_m_style), i_initial_balance_m_width) : na
        self.mid_label := i_show_labels and i_show_mid_levels ? label.new(right_bar_index + i_label_offset, range_mid, str.tostring(range_mid, format.mintick) + " IB Mid", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_m_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_m_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance Mid") : na
        self.low_line := line.new(self.region_start_bar_index, self.region_low, right_bar_index, self.region_low, xloc.bar_index, extend.none, i_initial_balance_l_color, get_line_style(i_initial_balance_l_style), i_initial_balance_l_width)
        self.low_label := i_show_labels ? label.new(right_bar_index + i_label_offset, self.region_low, str.tostring(self.region_low, format.mintick) + " IB Low", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_l_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_l_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance Low") : na
        if i_show_ext_levels != ShowExtensionLevels.none
            range_high_ext1 = self.region_high + (self.region_high - self.region_low)
            range_high_ext1_mid = self.region_high + 0.5 * (self.region_high - self.region_low)
            range_low_ext1_mid = self.region_low - 0.5 * (self.region_high - self.region_low)
            range_low_ext1 = self.region_low - (self.region_high - self.region_low)
            self.high_ext1_line := line.new(self.region_start_bar_index, range_high_ext1, right_bar_index, range_high_ext1, xloc.bar_index, extend.none, i_initial_balance_h_color, get_line_style(i_initial_balance_h_style), i_initial_balance_h_width)
            self.high_ext1_label := i_show_labels ? label.new(right_bar_index + i_label_offset, range_high_ext1, str.tostring(range_high_ext1, format.mintick) + " IB High Ext1", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_h_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_h_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance High Ext1") : na
            self.high_ext1_mid_line := i_show_mid_levels ? line.new(self.region_start_bar_index, range_high_ext1_mid, right_bar_index, range_high_ext1_mid, xloc.bar_index, extend.none, i_initial_balance_m_color, get_line_style(i_initial_balance_m_style), i_initial_balance_m_width) : na
            self.high_ext1_mid_label := i_show_labels and i_show_mid_levels ? label.new(right_bar_index + i_label_offset, range_high_ext1_mid, str.tostring(range_high_ext1_mid, format.mintick) + " IB High Ext1 Mid", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_m_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_m_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance High Ext1 Mid") : na
            self.low_ext1_mid_line := i_show_mid_levels ? line.new(self.region_start_bar_index, range_low_ext1_mid, right_bar_index, range_low_ext1_mid, xloc.bar_index, extend.none, i_initial_balance_m_color, get_line_style(i_initial_balance_m_style), i_initial_balance_m_width) : na
            self.low_ext1_mid_label := i_show_labels and i_show_mid_levels ? label.new(right_bar_index + i_label_offset, range_low_ext1_mid, str.tostring(range_low_ext1_mid, format.mintick) + " IB Low Ext1 Mid", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_m_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_m_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance Low Ext1 Mid") : na
            self.low_ext1_line := line.new(self.region_start_bar_index, range_low_ext1, right_bar_index, range_low_ext1, xloc.bar_index, extend.none, i_initial_balance_l_color, get_line_style(i_initial_balance_l_style), i_initial_balance_l_width)
            self.low_ext1_label := i_show_labels ? label.new(right_bar_index + i_label_offset, range_low_ext1, str.tostring(range_low_ext1, format.mintick) + " IB Low Ext1", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_l_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_l_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance Low Ext1") : na
        if i_show_ext_levels == ShowExtensionLevels.two or i_show_ext_levels == ShowExtensionLevels.three
            range_high_ext2 = self.region_high + 2 * (self.region_high - self.region_low)
            range_high_ext2_mid = self.region_high + 1.5 * (self.region_high - self.region_low)
            range_low_ext2_mid = self.region_low - 1.5 * (self.region_high - self.region_low)
            range_low_ext2 = self.region_low - 2 * (self.region_high - self.region_low)
            self.high_ext2_line := line.new(self.region_start_bar_index, range_high_ext2, right_bar_index, range_high_ext2, xloc.bar_index, extend.none, i_initial_balance_h_color, get_line_style(i_initial_balance_h_style), i_initial_balance_h_width)
            self.high_ext2_label := i_show_labels ? label.new(right_bar_index + i_label_offset, range_high_ext2, str.tostring(range_high_ext2, format.mintick) + " IB High Ext2", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_h_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_h_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance High Ext2") : na
            self.high_ext2_mid_line := i_show_mid_levels ? line.new(self.region_start_bar_index, range_high_ext2_mid, right_bar_index, range_high_ext2_mid, xloc.bar_index, extend.none, i_initial_balance_m_color, get_line_style(i_initial_balance_m_style), i_initial_balance_m_width) : na
            self.high_ext2_mid_label := i_show_labels and i_show_mid_levels ? label.new(right_bar_index + i_label_offset, range_high_ext2_mid, str.tostring(range_high_ext2_mid, format.mintick) + " IB High Ext2 Mid", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_m_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_m_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance High Ext2 Mid") : na
            self.low_ext2_mid_line := i_show_mid_levels ? line.new(self.region_start_bar_index, range_low_ext2_mid, right_bar_index, range_low_ext2_mid, xloc.bar_index, extend.none, i_initial_balance_m_color, get_line_style(i_initial_balance_m_style), i_initial_balance_m_width) : na
            self.low_ext2_mid_label := i_show_labels and i_show_mid_levels ? label.new(right_bar_index + i_label_offset, range_low_ext2_mid, str.tostring(range_low_ext2_mid, format.mintick) + " IB Low Ext2 Mid", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_m_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_m_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance Low Ext2 Mid") : na
            self.low_ext2_line := line.new(self.region_start_bar_index, range_low_ext2, right_bar_index, range_low_ext2, xloc.bar_index, extend.none, i_initial_balance_l_color, get_line_style(i_initial_balance_l_style), i_initial_balance_l_width)
            self.low_ext2_label := i_show_labels ? label.new(right_bar_index + i_label_offset, range_low_ext2, str.tostring(range_low_ext2, format.mintick) + " IB Low Ext2", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_l_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_l_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance Low Ext2") : na
        if i_show_ext_levels == ShowExtensionLevels.three
            range_high_ext3 = self.region_high + 3 * (self.region_high - self.region_low)
            range_high_ext3_mid = self.region_high + 2.5 * (self.region_high - self.region_low)
            range_low_ext3_mid = self.region_low - 2.5 * (self.region_high - self.region_low)
            range_low_ext3 = self.region_low - 3 * (self.region_high - self.region_low)
            self.high_ext3_line := line.new(self.region_start_bar_index, range_high_ext3, right_bar_index, range_high_ext3, xloc.bar_index, extend.none, i_initial_balance_h_color, get_line_style(i_initial_balance_h_style), i_initial_balance_h_width)
            self.high_ext3_label := i_show_labels ? label.new(right_bar_index + i_label_offset, range_high_ext3, str.tostring(range_high_ext3, format.mintick) + " IB High Ext3", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_h_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_h_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance High Ext3") : na
            self.high_ext3_mid_line := i_show_mid_levels ? line.new(self.region_start_bar_index, range_high_ext3_mid, right_bar_index, range_high_ext3_mid, xloc.bar_index, extend.none, i_initial_balance_m_color, get_line_style(i_initial_balance_m_style), i_initial_balance_m_width) : na
            self.high_ext3_mid_label := i_show_labels and i_show_mid_levels ? label.new(right_bar_index + i_label_offset, range_high_ext3_mid, str.tostring(range_high_ext3_mid, format.mintick) + " IB High Ext3 Mid", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_m_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_m_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance High Ext3 Mid") : na
            self.low_ext3_mid_line := i_show_mid_levels ? line.new(self.region_start_bar_index, range_low_ext3_mid, right_bar_index, range_low_ext3_mid, xloc.bar_index, extend.none, i_initial_balance_m_color, get_line_style(i_initial_balance_m_style), i_initial_balance_m_width) : na
            self.low_ext3_mid_label := i_show_labels and i_show_mid_levels ? label.new(right_bar_index + i_label_offset, range_low_ext3_mid, str.tostring(range_low_ext3_mid, format.mintick) + " IB Low Ext3 Mid", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_m_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_m_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance Low Ext3 Mid") : na
            self.low_ext3_line := line.new(self.region_start_bar_index, range_low_ext3, right_bar_index, range_low_ext3, xloc.bar_index, extend.none, i_initial_balance_l_color, get_line_style(i_initial_balance_l_style), i_initial_balance_l_width)
            self.low_ext3_label := i_show_labels ? label.new(right_bar_index + i_label_offset, range_low_ext3, str.tostring(range_low_ext3, format.mintick) + " IB Low Ext3", xloc.bar_index, yloc.price, get_label_bg_color(i_label_style, i_initial_balance_l_color), get_label_style(i_label_style), get_label_text_color(i_label_style, i_initial_balance_l_color), get_label_size(i_label_size), get_label_textalign(i_label_style), "Initial Balance Low Ext3") : na
        self.region_closed := true

method extend(InitialBalance self, int right_bar_index) =>
    // Extend range
    if not na(self.range_box)
        self.range_box.set_right(right_bar_index)
    // Extend lines
    if not na(self.high_ext3_line)
        self.high_ext3_line.set_x2(right_bar_index)
    if not na(self.high_ext3_mid_line)
        self.high_ext3_mid_line.set_x2(right_bar_index)
    if not na(self.high_ext2_line)
        self.high_ext2_line.set_x2(right_bar_index)
    if not na(self.high_ext2_mid_line)
        self.high_ext2_mid_line.set_x2(right_bar_index)
    if not na(self.high_ext1_line)
        self.high_ext1_line.set_x2(right_bar_index)
    if not na(self.high_ext1_mid_line)
        self.high_ext1_mid_line.set_x2(right_bar_index)
    if not na(self.high_line)
        self.high_line.set_x2(right_bar_index)
    if not na(self.mid_line)
        self.mid_line.set_x2(right_bar_index)
    if not na(self.low_line)
        self.low_line.set_x2(right_bar_index)
    if not na(self.low_ext1_mid_line)
        self.low_ext1_mid_line.set_x2(right_bar_index)
    if not na(self.low_ext1_line)
        self.low_ext1_line.set_x2(right_bar_index)
    if not na(self.low_ext2_mid_line)
        self.low_ext2_mid_line.set_x2(right_bar_index)
    if not na(self.low_ext2_line)
        self.low_ext2_line.set_x2(right_bar_index)
    if not na(self.low_ext3_mid_line)
        self.low_ext3_mid_line.set_x2(right_bar_index)
    if not na(self.low_ext3_line)
        self.low_ext3_line.set_x2(right_bar_index)
    // Extend labels
    if not na(self.high_ext3_label)
        self.high_ext3_label.set_x(right_bar_index + i_label_offset)
    if not na(self.high_ext3_mid_label)
        self.high_ext3_mid_label.set_x(right_bar_index + i_label_offset)
    if not na(self.high_ext2_label)
        self.high_ext2_label.set_x(right_bar_index + i_label_offset)
    if not na(self.high_ext2_mid_label)
        self.high_ext2_mid_label.set_x(right_bar_index + i_label_offset)
    if not na(self.high_ext1_label)
        self.high_ext1_label.set_x(right_bar_index + i_label_offset)
    if not na(self.high_ext1_mid_label)
        self.high_ext1_mid_label.set_x(right_bar_index + i_label_offset)
    if not na(self.high_label)
        self.high_label.set_x(right_bar_index + i_label_offset)
    if not na(self.mid_label)
        self.mid_label.set_x(right_bar_index + i_label_offset)
    if not na(self.low_label)
        self.low_label.set_x(right_bar_index + i_label_offset)
    if not na(self.low_ext1_mid_label)
        self.low_ext1_mid_label.set_x(right_bar_index + i_label_offset)
    if not na(self.low_ext1_label)
        self.low_ext1_label.set_x(right_bar_index + i_label_offset)
    if not na(self.low_ext2_mid_label)
        self.low_ext2_mid_label.set_x(right_bar_index + i_label_offset)
    if not na(self.low_ext2_label)
        self.low_ext2_label.set_x(right_bar_index + i_label_offset)
    if not na(self.low_ext3_mid_label)
        self.low_ext3_mid_label.set_x(right_bar_index + i_label_offset)
    if not na(self.low_ext3_label)
        self.low_ext3_label.set_x(right_bar_index + i_label_offset)

method cleanup_labels(InitialBalance self) =>
    // Cleanup labels
    if not na(self.high_ext3_label)
        self.high_ext3_label.delete()
        self.high_ext3_label := na
    if not na(self.high_ext3_mid_label)
        self.high_ext3_mid_label.delete()
        self.high_ext3_mid_label := na
    if not na(self.high_ext2_label)
        self.high_ext2_label.delete()
        self.high_ext2_label := na
    if not na(self.high_ext2_mid_label)
        self.high_ext2_mid_label.delete()
        self.high_ext2_mid_label := na
    if not na(self.high_ext1_label)
        self.high_ext1_label.delete()
        self.high_ext1_label := na
    if not na(self.high_ext1_mid_label)
        self.high_ext1_mid_label.delete()
        self.high_ext1_mid_label := na
    if not na(self.high_label)
        self.high_label.delete()
        self.high_label := na
    if not na(self.mid_label)
        self.mid_label.delete()
        self.mid_label := na
    if not na(self.low_label)
        self.low_label.delete()
        self.low_label := na
    if not na(self.low_ext1_mid_label)
        self.low_ext1_mid_label.delete()
        self.low_ext1_mid_label := na
    if not na(self.low_ext1_label)
        self.low_ext1_label.delete()
        self.low_ext1_label := na
    if not na(self.low_ext2_mid_label)
        self.low_ext2_mid_label.delete()
        self.low_ext2_mid_label := na
    if not na(self.low_ext2_label)
        self.low_ext2_label.delete()
        self.low_ext2_label := na
    if not na(self.low_ext3_mid_label)
        self.low_ext3_mid_label.delete()
        self.low_ext3_mid_label := na
    if not na(self.low_ext3_label)
        self.low_ext3_label.delete()
        self.low_ext3_label := na

method cleanup(InitialBalance self) =>
    // Cleanup range
    if not na(self.range_box)
        self.range_box.delete()
        self.range_box := na
    // Cleanup region
    if not na(self.region_box)
        self.region_box.delete()
        self.region_box := na
    // Cleanup lines
    if not na(self.high_ext3_line)
        self.high_ext3_line.delete()
        self.high_ext3_line := na
    if not na(self.high_ext3_mid_line)
        self.high_ext3_mid_line.delete()
        self.high_ext3_mid_line := na
    if not na(self.high_ext2_line)
        self.high_ext2_line.delete()
        self.high_ext2_line := na
    if not na(self.high_ext2_mid_line)
        self.high_ext2_mid_line.delete()
        self.high_ext2_mid_line := na
    if not na(self.high_ext1_line)
        self.high_ext1_line.delete()
        self.high_ext1_line := na
    if not na(self.high_ext1_mid_line)
        self.high_ext1_mid_line.delete()
        self.high_ext1_mid_line := na
    if not na(self.high_line)
        self.high_line.delete()
        self.high_line := na
    if not na(self.mid_line)
        self.mid_line.delete()
        self.mid_line := na
    if not na(self.low_line)
        self.low_line.delete()
        self.low_line := na
    if not na(self.low_ext1_mid_line)
        self.low_ext1_mid_line.delete()
        self.low_ext1_mid_line := na
    if not na(self.low_ext1_line)
        self.low_ext1_line.delete()
        self.low_ext1_line := na
    if not na(self.low_ext2_mid_line)
        self.low_ext2_mid_line.delete()
        self.low_ext2_mid_line := na
    if not na(self.low_ext2_line)
        self.low_ext2_line.delete()
        self.low_ext2_line := na
    if not na(self.low_ext3_mid_line)
        self.low_ext3_mid_line.delete()
        self.low_ext3_mid_line := na
    if not na(self.low_ext3_line)
        self.low_ext3_line.delete()
        self.low_ext3_line := na
    // Cleanup labels
    self.cleanup_labels()


////////////////////////////////////////////////////////////////////////////////////////////////////
// Initial Balance
////////////////////////////////////////////////////////////////////////////////////////////////////
var initial_balances_list = array.new<InitialBalance>(i_show_num_days)

var string initial_balance_session = build_initial_balance_session(i_session, INITIAL_BALANCE_DURATION)
var bool show_initial_balance = (timeframe.in_seconds() <= (INITIAL_BALANCE_DURATION * 60)) and (((INITIAL_BALANCE_DURATION * 60) % timeframe.in_seconds()) == 0)
varip int cached_range_start_bar_index = na
varip int cached_range_end_bar_index = na

if show_initial_balance
    // Check for entering Initial Balance
    if na(time("", initial_balance_session, i_tz, 1)) and not na(time("", initial_balance_session, i_tz))
        // Just entered Initial Balance, truncate current_initial_balance if necessary
        current_initial_balance = initial_balances_list.get(-1)
        if not na(current_initial_balance) and current_initial_balance.range_end_bar_index >= bar_index
            cached_range_end_bar_index := bar_index - 1
            current_initial_balance.extend(cached_range_end_bar_index)
            current_initial_balance.cleanup_labels()
        cached_range_start_bar_index := bar_index
        new_initial_balance = InitialBalance.new()
        new_initial_balance.initialize(bar_index, high, low)
        dead_initial_balance = initial_balances_list.shift()
        initial_balances_list.push(new_initial_balance)
        if not na(dead_initial_balance)
            dead_initial_balance.cleanup()
    else if not na(time("", initial_balance_session, i_tz))
        // In Initial Balance
        current_initial_balance = initial_balances_list.get(-1)
        if not na(current_initial_balance)
            current_initial_balance.update_levels(bar_index, high, low)
    else if not na(time("", initial_balance_session, i_tz, 1)) and na(time("", initial_balance_session, i_tz))
        // Just left Initial Balance
        if barstate.isnew
            cached_range_end_bar_index := get_end_bar_index()
        current_initial_balance = initial_balances_list.get(-1)
        if not na(current_initial_balance)
            current_initial_balance.confirm_levels(cached_range_end_bar_index)
    else if not na(time("", i_session, i_tz))
        // Outside Initial Balance but still inside Session
        if barstate.isnew
            cached_range_end_bar_index := get_end_bar_index()
        current_initial_balance = initial_balances_list.get(-1)
        if not na(current_initial_balance)
            current_initial_balance.extend(cached_range_end_bar_index)
    else if not na(time("", i_session, i_tz, 1)) and na(time("", i_session, i_tz))
        // Just left Session
        current_initial_balance = initial_balances_list.get(-1)
        if not na(current_initial_balance) and i_show_labels
            current_initial_balance.cleanup_labels()

avwap = ta.vwap(ohlc4, na(time("", i_session, i_tz, 1)) and not na(time("", i_session, i_tz)))
plot(i_show_avwap and not na(time("", i_session, i_tz)) ? avwap : na, "IB AVWAP", i_avwap_color, 2, plot.style_linebr, display = display.pane)

Overview

LunarTick Initial Balance plots the high, low, and midpoint of the opening balance, with optional session extension levels, an anchored VWAP, and shaded highlight regions. Configurable settings allow adjustments to the session hours, timezone, historical days displayed, label styling, and line aesthetics. Levels update as the initial balance forms during active sessions.

Details

Published by

lunarticktrading on GitHub

Code credit

gambcl, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Sessions & time, Support & resistance

Markets

Indices, Stocks

Timeframes

M1-M5, M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Sessionsession0930-1600i_session
TimezonestringAmerica/New_Yorki_tz
Show Daysint5i_show_num_days
Show Extension LevelsenumShowExtensionLevels.nonei_show_ext_levels
Show Mid Levelsbooltruei_show_mid_levels
Highlight Initial Balance Regionbooltruei_highlight_initial_balance_region
i_initial_balance_region_colorcolorcolor.new(color.aqua, 67)i_initial_balance_region_color
Highlight Initial Balance Rangeboolfalsei_highlight_initial_balance_range
i_initial_balance_range_colorcolorcolor.new(color.aqua, 67)i_initial_balance_range_color
Initial Balance Highcolorcolor.greeni_initial_balance_h_color
i_initial_balance_h_styleenumLineStyle.solidi_initial_balance_h_style
i_initial_balance_h_widthint2i_initial_balance_h_width
Show all 27 settings
SettingTypeDefaultVariable
Initial Balance Midcolorcolor.orangei_initial_balance_m_color
i_initial_balance_m_styleenumLineStyle.dashedi_initial_balance_m_style
i_initial_balance_m_widthint1i_initial_balance_m_width
Initial Balance Lowcolorcolor.redi_initial_balance_l_color
i_initial_balance_l_styleenumLineStyle.solidi_initial_balance_l_style
i_initial_balance_l_widthint2i_initial_balance_l_width
Show AVWAPboolfalsei_show_avwap
i_avwap_colorcolorcolor.aquai_avwap_color
Show Labelsbooltruei_show_labels
Label Offsetint1i_label_offset
Label StyleenumLabelStyle.label_lefti_label_style
Label SizeenumLabelSize.normali_label_size
Label Transparencyint0i_label_bg_transparency
Label Text Colorcolorcolor.whitei_label_text_color
VersionenumVersionEnum.versioni_version
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

575 stars

This indicator builds Market Profile Time Price Opportunity (TPO) structures across defined session timeframes directly on the main price chart. It visualizes the Point of Control (POC) and Value Area boundaries using dynamic box drawings. An integrated multi-

Indicator v5 Support & resistanceScreeners & dashboards
Updated 1 year ago · MPL-2.0 GitHub repository of Market Profile with Screener
257 stars

This indicator plots auto trade signals for E-mini S&P 500 futures on the price chart. It computes an EMA200 filter along with day ETH RTH session levels and percentage-based entry zones. Signals trigger CALL or PUT cases when price crosses defined thresholds

Indicator v6 TrendSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of Auto Trade Futures ES
219 stars

This indicator plots reference levels for previous daily high and low prices directly on the price chart. It requests daily timeframe data using the security function and plots the offset values as continuous lines, displaying resistance in red and support in

Indicator v4 Support & resistanceSessions & time
Updated 5 years ago · None stated GitHub repository of 昨日高低點標示

Golden Bullet Strategy

by TradersPost
218 stars

The Golden Bullet Strategy is an overlay signal indicator based on Smart Money Concepts and time-based liquidity models. It identifies liquidity sweeps of a designated reference session range during a specified hunt window. It then confirms Market Structure Sh

Indicator v6 SMC / ICT / price actionSessions & time
Updated 3 months ago · None stated GitHub repository of Golden Bullet Strategy

Moon Dev GEX Arrows

by moondevonyt
92 stars

This overlay indicator plots option strike walls and an optional session VWAP on the price chart for SPY or ES. It spaces major strikes by the selected instrument, draws a set number of strikes on each side, and marks wall-trap and sweep events after a cooldow

Indicator v5 Support & resistanceSessions & time
Updated 8 weeks ago · Custom licence GitHub repository of Moon Dev GEX Arrows

Key Levels

by cryptorife
75 stars

This indicator plots reference levels and session markers directly on the price chart. It calculates and draws lines for the daily open, weekly open, monthly open, previous day OHLC, and previous week extremes, along with a selectable day-of-week range and ini

Indicator v4 Support & resistanceSessions & time Repaints
Updated 3 years ago · GPL-3.0 GitHub repository of Key Levels

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers