PINE LIBRARY
업데이트됨 BacktestLibrary

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.
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.
릴리즈 노트
v2Added:
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.
파인 라이브러리
트레이딩뷰의 진정한 정신에 따라, 작성자는 이 파인 코드를 오픈소스 라이브러리로 게시하여 커뮤니티의 다른 파인 프로그래머들이 재사용할 수 있도록 했습니다. 작성자에게 경의를 표합니다! 이 라이브러리는 개인적으로 사용하거나 다른 오픈소스 게시물에서 사용할 수 있지만, 이 코드의 게시물 내 재사용은 하우스 룰에 따라 규제됩니다.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
파인 라이브러리
트레이딩뷰의 진정한 정신에 따라, 작성자는 이 파인 코드를 오픈소스 라이브러리로 게시하여 커뮤니티의 다른 파인 프로그래머들이 재사용할 수 있도록 했습니다. 작성자에게 경의를 표합니다! 이 라이브러리는 개인적으로 사용하거나 다른 오픈소스 게시물에서 사용할 수 있지만, 이 코드의 게시물 내 재사용은 하우스 룰에 따라 규제됩니다.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.