Skip to content

Complete fixed-point/subpenny migration per Kalshi API#4

Merged
arshka merged 4 commits intoarshka:mainfrom
Agnimandur:fixed-point-migration
Mar 10, 2026
Merged

Complete fixed-point/subpenny migration per Kalshi API#4
arshka merged 4 commits intoarshka:mainfrom
Agnimandur:fixed-point-migration

Conversation

@Agnimandur
Copy link
Collaborator

@Agnimandur Agnimandur commented Mar 8, 2026

Summary

Complete migration of all pricing and quantity fields to Kalshi's new fixed-point string format, with a backward-compatibility layer for smooth migration.

What changed

All integer cent fields → _dollars string fields:

  • yes_bid, yes_ask, no_bid, no_askyes_bid_dollars, yes_ask_dollars, etc.
  • last_price, previous_yes_bid, previous_yes_ask, previous_price*_dollars variants
  • settlement_value, notional_value, tick_size*_dollars variants
  • yes_price, no_price (orders/trades) → yes_price_dollars, no_price_dollars
  • All OHLC price data, orderbook levels, WebSocket feed prices → *_dollars

All integer count fields → _fp string fields:

  • count, initial_count, fill_count, remaining_count*_fp variants
  • position, total_tradedposition_fp, total_traded_dollars
  • volume, volume_24h, open_interest*_fp variants

Backward-compatibility layer (CompatModel):

  • Old field names (e.g. market.yes_bid, order.count) still work via __getattr__ with DeprecationWarning
  • model_validator(mode='before') remaps old API field names during the transition period (until March 12)

Model field name corrections

Several models had incorrect field names after the initial migration which blindly renamed all fields. Fixed to match actual Kalshi API responses:

  • BalanceModel: reverted to balance/portfolio_value (int cents — API never migrated these)
  • FillModel: yes_price_dollars/no_price_dollarsyes_price_fixed/no_price_fixed (API uses _fixed suffix for fill prices)
  • PositionModel: total_traded_fptotal_traded_dollars (it's a dollar amount, not a count)
  • MarketModel: liquidity_fpliquidity_dollars
  • SettlementModel: reverted costs/revenue to int cents, kept fee_cost as str (FixedPointDollars)
  • QueuePositionModel: queue_position (int) → queue_position_fp (str)

New features

  • Tick size validation: _validate_tick_size() validates price alignment for linear_cent, deci_cent, and tapered_deci_cent structures
  • Fractional trading validation: _validate_fractional() rejects fractional count_fp on non-fractional markets
  • OrderbookResponse: accepts both orderbook and orderbook_fp API keys via AliasChoices

Test plan

  • All 309 unit tests pass
  • Field names verified against official Kalshi API documentation
  • Integration tests against demo API

🤖 Generated with Claude Code

Agnimandur and others added 4 commits March 7, 2026 18:33
Migrate all integer cent fields to `_dollars` string fields and all integer
count fields to `_fp` string fields, matching Kalshi's fixed-point migration
guide. Add order validation for price_level_structure tick sizes and
fractional trading.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Existing code using old integer field names (yes_bid, count, balance, etc.)
now works via CompatModel with deprecation warnings pointing to new names.

GET (reading): Legacy integer accessors derive from new string fields via
__getattr__, e.g. market.yes_bid → int(Decimal("0.45") * 100) = 45.

POST (writing): Old integer params (count=5, yes_price=45) convert to new
string params (count_fp="5", yes_price_dollars="0.45") with warnings.

- New pykalshi/_compat.py: CompatModel base class, conversion functions,
  convert_legacy_kwargs helper, and standard legacy param mappings
- Updated 20 model classes in models.py with _legacy_fields mappings
- Updated 6 WebSocket message models in feed.py with _legacy_fields
- Added legacy params to all POST methods in portfolio.py (sync+async)
- Added legacy params to Order.amend/decrease in orders.py (sync+async)
- Added legacy params to create_rfq in communications.py (sync+async)
- 34 new tests in tests/test_compat.py (294 total tests passing)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…put format

spread_bps would crash with TypeError when spread/mid were None (empty or
one-sided orderbook). cents_to_dollars and int_to_fp now produce canonical
fixed-point strings (e.g. "0.50", "5.00") matching the Kalshi API format.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Cover _validate_tick_size for all three price_level_structures
(linear_cent, deci_cent, tapered_deci_cent) including boundary
conditions, and _validate_fractional for enabled/disabled cases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@arshka arshka enabled auto-merge (squash) March 10, 2026 00:22
@arshka arshka merged commit 3d87df0 into arshka:main Mar 10, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants