The quantity of a closed trade; positive for a long trade, negative for a short trade.
strategy.closedtrades.size(trade_num) returns the number of shares, contracts or units the trade held. The sign gives the direction: a long of 10 returns 10 and a short of 10 returns -10.
When one exit closes only part of an entry, the closed part becomes its own trade with the quantity that was closed, and the rest stays open.
Syntax
strategy.closedtrades.size(trade_num: series int) -> series floatArguments
| Name | Type | Default | Description |
|---|---|---|---|
| trade_num | series int | required | Zero-based index of the closed trade. 0 is the first trade the strategy closed and strategy.closedtrades - 1 is the most recent. |
Returns
series float: The trade's quantity: positive for a long, negative for a short. na when the trade does not exist.
Remarks
- Returns na when trade_num is negative, is not below
strategy.closedtrades, or points at a trade that has dropped out of the history (seestrategy.closedtrades.first_index). - Use
math.abs(strategy.closedtrades.size(i)) when only the magnitude matters, andmath.signto read the direction.
Examples
//@version=6
strategy("Trade directions", overlay = false)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
longs = 0
shorts = 0
for i = strategy.closedtrades.first_index to strategy.closedtrades - 1
if strategy.closedtrades.size(i) > 0
longs += 1
else
shorts += 1
plot(longs, "Long trades", color.green)
plot(shorts, "Short trades", color.red)//@version=6
strategy("Position size", overlay = false)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
plot(strategy.position_size, "Position", color.blue, style = plot.style_stepline)
plot(strategy.closedtrades, "Closed", color.gray, style = plot.style_stepline)See also
strategy.opentrades.sizestrategy.position_sizestrategy.closedtrades.profitstrategy.closedtrades.entry_price
Runs on Sidechair