Imbalance Zone Script Summary

Создано: 9 января 2025 г.

Отвечено с использованием OpenAI 01 от OpenAI01

Вопрос

//@version=6
indicator('Imbalanced zone', overlay=true)

// Inputs
tf = input.timeframe('', 'TimeFrame')
penetrationRatio = input.float(title='Penetration Ratio', defval=0.2, minval=0, maxval=1, step=0.1)
reduce = input.bool(true, 'Reduce boxes')
extendBoxes = input.bool(true, 'Extend boxes')
imbalancedDownColor = input.color(color.rgb(120, 120, 120, 80), 'Down Box Color')
imbalancedDownMidLineColor = input.color(color.rgb(120, 120, 120), 'Down Mid Line Color')
imbalancedUpColor = input.color(color.rgb(120, 120, 120, 80), 'Up Box Color')
imbalancedUpMidLineColor = input.color(color.rgb(120, 120, 120), 'Up Mid Line Color')

showDailyImbalance = input.bool(false, 'Show Daily Imbalance Zone')
dailyImbalanceColor = input.color(color.rgb(252, 227, 0, 90), 'Daily Imbalance Zone Color')
dailyImbalanceHistory = input.int(5, 'Daily Imbalance Zone History')

showWeeklyImbalance = input.bool(false, 'Show Weekly Imbalance Zone')
weeklyImbalanceColor = input.color(color.rgb(120, 0, 0, 90), 'Weekly Imbalance Zone Color')
weeklyImbalanceHistory = input.int(4, 'Weekly Imbalance Zone History')

tfVi = input.timeframe('', 'Volume Imbalance TimeFrame')
showVolumeImbalance = input.bool(true, 'Show Volume Imbalance')
volumeImbalanceColor = input.color(color.rgb(33, 149, 243, 85), 'Volume Imbalance Color')

maxBarHistory = input.int(500, 'Max IPA Age')

// Arrays
var array<box> imbalancedDownBoxes = array.new_box()
var array<box> imbalancedUpBoxes = array.new_box()
var array<line> imbalancedDownMidLines = array.new_line()
var array<line> imbalancedUpMidLines = array.new_line()
var array<box> volumeImbalances = array.new_box()
var array<box> dailyImbalanceZones = array.new_box()
var array<box> weeklyImbalanceZones = array.new_box()

// Functions
findImbalance(h1, h3, l1, l3) =>
if h3 < l1 and l3 != nz(l3[1])
array.push(imbalancedDownBoxes, box.new(bar_index - 2, h3, bar_index + 20, l1, bgcolor=imbalancedDownColor, border_color=imbalancedDownColor, extend=extendBoxes ? extend.right : extend.none))
array.push(imbalancedDownMidLines, line.new(bar_index - 2, (h3 + l1) / 2, bar_index + 20, (h3 + l1) / 2, style=line.style_dotted, color=imbalancedDownMidLineColor, extend=extendBoxes ? extend.right : extend.none))
if l3 > h1 and h3 != nz(h3[1])
array.push(imbalancedUpBoxes, box.new(bar_index - 2, l3, bar_index + 20, h1, bgcolor=imbalancedUpColor, border_color=imbalancedUpColor, extend=extendBoxes ? extend.right : extend.none))
array.push(imbalancedUpMidLines, line.new(bar_index - 2, (h1 + l3) / 2, bar_index + 20, (h1 + l3) / 2, style=line.style_dotted, color=imbalancedUpMidLineColor, extend=extendBoxes ? extend.right : extend.none))

manageBoxes(boxes, midLines, isUp) =>
for i = array.size(boxes) - 1 to 0 by -1
box = array.get(boxes, i)
if box.exists()
top = box.get_top()
bottom = box.get_bottom()
invalidationLimit = (top - bottom) * penetrationRatio
box.set_right(bar_index + 20)
if i < array.size(midLines)
line = array.get(midLines, i)
if line.exists()
line.set_x2(bar_index + 20)
if (bar_index - box.get_left() > maxBarHistory)
box.delete(box)
if i < array.size(midLines)
line = array.get(midLines, i)
if line.exists()
line.delete(line)
array.remove(boxes, i)
array.remove(midLines, i)
else
if reduce and (isUp ? high > bottom : low < top)
box.set_bottom(isUp ? high : low)
if (isUp ? high >= bottom + invalidationLimit : low <= top - invalidationLimit)
box.delete(box)
if i < array.size(midLines)
line = array.get(midLines, i)
if line.exists()
line.delete(line)
array.remove(boxes, i)
array.remove(midLines, i)

// Main logic
[h1, h3, l1, l3] = request.security(syminfo.tickerid, tf, [high[1], high[3], low[1], low[3]], lookahead=barmerge.lookahead_on)
findImbalance(h1, h3, l1, l3)
manageBoxes(imbalancedUpBoxes, imbalancedUpMidLines, true)
manageBoxes(imbalancedDownBoxes, imbalancedDownMidLines, false)

