Skip to content

Commit ff552a0

Browse files
Christopher Smithjenkins
authored andcommitted
Merge branch 'master' into 'QPR-13324'
# Conflicts: # ore/ORE-SWIG/OREData-SWIG/SWIG/ored_tradegenerator.i
1 parent ddaedea commit ff552a0

4 files changed

Lines changed: 64 additions & 33 deletions

File tree

Examples/ORE-Python/TradeGenerator/trade_generator_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
#add path to compiled version of ORE.py, _OREP.pyd
3-
sys.path.append(r"..\..\..\..\build\ore\ORE-SWIG\OREAnalytics-SWIG\Python")
4-
sys.path.append(r"..\..\..\..\build\ore\ORE-SWIG\OREAnalytics-SWIG\Python\RelWithDebInfo")
3+
sys.path.append(r"..\..\..\..\build\ore\ORE-SWIG")
4+
sys.path.append(r"..\..\..\..\build\ore\ORE-SWIG\RelWithDebInfo")
55
sys.path.append(r"..\..\input")
66

77
from ORE import *
@@ -16,10 +16,10 @@
1616
curveConfig = curveConfigs[curveConfigs.keys()[0]]
1717

1818
tg = TradeGenerator(curveConfig, refData, "CP", "NS")
19-
tg.buildSwap("GBP-SONIA", 1000, "1Y", 0.01, True, "SWAPPY-SWAP")
19+
tg.buildSwap("GBP-SONIA", 1000, "1Y", 0.01, True, "", "Swappy-Swap")
2020
tg.buildInflationSwap("UKRPI", 1000, "5Y", "GBP-SONIA", 200, 0.02, True, "inflation swap!")
2121
tg.buildFxForward("USD", 1000, "EUR", 1100, "5Y", True)
22-
tg.buildSwap("USD-FedFunds", 1000, "5Y", "USD-LIBOR-3M", 0.01, True)
22+
tg.buildSwap("USD-FedFunds", 1000, "5Y", "USD-LIBOR-3M", 0.01, True, "1W", "1W Start Lag Swap")
2323
tg.buildSwap("USD-LIBOR-3M", 1000, "5Y", 0.01, False)
2424
tg.buildCapFloor("USD-LIBOR-3M", 0.01, 1000, "2Y", True, True)
2525
tg.buildEquitySwap("SP5", "Total", 1000, "USD-FedFunds", 1000, "10Y", True)

ORE-SWIG/OREData-SWIG/SWIG/ored_tradegenerator.i

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,28 @@
2121
%include ored_portfolio.i
2222
%include ored_referencedatamanager.i
2323
%include ored_xmlutils.i
24+
%include qle_common.i
2425
%include std_set.i
2526

2627
%{
2728
using ore::data::Portfolio;
2829
using ore::data::TradeGenerator;
29-
using ore::data::LegData;
30+
using ore::data::LegData;void test(QuantLib::ext::optional<bool> test_);
3031
using ore::data::XMLUtils;
3132
using ore::data::XMLSerializable;
3233
%}
3334

