diff --git a/doc/api/content/server/getting-started/orderbook.png b/doc/api/content/server/getting-started/orderbook.png deleted file mode 100644 index d51380963..000000000 Binary files a/doc/api/content/server/getting-started/orderbook.png and /dev/null differ diff --git a/doc/api/content/server/getting-started/overview.md b/doc/api/content/server/getting-started/overview.md index 03db6afbc..63c9b71c0 100644 --- a/doc/api/content/server/getting-started/overview.md +++ b/doc/api/content/server/getting-started/overview.md @@ -3,15 +3,16 @@ title = "Overview" weight = 0 +++ -Using the **Spark Server SDK**, users can create new order types ("Algos") and soon new market data sources and execution gateways. In this case, users implement the new functionality in C++, producing a shared library that is loaded at run time by our Spark Server. +Using the **Spark Server SDK**, users can create new order types ("Algos"). In this case, users implement the new functionality in C++, producing a shared library that is loaded at run time by our Spark Server. -If you are interested in sending orders to or otherwise controlling a Spark server remotely, please see the BTS [Spark Client SDK](/client/). +If you are interested in accessing Spark server remotely, please see the [Spark Client SDK](/client/). ## Speed The Spark platform offers a sub-10 microsecond socket to socket response times for algorithmic trading. With the **Spark Server SDK**, you can create your order types in high-performance C++ and load them into the Spark server. -Since your order is running inside the Spark Server, there is virtually no overhead allowing your algorithm to take full advantage of the speed Spark offers. This in-process model enables your Algo to respond to market events **hundreds or thousands of times faster** than a typical "drag and drop" Algo builder. +Since your order is running inside the Spark Server, there is virtually no overhead allowing your algorithm to take full advantage of the speed Spark offers. Using a high-performance language like C++ and hosting your Algo directly in the Spark server process allows it to respond to the market **hundreds to thousands of times faster** than an Algo built using a proprietary drag and drop style tool. + ## Easy Setup @@ -21,7 +22,7 @@ Docker works across Windows, Mac, and Linux, so you don't need to change your de ## Implementation -To create a new Algo, you will implement a regular C++ class deriving from IAlgoOrder. Start from the algo_template example to get going quickly. +To create a new Algo, you will implement a regular C++ class deriving from IAlgoOrder. Start from the [algo_template](/server/samples/template) example to get going quickly. When an Algo is created, the first call it receives is [do_before_launch](/server/ref/initialization#before-launch), which is an opportunity to validate parameters and allocate any long-lived resources needed during execution. Next, it receives either [do_launch](/server/ref/initialization#launch-launch-attached) in the case of a new order or [do_launch_attached](/server/ref/initialization#launch-launch-attached) if the order is attaching to an existing order. @@ -29,20 +30,15 @@ The Algo receives a reference to [IAlgoServices](/server/ref/subscriptions/), wh ## Fully Integrated -When a Spark Server plugin is loaded, it registers its exposed Algos with the Spark Server though the [init_spark_plugin](/server/ref/registration) callback. This callback allows the Algo to describe the parameters it expects so they can be automatically entered in the [Spark Client](https://www.bluetradingsystems.com/bts-spark/) and saved in the Algo Studio. +When a Spark Server plugin is loaded, it registers its exposed Algos with the Spark Server though the [init_spark_plugin](/server/ref/registration) callback. This callback allows the Algo to describe the parameters it expects so they can be automatically entered in the [Spark Client](https://www.bluetradingsystems.com/bts-spark/). -### Order Book + + -View and change parameters directly in the Spark order book window. - - - -### Algo Studio - -Compose your Algo with out of the box Algos to create new functionality on the fly. +Compose your Algo with out of the box Algos in the Spark Algo Studio to create new functionality on the fly.
- + diff --git a/doc/api/content/server/getting-started/paramsui.png b/doc/api/content/server/getting-started/paramsui.png new file mode 100644 index 000000000..d81b39c35 Binary files /dev/null and b/doc/api/content/server/getting-started/paramsui.png differ diff --git a/doc/api/content/server/ref/registration.md b/doc/api/content/server/ref/registration.md index f77f3a17e..c909a42ac 100644 --- a/doc/api/content/server/ref/registration.md +++ b/doc/api/content/server/ref/registration.md @@ -42,6 +42,10 @@ The above parameters look like this when rendered in the Spark ladder. +And like this when viewed in the Spark order book. + + + Each descriptor can be one of the following types: ``` // Primitive diff --git a/doc/api/content/server/samples/template.md b/doc/api/content/server/samples/template.md new file mode 100644 index 000000000..48c8150c0 --- /dev/null +++ b/doc/api/content/server/samples/template.md @@ -0,0 +1,146 @@ ++++ +title = "Template" +weight = 5 ++++ + +A mostly blank Algo template that can be used as a jump-start for developing a new Algo. + +
+ +This sample is part of the Server SDK examples included in the spark_sdk.zip download. +
+ +```cpp +#include "spark/lib/config/config.h" +#include "spark/lib/config/protos/system.pb.h" +#include "spark/lib/domain_model/Security.h" +#include "spark/lib/orderman/orders/algo/IAlgoOrder.h" +#include "spark/lib/orderman/orders/algo/IAlgoServices.h" +#include "spark/lib/plugin/SparkServices.h" + +#include "spark/lib/orderman/order_factory/IOrderFactory.h" +#include "spark/lib/orderman/orderman_utils/Book.h" +#include "spark/lib/orderman/orderman_utils/orderman_utils.h" +#include "spark/lib/utils/PBUtils.h" + +using namespace bts::api::orderman; +using namespace bts::orderman; + +namespace bts { +namespace order_example { + +// MyAlgo is a blank IAlgoOrder which you can copy when starting a new Algo +class MyAlgo : public IAlgoOrder { + public: + MyAlgo(); + ~MyAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_book(SecurityKey, Book const &) override; + void on_child_change(OrderKey, ChildState const &) override; + + private: + IAlgoServices* algo_services; + ChildTemplate *child_template = nullptr; + OrderKey child_key = null_order_key(); +}; + + +MyAlgo::MyAlgo() : algo_services(nullptr) {} +MyAlgo::~MyAlgo() {} + +// This is called outside of the latency sensitive path. +// Expensive initialization should be done here, rather than in do_launch() +OrderOpResult MyAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { + LOG(Sdk,Info, "MyAlgo: do_before_launch"); + // If we are not attaching, create a child template so we can + // quickly launch our child when the time comes. + if (!serv.is_attaching()) { + // We will be sending a limit order with the same side, + // security, trader, account, etc, as this algo order, so just + // copy our definition to the child definition to start with. + auto child_def = serv.definition(); + // Now add a limit order component to mark our child as a limit + // order. Limit orders do not require any extra parameters + // beyond those already inherited from this algo order + // (i.e. beyond what we obtained through serv.definition()) so + // we can just use a default constructed LimitOrderDefinition. + *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; + // Now just build the child template and cache it for future use + child_template = &(serv.make_child_template(child_def)); + } + // Default constructed result is successful + return {}; +} + +// Called in the latency sensitive path, once, when the algo is first launched. +// If the Algo is attaching to an existing order do_launch_attached is called instead. +void MyAlgo::do_launch(IAlgoServices &services) { + algo_services = &services; + // Subscribe to book updates for this security + services.subscribe_book(SecurityKey(services.security().id())); + // Launch a child limit order + child_key = + services.launch_child(*child_template, services.price(), services.quantity()); + LOG(Sdk,Info, "MyAlgo: do_launch"); +} + +// Called in the latency sensitive path, once, when the algo is first +// launched. In this case the algo is provided with an existing order +// it is now responsible for managing. +void MyAlgo::do_launch_attached(IAlgoServices &services, + OrderKey attached_order, ChildState st) { + algo_services = &services; + // Subscribe to book updates for this security + services.subscribe_book(SecurityKey(services.security().id())); + // Keep track of the child key + child_key = attached_order; + LOG(Sdk,Info, "MyAlgo: do_launch_attached"); +} + +// Called when a human (or a supervising algo) updates a parameter for +// this algo. The implementation validates the update and depending on +// the validation outcome either applies the update or terminates the algo. +void MyAlgo::do_update(api::orderman::Update const &update) { +} + +// Called when the book for the specified security has changed +void MyAlgo::on_book(SecurityKey, Book const &b) { +} + +// Our child reported a change +void MyAlgo::on_child_change(OrderKey, ChildState const &st) { +} + +// This is called by the spark daemon at startup. In this call we +// describe our algo's parameters and register a factory function to +// create an instance of the algo. +extern "C" void init_spark_plugin(bts::SparkServices &serv) { + using namespace bts; + + DynamicOrderDescriptor desc; + + // Start by naming our order type + desc.set_dynamic_order_type("My Algo"); + + *desc.add_param_desc() = param_bool("Enable Feature"); + *desc.add_param_desc() = param_uint32("Feature Quantity"); + + // Associate the descriptor with a builder function and register + // that with the Spark server. + auto result = + serv.register_dynamic_algo_order( + desc, []() { return std::make_unique(); }); + + if (result != bts::AlgoRegistrationResult::SUCCESS) { + // The desc has errors + // Check the error log (/sparkdata/data/logging/log) for more details + } +} + +} // namespace order_example +} // namespace bts +``` \ No newline at end of file diff --git a/src/lib/plugin/plugin.cpp b/src/lib/plugin/plugin.cpp index 5cab0e815..0c72dad5c 100644 --- a/src/lib/plugin/plugin.cpp +++ b/src/lib/plugin/plugin.cpp @@ -9,11 +9,14 @@ #include #include +void init_spark_plugin(bts::SparkServices &serv); + namespace bts { namespace plugin { using namespace bts::orderman; + void init_plugins(SparkServices &services) { static std::vector( diff --git a/src/sdk/examples/algo_template/algo_template.cpp b/src/sdk/examples/algo_template/algo_template.cpp index 276177dda..41e55969a 100644 --- a/src/sdk/examples/algo_template/algo_template.cpp +++ b/src/sdk/examples/algo_template/algo_template.cpp @@ -31,6 +31,8 @@ class MyAlgo : public IAlgoOrder { private: IAlgoServices* algo_services; + ChildTemplate *child_template = nullptr; + OrderKey child_key = null_order_key(); }; @@ -41,6 +43,22 @@ MyAlgo::~MyAlgo() {} // Expensive initialization should be done here, rather than in do_launch() OrderOpResult MyAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { LOG(Sdk,Info, "MyAlgo: do_before_launch"); + // If we are not attaching, create a child template so we can + // quickly launch our child when the time comes. + if (!serv.is_attaching()) { + // We will be sending a limit order with the same side, + // security, trader, account, etc, as this algo order, so just + // copy our definition to the child definition to start with. + auto child_def = serv.definition(); + // Now add a limit order component to mark our child as a limit + // order. Limit orders do not require any extra parameters + // beyond those already inherited from this algo order + // (i.e. beyond what we obtained through serv.definition()) so + // we can just use a default constructed LimitOrderDefinition. + *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; + // Now just build the child template and cache it for future use + child_template = &(serv.make_child_template(child_def)); + } // Default constructed result is successful return {}; } @@ -51,6 +69,9 @@ void MyAlgo::do_launch(IAlgoServices &services) { algo_services = &services; // Subscribe to book updates for this security services.subscribe_book(SecurityKey(services.security().id())); + // Launch a child limit order + child_key = + services.launch_child(*child_template, services.price(), services.quantity()); LOG(Sdk,Info, "MyAlgo: do_launch"); } @@ -60,7 +81,10 @@ void MyAlgo::do_launch(IAlgoServices &services) { void MyAlgo::do_launch_attached(IAlgoServices &services, OrderKey attached_order, ChildState st) { algo_services = &services; + // Subscribe to book updates for this security services.subscribe_book(SecurityKey(services.security().id())); + // Keep track of the child key + child_key = attached_order; LOG(Sdk,Info, "MyAlgo: do_launch_attached"); }