PINE LIBRARY

BacktestLibrary

Cập nhật
Library "BacktestLibrary"
A library providing functions for equity calculation and performance metrics.

since(date, active)
  : Calculates the number of candles since a specified date.
  Parameters:
    date (simple float): (simple float): The starting date in timestamp format (e.g., input.time(timestamp()))
    active (simple bool): (simple bool): If true, counts the number of candles since the date; if false, returns 0.
  Returns: (int): The number of candles since the specified date.

buy_and_hold(r, startDate)
  : Calculates the Buy and Hold Equity from a specified date.
  Parameters:
    r (float): (series float): Daily returns of the asset (e.g., 0.02 for 2% move).
    startDate (simple float): (simple float): Timestamp of the starting date for the equity calculation.
  Returns: (float): Buy and Hold Equity of the asset from the specified date.

equity(sig, threshold, r, startDate, signals)
  : Calculates the strategy's equity on a candle-by-candle basis.
  Parameters:
    sig (float): (series float): Signal values; positive for long, negative for short.
    threshold (simple float): (simple float): Signal threshold for entering trades.
    r (float): (series float): Daily returns of the asset (e.g., 0.02 for 2% move).
    startDate (simple float): (simple float): Timestamp of the starting date for the equity calculation.
    signals (simple string): (simple string): Type of signals to backtest ("Long & Short", "Long Only", "Short Only").
  Returns: (float): Strategy equity on a candle-by-candle basis.

PerformanceMetrics(base, Lookback, startDate)
  : Calculates performance metrics of a strategy from a specified date.
  Parameters:
    base (float): (series float): Equity values of the strategy or Buy and Hold equity.
    Lookback (int): (series int): Number of periods since the start date; recommended to use the 'since' function.
    startDate (simple float): (simple float): Timestamp of the starting date for the equity calculation.
  Returns: (float[]): Array of performance metrics.

PerfMetricTable(buy_and_hold, strategy)
  : Plots a table comparing performance metrics of Buy and Hold and Strategy equity.
  Parameters:
    buy_and_hold (array<float>): (float[]): Metrics from the PerformanceMetrics() function for Buy and Hold.
    strategy (array<float>): (float[]): Metrics from the PerformanceMetrics() function for the strategy.
  Returns: : Table displaying the performance metrics comparison.
Phát hành các Ghi chú
v2

Added:
TradeMetrics(startDate, initial_capital, trade_size, Long_entry_bool, Short_entry_bool, Long_exit_bool, Short_exit_bool)
  : Calculates key trading performance metrics based on entry, exit, and trade size logic.
  Parameters:
    startDate (simple float): (simple float): Timestamp of the starting date for the equity calculation.
    initial_capital (simple float): (simple float): The starting capital for trade calculations.
    trade_size (simple float): (simple float): The proportion of capital allocated per trade.
    Long_entry_bool (bool): (series bool): Boolean condition indicating a long entry signal.
    Short_entry_bool (bool): (series bool): Boolean condition indicating a short entry signal.
    Long_exit_bool (bool): (series bool): Boolean condition indicating a long exit signal.
    Short_exit_bool (bool): (series bool): Boolean condition indicating a short exit signal.
  Returns: (tuple): A tuple containing:
- signal (int): Current trade signal (-1 for short, 1 for long, 0 for neutral).
- closed_trades_count (int): Total number of closed trades.
- closed_long (int): Total number of closed long trades.
- closed_short (int): Total number of closed short trades.
- win_trades (int): Total number of winning trades.
- win_long (int): Total number of winning long trades.
- win_short (int): Total number of winning short trades.
- gross_profit (float): Total gross profit from all trades.
- gross_losses (float): Total gross losses from all trades.
- gross_L_profit (float): Gross profit from long trades.
- gross_L_losses (float): Gross losses from long trades.
- gross_S_profit (float): Gross profit from short trades.
- gross_S_losses (float): Gross losses from short trades.

TradeMetricsTable(initial_capital, closed_trades_count, closed_long, closed_short, win_trades, win_long, win_short, gross_profit, gross_losses, gross_L_profit, gross_L_losses, gross_S_profit, gross_S_losses)
  : Generates a visual table displaying trade performance metrics for all trades, long trades, and short trades.
  Parameters:
    initial_capital (simple float): (simple float): The starting capital for trade calculations.
    closed_trades_count (int): (series int): Total number of closed trades.
    closed_long (int): (series int): Total number of closed long trades.
    closed_short (int): (series int): Total number of closed short trades.
    win_trades (int): (series int): Total number of winning trades.
    win_long (int): (series int): Total number of winning long trades.
    win_short (int): (series int): Total number of winning short trades.
    gross_profit (float): (series float): Total gross profit from all trades.
    gross_losses (float): (series float): Total gross losses from all trades.
    gross_L_profit (float): (series float): Gross profit from long trades.
    gross_L_losses (float): (series float): Gross losses from long trades.
    gross_S_profit (float): (series float): Gross profit from short trades.
    gross_S_losses (float): (series float): Gross losses from short trades.
  Returns: (table): A table object displaying detailed trade metrics, including:
- Total trades, winning trades, and losing trades.
- Gross and net profit/loss in absolute and percentage terms.
- Profit factor, average trade metrics, and win/loss ratios.
- Metrics categorized by all trades, long trades, and short trades.
displaystatisticsstrategies

Thư viện Pine

Theo tinh thần TradingView thực sự, tác giả đã xuất bản mã Pine này dưới dạng thư viện nguồn mở để các lập trình viên Pine khác trong cộng đồng của chúng tôi có thể sử dụng lại. Chúc mừng tác giả! Bạn có thể sử dụng thư viện này riêng tư hoặc trong các bài đăng nguồn mở khác. Tuy nhiên, bạn cần sử dụng lại mã này theo Quy tắc nội bộ.

Thông báo miễn trừ trách nhiệm