Portfolio Backtester Engine


Portfolio Backtester Engine (PBTE). This tool will allow you to backtest strategies across multiple securities at once. Allowing you to easier understand if your strategy is robust. If you are familiar with the PineCoders backtesting engine, then you will find this indicator pleasant to work with as it is an adaptation based on that work. Much of the functionality has been kept the same, or enhanced, with some minor adjustments I made on the account of creating a more subjectively intuitive tool.


The original purpose of the backtesting engine (`BTE`) was to bridge the gap between strategies and studies. Previously, strategies did not contain the ability to send alerts, but were necessary for backtesting. Studies on the other hand were necessary for sending alerts, but could not provide backtesting results . Often, traders would have to manage two separate Pine scripts to take advantage of each feature, this was less than ideal.

The `BTE` published by PineCoders offered a solution to this issue by generating backtesting results under the context of a study(). This allowed traders to backtest their strategy and simultaneously generate alerts for automated trading, thus eliminating the need for a separate strategy() script (though, even converting the engine to a strategy was made simple by the PineCoders!).

Fast forward a couple years and PineScript evolved beyond these issues and alerts were introduced into strategies. The BTE was not quite as necessary anymore, but is still extremely useful as it contains extra features and data not found under the strategy() context. Below is an excerpt of features contained by the BTE:

More than `40` built-in strategies,
Customizable components,
Coupling with your own external indicator,
Simple conversion from Study to Strategy modes,
Post-Exit analysis to search for alternate trade outcomes,
Use of the Data Window to show detailed bar by bar trade information and global statistics, including some not provided by TV backtesting,
Plotting of reminders and generation of alerts on in-trade events.

Before I go any further, I want to be clear that the BTE is STILL a good tool and it is STILL very useful. The Portfolio Backtesting Engine I am introducing is only a tangental advancement and not to be confused as a replacement, this tool would not have been possible without the `BTE`.


Most strategies built in Pine are limited by one thing. Data. Backtesting should be a rigorous process and researchers should examine the performance of their strategy across all market regimes; that includes, bullish and bearish markets, ranging markets, low volatility and high volatility . Depending on your TV subscription The Pine Engine is limited to 5k-20k historical bars available for backtesting, which can often leave the strategy results wanting. As a general rule of thumb, strategies should be tested across a quantity of historical bars which will allow for at least 100 trades. In many cases, the lack of historical bars available for backtesting and frequency of the strategy signals produces less than 100 trades, rendering your strategy results inconclusive.


In order to be confident that we have a robust strategy we must test it across all market regimes and we must have over 100 trades. To do this effectively, researchers can use the Portfolio Backtesting Engine (PBTE).

By testing a strategy across a carefully selected portfolio of securities, researchers can now gather 5k-20k historical bars per security! Currently, the PTBE allows up to 5 securities, which amounts to 25k-100k historical bars.


1 — Add the indicator to your chart.
 • Confirm inputs. These will be the most important initial values which you can change later by clicking the gear icon ⚙ and opening up the settings of the indicator.

2 — Select a portfolio.
 • You will want to spend some time carefully selecting a portfolio of securities.
   • Each security should be uncorrelated.
   • The entire portfolio should contain a mix of different market regimes.

   You should understand that strategies generally take advantage of one particular type of market regime. (trending, ranging, low/high volatility )

   For example, the default RSI strategy is typically advantageous during ranging markets, whereas a typical moving average crossover strategy is advantageous in trending markets.
   If you were to use the standard RSI strategy during a trending market, you might be selling when you should be buying.
   Similarily, if you use an SMA crossover during a ranging market, you will find that the MA's may produce many false signals.

   Even if you build a strategy that is designed to be used only in a trending market, it is still best to select a portfolio of all market regimes
   as you will be able to test how your strategy will perform when the market does something unexpected.

