Skip to content
Algo Trade Analytics Docs

strategy.closedtrades.size() - Pine Script Function

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.

strategy.closedtrades.size(trade_num) → series float
NameTypeDescription
trade_numseries intThe trade number of the closed trade. The number of the first trade is zero.
// Calculates the average profit percentage
for all closed trades.//@version=6
strategy("`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 == 0
strategy.entry("Long", strategy.long)
if bar_index % 20 == 0
strategy.close("Long")// Calculate profit
for both closed trades.profitPct = 0.0
for 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 percent
for both closed trades.avgProfitPct =
nz(profitPct / strategy.closedtrades)
plot(avgProfitPct)