[VBA] Impact a Stock of data using FIFO with additional condition

yorgosnc

New Member
Joined
Dec 4, 2015
Messages
33
Hi guys, need some help to finish my code to execute FIFO (First In First Out) in stock of data.

So basically I have a big stock of trades (Equity Swaps by the away) and I receive in a daily basis a few number of new trades that can be: a Unwind, meaning, a partial or a full liquidation of an existing deal or a real new deal. So my big problem is for the unwind deals because I have to execute a FIFO impact based on the same counterparty, same equity and in the oldest deal. So I will give an example, in the sheet "Trades" is my new trades for the day, the first line is a UNWIND deal (Possible to see in Column J or K) for the Equity "ALFAA MM Equity" , so what to I need to do is go in my sheet "Swaps" find all swap from the same Equity (Column "E") and Counterparty (Column "B"), find the oldest deal by the trade date, in this case the line 26 (Trade date 20/08 get in the sheet "Trades" the quantity that it's liquidating and return in this specific deal and decrease the quantity by this amount.

When it's not a UNWIND deal, I just need to take the info for the Trades and insert in the next line after the last row in the sheet "Swaps". A few points here:
- It's possible to have a day trade, so in the sheet "trades" will be a new trade and a Unwind trade with the same amount -> In this case I need to exclude them and not impact
- Still in this "day trade matter", it's possible to have a day trade that before I impact my stock, I need to find the net quantity of the trade it self.

Below is the code that I have so far, for the day trade deals the only thing that I did it's to color the lines of possible day trades to analyse by my self but if anyone has an idea how to do this plus the FIFO code, I really appreciate. I was trying to find a match value and set a range, and after that find the oldest one using Redim Preserve. I'm uncertain if it's that best way to pursue this, please be free to suggest another way. Didn't find a way here to post my spreadh sheet I will try to put the layout below:

STOCK DEALS
ManagerCounterPartyB/SFundEquityQaunittyTrade DateNET PriceSpreadEff./Reset Date
BTGBOFABBTG FUNDBNP FP EQUITY10,773.0020/08/201851.99000000.300%03/10/2018

<tbody>
</tbody>
BTGCITIBBTG FUNDSUZB3 BZ EQUITY44,500.0020/09/201848.00000000.750%04/10/2018
BTGCITIBBTG FUNDTRPL4 BZ EQUITY9,400.0020/08/201858.55000000.750%04/10/2018
BTGCITISBTG FUNDALFAA MM EQUITY81,590.0020/08/201823.88000000.500%03/10/2018

<tbody>
</tbody>

NEW TRADES
DATEFUND B/STICKERQTYGROSS PRICECOMM (BPS)NET PRICECURNCYRATESPREADSETTLE DATEEND DATERESET FREQDEALERBASECASHCONTA LIQ.SETTLE DEALER
05/10/2018EDGE MASTER FUNDBALFAA MM EQUITY 5,155.0048.22721223.2901MXNUNWINDUNWIND09/10/201826/09/2019MonthlyMORGAN STANLEY3,197.17EDMPBMXNMORGAN STANLEY
05/10/2018EDGE MASTER FUNDBATD/B CN EQUITY 1,625.0048.22722.0748.2372USDUS0001M INDEX0.450%10/10/201827/09/2019MonthlyGOLDMAN SACHSAct/3600.00-GOLDMAN SACHS

<tbody>
</tbody>

Code:
Sub TesteFIFO()
Dim rngBoletas As Range
Dim rngFundos As Range
Dim rngContraparte As Range
Dim rngEquity As Range
Dim rngAux As Range
Dim rngNovo As Range
Dim lngLinhaPainel As Long
Dim lngLinhaSwaps As Long
Dim varQuantidade As Variant

Application.ScreenUpdating = False
shtTrades.Select
lngLinhaPainel = fUltimaLinha(shtTrades)

''Impact trades in stock
'Loop Trades and identify day trades
shtTrades.Select
Set rngtrades = Range("D2:D" & lngLinhaPainel)
Set rngsides = Range("C2:C" & lngLinhaPainel)
Range("A2:A" & lngLinhaPainel).EntireRow.Interior.Color = RGB(255, 255, 255)
LRowSwap = fUltimaLinha(shtSwaps)

i = 0
For Each Cell In rngtrades
If WorksheetFunction.CountIf(rngtrades, Cell) > 1 Then
    If WorksheetFunction.CountIfs(rngtrades, Cell, rngsides, Cell.Offset(0, -1)) <> WorksheetFunction.CountIf(rngtrades, Cell) Then
    Cell.EntireRow.Interior.Color = RGB(255, 192, 0)
    i = i + 1
    Else
    'Verifica se é UNWIND ou Trade Novo
        'Se o trade for UNWIND faz o FIFO
        If Cell.Offset(0, 6).Value = "UNWIND" Then
        trdequity = Cell.Offset(0, 0)
        trdqtd = Cell.Offset(0, 1)
        trdnetprice = Cell.Offset(0, 4)
        trdcontrprt = Cell.Offset(0, 15)
        ReDim ArrayEquity(1 To 1000) As Variant
        
        shtSwaps.Activate
        CurrentRow = 1
        For r = 2 To LRowSwap
            j = 1
            If Range("E" & CurrentRow).Value = trdequity And Range("B" & CurrentRow).Value = trdcontrprt Then
            ArrayEquity(j) = Range("G" & r).Value
            j = j + 1
            Else
            End If
            ReDim Preserve ArrayEquity(1 To j - 1)
        Next r
        
        Else
        End If
          
End If
End If
Next Cell

'Aviso que há possíveis day trades para serem tratados manualmente
If i > 0 Then
MsgBox "Verificar se os swaps marcados são daytrades e tratar manualmente.", vbExclamation
Else
End If

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,214,795
Messages
6,121,624
Members
449,041
Latest member
Postman24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top