strategy.opentrades.size() - Pine Script Function
strategy.opentrades.size()
Section titled “strategy.opentrades.size()”Overview
Section titled “Overview”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.
Syntax
Section titled “Syntax”strategy.opentrades.size(trade_num) → series floatParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| trade_num | series int | The trade number of the open 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 open trades.//@version=6strategy("`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 == 0strategy.entry("Long", strategy.long)if bar_index % 20 == 0strategy.close("Long")// Calculate profitfor all open trades.profitPct = 0.0for 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 percentfor all open trades.avgProfitPct =nz(profitPct / strategy.opentrades)plot(avgProfitPct)