OPEN-SOURCE SCRIPT

Monthly Strategy Performance Table

מעודכן
What Is This?

This script code adds a Monthly Strategy Performance Table to your Pine Script strategy scripts so you can see a month-by-month and year-by-year breakdown of your P&L as a percentage of your account balance.

The table is based on realized equity rather than open equity, so it only updates the metrics when a trade is closed.

That's why some numbers will not match the Strategy Tester metrics (such as max drawdown), as the Strategy Tester bases metrics like max drawdown on open trade equity and not realized equity (closed trades).

The script is still a work-in-progress, so make sure to read the disclaimer below. But I think it's ready to release the code for others to play around with.

How To Use It

The script code includes one of my strategies as an example strategy. You need to replace my strategy code with your own. To do that just copy the source code below into a blank script, delete lines 11 -> 60 and paste your strategy code in there instead of mine. The script should work with most systems, but make sure to read the disclaimer below.

It works best with a significant amount of historical data, so it may not work very effectively on intraday timeframes as there is a severe limitation of available bars on TradingView. I recommend using it on 4HR timeframes and above, as anything less will produce very little usable data. Having a premium TradingView plan will also help boost the number of available bars.

You can hover your mouse over a table cell to get more information in the form of tooltips (such as the Long and Short win rate if you hover over your total return cell).

Credit

The code in this script is based on open-source code originally written by QuantNomad, I've made significant changes and additions to the original script but all credit for the idea and especially the display table code goes to them - I just built on top of it:

Monthly Returns in PineScript Strategies


Why Did I Make This?

None of this is trading or investment advice, just my personal opinion based on my experience as a trader and systems developer these past 6+ years:

The TradingView Strategy Tester is severely limited in some important ways. And unless you use complex Excel formulas on exported test data, you can't see a granular perspective of your system's historical performance.

There is much more to creating profitable and tradeable systems than developing a strategy with a good win rate and a good return with a reasonable drawdown.

Some additional questions we need to ask ourselves are:

  • What did the system's worst drawdown look like?
  • How long did it last?
  • How often do drawdowns occur, and how quickly are they typically recovered?
  • How often do we have a break-even or losing month or year?
  • What is our expected compounded annual growth rate, and how does that growth rate compare to our max drawdown?


And many more questions that are too long to list and take a lifetime of trading experience to answer.

Without answering these kinds of questions, we run the risk of developing systems that look good on paper, but when it comes to live trading, we are uncomfortable or incapable of enduring the system's granular characteristics.

This Monthly Performance Table script code is intended to help bridge some of that gap with the Strategy Tester's limited default performance data.

Disclaimer

I've done my best to ensure the numbers this code outputs are accurate, and according to my testing with my personal strategy scripts it appears to work fine. But there is always a good chance I've missed something, or that this code will not work with your particular system.

The majority of my TradingView systems are extremely simple single-target systems that operate on a closed-candle basis to minimize many of the data reliability issues with the Strategy Tester, so I was unable to do much testing with multiple targets and pyramiding etc.

I've included a Debug option in the script that will display important data and information on a label each time a trade is closed. I recommend using the Debug option to confirm that the numbers you see in the table are accurate and match what your strategy is actually doing.

Always do your own due diligence, verify all claims as best you can, and never take anyone's word for anything.

Take care, and best of luck with your trading :)

Kind regards,
Matt.

PS. If you're interested in learning how this script works, I have a free hour-long video lesson breaking down the source code - just check out the links below this script or in my profile.
הערות שחרור
(Disclaimer: this script is still a work-in-progress, and it cannot possibly take into account every nuance of every tradeable market or asset class, so use at your own risk and always take the time to verify the numbers are correct)

Huge thanks to Marc (tradingview.com/u/Mrcrbw/) for providing major improvements and fixes to the code that made this script much better! The script now *appears* to work on markets it didn't work on before (like crypto).

Added/changed:
- Pages (select a 'page' of results to display, useful for markets with decades of data)
- Page size (configure X number of rows/years to display per page - 20 by default)
- Text size (configure size of table text, set to small by default)
- Removed all table inputs displaying on status bar and cluttering up indicator values
הערות שחרור
Latest Changes:
- Added 'DD Bars' to strategy stats. This metric counts the maximum number of bars the system endured during its longest drawdown (note: longest drawdown doesn't necessarily correspond to "worst" drawdown)
- Added Average Max Drawdown metric (check MaxDD tooltip and DD Bars tooltip for averages)
- Fixed Short trade count being equal to Long trade count on Return tooltip
הערות שחרור
Fixed compiler warning and explicitly defined some data types.
הערות שחרור
Added LeonardoUribe's suggestion from the comment section:

The only suggestion I have is to change the CAGR calculation on line 391 to:



it solves the problem of CAGR underestimation in the event that the strategy date period is small (months or a few years).
הערות שחרור
Fixed code unintentionally double-counting drawdown bars whenever a trade was closed during drawdown
הערות שחרור
Added annual commission drag estimation to "Return" tooltip (this number is the minimum ROR to break-even after commissions, and is a rough estimate based on whatever commission pricing and starting balance you use with the strategy settings) - formula: Total Commission Costs / Initial Capital / Total Years in Test
הערות שחרור
Improved the commission tracking for commission drag estimation (the script now uses strategy.closedtrades.commission() instead of whatever convoluted method I was using before). Also added some debug plots to the data window.
backtestingeducationalMonthly ChartsperformancePine utilitiesstatisticsstrategytabletoolyearly

סקריפט קוד פתוח

ברוח TradingView אמיתית, מחבר הסקריפט הזה פרסם אותו בקוד פתוח, כך שסוחרים יוכלו להבין ולאמת אותו. כל הכבוד למחבר! אתה יכול להשתמש בו בחינם, אבל השימוש החוזר בקוד זה בפרסום כפוף לכללי הבית. אתה יכול להכניס אותו למועדפים כדי להשתמש בו בגרף.

רוצה להשתמש בסקריפ זה בגרף?


My Resources Hub: theartoftrading.com
FREE YouTube Lessons: rebrand.ly/zyt
FREE Pine Script Basics Course: rebrand.ly/zpsbc
Pine Script MASTERY Course: rebrand.ly/zpsmc
My Indicators & Strategies: rebrand.ly/zmisc
גם על:

כתב ויתור