Skip to content

Commit

Permalink
added factory for digital
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonymakarewicz committed Aug 1, 2024
1 parent 6082d81 commit 7dcf7ad
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ add_library(option SHARED
src/option/factory_option.cpp
src/option/single_path/factory_single_path_option.cpp
src/option/single_path/factory_european_option.cpp
include/option/single_path/factory_digital_option.h
src/option/single_path/factory_digital_option.cpp
)

# Link the option library against marketdata and payoff
Expand Down
1 change: 1 addition & 0 deletions include/option/single_path/digital_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace OptionPricer {
private:
DigitalOption(const std::string& ticker, std::unique_ptr<Payoff> payoff, const double& T,
std::shared_ptr<IMarketData> marketData);
friend class DigitalOptionFactory;
};
}

Expand Down
12 changes: 12 additions & 0 deletions include/option/single_path/factory_digital_option.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef FACTORY_DIGITAL_OPTION_H
#define FACTORY_DIGITAL_OPTION_H

#include "option/single_path/factory_single_path_option.h"

namespace OptionPricer {
class DigitalOptionFactory final: public SinglePathOptionFactory {
std::shared_ptr<SinglePathOption> createOption(const ParameterObject& params, const std::string& type) override;
};
}

#endif //FACTORY_DIGITAL_OPTION_H
2 changes: 0 additions & 2 deletions include/option/single_path/factory_european_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ namespace OptionPricer {
};
}



#endif //FACTORY_EUROPEAN_OPTION_H
30 changes: 30 additions & 0 deletions src/option/single_path/factory_digital_option.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "option/single_path/factory_digital_option.h"

#include <market_data/market_data.h>
#include "option/single_path/digital_option.h"
#include "payoff/single_strike/factory_payoff_digital.h"

namespace OptionPricer {
std::shared_ptr<SinglePathOption> DigitalOptionFactory::createOption(const ParameterObject &params,
const std::string &type) {
auto marketData = MarketData::getInstance();
std::shared_ptr<SinglePathOption> eurOption = nullptr;

try {
PayoffDigitalFactory payoffFactory;
auto payoff = payoffFactory.createPayoff(type, params.getParameter<double>("K"));
eurOption = std::shared_ptr<SinglePathOption>(new DigitalOption(
params.getParameter<std::string>("ticker"),
std::move(payoff),
params.getParameter<double>("T"),
marketData
));
} catch (const std::invalid_argument& e) {
const std::string err = std::string(e.what()) + "\n" + invalidParams("Digital Option");
throw std::invalid_argument(err); // Re-throw the caught exception
}

eurOption->initialize();
return eurOption;
}
}
2 changes: 1 addition & 1 deletion src/option/single_path/factory_european_option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace OptionPricer {
try {
PayoffVanillaFactory payoffFactory;
auto payoff = payoffFactory.createPayoff(type, params.getParameter<double>("K"));
eurOption = std::shared_ptr<SinglePathOption>( new EuropeanOption(
eurOption = std::shared_ptr<SinglePathOption>(new EuropeanOption(
params.getParameter<std::string>("ticker"),
std::move(payoff),
params.getParameter<double>("T"),
Expand Down

0 comments on commit 7dcf7ad

Please sign in to comment.