はじめに
PineBag を一度セットアップしてから、数分で最初のテストを準備します。
必要なものは 3 つあります
- 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コピーバッグ
すべてのティッカーを 1 つずつコピーします
EXCHANGE:SYMBOL1 行につき。 - 5ペーストバッグ
行、カンマ、セミコロン、またはスペースを受け入れ、重複を無視します。
- 6クリア
2 回目のクリックで確定し、ティッカーをコピーしてからリストを空にします。「ティッカーを貼り付け」で元に戻せます。
- 7ティッカータグ
現在のバッグを表示します。記号を 1 つ削除するには × を使用します。最大 500 まで加算します。
- 8バックテストを実行
セットアップを検証し、順次ポートフォリオの実行を開始します。
- 9レポート
最新 50 件のレポートをローカルに保存し、JSON または CSV をエクスポートします。
- 10残り日数
Active days: 05— 残り日数. - 11サポート
サポートは、技術、請求、ライセンス、プライバシーのリクエストに利用できます。
戦略をロードする
- ソースを選択してください。
ドロップ.pineファイルを Strategy に貼り付けるか、エディターを開いてコードを貼り付けます。 - 検証メッセージを修正します。
スクリプトには以下を含める必要がありますstrategy()。 PineBag が使用できない場合は、明らかなエラーが表示されます。 - 概要を確認してください。
実行の上に表示されているスクリプト名、タイムフレーム、資本、注文サイズを確認します。
indicator()、PineBag を実行する前に戦略に変換します。ストラテジーのテンプレート
TradingView の Properties タブの各項目は strategy() の引数に対応しています。値はスクリプト冒頭の名前付き定数として保持してください。スクリプトの優先度を使うと、すべてのティッカーが同じ前提でテストされます。
TIMEFRAME時間足
INITIAL_CAPITAL · CURRENCY初期資本
ORDER_SIZE · ORDER_SIZE_TYPEデフォルトの注文サイズ
PYRAMIDINGピラミッディング
BAR_MAGNIFIERバーの詳細度
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.com/pricing に記載のとおり TradingView のプランによって異なります。
日、週、月。
"1D" "1W" "1M" "3M" "6M" "12M"
分、時間、レンジバー。分には文字を付けません。1 時間は 60 です。
"1" "3" "5" "15" "30" "45""60" "120" "180" "240""1R" "10R" "100R" "1000R"
秒足。
"1S" "5S" "10S" "15S" "30S" "45S"
ティック足。
"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 はその意味を表します。2 つで TradingView の Properties タブにある Order size 欄を設定します。
strategy.percent_of_equity
現在の資産に対する割合。10 は 1 取引あたり資産の 10% を意味します。
strategy.cash
ストラテジーの通貨での金額。1000 は 1 取引あたり 1,000 USD を意味します。
strategy.fixed
契約数、株数、コイン数。0.5 は 1 取引あたり 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)