from lseg_analytics.pricing.instruments.ir_swaps import IrMeasure, IrSwapDefinitionInstrument,FloatingRateDefinition, FixedRateDefinition, IrPricingParameters, IrSwapSolvingParameters, IrSwapSolvingVariable, IrSwapSolvingTarget, IrSwapDefinition, IrPricingParameters, InterestRateLegDefinition, BusinessDayAdjustmentDefinition, ScheduleDefinition, OffsetDefinition, PrincipalDefinition
from lseg_analytics.pricing.common import AdjustableDate, RelativeAdjustableDate
from lseg_analytics.pricing._basic_client.models._models import Rate, CapFloorDefinition
from lseg_analytics.pricing.instruments.ir_swaps import solve, value
import json as js

business_day_adj = BusinessDayAdjustmentDefinition(calendars= [] , convention= "ModifiedFollowing")
start = AdjustableDate(date= "2021-03-02")
end =  RelativeAdjustableDate( reference_date= "StartDate", tenor= "10Y")
interest_periods = ScheduleDefinition( start_date= start , end_date= end,frequency= "Annual", roll_convention= "Same")
offset_def = OffsetDefinition( business_day_adjustment= business_day_adj, direction= "Backward", reference_date= "PeriodEndDate", tenor= "0D" )
principal_definition = PrincipalDefinition( amount= 1000000, currency= "EUR")

fixed_rate = Rate( value= -0.5768980764458, unit= "Percentage")
fixedRate_definition = FixedRateDefinition(rate= fixed_rate)
firstIrLegDefinition = InterestRateLegDefinition(rate=fixedRate_definition, interest_periods= interest_periods, payment_offset= offset_def, coupon_day_count= 'Dcb_Actual_360', accrual_day_count= 'Dcb_Actual_360', principal= principal_definition, payer= 'Party1', receiver= 'Party2')

cap_rate = Rate( value= 1, unit= "Percentage")
cap_definition = CapFloorDefinition(effective_date = "2025-01-01", type = "Standard", rate = cap_rate)
capped_Rate_definition = FloatingRateDefinition(index= "LSEG/EUR_EURIBOR_1Y", cap = cap_definition)
secondCappedIrLegDefinition = InterestRateLegDefinition(rate=capped_Rate_definition, interest_periods= interest_periods, payment_offset= offset_def, coupon_day_count= 'Dcb_Actual_360', accrual_day_count= 'Dcb_Actual_360', principal= principal_definition, payer= 'Party2', receiver= 'Party1')

capped_Swap_definition  = IrSwapDefinition( first_leg=firstIrLegDefinition, second_leg= secondCappedIrLegDefinition)
capped_Swap_definition_Instrument = IrSwapDefinitionInstrument(definition=capped_Swap_definition)
pricing_parameters = IrPricingParameters( valuation_date= '2021-03-02')

# value the swap
valuation_response = value(
    definitions=[capped_Swap_definition_Instrument],
    pricing_preferences=pricing_parameters
)

print(js.dumps(valuation_response.analytics[0].valuation.as_dict(), indent=4))

floor_rate = Rate( value= 0.5, unit= "Percentage")
floor_definition = CapFloorDefinition(effective_date = "2025-01-01", type = "Standard", rate = floor_rate)
floored_Rate_definition = FloatingRateDefinition(index= "LSEG/EUR_EURIBOR_1Y", floor = floor_definition)
secondFlooredIrLegDefinition = InterestRateLegDefinition(rate=floored_Rate_definition, interest_periods= interest_periods, payment_offset= offset_def, coupon_day_count= 'Dcb_Actual_360', accrual_day_count= 'Dcb_Actual_360', principal= principal_definition, payer= 'Party2', receiver= 'Party1')

floored_Swap_definition  = IrSwapDefinition( first_leg=firstIrLegDefinition, second_leg= secondFlooredIrLegDefinition)
floored_Swap_definition_Instrument = IrSwapDefinitionInstrument(definition=floored_Swap_definition)
pricing_parameters = IrPricingParameters( valuation_date= '2021-03-02')

