kalshi-cpp 0.8.0
C++23 client for Kalshi's Predictions API
Loading...
Searching...
No Matches
helpers.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "kalshi/error.hpp"
5#include "kalshi/models.hpp"
6
7#include <chrono>
8#include <cstdint>
9#include <string_view>
10
11namespace kalshi {
12
15[[nodiscard]] inline Result<std::int64_t> to_cents(std::string_view dollars) {
17 if (!value) {
18 return std::unexpected(std::move(value.error()));
19 }
20 return value->scaled_integer(2);
21}
22
25[[nodiscard]] inline Result<std::int64_t> to_contracts(std::string_view count) {
27 if (!value) {
28 return std::unexpected(std::move(value.error()));
29 }
30 return value->scaled_integer(0);
31}
32
33using Timestamp = std::chrono::sys_time<std::chrono::milliseconds>;
34
38[[nodiscard]] Result<Timestamp> parse_timestamp(std::string_view text);
39
42[[nodiscard]] constexpr OutcomeSide outcome_side(Side side, Action action) noexcept {
43 if (side == Side::Unknown || action == Action::Unknown) {
45 }
46 const bool yes = (side == Side::Yes) == (action == Action::Buy);
47 return yes ? OutcomeSide::Yes : OutcomeSide::No;
48}
49
52[[nodiscard]] constexpr BookSide book_side(Side side, Action action) noexcept {
53 switch (outcome_side(side, action)) {
55 return BookSide::Bid;
56 case OutcomeSide::No:
57 return BookSide::Ask;
59 break;
60 }
61 return BookSide::Unknown;
62}
63
64} // namespace kalshi
static Result< FixedPoint > parse(std::string_view value)
Definition fixed_point.hpp:15
Definition api.hpp:15
constexpr BookSide book_side(Side side, Action action) noexcept
The single-book side for a (side, action) pair: yes exposure bids.
Definition helpers.hpp:52
Side
Definition models.hpp:867
@ Unknown
A value this SDK version does not know.
Result< std::int64_t > to_cents(std::string_view dollars)
Converts a dollar string such as "0.5600" to whole cents.
Definition helpers.hpp:15
Action
Deprecated.
Definition models.hpp:35
@ Unknown
A value this SDK version does not know.
Result< std::int64_t > to_contracts(std::string_view count)
Converts a fixed-point count such as "10.00" to whole contracts.
Definition helpers.hpp:25
std::expected< T, Error > Result
Result type for SDK operations.
Definition error.hpp:50
BookSide
Side of the book for an order or trade.
Definition models.hpp:110
@ Unknown
A value this SDK version does not know.
std::chrono::sys_time< std::chrono::milliseconds > Timestamp
Definition helpers.hpp:33
Result< Timestamp > parse_timestamp(std::string_view text)
Parses the RFC 3339 timestamps Kalshi returns, such as "2026-09-25T14:00:00.123Z" or "2026-09-25T10:0...
OutcomeSide
Outcome side.
Definition models.hpp:644
@ Unknown
A value this SDK version does not know.
constexpr OutcomeSide outcome_side(Side side, Action action) noexcept
The outcome a (side, action) pair is exposed to: buying yes or selling no is yes exposure.
Definition helpers.hpp:42