Skip to Content
raikiri

Write a strategy

A strategy is a C++ class. Write one, compile it into strategyd and run it on paper.

A strategy overrides the callbacks it needs and trades through Context. All strategies run on one thread, so you need no locks, and a callback that blocks holds up every other strategy.

1. Write it

This one keeps a single lot resting at the best BTC/USDT bid.

libs/strategies/include/raikiri/strategies/join_bid.hpp
#pragma once

#include <raikiri/strategy/strategy.hpp>

namespace raikiri::strategies {

class JoinBid final : public strategy::Strategy {
public:
    void on_bbo(strategy::Context& ctx, const Bbo& bbo) override {
        const auto* instrument = ctx.instrument(bbo.instrument);
        if (instrument == nullptr || base_of(*instrument).local_symbol.view() != "BTC/USDT") {
            return; // every instrument of every attached gateway arrives here
        }
        if (bbo.bid.is_undef() || bbo.bid == price_) {
            return;
        }
        if (resting_.valid()) {
            (void)ctx.cancel(resting_);
        }
        strategy::NewOrder order{};
        order.instrument = bbo.instrument;
        order.side = Side::Buy;
        order.flags = OrderFlags::PostOnly;
        order.price = bbo.bid;
        order.qty = QtyLots::from_raw(1);
        resting_ = ctx.submit(order).value_or(ClientOrderId{});
        price_ = bbo.bid;
    }

private:
    ClientOrderId resting_{};
    PriceTicks price_{};
};

} // namespace raikiri::strategies

It is an example, not a strategy to run with money: it buys on every fill and never sells. Attach it to one gateway, as below.

2. Register it

Include the header in libs/strategies/src/register.cpp and register the type in register_all:

strategy::register_type<JoinBid>(types, "join-bid");

Then rebuild: cmake --build --preset dev-asan --target strategyd gatewayd.

3. Run it on the simulated venue

build/dev-asan/apps/gatewayd/gatewayd --venue=sim &
build/dev-asan/apps/strategyd/strategyd --gateway=sim --strategy join-bid --duration 60

The shipped risk limits allow orders up to 20 and one simulated BTC lot is worth about 50, so give join-bid its own entry in config/risk.yaml first.

To run it on live accounts, see raikiri Pro. --duration defaults to 10 seconds.

Callbacks

CallbackWhen
on_start, on_stopthe strategy starts or stops; after a stop its open orders are cancelled
on_bbothe best bid or ask changed; only the newest value is delivered
on_trade, on_fundinga public trade, a funding update
on_order_eventone of your orders was accepted, filled, cancelled or rejected
on_timera timer set with set_timer came due; timers fire once

Context

CallDoes
submit(order)sends an order; returns its id, or Risk, Duplicate, VenueDown or Capacity
cancel, cancel_all, amendact on your own orders
set_target(target)declares the position to hold; the difference goes out as a market order
bbo, instrument, position, unrealizedread the latest state

The full interface is strategy.hpp, and demo_maker.hpp quotes both sides.

Edit this page on GitHub

On this page