Skip to content

Commit

Permalink
Merge pull request amaggiulli#212 from amaggiulli/feature/211-FDDivid…
Browse files Browse the repository at this point in the history
…endAmericanEngine

 Fix for Issue amaggiulli#211.Thanks to Jakub Pstrusiński.
  • Loading branch information
amaggiulli authored Jul 24, 2018
2 parents 2e48bf0 + a1d51d6 commit 668dcdf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/QLNet/Pricingengines/vanilla/FDConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class FDConditionEngineTemplate : FDVanillaEngine
protected virtual void initializeStepCondition()
{
if (stepConditionImpl_ == null)
throw new NotSupportedException();
stepCondition_ = new NullCondition<Vector>();
else
stepCondition_ = stepConditionImpl_();
}
Expand Down
5 changes: 0 additions & 5 deletions src/QLNet/Pricingengines/vanilla/FDMultiPeriodEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ protected FDMultiPeriodEngine(GeneralizedBlackScholesProcess process, int timeSt
protected virtual void executeIntermediateStep(int step) { throw new NotSupportedException(); }


protected override void initializeStepCondition()
{
stepCondition_ = new NullCondition<Vector>();
}

protected virtual void initializeModel()
{
model_ = new FiniteDifferenceModel<CrankNicolson<TridiagonalOperator>>(finiteDifferenceOperator_, BCs_);
Expand Down
57 changes: 57 additions & 0 deletions tests/QLNet.Tests/T_AmericanOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,5 +634,62 @@ public void testFdImpliedVol()
if (Math.Abs(impliedVol - volatility) > tolerance)
QAssert.Fail(string.Format("Implied volatility calculation failed. Expected {0}. Actual {1}", volatility, impliedVol));
}

#if NET40 || NET45
[TestMethod()]
#else
[Fact]
#endif
public void testFDDividendAmericanEngine()
{
/*
Valuation date: 20 July 2018
Maturity date: 17 Aug 2018
Type: Call
Spot: 2900
Strike: 2800
Volatility: 20 %
Interest rate: 0 %
Dividend(paid one day before expiry)
Date: 16 Aug 2018
Value: 40
NPV = 124.37658
*/
var result = 124.37658;
var settlementDate = new Date(20, 7, 2018);
Settings.setEvaluationDate(settlementDate);

var calendar = new TARGET();
var dayCounter = new Actual365Fixed();

var spot = new Handle<Quote>(new SimpleQuote(2900));
var qRate = new Handle<Quote>(new SimpleQuote(0.0));
var rRate = new Handle<Quote>(new SimpleQuote(0.0));
var vol = new Handle<Quote>(new SimpleQuote(0.2));

var flatDividendYield = new Handle<YieldTermStructure>(new FlatForward(settlementDate, qRate, dayCounter));
var flatTermStructure = new Handle<YieldTermStructure>(new FlatForward(settlementDate, rRate, dayCounter));
var flatVolatility = new Handle<BlackVolTermStructure>(new BlackConstantVol(settlementDate, calendar, vol, dayCounter));
var process = new BlackScholesMertonProcess(spot, flatDividendYield, flatTermStructure, flatVolatility);
var exercise = new AmericanExercise(new Date(17, 8, 2018));
var pricingEngine = new FDDividendAmericanEngine(process);
var payoff = new PlainVanillaPayoff(Option.Type.Call, 2800);
var dividendDates = new[] { new Date(16, 8, 2018) };
var dividendAmounts = new[] { 40d };
var option = new DividendVanillaOption(payoff, exercise, dividendDates.ToList(), dividendAmounts.ToList());
option.setPricingEngine(pricingEngine);

var npv = option.NPV();

const double tolerance = 1.0e-5;

if (Math.Abs(npv - result) > tolerance)
QAssert.Fail(string.Format("NPV calculation failed. Expected {0}. Actual {1}", result, npv));

}


}
}

0 comments on commit 668dcdf

Please sign in to comment.