35+
36+
3437
%shared_ptr(TradeGenerator)
3538
class TradeGenerator : public Portfolio {
3639
public:
3740

38-
TradeGenerator(std::string counterpartyId = "", std::string nettingSetId = "");
41+
TradeGenerator(std::string counterpartyId = "", std::string nettingSetId = "", std::string startDate = "");
3942
TradeGenerator(ext::shared_ptr<CurveConfigurations> curveConfig, ext::shared_ptr < BasicReferenceDataManager> refData,
40-
std::string counterpartyId = "", std::string nettingSetId = "");
41-
43+
std::string counterpartyId = "", std::string nettingSetId = "", std::string startDate = "");
4244
void buildSwap(std::string indexId, QuantLib::Real notional, std::string maturity, QuantLib::Real rate, bool firstLegPays,
43-
std::string start = std::string(), std::string tradeId = "", std::map<std::string, std::string> mapPairs = {});
45+
std::string start = std::string(), std::string tradeId = std::string());
4446
void buildSwap(std::string indexId, QuantLib::Real notional, std::string maturity, std::string recIndexId, QuantLib::Real spread, bool firstLegPays,
4547
std::string start = std::string(), std::string tradeId = std::string(), std::map<std::string, std::string> mapPairs = {});
4648
void buildCapFloor(std::string indexName, QuantLib::Real capFloorRate, QuantLib::Real notional, std::string maturity, bool isLong, bool isCap,
@@ -66,11 +68,19 @@ public:
6668
std::string floatType, bool firstLegPays, std::string tradeId = "");
6769
void buildCommodityOption(std::string commodityId, QuantLib::Real quantity, std::string maturity, QuantLib::Real strike, std::string priceType,
6870
bool isLong, bool isCall);
69-
Date today_;
7071
void buildCommodityForward(std::string commodityId, QuantLib::Real quantity, std::string maturity, QuantLib::Real strike,
7172
bool isLong, std::string tradeId = "");
7273
void fromXML(XMLNode* node) override;
7374
XMLNode* toXML(XMLDocument& doc) const override;
74-
};
75+
76+
private:
77+
LegData buildOisLeg(QuantLib::ext::shared_ptr < Convention> conv, QuantLib::Real notional, std::string maturity, bool isPayer,
78+
std::map<std::string, std::string> mapPairs = {});
79+
LegData buildIborLeg(QuantLib::ext::shared_ptr < Convention> conv, QuantLib::Real notional, std::string maturity, bool isPayer,
80+
std::map<std::string, std::string> mapPairs = {});
81+
Date startDate;
82+
};
83+
84+
7585

7686
#endif

