Skip to content

示例

本页通过两个完整的 Pine 脚本,逐行讲解每行代码的用途。每个示例都以贴近实际的方式展示核心语言特性。

示例一 — SMA 均线交叉指标

该指标绘制快速和慢速简单移动均线,并在图表上标记金叉和死叉事件。涉及变量、series、ta 模块、plotplotshape 等特性。

pine
//@version=6
// ① 将此脚本声明为指标。
//   - title        : 显示在图例中的名称
//   - overlay=true : 叠加在 K 线上绘制(而非独立面板)
indicator("SMA Crossover", overlay = true)

// ② 用户可配置的输入项。
//   input.int() 创建一个整数输入,显示在脚本设置面板中。
//   第二个参数为默认值;title 为在 UI 中显示的标签。
fastLen = input.int(9,  title = "Fast Length")
slowLen = input.int(21, title = "Slow Length")

// ③ 计算两条 SMA series。
//   这些是 series<float>:每个 Bar 产生一个值。
fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)

// ④ 检测金叉和死叉事件。
//   ta.crossover(a, b)  → 在 a 向上穿越 b 的那根 Bar 返回 true
//   ta.crossunder(a, b) → 在 a 向下穿越 b 的那根 Bar 返回 true
crossUp   = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)

// ⑤ 将两条均线绘制为连续折线。
//   linewidth=2 使线条略粗于默认值 1。
plot(fast, title = "Fast SMA", color = color.blue,   linewidth = 2)
plot(slow, title = "Slow SMA", color = color.orange, linewidth = 2)

// ⑥ 在金叉 Bar 的下方绘制向上的三角形标记。
//   plotshape 仅在第一个参数为 true 的 Bar 上绘制。
plotshape(crossUp,
    title    = "Bullish Cross",
    style    = shape.triangleup,
    location = location.belowbar,
    color    = color.green,
    size     = size.small)

// ⑦ 在死叉 Bar 的上方绘制向下的三角形标记。
plotshape(crossDown,
    title    = "Bearish Cross",
    style    = shape.triangledown,
    location = location.abovebar,
    color    = color.red,
    size     = size.small)

// ⑧ 在金叉或死叉 Bar 上为图表背景添加淡色高亮。
//   color.new(c, transp) 创建带透明度的颜色(0=不透明,100=完全透明)。
//   三元运算符 ?: 根据事件类型选择颜色。
bgColor = crossUp   ? color.new(color.green, 85) :
          crossDown ? color.new(color.red,   85) : na
bgcolor(bgColor)

涉及的核心概念

概念位置
脚本声明indicator(...) — 行 ①
用户输入input.int — 行 ②
series<float> 变量fastslowcrossUpcrossDown — 行 ③④
ta 模块ta.smata.crossoverta.crossunder — 行 ③④
绘制折线plot(...) — 行 ⑤
绘制形状标记plotshape(...) — 行 ⑥⑦
透明颜色color.new(c, transp) — 行 ⑧
三元运算符c ? a : b — 行 ⑧
na 表示"无值"na 用于跳过背景着色 — 行 ⑧

示例二 — 布林带策略

该策略在收盘价向上突破上轨时做多,跌回中轨以下时平多;反之,向下突破下轨时做空,反弹回中轨以上时平空。涉及 strategy()ta.bb、元组解构、历史运算符和 strategy.entry / strategy.close 等特性。

pine
//@version=6
// ① 将此脚本声明为策略。
//   - default_qty_type  : 使用固定数量(合约/股数)
//   - default_qty_value : 每笔交易默认 1 手
strategy("Bollinger Bands Breakout",
    default_qty_type  = strategy.fixed,
    default_qty_value = 1)

// ② 计算布林带。
//   ta.bb 返回一个元组:[中轨, 上轨, 下轨]。
//   [a, b, c] = ... 语法将元组解构为三个变量。
[basis, upper, lower] = ta.bb(close, 20, 2.0)

// ③ 入场条件。
//   close[1] 引用*上一根* Bar 的收盘价(历史运算符 [])。
//   结合当前 Bar 与上一 Bar 的值,可精确定位价格穿越轨道的那根 Bar。
longEntry  = close > upper and close[1] <= upper[1]
shortEntry = close < lower and close[1] >= lower[1]

// ④ 出场条件:价格穿越中轨。
longExit  = close < basis
shortExit = close > basis

// ⑤ 发出策略指令。
//   strategy.entry 开仓;strategy.close 按交易 ID 平仓。
if longEntry
    strategy.entry("Long",  strategy.long)
if shortEntry
    strategy.entry("Short", strategy.short)

if longExit
    strategy.close("Long")
if shortExit
    strategy.close("Short")

涉及的核心概念

概念位置
策略声明strategy(...) — 行 ①
元组解构[basis, upper, lower] = ta.bb(...) — 行 ②
ta.bb 布林带行 ②
历史运算符 []close[1]upper[1] — 行 ③
布尔 serieslongEntryshortEntrylongExitshortExit — 行 ③④
if 语句策略指令块 — 行 ⑤
strategy.entry / strategy.close开仓与平仓 — 行 ⑤

延伸阅读

基于 MIT 许可证发布。