1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2006 Giorgio Facchinetti
5 
6  This file is part of QuantLib, a free-software/open-source library
7  for financial quantitative analysts and developers - http://quantlib.org/
8 
9  QuantLib is free software: you can redistribute it and/or modify it
10  under the terms of the QuantLib license.  You should have received a
11  copy of the license along with this program; if not, please email
12  <quantlib-dev@lists.sf.net>. The license is also available online at
13  <http://quantlib.org/license.shtml>.
14 
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the license for more details.
18 */
19 
20 #include <ql/models/marketmodels/products/multistep/multistepswap.hpp>
21 #include <ql/models/marketmodels/curvestate.hpp>
22 #include <ql/models/marketmodels/utilities.hpp>
23 #include <ql/auto_ptr.hpp>
24 
25 namespace QuantLib {
26 
MultiStepSwap(const std::vector<Time> & rateTimes,const std::vector<Real> & fixedAccruals,const std::vector<Real> & floatingAccruals,const std::vector<Time> & paymentTimes,Real fixedRate,bool payer)27     MultiStepSwap::MultiStepSwap(const std::vector<Time>& rateTimes,
28                                  const std::vector<Real>& fixedAccruals,
29                                  const std::vector<Real>& floatingAccruals,
30                                  const std::vector<Time>& paymentTimes,
31                                  Real fixedRate,
32                                  bool payer)
33     : MultiProductMultiStep(rateTimes),
34       fixedAccruals_(fixedAccruals), floatingAccruals_(floatingAccruals),
35       paymentTimes_(paymentTimes), fixedRate_(fixedRate),
36       multiplier_(payer ? 1.0 : -1.0), lastIndex_(rateTimes.size()-1) {
37         checkIncreasingTimes(paymentTimes);
38     }
39 
nextTimeStep(const CurveState & currentState,std::vector<Size> & numberCashFlowsThisStep,std::vector<std::vector<MarketModelMultiProduct::CashFlow>> & genCashFlows)40     bool MultiStepSwap::nextTimeStep(
41             const CurveState& currentState,
42             std::vector<Size>& numberCashFlowsThisStep,
43             std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
44                                                                  genCashFlows)
45     {
46         Rate liborRate = currentState.forwardRate(currentIndex_);
47 
48         genCashFlows[0][0].timeIndex = currentIndex_;
49         genCashFlows[0][0].amount =
50             -multiplier_*fixedRate_*fixedAccruals_[currentIndex_];
51 
52         genCashFlows[0][1].timeIndex = currentIndex_;
53         genCashFlows[0][1].amount =
54             multiplier_*liborRate*floatingAccruals_[currentIndex_];
55 
56         numberCashFlowsThisStep[0] = 2;
57 
58         ++currentIndex_;
59 
60         return (currentIndex_ == lastIndex_);
61     }
62 
63     QL_UNIQUE_OR_AUTO_PTR<MarketModelMultiProduct>
clone() const64     MultiStepSwap::clone() const {
65         return QL_UNIQUE_OR_AUTO_PTR<MarketModelMultiProduct>(
66                                                  new MultiStepSwap(*this));
67     }
68 
69 }
70 
71