// Daily Imbalance Zone
if showDailyImbalance and timeframe.isdaily
[o, c, t, tc] = request.security(syminfo.tickerid, '1D', [open, close, time, time_close], lookahead=barmerge.lookahead_on)
if o != c
array.push(dailyImbalanceZones, box.new(t, math.max(o, c), tc, math.min(o, c), xloc=xloc.bar_time, bgcolor=dailyImbalanceColor, border_color=color.rgb(0, 0, 0, 100)))
if array.size(dailyImbalanceZones) > dailyImbalanceHistory
box.delete(array.shift(dailyImbalanceZones))

// Weekly Imbalance Zone
if showWeeklyImbalance and timeframe.isweekly
[wo, wc, wt, wtc] = request.security(syminfo.tickerid, '1W', [open, close, time, time_close], lookahead=barmerge.lookahead_on)
if wo != wc
array.push(weeklyImbalanceZones, box.new(wt, math.max(wo, wc), wtc, math.min(wo, wc), xloc=xloc.bar_time, bgcolor=weeklyImbalanceColor, border_color=color.rgb(0, 0, 0, 100)))
if array.size(weeklyImbalanceZones) > weeklyImbalanceHistory
box.delete(array.shift(weeklyImbalanceZones))

// Volume Imbalance
if showVolumeImbalance
[o1, c1, o2, c2, viTime] = request.security(syminfo.tickerid, tfVi, [open[1], close[1], open[2], close[2], time[2]], lookahead=barmerge.lookahead_on)
volumeGap = o2 < c2 ? c2 < o1 and c2 < c1 : c2 > o1 and c2 > c1
if not volumeGap
volumeGap := math.min(o2, c2) > math.max(o1, c1) or math.max(o2, c2) < math.min(o1, c1)
if volumeGap
bottom = math.min(o1, c1) > math.max(o2, c2) ? math.max(o2, c2) : math.max(o1, c1)
top = math.min(o1, c1) > math.max(o2, c2) ? math.min(o1, c1) : math.min(o2, c2)
array.push(volumeImbalances, box.new(viTime, top, viTime + 20 * 60 * 1000, bottom, xloc=xloc.bar_time, bgcolor=volumeImbalanceColor, border_color=color.rgb(0, 0, 0, 100)))
for i = array.size(volumeImbalances) - 1 to 0 by -1
viBox = array.get(volumeImbalances, i)
if viBox.exists()
if low <= box.get_bottom(viBox) and high >= box.get_top(viBox)
box.delete(viBox)
array.remove(volumeImbalances, i)
else
box.set_right(viBox, time_close + 20 * 60 * 1000)

Мысль

Assessing the code

The user has uploaded the code snippet for a Pine Script drawing imbalanced zones. No explicit questions are present, but it suggests an implicit request for explanation or enhancements.

Pondering possibilities

The user’s code snippet might indicate a need for clarification. It’s worth asking if an explanation or specific guidance is sought, or if further clarification is needed for progress.

Breaking down the code

I’ve examined the Pine Script code and found no errors or questions. It effectively manages daily, weekly, or volume imbalances, potentially requiring a thorough explanation of its mechanics.

Pulling together

I'm organizing a detailed summary that explains the script’s purpose, identifies its features like creating imbalance zones for various timeframes, and tackles potential issues such as referencing box.delete(box).

Thought about imbalance zone indicator pendant 15 secondes

Ответ

Below is an overview of how this script is structured and what it does. If you have follow-up questions or need modifications, just let me know!


1. Overview

This script (written in Pine Script, version 6) plots various types of “imbalances” (often called “fair value gaps”) on a TradingView chart. It also has the option to highlight daily/weekly price imbalances and a “volume imbalance” based on multi-timeframe data.

In short, the script:

  1. Identifies price imbalances on a user-specified timeframe (tf), drawing boxes and dotted mid-lines for each imbalance.
  2. Optionally shows daily and/or weekly imbalance zones using higher-timeframe data.
  3. Optionally shows volume imbalances by comparing price gaps in the selected volume imbalance timeframe (tfVi).

You can enable or disable each part using the inputs at the top of the script.


2. Key Input Parameters

  1. TimeFrame (tf)

    • The user-defined timeframe for identifying price imbalances.
    • If left blank, it defaults to the chart’s timeframe.
  2. Penetration Ratio (penetrationRatio)

    • Determines how far price has to penetrate into a box before that box is invalidated and removed.
  3. Reduce boxes (reduce)

    • If set to true, the script will “shrink” a box (the imbalance zone) when price partially fills or penetrates it.
  4. Extend boxes (extendBoxes)

    • If true, newly created boxes and lines will continue extending into the future until price invalidates them or the max bar age is reached.
  5. Down/Up Box Colors & mid-line colors

    • Control the visuals (color, transparency) of imbalanced zones.
  6. Daily/Weekly Imbalance toggles

    • You can enable or disable drawing daily/weekly imbalance zones.
    • Change the color, and how many historical zones to keep on the chart.
  7. Volume Imbalance

    • Toggle on/off to show a separate type of imbalance (volume-based).
    • Color and timeframe (tfVi) can be chosen separately from the primary price imbalance timeframe.
  8. Max IPA Age (maxBarHistory)

    • How many bars back an imbalance box can remain on the chart before it is automatically removed.

