文档/入门

开始使用

设置 PineBag 一次,然后在几分钟内准备您的第一个测试。

扩展侧面板旁边的 PineBag 投资组合报告
真正的 PineBag 工作区:扩展程序准备运行,同时完整报告在其旁边保持打开状态。

您需要三件事

5 天试用期自动开始。它不需要卡。运行可用性受到您的 TradingView 计划的限制。

安装并打开PineBag

  1. 安装扩展。
    使用 PineBag 安装按钮。如果您想快速访问,请固定扩展程序。
  2. 打开 TradingView 图表。
    首先登录,然后打开任何支持的市场和时间范围。
  3. 重新加载图表选项卡。
    在安装或更新 PineBag 后执行此操作,以便图表可以连接。
  4. 打开 PineBag。
    选择 PineBag 图标。当图表准备就绪时,连接指示器将变为绿色。

侧面板,通过控件进行控制

真实界面上的数字与说明相符。此图像中的任何内容都不是模型。

1 2 3 4 5 6 7 8 9 10 11
  1. 1
    策略

    已加载的策略。点击卡片即可在编辑器中打开;分区标题中的垃圾桶可将其移除。

  2. 2
    运行摘要

    将使用的时间框架、资本、订单大小和佣金。

  3. 3
    搜索

    打开 TradingView 交易品种搜索,以便您添加符合交易所资格的代码。

  4. 4
    文案包

    复制每个股票代码,一个 EXCHANGE:SYMBOL 每个

  5. 5
    粘贴包

    接受行、逗号、分号或空格并忽略重复项。

  6. 6
    清除

    需要再点击一次确认,先复制代码,再清空列表;“粘贴代码”可将其恢复。

  7. 7
    股票代码

    显示当前包。用×去掉一个符号;加起来最多为 500。

  8. 8
    运行回测

    验证设置并启动顺序投资组合

  9. 9
    报告

    在本地保存最新 50 份报告并导出 JSON 或 CSV。

  10. 10
    剩余天数

    Active days: 05 — 剩余天数.

  11. 11
    支持

    为技术、计费、许可和隐私请求提供支持。

加载策略

具有 Pine Script策略的真实 PineBag 编辑器
内置编辑器使用行号和语法突出显示,并自动保存工作源。
  1. 选择来源。
    删除 .pine 将文件保存到策略中,或打开编辑器并粘贴代码。
  2. 修复所有验证消息。
    脚本必须包含 strategy()。如果无法使用,PineBag 会显示明显的错误。
  3. 检查摘要。
    确认运行上方显示的脚本名称、时间范围、资金和订单大小。
指标脚本无法回溯测试。 如果源开头为 indicator(),在运行 PineBag 之前将其转换为策略。
准备: 连接为绿色,策略摘要可见,并且您的股票包不为空。

策略模板

TradingView Properties 选项卡中的每个字段都对应一个 strategy() 参数。将这些值作为命名常量放在脚本顶部:使用脚本优先级时,每个标的都按相同的假设进行测试。

TIMEFRAME

周期

INITIAL_CAPITAL · CURRENCY

初始资本

ORDER_SIZE · ORDER_SIZE_TYPE

默认订单大小

PYRAMIDING

金字塔加仓

BAR_MAGNIFIER

K线细节

CALC_EVERY_TICK · CALC_ON_FILLS

脚本执行

COMMISSION · COMMISSION_TYPE

佣金

MARGIN_LONG · MARGIN_SHORT

多空杠杆

SLIPPAGE

滑点

LIMIT_FILL_TICKS

限价单成交

FILL_ON_BAR_CLOSE

订单执行延迟

runtime.error()

在其他任何时间周期上停止脚本。

Pine Script® v6trend-breakout-atr-risk.pine
//@version=6
// ============================================================================
// Trend breakout · ATR risk — PineBag strategy template
// ============================================================================

