18#include <algotradelib/analytics>
19#include <algotradelib/core>
20#include <algotradelib/krx>
47using analytics::observed;
48using analytics::Scalar;
51std::string cell(Scalar value) {
55 std::ostringstream text;
56 text << std::fixed << std::setprecision(2) << std::setw(7) << value;
67math::Vector period_returns(math::SeriesView prices) {
68 math::Vector result = math::Vector::Constant(prices.size(), analytics::missing());
69 for (math::Index row = 1; row < prices.size(); ++row) {
70 if (
observed(prices[row]) &&
observed(prices[row - 1]) && prices[row - 1] > 0) {
71 result[row] = prices[row] / prices[row - 1] - 1;
80 Scalar mean_{analytics::missing()};
82 Scalar deviation_{analytics::missing()};
86Moments moments(math::SeriesView values) {
90 for (math::Index row = 0; row < values.size(); ++row) {
99 result.mean_ = sum / count;
104 for (math::Index row = 0; row < values.size(); ++row) {
106 squares += (values[row] - result.mean_) * (values[row] - result.mean_);
109 result.deviation_ = std::sqrt(squares / (count - 1));
123 static constexpr const char* closes[] = {
145 static constexpr const char* highs[] = {
167 static constexpr const char* lows[] = {
191 series.instrument_ = InstrumentId{Country::KR, Venue::COMBINED,
"005930"};
192 series.interval_ = BarInterval::ONE_DAY;
193 series.currency_ = Currency{
"KRW"};
194 series.received_at_ = std::chrono::system_clock::now();
196 const auto start = std::chrono::system_clock::time_point{} + std::chrono::hours(24 * 20000);
197 for (std::size_t day = 0; day < std::size(closes); ++day) {
199 bar.timestamp_ =
start + std::chrono::hours(24 *
static_cast<int>(day));
203 bar.open_ = Decimal64::parse(closes[day]);
205 bar.high_ = Decimal64::parse(highs[day]);
206 bar.low_ = Decimal64::parse(lows[day]);
207 bar.close_ = Decimal64::parse(closes[day]);
208 bar.volume_ = Decimal64::parse(
"1200000");
209 series.bars_.push_back(bar);
217 const BarSeries
bars = fixture();
221 const analytics::SeriesFrame frame = analytics::from_bars(bars);
225 const math::SeriesView close = frame.close();
226 const math::SeriesView open = frame.open();
227 const math::Vector returns = period_returns(close);
229 std::cout <<
bars.instrument_.symbol_ <<
", " <<
bars.bars_.size() <<
" daily bars, no network\n\n";
230 std::cout <<
"row close open return\n";
231 for (math::Index row = 0; row < close.size(); ++row) {
233 << std::setw(3) << row <<
" " << cell(close[row]) <<
" " << cell(open[row]) <<
" " << cell(returns[row] * 100.0)
239 std::cout <<
"\nThe dashes are the point. Row 0 has no return, and row 3 has no open at all.\n";
241 const auto daily = moments(returns);
243 <<
"\nOver the whole series:\n"
244 <<
" mean daily return " << std::fixed << std::setprecision(4) << daily.mean_ * 100.0 <<
" %\n"
245 <<
" daily volatility " << daily.deviation_ * 100.0 <<
" %\n"
246 <<
" per-period Sharpe " << analytics::sharpe(returns) <<
"\n"
247 <<
" annualized Sharpe " << analytics::annualize_ratio(analytics::sharpe(returns), 245.0) <<
"\n"
248 <<
" worst drawdown " << analytics::max_drawdown(close).magnitude_ * 100.0 <<
" %\n";
253 const auto recent = moments(close.tail(5));
255 const auto limit = analytics::round_scalar_to_tick(recent.mean_ + 2.0 * recent.deviation_, rule, OrderSide::SELL);
257 <<
"\nA sell limit two deviations above the last five closes' average, rounded onto the KRX tick grid: "
258 << (limit ? limit->str() : std::string(
"refused")) <<
"\n";
A three-letter currency code, such as KRW or USD (ISO 4217 style).
An exact signed decimal: an int64 coefficient times 10^-scale.
Series buffers and frames, the conversions to and from exact decimals, and performance metrics.
PriceRule equity_price_rule()
Returns the KRX stock tick table (January 2023 revision).
The numeric vocabulary: Scalar, missing(), and the matrix and vector types.
double Scalar
The floating-point type the analytics and math layers compute in (double).
bool observed(Scalar value)
Tests whether a value is an observation.
void start(S &strategy, Ctx &context)
Calls strategy.on_start(context) if the strategy declares on_start; otherwise does nothing.
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.
SecurityType
What kind of security an instrument is.
Country
A country (market) the library supports.
Venue
A trading venue: an exchange, or the combined view across exchanges.
BarInterval
The period one bar covers.
OrderSide
The side of an order: its economic direction, buy or sell.
The bars of one instrument and interval, oldest first.
One bar: the prices and volume of one period, as exact decimals.
Identifies an instrument: its country, venue and symbol.