3. How the Script Works

A. Finding Price Imbalances

  1. Data Request

    [h1, h3, l1, l3] = request.security(syminfo.tickerid, tf, [high[1], high[3], low[1], low[3]], ...)
    
    • Pulls high and low values from one bar ago ([1]) and three bars ago ([3]) on the specified timeframe tf.
    • lookahead=barmerge.lookahead_on allows the script to capture data without repaints, though it can behave differently depending on your preference.
  2. Logic

    • If h3 < l1 then it’s considered a “down imbalance” (price jumped down, leaving a gap).
    • If l3 > h1 then it’s considered an “up imbalance” (price jumped up, leaving a gap).
  3. Creating Boxes & Mid-lines

    • Whenever the script identifies an imbalance, it creates a box (box.new(...)) that spans from bar_index - 2 to bar_index + 20, and also creates a dotted mid-line showing the midpoint of the gap.
    • These boxes and lines are stored in arrays (imbalancedDownBoxes, imbalancedUpBoxes, etc.) so they can be managed later.

B. Managing Boxes

The manageBoxes() function takes care of pruning or adjusting existing imbalance boxes:

  1. Age Out

    • If the box has been on the chart longer than maxBarHistory, it’s deleted.
  2. Reduction

    • If price partially penetrates a box and reduce = true, the script shrinks the box by adjusting its top or bottom boundary.
  3. Invalidation

    • If price has penetrated into the box at least as far as penetrationRatio times the box’s height, the box is considered “filled” and is removed.

This keeps the chart from accumulating old imbalance boxes indefinitely.


C. Daily Imbalance Zones

  1. Check if daily timeframe

    if showDailyImbalance and timeframe.isdaily
    
    • Only plots daily imbalance zones on a daily chart (or if your chart’s timeframe is set to 1D).
  2. Creates boxes for daily candle bodies

    [o, c, t, tc] = request.security(syminfo.tickerid, '1D', [open, close, time, time_close], ...)
    
    • Each daily bar that shows a difference between open and close is considered an imbalance zone (in the sense of a candle body).
    • A box is drawn from the daily candle’s open to close (or close to open) with the specified color.
    • The script deletes the oldest boxes beyond the dailyImbalanceHistory limit.

D. Weekly Imbalance Zones

Same idea, but for weekly data:

if showWeeklyImbalance and timeframe.isweekly
    [wo, wc, wt, wtc] = request.security(syminfo.tickerid, '1W', ...)

A box is drawn for each weekly candle body, with a user-defined color and history limit.


E. Volume Imbalances

  1. Data Request

    [o1, c1, o2, c2, viTime] = request.security(...)
    
    • Gathers open/close data from the selected volume imbalance timeframe (tfVi), a bar or two bars ago.
  2. Volume Gap Detection

    • The logic checks if there’s a gap between the older candle (o1, c1) and the newer candle (o2, c2).
    • If so, it creates another box that extends 20 bars into the future (though here it’s using time in milliseconds: time + 20 * 60 * 1000 for a 20-minute extension).
  3. Pruning Volume Imbalance Boxes

    • If the newest bar’s high/low fully covers the box, that box is removed.

4. Tips & Usage

  • Adding to Your Chart

    1. Copy/paste the script into TradingView’s Pine Editor.
    2. Save it and click “Add to Chart.”
  • Customizing

    • Adjust the various input parameters in the “Settings” dialog (gear icon). This lets you change timeframes, color transparency, etc.
  • Performance Note

    • Because this script uses multiple request.security() calls and draws multiple boxes, it can be somewhat heavy on resources if you enable everything or use a large maxBarHistory. Reduce the history or disable unneeded features to lighten it.
  • Potential Edge Cases

    1. Very illiquid or low-volume symbols might produce frequent or spurious “volume gaps.”
    2. “lookahead=barmerge.lookahead_on” can behave differently than barmerge.lookahead_off if you’re trying to avoid repainting.

5. Conclusion

This script identifies and manages multiple types of imbalances (regular “price” FVGs, candle-body “imbalances” for daily/weekly, and volume-based imbalances). Boxes are dynamically extended, reduced, or removed based on price action and user settings.

If you have specific questions (e.g., debugging issues, repainting concerns, or ideas for further customization), feel free to ask!

Поделиться этим Q&A