strategy.closedtrades.size() - Pine Script Function
strategy.closedtrades.size()
Section titled “strategy.closedtrades.size()”Overview
Section titled “Overview”Returns the direction and the number of contracts traded in the closed trade. If the value is > 0, the market position was long. If the value is < 0, the market position was short.
Syntax
Section titled “Syntax”strategy.closedtrades.size(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”// Calculates the average profit percentagefor all closed trades.//@version=6strategy("`strategy.closedtrades.size` Example 2")// Strategy calls to enter long trades every 15 bars and exit long trades every 20 bars.if bar_index % 15 == 0strategy.entry("Long", strategy.long)if bar_index % 20 == 0strategy.close("Long")// Calculate profitfor both closed trades.profitPct = 0.0for tradeNo = 0 to strategy.closedtrades - 1 entryP =strategy.closedtrades.entry_price(tradeNo)exitP = strategy.closedtrades.exit_price(tradeNo)profitPct += (exitP - entryP) / entryP * strategy.closedtrades.size(tradeNo) * 100// Calculate average profit percentfor both closed trades.avgProfitPct =nz(profitPct / strategy.closedtrades)plot(avgProfitPct)