How can I calculate or memorize the profit and loss status of the last transaction in Pine Script? For example, If my last trade lost 10%, I want to use a different condition in the next trade. So I need to be able to calculate or memorize the last transaction. Thank you for helpings.
You need one built-in function and one built-in variable for that.
strategy.closedtrades.profit(): Returns the profit/loss of the closed trade. Losses are expressed as negative values.
strategy.closedtrades: Number of trades, which were closed for the whole trading interval.
So, what you want to do is feed number of trades into strategy.closedtrades.profit()
function.
Here is an example:
//@version=5
strategy("My Strategy", overlay=true, pyramiding=1)
pl = strategy.closedtrades.profit(strategy.closedtrades - 1) + strategy.closedtrades.commission(strategy.closedtrades - 1)
strategy.entry("buy", strategy.long, when = open[1] > close[1])
strategy.entry("sell", strategy.short, when = open[1] < close[1])
plot(pl, color=color.orange)
plot(strategy.closedtrades, color=color.red)