本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// © jan80hansen
//@version=6
indicator("Adaptive TPO & Single Prints", overlay=true, max_boxes_count=500, max_lines_count=100)
// -----------------------------------------------------------------------------
// 1. INTERACTIVE INPUTS (CLICK & DRAG)
// -----------------------------------------------------------------------------
gp_time = "Select Area on Chart (Click & Drag)"
start_time_input = input.time(timestamp("2026-01-01 00:00"), title="Start Point", confirm=true, group=gp_time)
end_time_input = input.time(timestamp("2026-01-02 00:00"), title="End Point", confirm=true, group=gp_time)
gp_set = "TPO Profile Settings"
bin_count = input.int(50, title="Number of Rows", minval=10, maxval=150, group=gp_set, tooltip="Lower number = thicker blocks.")
hist_width = input.int(30, title="Profile Width (%)", minval=5, maxval=100, group=gp_set)
row_spacing = input.int(15, title="Row Spacing Margin (%)", minval=0, maxval=40, group=gp_set, tooltip="Creates the visual gap between bars.")
gp_va = "Value Area Settings"
show_va = input.bool(true, title="Show Value Area (VAH / VAL)", group=gp_va)
va_pct = input.float(70.0, title="Value Area TPO %", minval=10, maxval=100, group=gp_va)
gp_sp = "Single Prints Settings"
show_sp = input.bool(true, title="Highlight Single Prints", group=gp_sp)
gp_col = "Colors"
col_tpo = input.color(color.new(#2962ff, 50), title="TPO Block Color", group=gp_col)
col_poc = input.color(color.new(color.yellow, 0), title="POC Line", group=gp_col)
col_va = input.color(color.new(color.blue, 0), title="VAH / VAL Lines", group=gp_col)
col_sp = input.color(color.new(color.fuchsia, 85), title="Single Print Zone", group=gp_col)
col_mid = input.color(color.new(color.gray, 0), title="TPO Midpoint Line", group=gp_col)
// -----------------------------------------------------------------------------
// 2. TRUE TIME ARRAYS (PRICE ACTION ONLY)
// -----------------------------------------------------------------------------
var float[] range_h = array.new_float()
var float[] range_l = array.new_float()
var float zone_high = na
var float zone_low = na
var int start_bar_index = na
var int end_bar_index = na
bool in_range = time >= start_time_input and time <= end_time_input
if in_range
array.push(range_h, high)
array.push(range_l, low)
zone_high := na(zone_high) ? high : math.max(zone_high, high)
zone_low := na(zone_low) ? low : math.min(zone_low, low)
start_bar_index := na(start_bar_index) ? bar_index : start_bar_index
end_bar_index := bar_index
var box[] hist_boxes = array.new_box()
var box[] sp_boxes = array.new_box()
var line poc_line = na
var label poc_label = na
var line vah_line = na
var label vah_label = na
var line val_line = na
var label val_label = na
var line mid_line = na
var label mid_label = na
// -----------------------------------------------------------------------------
// 3. GENERATE TPO PROFILE ON LAST BAR
// -----------------------------------------------------------------------------
if barstate.islast
if array.size(hist_boxes) > 0
for i = 0 to array.size(hist_boxes) - 1
box.delete(array.get(hist_boxes, i))
array.clear(hist_boxes)
if array.size(sp_boxes) > 0
for i = 0 to array.size(sp_boxes) - 1
box.delete(array.get(sp_boxes, i))
array.clear(sp_boxes)
if not na(poc_line)
line.delete(poc_line)
if not na(poc_label)
label.delete(poc_label)
if not na(vah_line)
line.delete(vah_line)
if not na(vah_label)
label.delete(vah_label)
if not na(val_line)
line.delete(val_line)
if not na(val_label)
label.delete(val_label)
if not na(mid_line)
line.delete(mid_line)
if not na(mid_label)
label.delete(mid_label)
int arr_size = array.size(range_h)
if arr_size > 0 and not na(zone_high) and not na(zone_low)
a_tpo = array.new_float(bin_count, 0)
float bin_size = (zone_high - zone_low) / bin_count
if bin_size == 0
bin_size := syminfo.mintick
float max_tpo_in_any_bin = 0.0
float total_tpo_count = 0.0
// KONSENTRERER TID (TPO) FOR HVER CANDLE I OMRÅDET
for i = 0 to arr_size - 1
float c_h = array.get(range_h, i)
float c_l = array.get(range_l, i)
if c_h == c_l
c_h := c_h + syminfo.mintick
int idx_bot = math.floor((c_l - zone_low) / bin_size)
int idx_top = math.floor((c_h - zone_low) / bin_size)
idx_bot := math.max(0, math.min(bin_count - 1, idx_bot))
idx_top := math.max(0, math.min(bin_count - 1, idx_top))
// Hver prisrad som ble truffet av denne candlen får 1 poeng (1 blokk)
for k = idx_bot to idx_top
array.set(a_tpo, k, array.get(a_tpo, k) + 1.0)
float poc_tpo = 0.0
int poc_idx = 0
for j = 0 to bin_count - 1
float bin_tpo = array.get(a_tpo, j)
total_tpo_count += bin_tpo
if bin_tpo > max_tpo_in_any_bin
max_tpo_in_any_bin := bin_tpo
if bin_tpo > poc_tpo
poc_tpo := bin_tpo
poc_idx := j
// 4. VALUE AREA ALGORITHM (70% av TPO blokker)
float target_va_tpo = total_tpo_count * (va_pct / 100.0)
float current_va_tpo = array.get(a_tpo, poc_idx)
int high_idx = poc_idx
int low_idx = poc_idx
while current_va_tpo < target_va_tpo
float tpo_above = (high_idx < bin_count - 1) ? array.get(a_tpo, high_idx + 1) : -1.0
float tpo_below = (low_idx > 0) ? array.get(a_tpo, low_idx - 1) : -1.0
if tpo_above == -1.0 and tpo_below == -1.0
break
if tpo_above >= tpo_below
current_va_tpo += tpo_above
high_idx += 1
else
current_va_tpo += tpo_below
low_idx -= 1
// 5. TEGN SINGLE PRINTS (Nivåer med kun 1 TPO blokk)
if show_sp
bool in_sp = false
float current_sp_bot = na
float current_sp_top = na
for j = 0 to bin_count - 1
float bin_tpo = array.get(a_tpo, j)
// Erklærer Single Print hvis kun 1 TPO-blokk eksisterer her
bool is_sp = bin_tpo == 1.0
if is_sp
if not in_sp
in_sp := true
current_sp_bot := zone_low + (j * bin_size)
current_sp_top := zone_low + ((j + 1) * bin_size)
else
if in_sp
box sp_b = box.new(left=start_bar_index, top=current_sp_top, right=end_bar_index, bottom=current_sp_bot, bgcolor=col_sp, border_width=0, text="Single Print", text_color=color.new(color.white, 30), text_halign=text.align_center, text_valign=text.align_center, text_size=size.small, extend=extend.right)
array.push(sp_boxes, sp_b)
in_sp := false
if in_sp
box sp_b = box.new(left=start_bar_index, top=current_sp_top, right=end_bar_index, bottom=current_sp_bot, bgcolor=col_sp, border_width=0, text="Single Print", text_color=color.new(color.white, 30), text_halign=text.align_center, text_valign=text.align_center, text_size=size.small, extend=extend.right)
array.push(sp_boxes, sp_b)
// 6. TEGN DET VISUELLE TPO HISTOGRAMMET MED MARGIN (SPACING)
int bars_in_range = end_bar_index - start_bar_index
float width_multiplier = (bars_in_range * (hist_width / 100.0)) / (max_tpo_in_any_bin == 0 ? 1 : max_tpo_in_any_bin)
float margin = bin_size * (row_spacing / 100.0)
for j = 0 to bin_count - 1
float b_tpo = array.get(a_tpo, j)
if b_tpo > 0
float bin_bottom = zone_low + (j * bin_size)
float bin_top = zone_low + ((j + 1) * bin_size)
float box_top = bin_top - margin
float box_bot = bin_bottom + margin
if box_top <= box_bot
box_top := bin_top
box_bot := bin_bottom
int end_box = start_bar_index + math.round(b_tpo * width_multiplier)
box bb = box.new(left=start_bar_index, top=box_top, right=end_box, bottom=box_bot, bgcolor=col_tpo, border_width=0)
array.push(hist_boxes, bb)
// 7. TEGN POC, VAH, VAL OG MIDPOINT
float poc_price = zone_low + (poc_idx * bin_size) + (bin_size / 2)
poc_line := line.new(x1=start_bar_index, y1=poc_price, x2=end_bar_index, y2=poc_price, color=col_poc, width=2, style=line.style_solid)
string poc_txt = "TPO POC: " + str.tostring(poc_price, format.mintick)
poc_label := label.new(x=end_bar_index, y=poc_price, text=poc_txt, color=color(na), textcolor=col_poc, style=label.style_label_left, size=size.small)
if show_va
float vah_price = zone_low + (high_idx * bin_size) + bin_size
float val_price = zone_low + (low_idx * bin_size)
vah_line := line.new(x1=start_bar_index, y1=vah_price, x2=end_bar_index, y2=vah_price, color=col_va, width=1, style=line.style_dashed)
string vah_txt = "VAH: " + str.tostring(vah_price, format.mintick)
vah_label := label.new(x=end_bar_index, y=vah_price, text=vah_txt, color=color(na), textcolor=col_va, style=label.style_label_left, size=size.small)
val_line := line.new(x1=start_bar_index, y1=val_price, x2=end_bar_index, y2=val_price, color=col_va, width=1, style=line.style_dashed)
string val_txt = "VAL: " + str.tostring(val_price, format.mintick)
val_label := label.new(x=end_bar_index, y=val_price, text=val_txt, color=color(na), textcolor=col_va, style=label.style_label_left, size=size.small)
// Beregner og tegner TPO Midpoint
float midpoint_price = (zone_high + zone_low) / 2
mid_line := line.new(x1=start_bar_index, y1=midpoint_price, x2=end_bar_index, y2=midpoint_price, color=col_mid, width=1, style=line.style_dotted)
string mid_txt = "Midpoint: " + str.tostring(midpoint_price, format.mintick)
mid_label := label.new(x=end_bar_index, y=midpoint_price, text=mid_txt, color=color(na), textcolor=col_mid, style=label.style_label_left, size=size.small)
plot(close, title="Reference", display=display.none)