// ----------------------------------------------------------------------------
// STRATEGY PROPERTIES (TradingView: Settings → Properties)
// ----------------------------------------------------------------------------
// TIMEFRAME takes a standard TradingView interval. Paste one of these, by the
// lowest TradingView plan that has it (tradingview.com/pricing, Charts):
//   Basic      days      "1D" "1W" "1M" "3M" "6M" "12M"
//   Essential  minutes   "1" "3" "5" "15" "30" "45"
//              hours     "60" "120" "180" "240"
//              ranges    "1R" "10R" "100R" "1000R"
//   Premium    seconds   "1S" "5S" "10S" "15S" "30S" "45S"
//   Ultimate   ticks     "1T" "10T" "100T" "1000T"
// Other intervals are custom and not used: a week is "1W", not "7D".
const string TIMEFRAME         = "1D"                         // Chart timeframe
const float  INITIAL_CAPITAL   = 100000                       // Initial capital
const string CURRENCY          = currency.USD                 // Initial capital currency
const float  ORDER_SIZE        = 100                          // Default order size
const string ORDER_SIZE_TYPE   = strategy.percent_of_equity   // Order size: % of equity
const int    PYRAMIDING        = 1                            // Pyramiding
const bool   BAR_MAGNIFIER     = false                        // Bar detalization: Default (4 ticks per bar)
const bool   CALC_EVERY_TICK   = false                        // Script execution: On bar close
const bool   CALC_ON_FILLS     = false                        // Script execution: no recalculation after fills
const float  COMMISSION        = 0                            // Commission
const string COMMISSION_TYPE   = strategy.commission.percent  // Commission: Percent
const float  MARGIN_LONG       = 100                          // Long leverage 1x (leverage = 100 / margin)
const float  MARGIN_SHORT      = 100                          // Short leverage 1x
const int    SLIPPAGE          = 0                            // Slippage, ticks
const int    LIMIT_FILL_TICKS  = 0                            // Limit order execution: Requested price
const bool   FILL_ON_BAR_CLOSE = false                        // Order execution delay: One tick

strategy("Trend breakout · ATR risk", overlay = true,
     initial_capital = INITIAL_CAPITAL,
     currency = CURRENCY,
     default_qty_type = ORDER_SIZE_TYPE,
     default_qty_value = ORDER_SIZE,
     pyramiding = PYRAMIDING,
     use_bar_magnifier = BAR_MAGNIFIER,
     calc_on_every_tick = CALC_EVERY_TICK,
     calc_on_order_fills = CALC_ON_FILLS,
     commission_type = COMMISSION_TYPE,
     commission_value = COMMISSION,
     margin_long = MARGIN_LONG,
     margin_short = MARGIN_SHORT,
     slippage = SLIPPAGE,
     backtest_fill_limits_assumption = LIMIT_FILL_TICKS,
     process_orders_on_close = FILL_ON_BAR_CLOSE)

// Stops with a clear message on any other timeframe; PineBag reads TIMEFRAME
// and selects it for the run.
if timeframe.period != TIMEFRAME
    runtime.error("Switch the chart to the " + TIMEFRAME + " timeframe.")

// ----------------------------------------------------------------------------
// INPUT SETTINGS (TradingView: Settings → Inputs)
// ----------------------------------------------------------------------------
fastLength     = input.int(20, "Fast EMA", minval = 1)
slowLength     = input.int(50, "Slow EMA", minval = 2)
breakoutLength = input.int(20, "Breakout lookback", minval = 2)
atrLength      = input.int(14, "ATR length", minval = 1)
atrStop        = input.float(2.0, "ATR stop multiplier", minval = 0.1, step = 0.1)

// ----------------------------------------------------------------------------
// CALCULATIONS
// ----------------------------------------------------------------------------
fast = ta.ema(close, fastLength)
slow = ta.ema(close, slowLength)
atr = ta.atr(atrLength)
highestBefore = ta.highest(high, breakoutLength)[1]

