3#include <algotradelib/core>
24namespace bootstrap_detail {
27inline std::invalid_argument refusal(
const char* who,
const char* why) {
28 return std::invalid_argument(std::string(who) +
": " + why);
33inline bool identity_draw(
34 std::size_t count,
const std::vector<std::size_t>& indices,
const std::vector<Decimal64>& prices,
bool any_sign,
35 const char* who,
const char* few,
const char* sign
37 if (count < 2 || indices.size() > count - 1) {
38 throw refusal(who, few);
40 bool identity = indices.size() == count - 1;
41 for (std::size_t k = 0; k < indices.size(); ++k) {
42 identity = identity && indices[k] == k;
43 if (indices[k] >= count - 1) {
44 throw refusal(who,
"an index names no return");
47 for (
const auto& price : prices) {
48 if (!any_sign && price.coefficient() <= 0) {
49 throw refusal(who, sign);
59 static constexpr std::int64_t
LIMIT = std::int64_t{1} << 53;
62 Grid(
const PriceRule& rule,
bool any_sign,
const char* who) : rule_(rule), any_sign_(any_sign), who_(who) {
64 throw refusal(who_,
"the price grid is unknown");
70 void widen(
const std::vector<Decimal64>& values) {
71 for (
const auto& value : values) {
72 scale_ = std::max(scale_, value.scale());
77 std::int64_t units(
const Decimal64& value)
const {
79 for (
auto step = value.
scale();; ++step) {
81 throw refusal(who_,
"a value at or beyond 2^53 grid units");
91 Decimal64 round(
double level,
const char* zero)
const {
92 constexpr auto limit =
static_cast<double>(
LIMIT);
94 while (band + 1 < grid_.size() &&
static_cast<double>(grid_[band + 1].first) <= level) {
99 !(level < limit) || !(level > -limit) || grid_.empty() ||
100 (floored &&
static_cast<double>(grid_[band].first) > level)
102 throw refusal(who_,
"a level at or beyond 2^53 grid units, or below the first band");
104 const auto tick = grid_[band].second;
105 auto lower =
static_cast<std::int64_t
>(std::floor(level /
static_cast<double>(tick)));
107 const std::int64_t lo = lower * tick;
108 const std::int64_t hi = lo + tick;
110 throw refusal(who_,
"a grid neighbour at or beyond 2^53 grid units");
112 if (
static_cast<double>(lo) > level) {
114 }
else if (
static_cast<double>(hi) <= level) {
117 const auto nearest = level -
static_cast<double>(lo) <
static_cast<double>(hi) - level ? lo : hi;
118 if (nearest == 0 && !any_sign_) {
119 throw refusal(who_, zero);
131 grid_.emplace_back(0, 1);
134 for (
const auto& band : rule_.bands_) {
135 scale_ = std::max({scale_, band.from_.scale(), band.tick_.scale()});
137 for (
const auto& band : rule_.bands_) {
138 const auto start = units(band.from_);
139 const auto tick = units(band.tick_);
140 if (tick <= 0 || start % tick != 0 || (!grid_.empty() && start % grid_.back().second != 0)) {
141 throw refusal(who_,
"a band's start is not a multiple of its tick and of the previous band's");
143 grid_.emplace_back(start, tick);
150 std::uint8_t scale_{0};
151 std::vector<std::pair<std::int64_t, std::int64_t>> grid_;
155template <
typename Join,
typename =
void>
156struct has_any_sign : std::false_type {};
158template <
typename Join>
159struct has_any_sign<Join, std::void_t<decltype(Join::any_sign)>> : std::bool_constant<Join::any_sign> {};
172 double join(
double level,
double from,
double value)
const {
173 return level * (value / from);
190 double join(
double level,
double from,
double value)
const {
191 constexpr auto limit =
static_cast<double>(bootstrap_detail::Grid::LIMIT);
192 const double step = value - from;
193 if (!(step < limit && step > -limit)) {
194 throw std::invalid_argument(
"Differences: a step at or beyond 2^53 grid units");
215 double join([[maybe_unused]]
double level, [[maybe_unused]]
double from,
double value)
const {
225template <
typename Join,
typename =
void>
229template <
typename Join>
233 std::is_convertible_v<decltype(std::declval<const Join&>().join(double{}, double{}, double{})), double>
235> : std::true_type {};
268template <
typename Join = Returns>
270 const std::vector<Decimal64>& closes,
const PriceRule& rule,
const std::vector<std::size_t>& indices,
271 const Join& join = Join{}
274 is_price_join<Join>::value,
275 "a bootstrap join needs the member double join(double level, double from, double value) const"
277 constexpr bool any_sign = bootstrap_detail::has_any_sign<Join>::value;
278 const bool identity = bootstrap_detail::identity_draw(
279 closes.size(), indices, closes, any_sign,
"resample_closes",
280 "needs two closes or more, and at most one index per return",
"a close at or below zero"
283 bootstrap_detail::Grid grid(rule, any_sign,
"resample_closes");
288 std::vector<double> in;
289 for (
const auto& close : closes) {
290 in.push_back(
static_cast<double>(grid.units(close)));
292 std::vector<Decimal64> out;
293 double level = in.front();
294 for (std::size_t k = 0; k <= indices.size(); ++k) {
296 level = join.join(level, in[indices[k - 1]], in[indices[k - 1] + 1]);
298 out.push_back(grid.round(level,
"a close rounded to zero"));
330template <
typename Join = Returns>
332 const std::vector<Bar>& bars,
const PriceRule& rule,
const std::vector<std::size_t>& indices,
333 const Join& join = Join{}
336 is_price_join<Join>::value,
337 "a bootstrap join needs the member double join(double level, double from, double value) const"
339 constexpr bool any_sign = bootstrap_detail::has_any_sign<Join>::value;
340 std::vector<Decimal64> prices;
341 for (
const auto& bar : bars) {
343 prices.push_back(*bar.open_);
345 prices.insert(prices.end(), {bar.high_, bar.low_, bar.close_});
347 const bool identity = bootstrap_detail::identity_draw(
348 bars.size(), indices, prices, any_sign,
"resample_bars",
"needs two bars or more, and at most one index per return",
349 "a price at or below zero"
351 bootstrap_detail::Grid grid(rule, any_sign,
"resample_bars");
357 for (
const auto& price : prices) {
360 const auto units = [&grid](
const Decimal64& value) {
361 return static_cast<double>(grid.units(value));
363 std::vector<Bar> out;
364 double level = units(
bars.front().close_);
365 for (std::size_t k = 0; k <= indices.size(); ++k) {
367 const auto& drawn = k == 0 ?
bars.front() :
bars[indices[k - 1] + 1];
368 const double from = k == 0 ? 0.0 : units(bars[indices[k - 1]].close_);
369 const auto price = [&](
const Decimal64& value) {
370 return grid.round(k == 0 ? units(value) : join.join(level, from, units(value)),
"a price rounded to zero");
373 bar.timestamp_ =
bars[k].timestamp_;
375 bar.open_ = price(*drawn.open_);
377 bar.high_ = price(drawn.high_);
378 bar.low_ = price(drawn.low_);
379 bar.close_ = price(drawn.close_);
381 level = join.join(level, from, units(drawn.close_));
382 bar.traded_value_.reset();
An exact signed decimal: an int64 coefficient times 10^-scale.
std::uint8_t scale() const noexcept
Returns the scale.
std::int64_t coefficient() const noexcept
Returns the coefficient.
Decimal64()=default
Constructs zero.
Replays a strategy over stored bars: fill and cost models, the replay ledger, and the report.
std::vector< Decimal64 > resample_closes(const std::vector< Decimal64 > &closes, const PriceRule &rule, const std::vector< std::size_t > &indices, const Join &join=Join{})
Rebuilds one instrument's closes along a bootstrap path (analytics::bootstrap_indices),...
std::vector< Bar > resample_bars(const std::vector< Bar > &bars, const PriceRule &rule, const std::vector< std::size_t > &indices, const Join &join=Join{})
Rebuilds one instrument's bars along a bootstrap path (analytics::bootstrap_indices): each drawn day'...
void bar(S &strategy, Ctx &context, const BarEvent &event)
Calls strategy.on_bar(context, event) if the strategy declares on_bar; otherwise does nothing.
void bars(S &strategy, Ctx &context, const BarSlice &slice)
Calls strategy.on_bars(context, slice) if the strategy declares on_bars; otherwise does nothing.
The library: broker-neutral types (core), and the modules below.
@ LIMIT
지정가: fills at the stated price or better.
The price grid (tick sizes) an instrument's order prices must sit on.
@ UNCONSTRAINED
The market imposes no grid (free decimals).
constexpr bool grid_known() const noexcept
Tests whether the rule says anything about the grid: a stated one, or none at all.
Chains the drawn changes (Paparoditis and Politis): the path's previous close plus value - from.
static constexpr bool any_sign
Prices at or below zero are joined, not refused.
double join(double level, double from, double value) const
Returns the path's price for a drawn price.
Joins the drawn prices as they are (Künsch; Politis and Romano): for a stationary series,...
static constexpr bool any_sign
Prices at or below zero are joined, not refused.
double join(double level, double from, double value) const
Returns the drawn price.
Chains the drawn returns: a path price is the path's previous close times value / from.
double join(double level, double from, double value) const
Returns the path's price for a drawn price.
Whether Join can join drawn blocks: true when a const Join has a member join(double,...