3 — Test a built-in strategy or add your own.
   • Navigate to gear icon ⚙ (settings) of strategy.
   • Choose your options.
      • Select a Main Entry Strat and Alternate Entry Strat.
         • If you want to add your own strategy, you will need to modify the source code and follow the built-in example.
         • You will only need to generate (buy 1 / sell -1/ neutral 0) signals.
      • Select a Filter, by default these are all off.
      • Select an Entry Stop - This will be your stop loss placed at the trade entry.
      • Select Pyamiding - This will allow you to stack positions. By default this is off.
      • Select Hard Exits - You can also think of these as Take Profits.
   • Let the strategy run and take note of the display tables results.
      • Portfolio - Shows each security.
         • The strategy runs on each asset in your portfolio.
         • The initial capital is equally distributed across each security.
          So if you have 5 securities and a starting capital of 100,000$ then each security will run the strategy starting with 20,000$
          The total row will aggregate the results on a bar by bar basis showing the total results of your initial capital.
      • Net Profit ( NP ) - Shows profitability.
      • Number of Trades (#T) - Shows # of trades taken during backtesting period.
         • Typically will want to see this number greater than 100 on the "Total" row.
      • Average Trade Length ( ATL ) - Shows average # of days in a trade.
      • Maximum Drawdown (MD) - Max peak-to-valley equity drawdown during backtesting period.
         • This number defines the minimum amount of capital required to trade the system.
         • Typically, this shouldn’t be lower than 34% and we will want to allow for at least 50% beyond this number.
      • Maximum Loss (ML) - Shows largest loss experienced on a per-trade basis.
         • Normally, don’t want to exceed more than 1-2 % of equity.
      • Maximum Drawdown Duration ( MDD ) - The longest duration of a drawdown in equity prior to a new equity peak.
         • This number is important to help us psychologically understand how long we can expect to wait for a new peak in account equity.
      • Maximum Consecutive Losses (MCL) - The max consecutive losses endured throughout the backtesting period.
         • Another important metric for trader psychology, this will help you understand how many losses you should be prepared to handle.
      • Profit to Maximum Drawdown (P:MD) - A ratio for the average profit to the maximum drawdown.
         • The higher the ratio is, the better. Large profits and small losses contribute to a good PMD .
         • This metric allows us to examine the profit with respect to risk.
      • Profit Loss Ratio (P:L) - Average profit over the average loss.
         • Typically this number should be higher in trend following systems.
         • Mean reversion systems show lower values, but compensate with a better win %.
      • Percent Winners (% W)- The percentage of winning trades.
         • Trend systems will usually have lower win percentages, since statistically the market is only trending roughly 30% of the time.
         • Mean reversion systems typically should have a high % W.
      • Time Percentage (Time %) - The amount of time that the system has an open position.
         • The more time you are in the market, the more you are exposed to market risk, not to mention you could be using that money for something else right?
      • Return on Investment ( ROI ) - Your Net Profit over your initial investment, represented as a percentage.
         • You want this number to be positive and high.
      • Open Profit (OP) - If the strategy has any open positions, the floating value will be represented here.
      • Trading Days ( TD ) - An important metric showing how many days the strategy was active.
         • This is good to know and will be valuable in understanding how long you will need to run this strategy in order to achieve results.


These are additional features that extend the original `BTE` features.
- Portfolio backtesting.
- Color coded performance results.
- Circuit Breakers that will stop trading.
- Position reversals on exit. (Simulating the function of always in the market. Similar to strategy.entry functionality)
- Whipsaw Filter
- Moving Average Filter
- Minimum Change Filter
- % Gain Equity Exit
- Popular strategies, ( MACD , MA cross, supertrend)

Below are features that were excluded from the original `BTE`
- 2 stage in-trade stops with kick-in rules (This was a subjective decision to remove. I found it to be complex and thwarted my use of the `BTE` for some time.)
- Simple conversion from Study to Strategy modes. (Not possible with multiple securities)
- Coupling with your own external indicator (Not really practical to use with multiple securities, but could be used if signals were generated based on some indicator which was not based on the current chart)
- Use of the Data Window to show detailed bar by bar trade information and global statistics.
- Post Exit Analysis.
- Plotting of reminders and generation of alerts on in-trade events.
- Alerts (These may be added in the future by request when I find the time.)


The whole PineCoders team for all their shared knowledge and original publication of the BTE and Richard Weismann for his ideas on building robust strategies.

릴리즈 노트: Added Signal Reversal Option
Added MACD Filter
Added Supertrend inputs
Added Channel Breakout Strategy
Cleaned some code
Removed some rounding issues
릴리즈 노트: Fixed bug where positions would reverse when getting stopped out.
Fixed the exit calculation to calculate values based on stopped out level when stopped out, previously stopped out conditions calculated exit fills on next bar, This should greatly improve accuracy of engine!
Decided to add back In Trade Stops, And I added a Move to Break Even Option. The break even option must use the "5. Price >= Entry +/- %" In trade kick in option. Otherwise the break even stop loss will be immediately set.
릴리즈 노트: 1. Added a Smoothed Rate of Change Transition Hard Exit Option. It takes the logarithmic percentage difference between the current bar and X bars ago and is smoothed by a EMA. Then it looks for rising or falling values. If you are in a long position and the values have been rising for X amount of bars and then they stop rising, the exit will occur. The opposite is true for short positions.
2. I removed some, but not all of the unused variables. The pine compiler seems to be running into compilation issues when I try to add in more features, so I seem to be hitting an unknown limit. I will continue to prune things and improve the efficiency.
3. I improved the table coloring function, by using the new built-in color.from_gradient function
오픈 소스 스크립트

진정한 TradingView의 정신에 따라, 이 스크립트의 작성자는 스크립트를 오픈소스로 게시했기에 거래자들이 이해하고 확인할 수 있습니다. 작가님께 건배! 스크립트를 무료로 사용할 수 있지만, 게시물에서 이 코드를 재사용하는 것은 하우스룰을 따릅니다. 당신은 스크립트를 차트에 사용하기 위해 그것을 즐겨찾기 할 수 있습니다.

차트에 이 스크립트를 사용하시겠습니까?


500 코인
+4 응답
Thank you!!! :-))
500 코인
+3 응답
pAulseperformance Meowella7777
@Meowella7777, my pleasure! :)
+1 응답
HermanBrummer pAulseperformance
@pAulseperformance, Nice work man! I took your course and really loved it :) Good to see you here!
100 코인
+1 응답
I have always appreciated the BTE, and this gives some great additional features. Its also helping me learn so thanks for the great script!
500 코인
This is amazing! Great job
+2 응답
@tweakerID, Thanks! Theirs still a lot of things todo, but I wanted to release this now so people could start working with it.
good job
+1 응답
pAulseperformance bappykhalidhasan0