1010#include < vector>
1111#include < memory>
1212#include < optional>
13+ #include < stdexcept>
1314#include < string>
1415#include < utility>
1516#include < iomanip>
1819#include < boost/decimal.hpp>
1920#include " tradeManager.hpp"
2021#include " models/symbolScale.hpp"
22+ #include " strategies/strategy.hpp"
2123#include " strategies/randomStrategy.hpp"
2224
2325namespace {
@@ -76,13 +78,24 @@ void reviewStopAndLimit(TradeManager& tradeManager, const PriceData& tick) {
7678 }
7779}
7880
81+ // Adding a new strategy means adding one branch here; nothing else in
82+ // Operations needs to know about the concrete type.
83+ std::unique_ptr<IStrategy>
84+ selectStrategy (const trading_definitions::Configuration& config) {
85+ const auto & name = config.STRATEGY .TRADING_VARIABLES .STRATEGY ;
86+ if (name == " RandomStrategy" ) {
87+ return std::make_unique<RandomStrategy>(config.STRATEGY );
88+ }
89+ throw std::runtime_error (" Unknown strategy: '" + name + " '" );
90+ }
91+
7992} // namespace
8093
8194void Operations::run (const std::vector<PriceData>& ticks,
8295 const trading_definitions::Configuration& config) {
8396
8497 auto tradeManager = std::make_unique<TradeManager>();
85- RandomStrategy strategy (config. STRATEGY );
98+ auto strategy = selectStrategy (config);
8699
87100 const auto & tradingVars = config.STRATEGY .TRADING_VARIABLES ;
88101
@@ -99,7 +112,7 @@ void Operations::run(const std::vector<PriceData>& ticks,
99112 // only open a trade if there is zero
100113 if (openTrades == 0 ) {
101114 // optional is false
102- if (auto signal = strategy. decide (tick)) {
115+ if (auto signal = strategy-> decide (tick)) {
103116 tradeManager->openTrade (tick,
104117 tradingVars.TRADING_SIZE ,
105118 *signal,
@@ -112,7 +125,7 @@ void Operations::run(const std::vector<PriceData>& ticks,
112125 // (e.g. trailing stops, partial closes). The default
113126 // RandomStrategy implementation is a no-op now that exits are
114127 // handled by reviewStopAndLimit above.
115- strategy. during (tickIndex, tick, *tradeManager);
128+ strategy-> during (tickIndex, tick, *tradeManager);
116129
117130 ++tickIndex;
118131 }
0 commit comments