// ----------------------------------------------------------------------------
// ENTRIES AND EXITS
// ----------------------------------------------------------------------------
longSignal = fast > slow and close > highestBefore
if longSignal and strategy.position_size == 0
    strategy.entry("Long", strategy.long)

stopPrice = strategy.position_avg_price - atr * atrStop
strategy.exit("ATR stop", "Long", stop = stopPrice)

// ----------------------------------------------------------------------------
// PLOTS
// ----------------------------------------------------------------------------
plot(fast, "Fast EMA", color = color.rgb(0, 153, 255))
plot(slow, "Slow EMA", color = color.rgb(123, 97, 255))

TIMEFRAME 取值

在引号中填写一个 TradingView 标准周期。能加载哪些周期取决于您的 TradingView 套餐,详见 tradingview.com/pricing。

Basic 及以上

日、周和月。

"1D" "1W" "1M" "3M" "6M" "12M"

Essential 及以上

分钟、小时和区间K线。分钟不带字母;一小时写作 60。

"1" "3" "5" "15" "30" "45"
"60" "120" "180" "240"
"1R" "10R" "100R" "1000R"

Premium 及以上

秒级周期。

"1S" "5S" "10S" "15S" "30S" "45S"

仅 Ultimate

Tick 周期。

"1T" "10T" "100T" "1000T"

Pine Script® v6TIMEFRAME
const string TIMEFRAME = "1D"     // 1 day
// const string TIMEFRAME = "1W"  // 1 week
// const string TIMEFRAME = "240" // 4 hours
// const string TIMEFRAME = "15"  // 15 minutes
// const string TIMEFRAME = "30S" // 30 seconds, Premium and higher
一周写作 "1W",而不是 "7D"。PineBag 会把 "7D" 读作 "1W",并拒绝其他自定义周期,例如 "2M" 或 "2D"。

订单规模:百分比、金额或数量

ORDER_SIZE 是数值,ORDER_SIZE_TYPE 说明它的含义。两者共同填写 TradingView 属性(Properties)选项卡中的 Order size 字段。

strategy.percent_of_equity

当前权益的百分比。10 表示每笔交易使用 10% 的权益。

strategy.cash

以策略货币计的金额。1000 表示每笔交易 1,000 USD。

strategy.fixed

合约、股票或币的数量。0.5 表示每笔交易 0.5 BTC。

qty = …

默认规模只适用于没有自身 qty 的订单。带 qty = … 的入场会改用该数值。

Pine Script® v6ORDER SIZE
// 10% of equity per trade
const float  ORDER_SIZE        = 10
const string ORDER_SIZE_TYPE   = strategy.percent_of_equity

// 1,000 USD per trade (CURRENCY = currency.USD)
// const float  ORDER_SIZE      = 1000
// const string ORDER_SIZE_TYPE = strategy.cash

// 0.5 contracts, shares or coins per trade
// const float  ORDER_SIZE      = 0.5
// const string ORDER_SIZE_TYPE = strategy.fixed

自定义变量

将自定义策略逻辑的设置放在单独的 INPUT SETTINGS 块中。每个 input.int()、input.float() 或 input.bool() 调用都会成为 TradingView Inputs 选项卡中的字段,无需修改代码即可调整。

Pine Script® v6INPUT SETTINGS
// ----------------------------------------------------------------------------
// INPUT SETTINGS (TradingView: Settings → Inputs)
// ----------------------------------------------------------------------------
fastLength     = input.int(20, "Fast EMA", minval = 1)
slowLength     = input.int(50, "Slow EMA", minval = 2)
breakoutLength = input.int(20, "Breakout lookback", minval = 2)
atrLength      = input.int(14, "ATR length", minval = 1)
atrStop        = input.float(2.0, "ATR stop multiplier", minval = 0.1, step = 0.1)
↓下载可编辑的 .pine 模板4 KB · Pine Script v6