How to calculate Profit and exit long if profit exceeds say 2%

I want to back testing strategy in which I exit the long position when I have made a profit of 2% or more. However, I have been unable to find a way to calculate the profit while using zerodha tradescript.

For simplicity purposes, here is a simple buy script

#buy when open is greater than previous close

And here is the Exit Long script
#sell - I want to add here that sell when the profit is 2% or more? How do I add that code

I’d tried by using a variable in the buy script SET X = OPEN and then using that variable in the exit long script

CLOSE > X * 1.02

But it says that the variable X is not defined in the exit long script.

How do I calculate the profit? There is no well-known variable in trade script.

There seem to be at least one similar query earlier made in July 2015 and the answer that was given was to use the price alerts. But that is completely separate from back testing and the person who posted that question also pointed that out.

But it is a common enough problem that everyone should be facing so I’m probably able to figure this out through documentation.

Thank you for any inputs.