入門
設定 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)