I am looking to get the "Avg # bars in trades" information in a pine script strategy. This information is displayed in the strategy overview tab in tradingview. Is this metric available as a variable in pine script. I need this to calculate CAGR in the script.
(The strategy information displayed does not seem to include CAGR, if there is already a way to get it to display CAGR, that would be even better. I would like to know it too). Thank you
There is no built-in variable for that but it is not difficult to calculate this.
If strategy.position_size != 0
, you know that you are in a trade.
You can use a var
variable to count the number of bars you are in a trade and increment it whenever strategy.position_size != 0
.
Then you can use the strategy.closedtrades
built-in variable to get the number of closed trades and divide your total count by the number of trades.
You might want to add a special handling for the open trades. You might want to wait adding your trade bar count to the global count until the trade is closed. That's up to what you want to do.