Hi everyone

I have developed a portfolio on excel and learnt quite a lot but I am blocking on this one, I am trying to get the average stock price with the FIFO method (first in, first out).

A B C D E F G H I J
Date Type Name Ticker Qty Price FX Rate Tax Fee Cashflow
28/04/2022 Buy Petroleo Brasileiro SA Petrobras NYSE:PBR 112 $13.34 1.240 0 3.42 £1,204.84
02/12/2021 Sell Petroleo Brasileiro SA Petrobras NYSE:PBR -85 $10.27 1.330 0 0 £654.96
13/09/2021 Buy Petroleo Brasileiro SA Petrobras NYSE:PBR 85 $10.20 1.380 0 0.68 £627.78
03/03/2021 Buy Petroleo Brasileiro SA Petrobras NYSE:PBR 100 $7.27 1.350 0 0 £536.56
13/04/2020 Buy Petroleo Brasileiro SA Petrobras NYSE:PBR 18 $8.15 1.350 0 0 £108.32

I have one page for my position and one page for my transactions: Summary_OSV

My current formula is: =SUMPRODUCT(Summary_OSV!D:D=B6,(Summary_OSV!B:B="Buy")-(Summary_OSV!B:B="Sell"),Summary_OSV!J:J)
Which only gives me the average cost, but it is not accurate because of the (small) gains made on 2/12/2021.

Would anyone know how to do this?
Thank you very much in advance for your help.