I'm currently learning, i.e. I'm working my way through the pine-script manual, trying out a lot of things and looking to see whether there are already solutions to my questions. But now I can't find a satisfactory solution to the following point: I want to display the trade numbers, which can be found in the first column in the strategy tester in the 'List of trades' tab, on the chart both at entry and at exit. I tried the following: The chart on tradingview is: BTCUSDTPERP PERPETUAL MIX CONTRACT.30.BITGET is the corresponding 30min chart with the following script (essentially from the manual):
strategy("Market order strategy", overlay = true)
// Calculate a 14-bar and 28-bar moving average of `close` prices.
float sma14 = ta.sma(close, 14)
float sma28 = ta.sma(close, 28)
float tradeNum = 0
int profitDistanceInput = input.int(10000, "Profit distance, in ticks", 1)
int lossDistanceInput = input.int(5000, "Loss distance, in ticks", 1)
// Place a market order to enter a long position when `sma14` crosses over `sma28`, exit with SL/TP.
if ta.crossover(sma14, sma28)
tradeNum := ta.cum(strategy.opentrades)
strategy.entry("L" + str.tostring(tradeNum), strategy.long)
strategy.exit("LE" + str.tostring(tradeNum), "L" + str.tostring(tradeNum), profit=profitDistanceInput, loss=lossDistanceInput)
// Place a market order to enter a short position when `sma14` crosses under `sma28`, exit with SL/TP.
if ta.crossunder(sma14, sma28)
tradeNum := ta.cum(strategy.opentrades)
strategy.entry("S" + str.tostring(tradeNum), strategy.short)
strategy.exit("SE" + str.tostring(tradeNum), "S" + str.tostring(tradeNum), profit=profitDistanceInput, loss=lossDistanceInput)
The numbers shown on the chart have nothing to do with the actual trades and I have no idea how to change that.
There are several issues with your code example:
tradeNum
variable is re-initialized with a value of 0 on each bar. Declaring the variable with the var keyword will ensure the script preserves its value from the previous bar instead of resetting it to 0.float
for the trade number, as it is logically always an integer
.ta.cum()
function accumulates values only within the logical block it enters, ignoring other scopes. Both ta.cum()
calls overwrite the existing tradeNum
value rather than stacking them, which contradicts the intended behavior described in your issue. Additionally, they consider only active trades that are not yet closed, ignoring any closed trades. In this case, both calls are redundant and will not help you achieve your goal.Instead, initialize the tradeNum
variable as a var int
and increment it by +1 for every trade.
//@version=6
strategy("Market order strategy", overlay = true, close_entries_rule = "ANY")
float sma14 = ta.sma(close, 14)
float sma28 = ta.sma(close, 28)
var int tradeNum = 0
int profitDistanceInput = input.int(10000, "Profit distance, in ticks", 1)
int lossDistanceInput = input.int(5000, "Loss distance, in ticks", 1)
if ta.crossover(sma14, sma28)
tradeNum += 1
string longNumEntryStr = str.format('L {0}', tradeNum)
string longNumExitStr = str.format('LE {0}', tradeNum)
strategy.entry(longNumEntryStr, strategy.long)
strategy.exit(longNumExitStr, longNumEntryStr, profit=profitDistanceInput, loss=lossDistanceInput)
if ta.crossunder(sma14, sma28)
tradeNum += 1
string shortNumEntryStr = str.format('S {0}', tradeNum)
string shortNumExitStr = str.format('SE {0}', tradeNum)
strategy.entry(shortNumEntryStr, strategy.short)
strategy.exit(shortNumExitStr, shortNumEntryStr, profit=profitDistanceInput, loss=lossDistanceInput)
Notes:
When the strategy places an exit order, if an opposite-direction entry order is triggered before the price reaches the specified profit=
or loss=
in ticks, the exit order will use the ID of the opposite-direction entry order instead.
In case of pyramiding and multiple opened orders in the same direction, to bypass the FIFO
(First-In-First-Out) regulatory requirement, which is applied to strategies by default, set the close_entries_rule=
parameter to ANY
.