Ejemplo n.º 1
0
def test_get_im_equityoption():
    option2 = EquityOption(notional=100,
                           strike=50,
                           maturity=ql.Period(3, ql.Years),
                           tradeType=TradeType.CALL,
                           tradeDirection=TradeDirection.LONG,
                           underlying=Stock.ADS)
    simm = SIMM()
    simm.add_trades(option2)
    im = simm.get_im_receive()
    asdf = 1
Ejemplo n.º 2
0
 def initialMargining(self, value: InitialMargining):
     self.check_margin_model_consistency(initialMargining=value)
     if value == InitialMargining.CCPIMM:
         self.im_model = CCPIMM(resultCurrency=self.margin_currency)
     elif value == InitialMargining.SIMM:
         self.im_model = SIMM(resultCurrency=self.margin_currency)
     elif value == InitialMargining.NO_IM:
         self.im_model = NOIMM(resultCurrency=self.margin_currency)
     self.im_model.add_trades(self.trades)
     self.__initialMargining = value
Ejemplo n.º 3
0
def test_get_im_swaption():
    tts = ql.Period(2, ql.Years)
    tte = ql.Period(10, ql.Years)
    swap1 = IRS(600, tts, tte, SwapDirection.RECEIVER,
                InterestRateIndex.USDLIBOR3M)
    swaption1 = Swaption(swap1, ql.Period(2, ql.Years))
    simm = SIMM()
    simm.add_trades(swaption1)
    im = simm.get_im_receive()
    swap2 = IRS(600, tts, tte, SwapDirection.RECEIVER,
                InterestRateIndex.EURIBOR6M)
    simm.add_trades(swap2)
    im = simm.get_im_receive()
Ejemplo n.º 4
0
def test_equality_with_simm_for_irs():
    irs = IRS(notional=100, timeToSwapStart=ql.Period(2, ql.Days),
              timeToSwapEnd=ql.Period(1, ql.Years),
              swapDirection=SwapDirection.RECEIVER,
              index=InterestRateIndex.EURIBOR6M)

    ois = OIS(notional=100, timeToSwapStart=ql.Period(2, ql.Days),
              timeToSwapEnd=ql.Period(1, ql.Years),
              swapDirection=SwapDirection.RECEIVER,
              index=InterestRateIndex.FEDFUNDS)

    ul_swap = IRS(notional=100, timeToSwapStart=ql.Period(1, ql.Years),
                  timeToSwapEnd=ql.Period(2, ql.Years),
                  swapDirection=SwapDirection.RECEIVER,
                  index=InterestRateIndex.USDLIBOR3M)

    swaption = Swaption(underlyingSwap=ul_swap, optionMaturity=ql.Period(1, ql.Years))

    simm = SIMM()
    simm.add_trades([irs, ois, ul_swap, swaption])

    ccpimm = CCPIMM()
    ccpimm.add_trades([irs, ois, ul_swap, swaption])

    assert simm.get_im_post() != 0
    assert ccpimm.get_im_post() != 0

    assert approx(simm.get_im_post(), rel=0.00001) == ccpimm.get_im_post() * sqrt(10 / 5)
Ejemplo n.º 5
0
 def get_im_post(self):
     im_role = ImRole.PLEDGOR
     if self.post_calculated:
         return self.im_post
     equity_trades = []
     irs_trades = []
     for t in self.trades:
         if t.assetClass == AssetClass.EQ:
             equity_trades.append(t)
         elif t.assetClass == AssetClass.IR:
             irs_trades.append(t)
         else:
             raise (Exception(
                 'can only handle equity and interest rate trades'))
     self.equity_model = SIMM()
     self.irs_model = SIMM()
     self.equity_model.add_trades(equity_trades)
     self.irs_model.add_trades(irs_trades)
     eq_im_post = self.equity_model.get_im_post() * sqrt(
         CCPIMM.equity_holding_period / CCPIMM.bilateral_holding_period)
     irs_im_post = self.irs_model.get_im_post() * sqrt(
         CCPIMM.irs_holding_period / CCPIMM.bilateral_holding_period)
     self.im_post = eq_im_post + irs_im_post
     return self.im_post
Ejemplo n.º 6
0
def test_get_im_ois():
    tts = ql.Period(2, ql.Days)
    tte = ql.Period(5, ql.Years)
    ois = OIS(1000,
              tts,
              tte,
              swapDirection=SwapDirection.RECEIVER,
              index=InterestRateIndex.FEDFUNDS)
    simm = SIMM()
    simm.add_trades([ois])
    im = simm.get_im_receive()
    ois2 = OIS(1000,
               tts,
               tte,
               swapDirection=SwapDirection.RECEIVER,
               index=InterestRateIndex.EONIA)
    simm.add_trades(ois2)
    im = simm.get_im_receive()
