Reputation: 572
I'm building an indicator that opens a trade at a certain condition and should close it at a certain position.
In TradingView you can use a tool that automatically defines the Long or Short Position profit percentage as shown in this example.
I set a condition of entry
entryCond = k > 0 and d > 0 and k < d and bbr > 0 and macd > 0 and signal > 0 and macd > signal and mf < 30 and cci < 0
And now I'm setting a condition for exit
exitCond = k > d and k > 80 and d > 80 and bbr > 1 and mf > 80 and cci > 100
I want to calculate the price of Exist minus the Price of Entry and convert it into a percentage to get a value like in screenshot 6.99
Upvotes: 0
Views: 2807
Reputation: 21342
Since you mentioned you are building an indicator
you need to do the calculations yourself.
The formula is percentage = math.abs(((exit_price - entry_price) / entry_price) * 100)
You need to store your entry and exit prices in a var
variable so they would keep their values each iteration and update them when you enter a new position.
For strategies, one can use the built-in variable strategy.closedtrades.profit
.
Upvotes: 1