kalshi-cpp 0.6.2
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
37[[nodiscard]] Result<Timestamp> parse_timestamp(std::string_view text);
38
41[[nodiscard]] constexpr OutcomeSide outcome_side(Side side, Action action) noexcept {
42 if (side == Side::Unknown || action == Action::Unknown) {
44 }
45 const bool yes = (side == Side::Yes) == (action == Action::Buy);
46 return yes ? OutcomeSide::Yes : OutcomeSide::No;
47}
48
51[[nodiscard]] constexpr BookSide book_side(Side side, Action action) noexcept {
52 switch (outcome_side(side, action)) {
54 return BookSide::Bid;
55 case OutcomeSide::No:
56 return BookSide::Ask;
58 break;
59 }
60 return BookSide::Unknown;
61}
62
63} // 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:51
Side
Deprecated.
Definition models.hpp:709
@ 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:558
@ 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:41