This EA will scan all closed trades in the history. And check which trade closed most recently. If we pass 0 , then it will fetch most recently closed trade, if we pass 1 then it will fetch the trade which closed before that and so on. We have symbol and magic number filter ...
from https://tradingrobots.net/get-nth-closed-trade-from-the-end-in-mt4-expert-advisor-for-metatrader-4/