strategy.closedtrades.profit() - Pine Script Function
strategy.closedtrades.profit()
Section titled “strategy.closedtrades.profit()”Overview
Section titled “Overview”Returns the profit/loss of the closed trade in the strategy’s account currency, reduced by the trade’s commissions. A positive returned value represents a profit, and a negative value represents a loss.
Syntax
Section titled “Syntax”strategy.closedtrades.profit(trade_num) → series floatParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| trade_num | series int | The trade number of the closed trade. The number of the first trade is zero. |
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6strategy("`strategy.closedtrades.profit()` example")// Enter a long trade every 15 bars, and close a long trade every 20 bars.if bar_index % 15 == 0strategy.entry("Long", strategy.long)if bar_index % 20 == 0strategy.close("Long")//@function Calculates the average gross profit from all available closed trades. avgGrossProfit() => var float result = 0.0if result == 0.0 or strategy.closedtrades > strategy.closedtrades[1] float sumGrossProfit = 0.0for tradeNo = 0 to strategy.closedtrades - 1 sumGrossProfit +=strategy.closedtrades.profit(tradeNo) result := nz(sumGrossProfit / strategy.closedtrades) resultplot(avgGrossProfit(), "Average gross profit")