You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TradingView Pine Script修改:手动绘制SpotGamma五类水平价格线

TradingView Pine Script:自定义SpotGamma水平价格线优化方案

以下是满足你需求的优化版脚本,实现了可修改线型、线条无限延伸、无冒号标签的功能:

//@version=5
indicator("SpotGamma 水平价格线", shorttitle="SpotGamma Levels", overlay=true)

// --- KEY GAMMA 设置 ---
key_gamma_price = input.float(1, title="KEY GAMMA", tooltip="手动输入KEY GAMMA价格水平")
key_gamma_color = input.color(#007bff, title="KEY GAMMA 颜色")
key_gamma_linewidth = input.int(2, title="KEY GAMMA 线宽", minval=1, maxval=5)
key_gamma_style = input.string("solid", title="KEY GAMMA 线型", options=["solid", "dashed", "dotted", "dashed_dotted"])

// --- KEY DELTA 设置 ---
key_delta_price = input.float(1, title="KEY DELTA", tooltip="手动输入KEY DELTA价格水平")
key_delta_color = input.color(#f2ff00, title="KEY DELTA 颜色")
key_delta_linewidth = input.int(2, title="KEY DELTA 线宽", minval=1, maxval=5)
key_delta_style = input.string("solid", title="KEY DELTA 线型", options=["solid", "dashed", "dotted", "dashed_dotted"])

// --- HEDGE WALL 设置 ---
hedge_wall_price = input.float(1, title="HEDGE WALL", tooltip="手动输入HEDGE WALL价格水平")
hedge_wall_color = input.color(#ff00a2, title="HEDGE WALL 颜色")
hedge_wall_linewidth = input.int(2, title="HEDGE WALL 线宽", minval=1, maxval=5)
hedge_wall_style = input.string("solid", title="HEDGE WALL 线型", options=["solid", "dashed", "dotted", "dashed_dotted"])

// --- CALL WALL 设置 ---
call_wall_price = input.float(1, title="CALL WALL", tooltip="手动输入CALL WALL价格水平")
call_wall_color = input.color(#00ff08, title="CALL WALL 颜色")
call_wall_linewidth = input.int(2, title="CALL WALL 线宽", minval=1, maxval=5)
call_wall_style = input.string("solid", title="CALL WALL 线型", options=["solid", "dashed", "dotted", "dashed_dotted"])

// --- PUT WALL 设置 ---
put_wall_price = input.float(1, title="PUT WALL", tooltip="手动输入PUT WALL价格水平")
put_wall_color = input.color(#ff0000, title="PUT WALL 颜色")
put_wall_linewidth = input.int(2, title="PUT WALL 线宽", minval=1, maxval=5)
put_wall_style = input.string("solid", title="PUT WALL 线型", options=["solid", "dashed", "dotted", "dashed_dotted"])

// --- 绘制水平线(无限延伸)---
hline(key_gamma_price, title="KEY GAMMA", color=key_gamma_color, linewidth=key_gamma_linewidth, style=key_gamma_style)
hline(key_delta_price, title="KEY DELTA", color=key_delta_color, linewidth=key_delta_linewidth, style=key_delta_style)
hline(hedge_wall_price, title="HEDGE WALL", color=hedge_wall_color, linewidth=hedge_wall_linewidth, style=hedge_wall_style)
hline(call_wall_price, title="CALL WALL", color=call_wall_color, linewidth=call_wall_linewidth, style=call_wall_style)
hline(put_wall_price, title="PUT WALL", color=put_wall_color, linewidth=put_wall_linewidth, style=put_wall_style)

// --- 绘制无冒号标签(可选,可通过注释此段关闭)---
var label kg_label = label.new(bar_index, key_gamma_price, "KEY GAMMA " + str.tostring(key_gamma_price, "#.##"), color=key_gamma_color, textcolor=color.white, style=label.style_label_left, tooltip="KEY GAMMA 价格水平")
var label kd_label = label.new(bar_index, key_delta_price, "KEY DELTA " + str.tostring(key_delta_price, "#.##"), color=key_delta_color, textcolor=color.black, style=label.style_label_left, tooltip="KEY DELTA 价格水平")
var label hw_label = label.new(bar_index, hedge_wall_price, "HEDGE WALL " + str.tostring(hedge_wall_price, "#.##"), color=hedge_wall_color, textcolor=color.white, style=label.style_label_left, tooltip="HEDGE WALL 价格水平")
var label cw_label = label.new(bar_index, call_wall_price, "CALL WALL " + str.tostring(call_wall_price, "#.##"), color=call_wall_color, textcolor=color.black, style=label.style_label_left, tooltip="CALL WALL 价格水平")
var label pw_label = label.new(bar_index, put_wall_price, "PUT WALL " + str.tostring(put_wall_price, "#.##"), color=put_wall_color, textcolor=color.white, style=label.style_label_left, tooltip="PUT WALL 价格水平")

// 更新标签位置到最新K线
label.set_xy(kg_label, bar_index, key_gamma_price)
label.set_xy(kd_label, bar_index, key_delta_price)
label.set_xy(hw_label, bar_index, hedge_wall_price)
label.set_xy(cw_label, bar_index, call_wall_price)
label.set_xy(pw_label, bar_index, put_wall_price)

// 更新标签文本(价格变化时同步更新)
label.set_text(kg_label, "KEY GAMMA " + str.tostring(key_gamma_price, "#.##"))
label.set_text(kd_label, "KEY DELTA " + str.tostring(key_delta_price, "#.##"))
label.set_text(hw_label, "HEDGE WALL " + str.tostring(hedge_wall_price, "#.##"))
label.set_text(cw_label, "CALL WALL " + str.tostring(call_wall_price, "#.##"))
label.set_text(pw_label, "PUT WALL " + str.tostring(put_wall_price, "#.##"))

优化点说明

  1. 线条无限延伸:使用Pine Script v5的hline()函数替代原脚本的plot(),该函数绘制的水平线默认贯穿整个图表区域,完全满足无限延伸需求。
  2. 可自定义线型:为每个价格线添加了线型选择输入,支持solid(实线)、dashed(虚线)、dotted(点线)、dashed_dotted(点虚线)四种样式,可在设置面板调整。
  3. 移除标签冒号:自定义标签文本格式,将原格式中的冒号(如KEY GAMMA: 123)改为空格分隔(KEY GAMMA 123),符合你的需求。如果不需要标签,直接注释掉标签相关代码即可。
  4. 额外优化:增加了颜色、线宽的自定义选项,让脚本更灵活;标签会自动跟随最新K线移动,价格修改时同步更新文本。

内容的提问来源于stack exchange,提问作者Asmodeus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 22:42:06