文件/入門

入門

設定 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