Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
istrategy.hpp
Go to the documentation of this file.
1#pragma once
2
12#include <slick/queue.hpp>
13#include <memory>
14#include <string>
15#include <string_view>
16#include <vector>
17#include <nlohmann/json.hpp>
18
19namespace slick::trader {
20
21using json = nlohmann::json;
22
23class PositionTracker;
24class PnLCalculator;
25class RiskManager;
26
27// What a strategy plugin is built against: this interface, StrategyBase and BrokerStatus, whose
28// layouts the plugin and the host share. Raise it on any change to them; StrategyLoader loads no
29// plugin built against another.
30inline constexpr uint32_t STRATEGY_API_VERSION = 2;
31
32// Strategy interface
33// All strategies must implement this interface
34// Strategies receive market data and order events, and can submit orders
35class IStrategy {
36public:
37 virtual ~IStrategy() = default;
38
39 // Strategy identification
40 virtual const char* name() const = 0;
41 virtual const char* version() const = 0;
42
43 // Subscription requirements
44 // Strategies declare what market data they need
45 virtual std::vector<SubscriptionRequest> get_subscriptions() const = 0;
46
47 // Lifecycle
48 virtual bool initialize(
49 const json& config,
50 PositionTracker& position_tracker,
51 PnLCalculator& pnl_calculator,
52 RiskManager& risk_manager,
53 slick::queue<uint8_t>& order_request_queue
54 ) = 0;
55
56 virtual void start() = 0;
57 virtual void stop() = 0;
58 virtual void shutdown() = 0;
59
60 // Market data event handlers
61 virtual void on_trade(const TradeEvent& event) = 0;
62 virtual void on_quote(const QuoteEvent& event) = 0;
63 // The level 2 book of `instrument` after an update. Every strategy is handed the books any
64 // strategy subscribed, not only its own: check the name.
65 virtual void on_order_book(std::string_view instrument, const slick::orderbook::OrderBookL2& book) = 0;
66
67 // Order event handlers
68 virtual void on_order_ack(const OrderAckEvent& event) = 0;
69 virtual void on_order_fill(const OrderFillEvent& event) = 0;
70 virtual void on_order_reject(const OrderRejectEvent& event) = 0;
71 virtual void on_order_cancel(const OrderCancelEvent& event) = 0;
72
73 // Static description of an instrument - tick size, contract multiplier, quantity
74 // limits - as its broker publishes it. Optional: the default ignores it. Position and
75 // risk accounting already apply the contract multiplier, whatever a strategy does here.
76 virtual void on_instrument_definition(const InstrumentDefinitionEvent& /*event*/) {}
77
78 // The market-by-order book of an instrument subscribed with SubscriptionType::MBO, after
79 // each exchange event has been applied to it. Optional: the default ignores it.
80 // queue_position() (market_data/queue_position.hpp) reads from it where one of the
81 // strategy's own orders stands in the queue.
82 virtual void on_order_book_l3(std::string_view /*instrument*/,
83 const slick::orderbook::OrderBookL3& /*book*/) {}
84
85 // An instrument's trading state changed - it opened, halted, closed - as its broker
86 // publishes it. Optional: the default ignores it. Nothing is done about a halt on a
87 // strategy's behalf: whether to pull its orders is the strategy's decision.
88 virtual void on_instrument_status(const InstrumentStatusEvent& /*event*/) {}
89};
90
91} // namespace slick::trader
virtual void shutdown()=0
virtual void on_order_cancel(const OrderCancelEvent &event)=0
virtual const char * version() const =0
virtual void start()=0
virtual void on_order_ack(const OrderAckEvent &event)=0
virtual void stop()=0
virtual void on_instrument_status(const InstrumentStatusEvent &)
Definition istrategy.hpp:88
virtual ~IStrategy()=default
virtual void on_quote(const QuoteEvent &event)=0
virtual const char * name() const =0
virtual void on_trade(const TradeEvent &event)=0
virtual std::vector< SubscriptionRequest > get_subscriptions() const =0
virtual bool initialize(const json &config, PositionTracker &position_tracker, PnLCalculator &pnl_calculator, RiskManager &risk_manager, slick::queue< uint8_t > &order_request_queue)=0
virtual void on_order_book_l3(std::string_view, const slick::orderbook::OrderBookL3 &)
Definition istrategy.hpp:82
virtual void on_order_fill(const OrderFillEvent &event)=0
virtual void on_instrument_definition(const InstrumentDefinitionEvent &)
Definition istrategy.hpp:76
virtual void on_order_reject(const OrderRejectEvent &event)=0
virtual void on_order_book(std::string_view instrument, const slick::orderbook::OrderBookL2 &book)=0
nlohmann::json json
constexpr uint32_t STRATEGY_API_VERSION
Definition istrategy.hpp:30