pAulseperformance

Portfolio Backtester Engine

█ OVERVIEW

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.


█ HISTORY

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`.


█ THE PROBLEM

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.


█ THE SOLUTION

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.


█ HOW TO USE

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.


█ FEATURES

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.)


█ THANKS

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.



═════════════════════════════════════════════════════════════════════════
Release Notes:
Added Signal Reversal Option
Added MACD Filter
Added Supertrend inputs
Added Channel Breakout Strategy
Cleaned some code
Removed some rounding issues
Release Notes:
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.
Release Notes:
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
Release Notes:
Had to make some changes because I reached an input limit when trying to add in more entry strategies for this tool.
Decided to remove all the pre-built strategies and their inputs and only include TWO template custom strategies.
This leaves more room for users to develop their own strategies without running into a vague "internal pine compilation error" and leaves more room to add in additional filters and conditions.
Of course I would rather have a huge list of built in functions, but that won't work so I feel it is a more practical tool this way. That's the way I have been using it at least.

I simply add basic entry strategy logic and then go through the parameters that make the most sense until I find a robust strategy. Do this enough times and you'll find it to be a very effective method of strategy building.

I also added labels to show when Signals are being generated. This should help users identify their strategy logic is working properly. It will also give insight into how the tool is working. I realize it is quite overwhelming.

  • Updated to Version 5
  • Portfolio now overlays onto chart
  • Added labels for entry/exits (only works for charted asset)
  • Removed Entry strategies.
  • Fixed some bugs

Trade, Code, & Travel.

www.pinescriptstrategy.com
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?