OREData/ored/portfolio/tradegenerator.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,25 @@ using ore::data::XMLUtils;
5757
namespace ore {
5858
namespace data {
5959
using namespace ore::data;
60+
6061
using std::map;
6162

6263

63-
void TradeGenerator::setup() {
64-
today_ = Settings::instance().evaluationDate();
64+
void TradeGenerator::setup(string startDate) {
65+
if (startDate != "") {
66+
try {
67+
68+
startDate_ = ore::data::parseDate(startDate);
69+
}
70+
71+
catch (QuantLib::Error e) {
72+
startDate_ = Settings::instance().evaluationDate();
73+
}
74+
75+
} else {
76+
77+
startDate_ = Settings::instance().evaluationDate();
78+
}
6579
addConventions();
6680
}
6781

@@ -146,7 +160,7 @@ bool TradeGenerator::validateDate(string date) {
146160
}
147161

148162
void TradeGenerator::buildSwap(string indexId, Real notional, string maturity, Real rate, bool firstLegPays,
149-
string start, string tradeId, std::map<string, string> mapPairs) {
163+
string start, string tradeId) {
150164
QuantLib::ext::shared_ptr<Convention> conv = conventions_.at(indexId);
151165
QuantLib::ext::shared_ptr<IborIndex> iborIndex;
152166
tryParseIborIndex(indexId, iborIndex);
@@ -197,11 +211,13 @@ void TradeGenerator::buildSwap(string indexId, Real notional, string maturity, R
197211
}
198212

199213
void TradeGenerator::buildSwap(string indexId, Real notional, string maturity, string recIndexId, Real spread,
200-
bool firstLegPays, string start, string tradeId, std::map<string, string> mapPairs) {
214+
bool firstLegPays, string start, string tradeId,
215+
std::map<string, string> mapPairs) {
201216
Envelope env = makeEnvelope();
202217
QuantLib::ext::shared_ptr<Convention> conv = conventions_.at(indexId);
203218
QuantLib::ext::shared_ptr<Convention> recConv = conventions_.at(recIndexId);
204219
string forwardStart = start.empty() ? "0D" : start;
220+
205221
LegData floatingLeg = buildLeg(conv, notional, maturity, forwardStart, firstLegPays);
206222
LegData recFloatingLeg = buildLeg(recConv, notional, maturity, forwardStart, firstLegPays);
207223
QuantLib::ext::shared_ptr<Swap> trade(
@@ -227,7 +243,7 @@ void TradeGenerator::buildInflationSwap(string inflationIndex, Real notional, st
227243

228244
void TradeGenerator::buildFxForward(string payCcy, Real payNotional, string recCcy, Real recNotional, string expiryDate, bool isLong, string tradeId, std::map<string, string> mapPairs) {
229245
Envelope env = makeEnvelope();
230-
expiryDate = to_string(getEndDate(expiryDate, to_string(today_)));
246+
expiryDate = to_string(getEndDate(expiryDate, to_string(startDate_)));
231247
QuantLib::ext::shared_ptr<FxForward> trade(QuantLib::ext::make_shared<FxForward> (ore::data::FxForward(
232248
env, expiryDate, recCcy, recNotional, payCcy, payNotional)));
233249
trade->id() = tradeId != "" ? tradeId : to_string(size() + 1) + "_" + payCcy + "-" + recCcy + "_FX_FORWARD";
@@ -260,7 +276,7 @@ void TradeGenerator::buildFxOption(string payCcy, Real payNotional, string recCc
260276
Envelope env = makeEnvelope();
261277
string longShort = isLong ? "Long" : "Short";
262278
string putCall = isCall ? "Call" : "Put";
263-
expiryDate = to_string(getEndDate(expiryDate, to_string(today_)));
279+
expiryDate = to_string(getEndDate(expiryDate, to_string(startDate_)));
264280
OptionData option(longShort, putCall, "European", false, vector<string>(1, to_string(expiryDate)), "Cash", "");
265281
QuantLib::ext::shared_ptr<FxOption> trade(
266282
QuantLib::ext::make_shared<FxOption>( ore::data::FxOption(env, option, recCcy, recNotional, payCcy, payNotional)));
@@ -283,7 +299,7 @@ void TradeGenerator::buildCommoditySwap(string commodityId, string maturity, Rea
283299
QuantLib::ext::dynamic_pointer_cast<CommodityCurveConfig>(curveConfigs_->commodityCurveConfig(commodityId));
284300
auto yieldConv = conventions_.at(comConfig->baseYieldCurveId() != "" ? comConfig->baseYieldCurveId()
285301
: comConfig->conventionsId());
286-
string startDate = to_string(today_);
302+
string startDate = to_string(startDate_);
287303
string endDate = getEndDate(maturity, startDate, comConv->advanceCalendar());
288304
string cal = to_string(comConv->advanceCalendar());
289305
string convention = to_string(comConv->bdc());
@@ -318,7 +334,7 @@ void TradeGenerator::buildCommodityOption(string commodityId, Real quantity, str
318334
auto yieldConv = conventions_.at(comConfig->baseYieldCurveId() != "" ? comConfig->baseYieldCurveId()
319335
: comConfig->conventionsId());
320336
//string rule = "Forward";
321-
string expiryDate = to_string(getEndDate(maturity, to_string(today_)));
337+
string expiryDate = to_string(getEndDate(maturity, to_string(startDate_)));
322338
string longShort = isLong ? "Long" : "Short";
323339
string putCall = isCall ? "Call" : "Put";
324340
string currency = to_string(comConfig->currency());
@@ -341,7 +357,7 @@ void TradeGenerator::buildCommodityForward(string commodityId, Real quantity, st
341357
QuantLib::ext::shared_ptr<CommodityCurveConfig> comConfig =
342358
QuantLib::ext::dynamic_pointer_cast<CommodityCurveConfig>(curveConfigs_->commodityCurveConfig(commodityId));
343359
string ccy = comConfig->currency();
344-
string expiryDate = to_string(getEndDate(maturity, to_string(today_)));
360+
string expiryDate = to_string(getEndDate(maturity, to_string(startDate_)));
345361
Envelope env = makeEnvelope();
346362
QuantLib::ext::shared_ptr<CommodityForward> trade(QuantLib::ext::make_shared<CommodityForward>( ore::data::CommodityForward(
347363
env, longShort, commodityId, ccy, quantity, expiryDate, strike)));
@@ -392,7 +408,7 @@ void TradeGenerator::buildEquitySwap(string equityCurveId, string returnType, Re
392408
auto forecastIndex = curveConfigs_->yieldCurveConfig(indexName);
393409
string ccy = forecastIndex->currency();
394410
string fixedFreq;
395-
string startDate = to_string(today_);
411+
string startDate = to_string(startDate_);
396412
string endDate = getEndDate(maturity, startDate, parseCalendar(ccy));
397413
string fixedDC;
398414
string conv;
@@ -447,7 +463,7 @@ void TradeGenerator::buildEquitySwap(string equityCurveId, string returnType, Re
447463
void TradeGenerator::buildEquityOption(string equityCurveId, Real quantity, string maturity, Real strike,
448464
string tradeId, std::map<string, string> mapPairs) {
449465
map<string, string> eqData = fetchEquityRefData(equityCurveId);
450-
string expiryDate = to_string(getEndDate(maturity, to_string(today_)));
466+
string expiryDate = to_string(getEndDate(maturity, to_string(startDate_)));
451467
string longShort = mapPairs.count("longShort") == 1 ? mapPairs["longShort"] : "Long";
452468
string putCall = mapPairs.count("putCall") == 1 ? mapPairs["putCall"] : "Call";
453469
TradeStrike tradeStrike(strike, eqData["currency"]);
@@ -464,7 +480,7 @@ void TradeGenerator::buildEquityForward(string equityCurveId, Real quantity, str
464480
string tradeId, std::map<string, string> mapPairs) {
465481
map<string, string> eqData = fetchEquityRefData(equityCurveId);
466482
string longShort = mapPairs.count("longShort") == 1 ? mapPairs["longShort"] : "Long";
467-
string expiryDate = to_string(getEndDate(maturity, to_string(today_)));
483+
string expiryDate = to_string(getEndDate(maturity, to_string(startDate_)));
468484
Envelope env = makeEnvelope();
469485
QuantLib::ext::shared_ptr<EquityForward> trade(QuantLib::ext::make_shared<EquityForward>(ore::data::EquityForward(
470486
env, longShort, EquityUnderlying(equityCurveId), eqData["currency"], quantity, expiryDate, strike)));
@@ -485,7 +501,7 @@ LegData TradeGenerator::buildCPILeg(QuantLib::ext::shared_ptr<Convention> conv,
485501
string rule = "";
486502
Natural spotDays = 0;
487503
string floatFreq = to_string(infSwapConv->observationLag());
488-
auto qlStartDate = today_ + spotDays;
504+
auto qlStartDate = startDate_ + spotDays;
489505
string startDate = to_string(qlStartDate);
490506
string endDate = getEndDate(maturity, startDate, infSwapConv->infCalendar());
491507

@@ -510,9 +526,8 @@ LegData TradeGenerator::buildOisLeg(QuantLib::ext::shared_ptr<Convention> conv,
510526
string cal = to_string(oisConv->fixedCalendar());
511527
string rule = to_string(oisConv->rule());
512528
string floatFreq = to_string(oisIndex->tenor());
513-
Date qlStartDate = today_ + oisConv->spotLag();
529+
Date qlStartDate = startDate_ + oisConv->spotLag();
514530
string startDate = getEndDate(start, to_string(qlStartDate), oisConv->fixedCalendar());
515-
516531
string endDate = getEndDate(maturity, startDate, oisConv->fixedCalendar());
517532
Natural spotDays = oisConv->spotLag();
518533
string floatDC = to_string(oisConv->fixedDayCounter());
@@ -545,7 +560,8 @@ LegData TradeGenerator::buildIborLeg(QuantLib::ext::shared_ptr<Convention> conv,
545560
{
546561
floatFreq = to_string(floatFreqEnum);
547562
}
548-
string startDate = getEndDate(start, to_string(today_), iborConv->fixedCalendar());
563+
564+
string startDate = getEndDate(start, to_string(startDate_), iborConv->fixedCalendar());
549565
string endDate = getEndDate(maturity, startDate, iborConv->fixedCalendar());
550566
Natural spotDays = 2;
551567
string floatDC = to_string(iborConv->fixedDayCounter());

OREData/ored/portfolio/tradegenerator.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <ored/configuration/curveconfigurations.hpp>
2727
#include <ored/portfolio/referencedata.hpp>
2828
#include <ored/utilities/parsers.hpp>
29+
#include <ql/optional.hpp>
2930
#include <qle/cashflows/equitycouponpricer.hpp>
3031
#include <qle/cashflows/indexedcoupon.hpp>
3132
#include <qle/instruments/payment.hpp>
@@ -46,21 +47,23 @@ class TradeGenerator : public Portfolio {
4647
public:
4748

4849
TradeGenerator(const QuantLib::ext::shared_ptr<CurveConfigurations>& curveConfig, const QuantLib::ext::shared_ptr < BasicReferenceDataManager>& refData, string counterpartyId = "",
49-
string nettingSetId = "")
50+
string nettingSetId = "", string startDate = "")
5051
: curveConfigs_(curveConfig), referenceData_(refData), counterpartyId_(counterpartyId),
5152
nettingSetId_(nettingSetId) {
52-
setup();
53+
setup(startDate);
54+
}
55+
TradeGenerator(string counterpartyId = "", string nettingSetId = "", string startDate = "")
56+
: counterpartyId_(counterpartyId), nettingSetId_(nettingSetId){
57+
setup(startDate);
5358
}
54-
TradeGenerator(string counterpartyId = "", string nettingSetId = "") { setup(); }
5559
virtual ~TradeGenerator() {}
56-
void setup();
5760
void addCurveConfigs(string curveConfigFile);
5861
void addReferenceData(string refDataFile);
5962
void setNettingSet(string nettingSetId);
6063
void setCounterpartyId(string counterpartyId);
6164
bool validateDate(string date);
6265
void buildSwap(string indexId, Real notional, string maturity, Real rate, bool firstLegPays,
63-
string start = string(), string tradeId = "", std::map<string, string> mapPairs = {});
66+
string start = string(), string tradeId = string());
6467
void buildSwap(string indexId, Real notional, string maturity, string recIndexId, Real spread, bool firstLegPays,
6568
string start = string(), string tradeId = string(), std::map<string, string> mapPairs = {});
6669
void buildFxForward(string payCcy, Real payNotional, string recCcy, Real recNotional, string expiryDate,
@@ -86,15 +89,16 @@ class TradeGenerator : public Portfolio {
8689
string tradeId = "");
8790
void buildInflationSwap(string inflationIndex, Real notional, string maturity, string floatIndex, Real baseRate,
8891
Real cpiRate, bool firstLegPays, string tradeId = "");
89-
Date today_;
9092
map<string, QuantLib::ext::shared_ptr<Convention>> conventions_;
9193
QuantLib::ext::shared_ptr <CurveConfigurations> curveConfigs_;
9294
QuantLib::ext::shared_ptr <BasicReferenceDataManager> referenceData_;
9395
QuantLib::ext::shared_ptr<TodaysMarketParameters> market_;
9496
string counterpartyId_;
9597
string nettingSetId_;
98+
9699

97100
private:
101+
void setup(string startDate = "");
98102
string getEndDate(string maturity, string startDate, Calendar cal = QuantLib::NullCalendar());
99103
LegData buildLeg(QuantLib::ext::shared_ptr<Convention> conv, Real notional, string maturity, string start,
100104
bool isPayer);
@@ -108,6 +112,7 @@ class TradeGenerator : public Portfolio {
108112
map<string, string> fetchEquityRefData(string equityId);
109113
Envelope makeEnvelope();
110114
void addConventions();
115+
Date startDate_;
111116
};
112117
} // namespace data
113118
} // namespace ore

0 commit comments

Comments
 (0)