PROTECTED SOURCE SCRIPT

Delta(samwong)

33
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
// © pdtrkm

//version=5
indicator("K线Delta显示(带总和)", overlay=true)

// 输入参数
show_labels = input.bool(true, title="在K线上显示Delta值")
show_cumulative = input.bool(true, title="显示累计Delta")
period_length = input.int(24, title="计算周期", minval=1)

// 数值格式化函数 - 将大数字转换为K/M/B单位
format_number(value) =>
abs_value = math.abs(value)
if abs_value >= 1000000000
str.tostring(value / 1000000000, "#.##") + "B"
else if abs_value >= 1000000
str.tostring(value / 1000000, "#.##") + "M"
else if abs_value >= 1000
str.tostring(value / 1000, "#.##") + "K"
else
str.tostring(value, "#.##")

// 基于价格位置的Delta计算
price_based_delta() =>
float delta = 0.0
body_mid = (open + close) / 2
range_high_low = high - low

if range_high_low > 0
// 计算价格在区间中的相对位置
position_ratio = (body_mid - low) / range_high_low
// 转换为-1到1的范围
normalized_position = (position_ratio - 0.5) * 2
delta := normalized_position * volume
else
delta := 0
delta

// 当前K线的Delta值
current_delta = price_based_delta()

// 计算指定周期内的Delta总和
cumulative_delta = math.sum(current_delta, period_length)

// 在每根K线下方显示简化后的Delta值
if show_labels and barstate.isconfirmed
// 确定标签位置(在K线下方)
label_y = low - (high - low) * 0.05

// 确定标签颜色和符号
delta_color = current_delta > 0 ? color.green : current_delta < 0 ? color.red : color.gray
delta_symbol = current_delta > 0 ? "▲" : current_delta < 0 ? "▼" : "●"

// 创建标签 - 使用简化单位
label.new(
bar_index,
label_y,
delta_symbol + format_number(current_delta),
color=delta_color,
textcolor=color.white,
style=label.style_label_center,
size=size.small,
yloc=yloc.price)

// 显示累计Delta值
if show_cumulative and barstate.isconfirmed
// 确定标签位置(在K线上方)
cumulative_y = high + (high - low) * 0.05

// 确定累计Delta颜色
cumulative_color = cumulative_delta > 0 ? color.green : cumulative_delta < 0 ? color.red : color.gray

// 创建累计Delta标签
label.new(
bar_index,
cumulative_y,
"∑: " + format_number(cumulative_delta),
color=cumulative_color,
textcolor=color.white,
style=label.style_label_center,
size=size.small,
yloc=yloc.price)

// 显示汇总信息表格
if barstate.islast
var table info_table = table.new(position.top_right, 2, 4,
bgcolor=color.new(color.white, 80),
border_width=1)

// 确定整体趋势颜色
trend_color = cumulative_delta > 0 ? color.green : cumulative_delta < 0 ? color.red : color.gray
trend_text = cumulative_delta > 0 ? "净买入" : cumulative_delta < 0 ? "净卖出" : "平衡"

table.cell(info_table, 0, 0, "Delta指标",
text_color=color.black, text_size=size.normal, width=8)
table.cell(info_table, 1, 0, "数值",
text_color=color.black, text_size=size.normal)

table.cell(info_table, 0, 1, "当前K线",
text_color=color.black, text_size=size.small)
table.cell(info_table, 1, 1, format_number(current_delta),
text_color=current_delta > 0 ? color.green : color.red, text_size=size.small)

table.cell(info_table, 0, 2, str.format("近{0}根总和", period_length),
text_color=color.black, text_size=size.small)
table.cell(info_table, 1, 2, format_number(cumulative_delta),
text_color=trend_color, text_size=size.small)

table.cell(info_table, 0, 3, "市场方向",
text_color=color.black, text_size=size.small)
table.cell(info_table, 1, 3, trend_text,
text_color=trend_color, text_size=size.small)

// 在图表上绘制累计Delta线
cumulative_line = ta.cum(current_delta)
plot(show_cumulative ? cumulative_line : na, "累计Delta", color=color.orange, linewidth=2)

Haftungsausschluss

Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.