3#include <algotradelib/core>
94 "all_market_phases must list every MarketPhase"
108 std::chrono::minutes
open_{std::chrono::hours(9)};
110 std::chrono::minutes
close_{std::chrono::hours(15) + std::chrono::minutes(30)};
114 std::string
source_{
"KRX regular session, 09:00-15:30 KST (library default)"};
132 std::chrono::system_clock::time_point now,
133 std::optional<bool> trading_day
142 const auto since_midnight =
143 std::chrono::duration_cast<std::chrono::minutes>(local - local_midnight(now));
144 if (since_midnight <
open_) {
147 if (
close_ <= since_midnight) {
163 using Days = std::chrono::duration<std::int64_t, std::ratio<86400>>;
165 std::chrono::duration_cast<Days>(local_midnight(now).time_since_epoch()).count()
176 std::chrono::system_clock::time_point now
185 std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> local_midnight(
186 std::chrono::system_clock::time_point now
188 using Days = std::chrono::duration<std::int64_t, std::ratio<86400>>;
189 return std::chrono::time_point_cast<std::chrono::seconds>(
238 return bar.timestamp_;
241 case BarInterval::ONE_MINUTE:
242 return bar.timestamp_ + std::chrono::minutes(1);
244 return bar.timestamp_ + std::chrono::minutes(5);
246 return bar.timestamp_ + std::chrono::minutes(15);
248 return bar.timestamp_ + std::chrono::minutes(30);
250 return bar.timestamp_ + std::chrono::hours(1);
251 case BarInterval::ONE_DAY:
252 return bar.timestamp_ + std::chrono::hours(24);
253 case BarInterval::ONE_WEEK:
254 return bar.timestamp_ + std::chrono::hours(24 * 7);
255 case BarInterval::ONE_MONTH:
257 return bar.timestamp_ + std::chrono::hours(24 * 31);
258 case BarInterval::ONE_YEAR:
259 return bar.timestamp_ + std::chrono::hours(24 * 366);
261 return bar.timestamp_;
281 event.interval_ = interval;
282 event.stamp_ = stamp;
284 event.bar_ = std::move(bar);
312 for (
const auto& event :
bars_) {
313 if (event.instrument_ ==
id) {
322namespace slice_detail {
331 bool empty() const noexcept {
return slices_.empty(); }
334 std::chrono::system_clock::time_point period_end() const noexcept {
return period_end_; }
337 bool holds(
const BarEvent& event)
const noexcept {
338 for (
const auto& slice : slices_) {
339 if (slice.interval_ == event.interval_) {
340 return slice.find(event.instrument_) !=
nullptr;
347 void add(
const BarEvent& event) {
348 if (slices_.empty()) {
349 period_end_ =
event.period_end_;
351 for (
auto& slice : slices_) {
352 if (slice.interval_ == event.interval_) {
353 slice.bars_.push_back(event);
359 slice.interval_ =
event.interval_;
360 slice.bars_.push_back(event);
361 slices_.push_back(std::move(slice));
365 BarSlice take_first() {
366 BarSlice slice = std::move(slices_.front());
367 slices_.erase(slices_.begin());
372 std::vector<BarSlice> take() {
373 std::vector<BarSlice> slices = std::move(slices_);
379 void clear() noexcept { slices_.clear(); }
382 std::chrono::system_clock::time_point period_end_{};
383 std::vector<BarSlice> slices_;
421 std::chrono::system_clock::time_point
at_{};
An exact signed decimal: an int64 coefficient times 10^-scale.
What a strategy sees and emits: events, intents, the portfolio, reconciliation and risk limits.
BarEvent make_bar_event(InstrumentId instrument, BarInterval interval, BarStamp stamp, Bar bar)
Makes a bar event, computing its period_end_ with bar_period_end.
MarketPhase
Where the clock stands relative to the trading session.
@ CLOSED
A date the market does not trade.
@ PRE_OPEN
A trading day, before the session opens.
@ UNKNOWN
The calendar does not cover this date; nothing is known about it.
@ POST_CLOSE
A trading day, after the session closed.
@ OPEN
Inside the session.
constexpr std::array< MarketPhase, 5 > all_market_phases
Every MarketPhase, in declaration order.
constexpr std::string_view market_phase_name(MarketPhase phase) noexcept
Returns a lowercase name for a phase, such as "pre_open", for logs and display.
std::chrono::system_clock::time_point bar_period_end(const Bar &bar, BarInterval interval, BarStamp stamp)
Computes the instant a bar's period ended.
constexpr bool market_phase_session_ended(MarketPhase phase) noexcept
Tests whether a phase means the day's session is over (or never opened), so a day order no longer wor...
The library: broker-neutral types (core), and the modules below.
BarStamp
Which edge of its period a bar's timestamp marks.
@ CLOSE
The timestamp is the instant the period ended.
constexpr bool lists_every_value(const std::array< Enum, N > &values, IsEnumerator is_enumerator) noexcept
Tests whether a list holds every value of an enum exactly once, in any order.
Date date_from_epoch_days(std::int64_t days)
Converts a day count since 1970-01-01 to a Gregorian date.
BarInterval
The period one bar covers.
@ FIVE_MINUTES
Five minutes.
@ THIRTY_MINUTES
Thirty minutes.
@ FIFTEEN_MINUTES
Fifteen minutes.
OrderSide
The side of an order: its economic direction, buy or sell.
One bar: the prices and volume of one period, as exact decimals.
A calendar date with no time of day or time zone.
Identifies an instrument: its country, venue and symbol.
A quote: the last traded price of an instrument at a point in time.
One bar, delivered to on_bar.
BarStamp stamp_
Whether bar_.timestamp_ marks the period's open or close.
std::chrono::system_clock::time_point period_end_
When the bar's period ended: the earliest moment the bar could be known.
InstrumentId instrument_
The instrument the bar is for.
BarInterval interval_
The period length the bar covers.
Every instrument's bar of one interval for one period end, delivered to on_bars.
std::chrono::system_clock::time_point period_end_
When the period ended, shared by every bar in bars_.
const BarEvent * find(const InstrumentId &id) const noexcept
Finds an instrument's bar.
std::vector< BarEvent > bars_
The bars, one per instrument, in the order they were fed.
BarInterval interval_
The period length every bar in bars_ covers.
One execution of an order the strategy placed, delivered to on_fill.
std::chrono::system_clock::time_point at_
When the execution happened.
InstrumentId instrument_
The instrument traded.
std::string tag_
The tag of the intent that produced the order.
OrderSide side_
The side of the order.
std::string order_id_
The broker's order id.
Decimal64 price_
The execution price.
Decimal64 quantity_
The quantity of this execution, not the order's running total.
A quote the runner has just read, delivered to on_quote.
QuoteSnapshot quote_
The quote, as the broker reported it.
The intraday session of a market, as the operator states it.
std::chrono::minutes open_
The session open, in minutes from local midnight.
std::string source_
A description of where the window came from, for reports.
std::chrono::minutes utc_offset_
The market's offset from UTC.
MarketPhase phase(std::chrono::system_clock::time_point now, std::optional< bool > trading_day) const
Returns the market phase at an instant.
std::chrono::system_clock::time_point opens_at(std::chrono::system_clock::time_point now) const
Returns the instant the session opens on the local date of an instant.
std::chrono::minutes close_
The session close, in minutes from local midnight.
Date local_date(std::chrono::system_clock::time_point now) const
Returns the market's local date at an instant.
static SessionWindow krx()
Returns the KRX regular session, 09:00-15:30 KST.