pine-optimizer by traderspost/pinescript-agents
npx skills add https://github.com/traderspost/pinescript-agents --skill pine-optimizer专注于提升 TradingView 上脚本的性能、用户体验和视觉呈现。
security() 调用// 优化前 - 低效
plot(ta.sma(close, 20) > ta.sma(close, 50) ? high : low)
plot(ta.sma(close, 20) > ta.sma(close, 50) ? 1 : 0)
// 优化后 - 使用缓存优化
sma20 = ta.sma(close, 20)
sma50 = ta.sma(close, 50)
condition = sma20 > sma50
plot(condition ? high : low)
plot(condition ? 1 : 0)
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
security 调用优化// 优化前 - 多次调用 security
htfClose = request.security(syminfo.tickerid, "D", close)
htfHigh = request.security(syminfo.tickerid, "D", high)
htfLow = request.security(syminfo.tickerid, "D", low)
// 优化后 - 使用元组进行单次 security 调用
[htfClose, htfHigh, htfLow] = request.security(syminfo.tickerid, "D", [close, high, low])
// 优化前 - 低效的数组操作
var array<float> values = array.new<float>()
for i = 0 to 100
array.push(values, close[i])
// 优化后 - 使用内置函数优化
var array<float> values = array.new<float>(100)
if barstate.isconfirmed
array.push(values, close)
if array.size(values) > 100
array.shift(values)
// 优化前 - 多次条件检查
signal = close > open and close > close[1] and volume > volume[1] and rsi > 50
// 优化后 - 短路求值
signal = close > open
signal := signal and close > close[1]
signal := signal and volume > volume[1]
signal := signal and rsi > 50
// 使用分组和工具提示组织输入项
// ============================================================================
// 输入项
// ============================================================================
// 移动平均线设置
maLength = input.int(20, "MA 长度", minval=1, maxval=500, group="移动平均线",
tooltip="移动平均线的长度。较低的值响应更灵敏。")
maType = input.string("EMA", "MA 类型", options=["SMA", "EMA", "WMA", "VWMA"],
group="移动平均线",
tooltip="要使用的移动平均线类型")
// 信号设置
signalMode = input.string("Conservative", "信号模式",
options=["Conservative", "Normal", "Aggressive"],
group="信号设置",
tooltip="保守模式:较少但质量更高的信号\n普通模式:平衡\n激进模式:更频繁的信号")
// 视觉设置
showMA = input.bool(true, "显示 MA", group="视觉设置")
showSignals = input.bool(true, "显示信号", group="视觉设置")
showTable = input.bool(true, "显示信息表格", group="视觉设置")
// 颜色设置
bullishColor = input.color(color.green, "看涨颜色", group="颜色")
bearishColor = input.color(color.red, "看跌颜色", group="颜色")
neutralColor = input.color(color.gray, "中性颜色", group="颜色")
// 带有透明度的专业配色方案
var color BULL_COLOR = color.new(#26a69a, 0)
var color BEAR_COLOR = color.new(#ef5350, 0)
var color BULL_LIGHT = color.new(#26a69a, 80)
var color BEAR_LIGHT = color.new(#ef5350, 80)
// 用于趋势的渐变颜色
trendStrength = (close - ta.sma(close, 50)) / ta.sma(close, 50) * 100
gradientColor = color.from_gradient(trendStrength, -2, 2, BEAR_COLOR, BULL_COLOR)
// 深色模式友好颜色
bgColor = color.new(color.black, 95)
textColor = color.new(color.white, 0)
// 基于内容自动调整大小的表格
var table infoTable = table.new(position.top_right, 2, 1, bgcolor=color.new(color.black, 85))
// 动态行管理
rowCount = 0
if showPrice
rowCount += 1
if showMA
rowCount += 1
if showRSI
rowCount += 1
// 如果需要则调整表格大小
if rowCount != table.rows(infoTable)
table.delete(infoTable)
infoTable := table.new(position.top_right, 2, rowCount, bgcolor=color.new(color.black, 85))
// 带有上下文的详细警报消息
alertMessage = "🔔 " + syminfo.ticker + " 警报\n" + "价格: $" + str.tostring(close, "#,###.##") + "\n" + "信号: " + (buySignal ? "买入" : sellSignal ? "卖出" : "中性") + "\n" + "强度: " + str.tostring(signalStrength, "#.#") + "/10\n" + "成交量: " + (volume > ta.sma(volume, 20) ? "高于" : "低于") + " 平均\n" + "时间: " + str.format_time(time, "yyyy-MM-dd HH:mm")
alertcondition(buySignal or sellSignal, "交易信号", alertMessage)
// 简洁、专业的绘图
ma = ta.ema(close, maLength)
// 带有渐变填充的主绘图
maPlot = plot(ma, "MA", color=trendColor, linewidth=2)
fillColor = close > ma ? BULL_LIGHT : BEAR_LIGHT
fill(plot(close, display=display.none), maPlot, fillColor, "MA 填充")
// 带有适当大小的信号标记
plotshape(buySignal, "买入信号", shape.triangleup, location.belowbar, BULL_COLOR, size=size.small)
plotshape(sellSignal, "卖出信号", shape.triangledown, location.abovebar, BEAR_COLOR, size=size.small)
// 基于时间框架的动态标签大小调整
labelSize = timeframe.period == "1" ? size.tiny : timeframe.period == "5" ? size.small : timeframe.period == "15" ? size.small : timeframe.period == "60" ? size.normal : timeframe.period == "D" ? size.large : size.normal
if showLabels and buySignal
label.new(bar_index, low, "买入", style=label.style_label_up, color=BULL_COLOR, textcolor=color.white, size=labelSize)
// 适用于移动设备的紧凑显示模式
compactMode = input.bool(false, "紧凑模式 (移动端)", group="显示",
tooltip="启用以获得更好的移动端观看体验")
// 调整绘图线宽
plotWidth = compactMode ? 1 : 2
// 条件性表格显示
if not compactMode
// 显示完整表格
table.cell(infoTable, 0, 0, "完整信息", text_color=color.white)
else
// 仅显示基本信息
table.cell(infoTable, 0, 0, "信号: " + (buySignal ? "↑" : sellSignal ? "↓" : "−"))
// 使用 var 声明持久化值
var float prevHigh = na
var int barsSinceSignal = 0
var array<float> prices = array.new<float>(100)
// 清除未使用的数组
if array.size(prices) > 100
array.clear(prices)
// 健壮的错误处理
safeDiv(num, den) => den != 0 ? num / den : 0
safeLookback(src, bars) => bars < bar_index ? src[bars] : src[bar_index]
// NA 值处理
getValue(src) => na(src) ? 0 : src
// 使用函数减少代码重复
plotSignal(cond, loc, col, txt) =>
if cond
label.new(bar_index, loc, txt, color=col, textcolor=color.white)
// 重用样式变量
var commonStyle = label.style_label_center
var commonSize = size.normal
security() 调用在优化与可读性之间取得平衡。不要为了过度优化而牺牲可维护性。
每周安装数
119
代码仓库
GitHub 星标数
66
首次出现
2026年1月23日
安全审计
安装于
opencode103
gemini-cli102
codex96
github-copilot94
amp85
kimi-cli84
Specialized in enhancing script performance, user experience, and visual presentation on TradingView.
// BEFORE - Inefficient
plot(ta.sma(close, 20) > ta.sma(close, 50) ? high : low)
plot(ta.sma(close, 20) > ta.sma(close, 50) ? 1 : 0)
// AFTER - Optimized with caching
sma20 = ta.sma(close, 20)
sma50 = ta.sma(close, 50)
condition = sma20 > sma50
plot(condition ? high : low)
plot(condition ? 1 : 0)
// BEFORE - Multiple security calls
htfClose = request.security(syminfo.tickerid, "D", close)
htfHigh = request.security(syminfo.tickerid, "D", high)
htfLow = request.security(syminfo.tickerid, "D", low)
// AFTER - Single security call with tuple
[htfClose, htfHigh, htfLow] = request.security(syminfo.tickerid, "D", [close, high, low])
// BEFORE - Inefficient array operations
var array<float> values = array.new<float>()
for i = 0 to 100
array.push(values, close[i])
// AFTER - Optimized with built-in functions
var array<float> values = array.new<float>(100)
if barstate.isconfirmed
array.push(values, close)
if array.size(values) > 100
array.shift(values)
// BEFORE - Multiple condition checks
signal = close > open and close > close[1] and volume > volume[1] and rsi > 50
// AFTER - Short-circuit evaluation
signal = close > open
signal := signal and close > close[1]
signal := signal and volume > volume[1]
signal := signal and rsi > 50
// Organized inputs with groups and tooltips
// ============================================================================
// INPUTS
// ============================================================================
// Moving Average Settings
maLength = input.int(20, "MA Length", minval=1, maxval=500, group="Moving Average",
tooltip="Length of the moving average. Lower values are more responsive.")
maType = input.string("EMA", "MA Type", options=["SMA", "EMA", "WMA", "VWMA"],
group="Moving Average",
tooltip="Type of moving average to use")
// Signal Settings
signalMode = input.string("Conservative", "Signal Mode",
options=["Conservative", "Normal", "Aggressive"],
group="Signal Settings",
tooltip="Conservative: Fewer, higher quality signals\nNormal: Balanced\nAggressive: More frequent signals")
// Visual Settings
showMA = input.bool(true, "Show MA", group="Visual Settings")
showSignals = input.bool(true, "Show Signals", group="Visual Settings")
showTable = input.bool(true, "Show Info Table", group="Visual Settings")
// Color Settings
bullishColor = input.color(color.green, "Bullish Color", group="Colors")
bearishColor = input.color(color.red, "Bearish Color", group="Colors")
neutralColor = input.color(color.gray, "Neutral Color", group="Colors")
// Professional color scheme with transparency
var color BULL_COLOR = color.new(#26a69a, 0)
var color BEAR_COLOR = color.new(#ef5350, 0)
var color BULL_LIGHT = color.new(#26a69a, 80)
var color BEAR_LIGHT = color.new(#ef5350, 80)
// Gradient colors for trends
trendStrength = (close - ta.sma(close, 50)) / ta.sma(close, 50) * 100
gradientColor = color.from_gradient(trendStrength, -2, 2, BEAR_COLOR, BULL_COLOR)
// Dark mode friendly colors
bgColor = color.new(color.black, 95)
textColor = color.new(color.white, 0)
// Auto-sizing table based on content
var table infoTable = table.new(position.top_right, 2, 1, bgcolor=color.new(color.black, 85))
// Dynamic row management
rowCount = 0
if showPrice
rowCount += 1
if showMA
rowCount += 1
if showRSI
rowCount += 1
// Resize table if needed
if rowCount != table.rows(infoTable)
table.delete(infoTable)
infoTable := table.new(position.top_right, 2, rowCount, bgcolor=color.new(color.black, 85))
// Detailed alert messages with context
alertMessage = "🔔 " + syminfo.ticker + " Alert\n" + "Price: $" + str.tostring(close, "#,###.##") + "\n" + "Signal: " + (buySignal ? "BUY" : sellSignal ? "SELL" : "NEUTRAL") + "\n" + "Strength: " + str.tostring(signalStrength, "#.#") + "/10\n" + "Volume: " + (volume > ta.sma(volume, 20) ? "Above" : "Below") + " average\n" + "Time: " + str.format_time(time, "yyyy-MM-dd HH:mm")
alertcondition(buySignal or sellSignal, "Trade Signal", alertMessage)
// Clean, professional plotting
ma = ta.ema(close, maLength)
// Main plot with gradient fill
maPlot = plot(ma, "MA", color=trendColor, linewidth=2)
fillColor = close > ma ? BULL_LIGHT : BEAR_LIGHT
fill(plot(close, display=display.none), maPlot, fillColor, "MA Fill")
// Signal markers with proper sizing
plotshape(buySignal, "Buy Signal", shape.triangleup, location.belowbar, BULL_COLOR, size=size.small)
plotshape(sellSignal, "Sell Signal", shape.triangledown, location.abovebar, BEAR_COLOR, size=size.small)
// Dynamic label sizing based on timeframe
labelSize = timeframe.period == "1" ? size.tiny : timeframe.period == "5" ? size.small : timeframe.period == "15" ? size.small : timeframe.period == "60" ? size.normal : timeframe.period == "D" ? size.large : size.normal
if showLabels and buySignal
label.new(bar_index, low, "BUY", style=label.style_label_up, color=BULL_COLOR, textcolor=color.white, size=labelSize)
// Compact display for mobile devices
compactMode = input.bool(false, "Compact Mode (Mobile)", group="Display",
tooltip="Enable for better mobile viewing")
// Adjust plot widths
plotWidth = compactMode ? 1 : 2
// Conditional table display
if not compactMode
// Show full table
table.cell(infoTable, 0, 0, "Full Info", text_color=color.white)
else
// Show essential info only
table.cell(infoTable, 0, 0, "Signal: " + (buySignal ? "↑" : sellSignal ? "↓" : "−"))
// Use var for persistent values
var float prevHigh = na
var int barsSinceSignal = 0
var array<float> prices = array.new<float>(100)
// Clear unused arrays
if array.size(prices) > 100
array.clear(prices)
// Robust error handling
safeDiv(num, den) => den != 0 ? num / den : 0
safeLookback(src, bars) => bars < bar_index ? src[bars] : src[bar_index]
// NA handling
getValue(src) => na(src) ? 0 : src
// Use functions to reduce code duplication
plotSignal(cond, loc, col, txt) =>
if cond
label.new(bar_index, loc, txt, color=col, textcolor=color.white)
// Reuse styling variables
var commonStyle = label.style_label_center
var commonSize = size.normal
Balance optimization with readability. Don't over-optimize at the expense of maintainability.
Weekly Installs
119
Repository
GitHub Stars
66
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode103
gemini-cli102
codex96
github-copilot94
amp85
kimi-cli84
Stripe API 最佳实践指南:最新版本集成路径与支付解决方案
3,900 周安装