Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
strategy_loader.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <memory>
5#include <string>
6
7// #ifdef _WIN32
8// #define SLICK_TRADER_EXPORT __declspec(dllexport)
9// #define SLICK_TRADER_IMPORT __declspec(dllimport)
10// #else
11// #define SLICK_TRADER_EXPORT __attribute__((visibility("default")))
12// #define SLICK_TRADER_IMPORT
13// #endif
14
15// // Macro for strategy plugin authors to export their strategy
16// // Usage in strategy .cpp:
17// // SLICK_TRADER_EXPORT_STRATEGY(MyStrategy)
18// #define SLICK_TRADER_EXPORT_STRATEGY(StrategyClass) \
19// extern "C" { \
20// SLICK_TRADER_EXPORT slick::trader::IStrategy* create_strategy() { \
21// return new StrategyClass(); \
22// } \
23// SLICK_TRADER_EXPORT void destroy_strategy(slick::trader::IStrategy* strategy) { \
24// delete strategy; \
25// } \
26// SLICK_TRADER_EXPORT const char* get_strategy_name() { \
27// static StrategyClass instance; \
28// return instance.name(); \
29// } \
30// SLICK_TRADER_EXPORT const char* get_strategy_version() { \
31// static StrategyClass instance; \
32// return instance.version(); \
33// } \
34// }
35
36namespace slick::logger {
37 class Logger;
38}
39
40namespace slick::trader {
41
42class BrokerStatusRegistry;
43
44// Strategy loader for dynamic library loading
45// Loads .so/.dll strategy plugins at runtime
47public:
48 StrategyLoader() = default;
50
51 // Non-copyable, non-movable
56
57 // Load strategy from shared library
58 // Returns nullptr on failure
59 std::shared_ptr<IStrategy> load(const std::string& library_path);
60
61 // Unload a previously loaded strategy
62 void unload();
63
64 // Check if a strategy is currently loaded
65 bool is_loaded() const { return handle_ != nullptr; }
66
67 // Get library path
68 const std::string& get_library_path() const { return library_path_; }
69
70 // Get last error message
71 const std::string& get_last_error() const { return last_error_; }
72
73private:
74 using GetApiVersionFunc = uint32_t (*)();
75 using CreateStrategyFunc = IStrategy* (*)();
76 using DestroyStrategyFunc = void (*)(IStrategy*);
77 using GetNameFunc = const char* (*)();
78 using GetVersionFunc = const char* (*)();
79 using SetLoggerFunc = void (*)(slick::logger::Logger*);
80 using SetBrokerStatusFunc = void (*)(BrokerStatusRegistry*);
81
82 void* handle_{nullptr};
83 std::string library_path_;
84 std::string last_error_;
85
86 CreateStrategyFunc create_func_{nullptr};
87 DestroyStrategyFunc destroy_func_{nullptr};
88
89 // Custom deleter for shared_ptr that calls destroy_strategy()
90 // Also keeps the DLL handle alive as long as the strategy exists
91 struct StrategyDeleter {
92 DestroyStrategyFunc destroy_func;
93 void* dll_handle; // Keep DLL loaded
94
95 void operator()(IStrategy* strategy) const {
96 if (destroy_func && strategy) {
97 destroy_func(strategy);
98 }
99 // Note: We intentionally don't unload dll_handle here
100 // The DLL will be unloaded when the process exits
101 // This prevents crashes when destructors are called during program shutdown
102 }
103 };
104};
105
106} // namespace slick::trader
StrategyLoader & operator=(StrategyLoader &&)=delete
const std::string & get_last_error() const
StrategyLoader(StrategyLoader &&)=delete
StrategyLoader & operator=(const StrategyLoader &)=delete
const std::string & get_library_path() const
StrategyLoader(const StrategyLoader &)=delete
std::shared_ptr< IStrategy > load(const std::string &library_path)