PINE LIBRARY

BackTestLib

160
Library "BackTestLib"
Allows backtesting indicator performance. Tracks typical metrics such as won/loss, profit factor, draw down, etc. Trading View strategy library provides similar (and more comprehensive)
functionality but only works with strategies. This libary was created to address performance tracking within indicators.

Two primary outputs are generated:
1. Summary Table: Displays overall performance metrics for the indicator over the chart's loaded timeframe and history
2. Details Table: Displays a table of individual trade entries and exits. This table can grow larger than the available chart space. It does have a max number of rows supported. I haven't
found a way to add scroll bars or scroll bar equivalents yet.

f_init(data, _defaultStopLoss, _defaultTakeProfit, _useTrailingStop, _useTraingStopToBreakEven, _trailingStopActivation, _trailingStopOffset)
  f_init Initialize the backtest data type. Called prior to using the backtester functions
  Parameters:
    data (backtesterData): backtesterData to initialize
    _defaultStopLoss (float): Default trade stop loss to apply
    _defaultTakeProfit (float): Default trade take profit to apply
    _useTrailingStop (bool): Trailing stop enabled
    _useTraingStopToBreakEven (bool): When trailing stop active, trailing stop will increase no further than the entry price
    _trailingStopActivation (int): When trailing stop active, trailing will begin once price exceeds base stop loss by this number of points
    _trailingStopOffset (int): When trailing stop active, it will trail the max price achieved by this number of points
  Returns: Initialized data set

f_buildResultStr(_resultType, _price, _resultPoints, _numWins, _pointsWon, _numLoss, _pointsLost)
  f_buildResultStr Helper function to construct a string of resutling data for exit tooltip labels
  Parameters:
    _resultType (string)
    _price (float)
    _resultPoints (float)
    _numWins (int)
    _pointsWon (float)
    _numLoss (int)
    _pointsLost (float)

f_buildResultLabel(data, labelVertical, labelOffset, long)
  f_buildResultLabel Helper function to construct an Exit label for display on the chart
  Parameters:
    data (backtesterData)
    labelVertical (bool)
    labelOffset (int)
    long (bool)

f_updateTrailingStop(_entryPrice, _curPrice, _sl, _tp, trailingStopActivationInput, trailingStopOffsetInput, useTrailingStopToBreakEven)
  f_updateTrailingStop Helper function to advance the trailing stop as price action dictates
  Parameters:
    _entryPrice (float)
    _curPrice (float)
    _sl (float)
    _tp (float)
    trailingStopActivationInput (float)
    trailingStopOffsetInput (float)
    useTrailingStopToBreakEven (bool)
  Returns: Updated stop loss for current price action

f_enterShort(data, entryPrice, fixedStopLoss)
  f_enterShort Helper function to enter a short and collect data necessary for tracking the trade entry
  Parameters:
    data (backtesterData)
    entryPrice (float)
    fixedStopLoss (float)
  Returns: Updated backtest data

f_enterLong(data, entryPrice, fixedStopLoss)
  f_enterLong Helper function to enter a long and collect data necessary for tracking the trade entry
  Parameters:
    data (backtesterData)
    entryPrice (float)
    fixedStopLoss (float)
  Returns: Updated backtest data

f_exitTrade(data)
  f_enterLong Helper function to exit a trade and update/reset tracking data
  Parameters:
    data (backtesterData)
  Returns: Updated backtest data

f_checkTradeConditionForExit(data, condition, curPrice, enableRealTime)
  f_checkTradeConditionForExit Helper function to determine if provided condition indicates an exit
  Parameters:
    data (backtesterData)
    condition (bool): When true trade will exit
    curPrice (float)
    enableRealTime (bool): When true trade will evaluate if barstate is relatime or barstate is confirmed; otherwise just checks on is confirmed
  Returns: Updated backtest data

f_checkTrade(data, curPrice, curLow, curHigh, enableRealTime)
  f_checkTrade Helper function to determine if current price action dictates stop loss or take profit exit
  Parameters:
    data (backtesterData)
    curPrice (float)
    curLow (float)
    curHigh (float)
    enableRealTime (bool): When true trade will evaluate if barstate is relatime or barstate is confirmed; otherwise just checks on is confirmed
  Returns: Updated backtest data

f_fillCell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor, _text_size)
  f_fillCell Helper function to construct result table cells
  Parameters:
    _table (table)
    _column (int)
    _row (int)
    _title (string)
    _value (string)
    _bgcolor (color)
    _txtcolor (color)
    _text_size (string)
  Returns: Table cell

f_prepareStatsTable(data, drawTesterSummary, drawTesterDetails, summaryTableTextSize, detailsTableTextSize, displayRowZero, summaryTableLocation, detailsTableLocation)
  f_fillCell Helper function to populate result table
  Parameters:
    data (backtesterData)
    drawTesterSummary (bool)
    drawTesterDetails (bool)
    summaryTableTextSize (string)
    detailsTableTextSize (string)
    displayRowZero (bool)
    summaryTableLocation (string)
    detailsTableLocation (string)
  Returns: Updated backtest data

backtesterData
  backtesterData - container for backtest performance metrics
  Fields:
    tradesArray (array<string>): Array of strings with entries for each individual trade and its results
    pointsBalance (series float): Running sum of backtest points won/loss results
    drawDown (series float): Running sum of backtest total draw down points
    maxDrawDown (series float): Running sum of backtest total draw down points
    maxRunup (series float): Running sum of max points won over the backtest
    numWins (series int): Number of wins of current backtes set
    numLoss (series int): Number of losses of current backtes set
    pointsWon (series float): Running sum of points won to date
    pointsLost (series float): Running sum of points lost to date
    entrySide (series string): Current entry long/short
    tradeActive (series bool): Indicates if a trade is currently active
    tradeComplete (series bool): Indicates if a trade just exited (due to stop loss or take profit)
    entryPrice (series float): Current trade entry price
    entryTime (series int): Current trade entry time
    sl (series float): Current trade stop loss
    tp (series float): Current trade take profit
    defaultStopLoss (series float): Default trade stop loss to apply
    defaultTakeProfit (series float): Default trade take profit to apply
    useTrailingStop (series bool): Trailing stop enabled
    useTrailingStopToBreakEven (series bool): When trailing stop active, trailing stop will increase no further than the entry price
    trailingStopActivation (series int): When trailing stop active, trailing will begin once price exceeds base stop loss by this number of points
    trailingStopOffset (series int): When trailing stop active, it will trail the max price achieved by this number of points
    resultType (series string): Current trade won/lost
    exitPrice (series float): Current trade exit price
    resultPoints (series float): Current trade points won/lost
    summaryTable (series table): Table to deisplay summary info
    tradesTable (series table): Table to display per trade info

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.