开始使用
设置 PineBag 一次,然后在几分钟内准备您的第一个测试。
您需要三件事
- Google Chrome 116 或更高版本。
- 登录的 TradingView 帐户可以访问您要测试的交易品种。
- 声明以下内容的 Pine Script 文件
strategy(),不仅仅是indicator().
安装并打开PineBag
-
安装扩展。
使用 PineBag 安装按钮。如果您想快速访问,请固定扩展程序。 -
打开 TradingView 图表。
首先登录,然后打开任何支持的市场和时间范围。 -
重新加载图表选项卡。
在安装或更新 PineBag 后执行此操作,以便图表可以连接。 -
打开 PineBag。
选择 PineBag 图标。当图表准备就绪时,连接指示器将变为绿色。
侧面板,通过控件进行控制
真实界面上的数字与说明相符。此图像中的任何内容都不是模型。
1
2
3
4
5
6
7
8
9
10
11
- 1策略
已加载的策略。点击卡片即可在编辑器中打开;分区标题中的垃圾桶可将其移除。
- 2运行摘要
将使用的时间框架、资本、订单大小和佣金。
- 3搜索
打开 TradingView 交易品种搜索,以便您添加符合交易所资格的代码。
- 4文案包
复制每个股票代码,一个
EXCHANGE:SYMBOL每个 - 5粘贴包
接受行、逗号、分号或空格并忽略重复项。
- 6清除
需要再点击一次确认,先复制代码,再清空列表;“粘贴代码”可将其恢复。
- 7股票代码
显示当前包。用×去掉一个符号;加起来最多为 500。
- 8运行回测
验证设置并启动顺序投资组合
- 9报告
在本地保存最新 50 份报告并导出 JSON 或 CSV。
- 10剩余天数
Active days: 05— 剩余天数. - 11支持
为技术、计费、许可和隐私请求提供支持。
加载策略
- 选择来源。
删除.pine将文件保存到策略中,或打开编辑器并粘贴代码。 - 修复所有验证消息。
脚本必须包含strategy()。如果无法使用,PineBag 会显示明显的错误。 - 检查摘要。
确认运行上方显示的脚本名称、时间范围、资金和订单大小。
indicator(),在运行 PineBag 之前将其转换为策略。策略模板
TradingView Properties 选项卡中的每个字段都对应一个 strategy() 参数。将这些值作为命名常量放在脚本顶部:使用脚本优先级时,每个标的都按相同的假设进行测试。
TIMEFRAME周期
INITIAL_CAPITAL · CURRENCY初始资本
ORDER_SIZE · ORDER_SIZE_TYPE默认订单大小
PYRAMIDING金字塔加仓
BAR_MAGNIFIERK线细节
CALC_EVERY_TICK · CALC_ON_FILLS脚本执行
COMMISSION · COMMISSION_TYPE佣金
MARGIN_LONG · MARGIN_SHORT多空杠杆
SLIPPAGE滑点
LIMIT_FILL_TICKS限价单成交
FILL_ON_BAR_CLOSE订单执行延迟
runtime.error()在其他任何时间周期上停止脚本。
//@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。
日、周和月。
"1D" "1W" "1M" "3M" "6M" "12M"
分钟、小时和区间K线。分钟不带字母;一小时写作 60。
"1" "3" "5" "15" "30" "45""60" "120" "180" "240""1R" "10R" "100R" "1000R"
秒级周期。
"1S" "5S" "10S" "15S" "30S" "45S"
Tick 周期。
"1T" "10T" "100T" "1000T"
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
订单规模:百分比、金额或数量
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 = … 的入场会改用该数值。
// 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 选项卡中的字段,无需修改代码即可调整。
// ----------------------------------------------------------------------------
// 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)