Skip to content
Algo Trade Analytics Docs

strategy.opentrades.size() - Pine Script Function

Returns the direction and the number of contracts traded in the open trade. If the value is > 0, the market position was long. If the value is < 0, the market position was short.

strategy.opentrades.size(trade_num) → series float
NameTypeDescription
trade_numseries intThe trade number of the open trade. The number of the first trade is zero.
// Calculates the average profit percentage
for all open trades.//@version=6
strategy("`strategy.opentrades.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 all open trades.profitPct = 0.0
for tradeNo = 0 to strategy.opentrades - 1 entryP =
strategy.opentrades.entry_price(tradeNo) exitP = close profitPct += (exitP - entryP) / entryP * strategy.opentrades.size(tradeNo) * 100// Calculate average profit percent
for all open trades.avgProfitPct =
nz(profitPct / strategy.opentrades)
plot(avgProfitPct)