Script to calculate Stop Loss

While back testing, I want to know in Sell script at what price last buy took place so if I am making profit/loss I would like to sell off else I would like to keep stocks on hold.