Ejemplo n.º 7
0
class CCPIMM(InitialMarginModel):
    equity_holding_period = 3
    irs_holding_period = 5
    bilateral_holding_period = 10

    def __init__(self, resultCurrency: Currency = Currency.USD):
        self.calculationCurrency = Currency.USD
        self.resultCurrency = resultCurrency
        self.trades = []
        self.im_receive = 0
        self.im_post = None
        self.equity_model = None
        self.irs_model = None
        self.receive_calculated = True
        self.post_calculated = False
        self.allocated = False

    def get_im_post(self):
        im_role = ImRole.PLEDGOR
        if self.post_calculated:
            return self.im_post
        equity_trades = []
        irs_trades = []
        for t in self.trades:
            if t.assetClass == AssetClass.EQ:
                equity_trades.append(t)
            elif t.assetClass == AssetClass.IR:
                irs_trades.append(t)
            else:
                raise (Exception(
                    'can only handle equity and interest rate trades'))
        self.equity_model = SIMM()
        self.irs_model = SIMM()
        self.equity_model.add_trades(equity_trades)
        self.irs_model.add_trades(irs_trades)
        eq_im_post = self.equity_model.get_im_post() * sqrt(
            CCPIMM.equity_holding_period / CCPIMM.bilateral_holding_period)
        irs_im_post = self.irs_model.get_im_post() * sqrt(
            CCPIMM.irs_holding_period / CCPIMM.bilateral_holding_period)
        self.im_post = eq_im_post + irs_im_post
        return self.im_post

    def get_im_receive(self):
        return self.im_receive

    def __resetCalculated__(self):
        self.im_post = None
        self.equity_model = None
        self.irs_model = None
        self.post_calculated = False
        self.allocated = False
Ejemplo n.º 8
0
def test_im_post_vs_receive_linear():
    # When setting up an IRS IM post should be = IM receive
    irs = IRS(notional=100,
              timeToSwapStart=ql.Period(2, ql.Days),
              timeToSwapEnd=ql.Period(1, ql.Years),
              swapDirection=SwapDirection.RECEIVER,
              index=InterestRateIndex.EURIBOR6M)
    simm = SIMM()
    simm.add_trades(irs)
    im_receive = simm.get_im_receive()
    im_post = simm.get_im_post()
    assert im_receive > 0
    assert im_post < 0
    assert -1 * im_receive == im_post
Ejemplo n.º 9
0
def test_setup_simm():
    tts = ql.Period(2, ql.Days)
    tte = ql.Period(10, ql.Years)
    swap = IRS(100,
               tts,
               tte,
               SwapDirection.PAYER,
               InterestRateIndex.EURIBOR6M,
               fixed_rate=0.05)
    simm = SIMM()
    simm.add_trades(swap)
    swap2 = IRS(600, tts, tte, SwapDirection.RECEIVER,
                InterestRateIndex.USDLIBOR3M)
    swap3 = IRS(400, tts, tte, SwapDirection.RECEIVER,
                InterestRateIndex.EURIBOR6M)
    simm.add_trades([swap2, swap3])
    yield simm
Ejemplo n.º 10
0
def test_equality_with_simm_for_eq():
    eq_opt = EquityOption(maturity=ql.Period(1, ql.Years),
                          tradeType=TradeType.CALL,
                          tradeDirection=TradeDirection.LONG,
                          underlying=Stock.DBK)

    eq_opt2 = EquityOption(maturity=ql.Period(1, ql.Years),
                           tradeType=TradeType.PUT,
                           tradeDirection=TradeDirection.SHORT,
                           underlying=Stock.ADS,
                           strike=EquitySpot.ADS.value.value() - 5)

    simm = SIMM()
    simm.add_trades([eq_opt, eq_opt2])

    ccpimm = CCPIMM()
    ccpimm.add_trades([eq_opt, eq_opt2])

    assert simm.get_im_post() != 0
    assert ccpimm.get_im_post() != 0

    assert approx(simm.get_im_post(), rel=0.00001) == ccpimm.get_im_post() * sqrt(10 / 3)
Ejemplo n.º 11
0
def test_im_post_vs_receive_option():
    # When setting up at the money options that expire soon enough the long side of the option should bear significant less IM than the short side
    swap = IRS(notional=100,
               timeToSwapStart=ql.Period(6, ql.Months),
               timeToSwapEnd=ql.Period(1, ql.Years),
               swapDirection=SwapDirection.RECEIVER,
               index=InterestRateIndex.EURIBOR6M)
    options = []
    swaption = Swaption(underlyingSwap=swap,
                        optionMaturity=ql.Period(6, ql.Months),
                        tradeDirection=TradeDirection.LONG)
    options.append(swaption)
    eqOpt1 = EquityOption(maturity=ql.Period(2, ql.Months),
                          tradeType=TradeType.CALL,
                          tradeDirection=TradeDirection.LONG)
    options.append(eqOpt1)
    for option in options:
        simm = SIMM()
        simm.add_trades(option)
        im_receive = simm.get_im_receive()
        im_post = -1 * simm.get_im_post()
        assert im_post * 1.1 < im_receive

    eqOpt2 = EquityOption(maturity=ql.Period(2, ql.Months),
                          tradeType=TradeType.PUT,
                          tradeDirection=TradeDirection.SHORT)

    simm = SIMM()
    simm.add_trades(eqOpt2)
    im_receive = simm.get_im_receive()
    im_post = -1 * simm.get_im_post()
    assert im_receive * 1.1 < im_post