# value the swap
valuation_response = value(
    definitions=[floored_Swap_definition_Instrument],
    pricing_preferences=pricing_parameters
)

print(js.dumps(valuation_response.analytics[0].valuation.as_dict(), indent=4))

capped_floored_Rate_definition = FloatingRateDefinition(index= "LSEG/EUR_EURIBOR_1Y", cap = cap_definition, floor = floor_definition)
secondCappedFlooredIrLegDefinition = InterestRateLegDefinition(rate=capped_floored_Rate_definition, interest_periods= interest_periods, payment_offset= offset_def, coupon_day_count= 'Dcb_Actual_360', accrual_day_count= 'Dcb_Actual_360', principal= principal_definition, payer= 'Party2', receiver= 'Party1')

capped_floored_Swap_definition  = IrSwapDefinition( first_leg=firstIrLegDefinition, second_leg= secondCappedFlooredIrLegDefinition)
capped_floored_Swap_definition_Instrument = IrSwapDefinitionInstrument(definition=capped_floored_Swap_definition)
pricing_parameters = IrPricingParameters( valuation_date= '2021-03-02')

# value the swap
valuation_response = value(
    definitions=[capped_floored_Swap_definition_Instrument],
    pricing_preferences=pricing_parameters
)

print(js.dumps(valuation_response.analytics[0].valuation.as_dict(), indent=4))

# prepare the Definition Instrument
inflationSwap_definition = IrSwapDefinitionInstrument(definition = capped_Swap_definition)

# set a solving variable between first and second leg and Fixed Rate or Spread
solving_variable = IrSwapSolvingVariable(leg='SecondLeg', name='Spread')

# Apply solving target(s)
solving_target=IrSwapSolvingTarget(market_value=IrMeasure(value=20000))

# Setup the solving parameter object
solving_parameters = IrSwapSolvingParameters(variable=solving_variable, target=solving_target) 

# instantiate pricing parameters
pricing_parameters = IrPricingParameters(solving_parameters=solving_parameters)

# solve the swap par rate
solving_response_general = solve(
    definitions=[inflationSwap_definition],
    pricing_preferences=pricing_parameters
    )

print(js.dumps(solving_response_general.as_dict(), indent=4))

# prepare the Definition Instrument
inflationSwap_definition = IrSwapDefinitionInstrument(definition = floored_Swap_definition)

# set a solving variable between first and second leg and Fixed Rate or Spread
solving_variable = IrSwapSolvingVariable(leg='SecondLeg', name='Spread')

# Apply solving target(s)
solving_target=IrSwapSolvingTarget(market_value=IrMeasure(value=20000))

# Setup the solving parameter object
solving_parameters = IrSwapSolvingParameters(variable=solving_variable, target=solving_target) 

# instantiate pricing parameters
pricing_parameters = IrPricingParameters(solving_parameters=solving_parameters)

# solve the swap par rate
solving_response_general = solve(
    definitions=[inflationSwap_definition],
    pricing_preferences=pricing_parameters
    )

print(js.dumps(solving_response_general.as_dict(), indent=4))

# prepare the Definition Instrument
inflationSwap_definition = IrSwapDefinitionInstrument(definition = capped_floored_Swap_definition)

# set a solving variable between first and second leg and Fixed Rate or Spread
solving_variable = IrSwapSolvingVariable(leg='SecondLeg', name='Spread')

# Apply solving target(s)
solving_target=IrSwapSolvingTarget(market_value=IrMeasure(value=20000))

# Setup the solving parameter object
solving_parameters = IrSwapSolvingParameters(variable=solving_variable, target=solving_target) 

# instantiate pricing parameters
pricing_parameters = IrPricingParameters(solving_parameters=solving_parameters)

# solve the swap par rate
solving_response_general = solve(
    definitions=[inflationSwap_definition],
    pricing_preferences=pricing_parameters
    )

print(js.dumps(solving_response_general.as_dict(), indent=4))