Pine Script如何计算或记忆上一笔交易的盈亏情况?

How can I calculate or memorize the profit and loss status of the last transaction in Pine Script?

Pine Script如何计算或记忆上一笔交易的盈亏情况? 例如, 如果我的上一笔交易亏损了 10%,我想在下一笔交易中使用不同的条件。所以我需要能够计算或记住最后一笔交易。 谢谢你的帮助。

你需要一个内置函数和一个内置变量。

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.

所以,您要做的是将交易数量输入 strategy.closedtrades.profit() 函数。

这是一个例子:

//@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)