0% found this document useful (0 votes)
5 views5 pages

Validation et Calcul de Swap

The document contains a VBA code for calculating the value of a swap, including functions to validate inputs, calculate fixed and floating legs, and obtain discount factors. It checks for required fields such as notional amounts, dates, and payment frequencies before performing calculations. The results are displayed in a specified cell with formatting applied.

Uploaded by

Zineb Labied
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Validation et Calcul de Swap

The document contains a VBA code for calculating the value of a swap, including functions to validate inputs, calculate fixed and floating legs, and obtain discount factors. It checks for required fields such as notional amounts, dates, and payment frequencies before performing calculations. The results are displayed in a specified cell with formatting applied.

Uploaded by

Zineb Labied
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Option Explicit

Private Function ValidateInputs() As Boolean

Dim ws As Worksheet

Set ws = [Link]

' Vérifier que les champs obligatoires sont remplis

' Notional

If IsEmpty([Link]("J8").Value) Or IsEmpty([Link]("M8").Value) Then

MsgBox "Les montants nominaux doivent être renseignés", vbExclamation

ValidateInputs = False

Exit Function

End If

' Dates

If IsEmpty([Link]("J9").Value) Or IsEmpty([Link]("J10").Value) Then

MsgBox "Les dates doivent être renseignées", vbExclamation

ValidateInputs = False

Exit Function

End If

' Fréquences de paiement

If IsEmpty([Link]("J12").Value) Or IsEmpty([Link]("M12").Value) Then

MsgBox "Les fréquences de paiement doivent être renseignées", vbExclamation

ValidateInputs = False

Exit Function

End If

ValidateInputs = True

End Function
Private Function CalculateFixedLeg(ByVal notional As Double, ByVal rate As Double, _

ByVal startDate As Date, ByVal maturityDate As Date, _

ByVal frequency As String) As Double

Dim nbPayments As Long

Dim paymentInterval As Long

' Convertir la fréquence en nombre de mois

Select Case UCase(frequency)

Case "QUARTERLY"

paymentInterval = 3

Case "SEMI-ANNUAL"

paymentInterval = 6

Case "ANNUAL"

paymentInterval = 12

Case Else

paymentInterval = 3 ' Default to quarterly

End Select

' Calculer le nombre de paiements

nbPayments = DateDiff("m", startDate, maturityDate) / paymentInterval

Dim i As Long

Dim paymentDate As Date

Dim paymentAmount As Double

Dim discountFactor As Double

paymentAmount = (notional * rate) / (12 / paymentInterval)

For i = 1 To nbPayments

paymentDate = DateAdd("m", i * paymentInterval, startDate)

discountFactor = GetDiscountFactor(paymentDate)
CalculateFixedLeg = CalculateFixedLeg + (paymentAmount * discountFactor)

Next i

End Function

Private Function CalculateFloatingLeg(ByVal notional As Double, _

ByVal startDate As Date, _

ByVal maturityDate As Date, _

ByVal frequency As String) As Double

' Code similaire à CalculateFixedLeg mais utilise les taux forwards MONIA

' À implémenter selon vos besoins spécifiques

CalculateFloatingLeg = 0 ' Placeholder

End Function

Private Function GetDiscountFactor(ByVal paymentDate As Date) As Double

Dim timeInYears As Double

timeInYears = DateDiff("d", Date, paymentDate) / 365

' Obtenir le taux zéro-coupon correspondant

Dim zeroRate As Double

zeroRate = GetInterpolatedRate(timeInYears)

GetDiscountFactor = 1 / ((1 + zeroRate) ^ timeInYears)

End Function

Private Function GetInterpolatedRate(ByVal timeInYears As Double) As Double

Dim ws As Worksheet

Set ws = [Link]

' Définir la plage contenant la courbe MONIA

Dim rateRange As Range

Set rateRange = [Link]("A9:E19") ' Ajuster selon votre structure


' Par défaut, retourner le taux MONIA actuel si on ne trouve pas de correspondance

GetInterpolatedRate = [Link]("B6").Value / 100

End Function

Public Sub CalculateSwapValue()

Dim ws As Worksheet

Set ws = [Link]

If Not ValidateInputs Then Exit Sub

' Récupérer les paramètres

Dim fixedNotional As Double, floatNotional As Double

Dim startDate As Date, maturityDate As Date

Dim fixedRate As Double, floatRate As Double

Dim fixedFreq As String, floatFreq As String

' Lire les valeurs

fixedNotional = [Link]("K7").Value

floatNotional = [Link]("N7").Value

startDate = [Link]("K8").Value

maturityDate = [Link]("K9").Value

fixedRate = [Link]("K10").Value / 100

floatRate = [Link]("N10").Value / 100

fixedFreq = [Link]("K11").Value

floatFreq = [Link]("N11").Value

' Calculer les jambes

Dim fixedLegValue As Double

Dim floatingLegValue As Double


fixedLegValue = CalculateFixedLeg(fixedNotional, fixedRate, startDate, maturityDate, fixedFreq)

floatingLegValue = CalculateFloatingLeg(floatNotional, startDate, maturityDate, floatFreq)

' Appliquer le sens du swap (Pay Fixed / Receive Floating)

If [Link]("I7").Text = "Pay" Then

fixedLegValue = -fixedLegValue

End If

If [Link]("M7").Text = "Pay" Then

floatingLegValue = -floatingLegValue

End If

' Afficher le résultat dans la cellule L15

[Link]("L15").Value = fixedLegValue + floatingLegValue

' Formater le résultat

[Link]("L15").NumberFormat = "#,##0.00 ""DH"""

End Sub

Common questions

Powered by AI

In the CalculateFixedLeg function, the discount factor for each payment is computed by first determining the time in years between the current date and the payment date using the DateDiff function with a 'd' parameter and dividing by 365. The function then retrieves the relevant zero-rate using the GetInterpolatedRate function based on the computed timeInYears. The discount factor is then calculated using this zero-rate by the formula: 1 / ((1 + zeroRate) ^ timeInYears). This gives the present value of each future payment .

The CalculateFixedLeg function ensures proper computation of present values for swap payments by iterating through each payment period within the swap's duration, from the start date to the maturity date. For each period, it calculates a payment amount based on the notional and interest rate, divides this by the payment interval, and discounts each future payment amount back to the present value using a discount factor. This factor is derived from the zero-coupon rates specific to each payment date, thus accurately accounting for the time value of money over multiple periods .

The number of payments in the CalculateFixedLeg function is calculated using the DateDiff function to find the total number of months between the startDate and maturityDate. This total number of months is then divided by the paymentInterval, which is determined based on the payment frequency (e.g., quarterly, semi-annual, annual). This division results in a long integer value that represents the number of payment periods over the life of the swap .

The CalculateSwapValue subroutine determines the net swap value by retrieving relevant swap parameters such as notional amounts, start and maturity dates, fixed and floating rates, and payment frequencies from the worksheet. It then calculates the value of the fixed and floating legs using CalculateFixedLeg and CalculateFloatingLeg functions. The polarity (sign) of these leg values is adjusted based on the direction of the swap (Pay Fixed or Pay Floating) as specified in the worksheet. Finally, the net swap value, which is the sum of the adjusted fixed and floating leg values, is displayed in cell L15 of the worksheet. This output is formatted to display two decimal places followed by "DH" to denote currency .

To enhance the Placeholder implementation of CalculateFloatingLeg, one could integrate the use of forward MONIA rates over the swap term, similar to the fixed leg. This would involve calculating cash flows at each determined interval and applying discount factors to determine present values. Additionally, the function could include adjustments based on historical data volatility or anticipated rate changes to reflect realistic market conditions. Error checks and more detailed validation for rates computation can also be added for robustness .

When the GetInterpolatedRate function does not find a matching interpolation for the given time in years, it defaults to using the current MONIA rate available in cell B6 of the worksheet. This default behavior ensures that the function returns a valid rate even if no direct match is found within the specified range A9:E19 of the worksheet .

The payment interval in the CalculateFixedLeg function is determined based on the frequency parameter by using a Select Case statement. For 'QUARTERLY', the interval is set to 3 months; for 'SEMI-ANNUAL', it is set to 6 months; and for 'ANNUAL', it is 12 months. If the frequency does not match any of these recognized values, the default interval is set to 3 months, assuming a quarterly payment frequency .

The CalculateFixedLeg function defaults to a quarterly payment frequency if an unrecognized value is provided to maintain consistency and ensure the function can proceed without errors. This default option is pragmatic, aligning with a common swap term frequency, thereby minimizing the risk of operational failure. However, this assumption might lead to inaccuracies if the true frequency intended was different (e.g., monthly, annually). Therefore, users must ensure feequency inputs are accurate and valid to avoid unintended financial implications .

The leg values in the CalculateSwapValue subroutine would be negative when the swap direction indicates a payment for that leg. Specifically, if cell I7 contains "Pay", the fixed leg value is negated, and similarly, if cell M7 contains "Pay", the floating leg value is negated. This adjustment reflects the cash outflow associated with making payments in a pay-fixed or pay-floating interest rate swap setup, ensuring the net result accurately represents financial inflow/outflow .

The ValidateInputs function requires several fields to be filled for it to return true. These fields include the notional amounts in cells J8 and M8, the dates in cells J9 and J10, and the payment frequencies in cells J12 and M12. The function checks if these fields are empty using the IsEmpty function. If any of these fields are empty, it displays a message box indicating which fields are not filled and returns false, thereby ensuring that no further calculations proceed until all mandatory information is provided .

You might also like