how to close last position

 
hi
please help me for closing the last position .
when I have 3 positions opened, what is the formula so that I close the last position, for exemple last position is sell EUR/USD in 1.2288 and take profite 50 PIP in 1.2238, if the chart retourn opposit the sell and IF chart = 1.2888, close this position .
Reason: