ghosalquant 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. {ghosalquant-0.2.0/src/ghosalquant.egg-info → ghosalquant-0.2.1}/PKG-INFO +1 -1
  2. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/__init__.py +1 -1
  3. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/recording/storage.py +49 -27
  4. {ghosalquant-0.2.0 → ghosalquant-0.2.1/src/ghosalquant.egg-info}/PKG-INFO +1 -1
  5. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_recording.py +34 -0
  6. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/LICENSE +0 -0
  7. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/README.md +0 -0
  8. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/pyproject.toml +0 -0
  9. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/setup.cfg +0 -0
  10. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/backtest_ui/__init__.py +0 -0
  11. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/backtest_ui/__main__.py +0 -0
  12. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/backtest_ui/app.py +0 -0
  13. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/backtest_ui/runner.py +0 -0
  14. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/backtester/__init__.py +0 -0
  15. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/backtester/backtester_base.py +0 -0
  16. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/backtester/dataframe_backtester.py +0 -0
  17. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/bench/__init__.py +0 -0
  18. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/bench/benchmark_buy_and_hold.py +0 -0
  19. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/bench/benchmark_trend_vol_target.py +0 -0
  20. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/blocks/__init__.py +0 -0
  21. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/blocks/nodes.py +0 -0
  22. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/blocks/runner.py +0 -0
  23. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/blocks/schema.py +0 -0
  24. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/blocks/strategy.py +0 -0
  25. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/__init__.py +0 -0
  26. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/broker_base.py +0 -0
  27. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/builder.py +0 -0
  28. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/commissions.py +0 -0
  29. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/fills.py +0 -0
  30. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/ibkr.py +0 -0
  31. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/open_orders.py +0 -0
  32. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/order_book_sim.py +0 -0
  33. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/simulated.py +0 -0
  34. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/brokerage/simulation.py +0 -0
  35. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/compatibility/legacybacktester.py +0 -0
  36. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/compatibility/plotManager.py +0 -0
  37. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/compatibility/test_strategy.py +0 -0
  38. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/compatibility/trade.py +0 -0
  39. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/core/__init__.py +0 -0
  40. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/core/blackscholes.py +0 -0
  41. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/core/contracts.py +0 -0
  42. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/core/depth.py +0 -0
  43. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/core/metadata_keys.py +0 -0
  44. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/core/types.py +0 -0
  45. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/core/volatility.py +0 -0
  46. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/__init__.py +0 -0
  47. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/cboe_datasource.py +0 -0
  48. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/dataLayer.py +0 -0
  49. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/dataframe_datasource.py +0 -0
  50. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/datasource_base.py +0 -0
  51. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/datasource_factory.py +0 -0
  52. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/dolt_datasource.py +0 -0
  53. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/listEditor.py +0 -0
  54. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/list_datasource.py +0 -0
  55. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/live_alpaca_datasource.py +0 -0
  56. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/live_ibkr_datasource.py +0 -0
  57. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/live_polygon_datasource.py +0 -0
  58. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/merge.py +0 -0
  59. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/nse_datasource.py +0 -0
  60. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/parquet_polygon_datasource.py +0 -0
  61. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/recording_datasource.py +0 -0
  62. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/shocks.py +0 -0
  63. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/staticUpdater.py +0 -0
  64. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/yfinance_datasource.py +0 -0
  65. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/data_layer/yfinance_multi_datasource.py +0 -0
  66. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/execution/__init__.py +0 -0
  67. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/execution/context.py +0 -0
  68. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/execution/execution_system.py +0 -0
  69. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/execution/sinks.py +0 -0
  70. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/execution/strategy_base.py +0 -0
  71. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/metrics/__init__.py +0 -0
  72. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/metrics/gates.py +0 -0
  73. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/metrics/markouts.py +0 -0
  74. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/metrics/risk_adjusted.py +0 -0
  75. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/__init__.py +0 -0
  76. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/attribution.py +0 -0
  77. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/basic_portfolio.py +0 -0
  78. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/fair_value.py +0 -0
  79. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/hedging.py +0 -0
  80. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/lots.py +0 -0
  81. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/marks.py +0 -0
  82. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/portfolio_base.py +0 -0
  83. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/portfolio/valuation.py +0 -0
  84. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/recording/__init__.py +0 -0
  85. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/recording/events.py +0 -0
  86. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/recording/intervals.py +0 -0
  87. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/recording/recorder.py +0 -0
  88. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/__init__.py +0 -0
  89. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/builder.py +0 -0
  90. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/buying_power.py +0 -0
  91. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/cash_allocation.py +0 -0
  92. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/risk_manager.py +0 -0
  93. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_buying_power.py +0 -0
  94. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_confidence_sizer.py +0 -0
  95. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_correlation_filter.py +0 -0
  96. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_drawdown_killswitch.py +0 -0
  97. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_integer_quantity.py +0 -0
  98. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_kelly_sizer.py +0 -0
  99. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_long_only.py +0 -0
  100. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_max_open_positions.py +0 -0
  101. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_max_symbol_exposure.py +0 -0
  102. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_price_collar.py +0 -0
  103. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_regime_filter.py +0 -0
  104. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_signal_quantity_cap.py +0 -0
  105. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_symbol_capital_sizer.py +0 -0
  106. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/rules/rule_tick_rounding.py +0 -0
  107. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/sizing.py +0 -0
  108. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/risk/types.py +0 -0
  109. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/state/__init__.py +0 -0
  110. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/state/atomic.py +0 -0
  111. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/state/event_store.py +0 -0
  112. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/state/json_store.py +0 -0
  113. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/util/backtester_utils.py +0 -0
  114. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/util/config.py +0 -0
  115. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/util/deprecation.py +0 -0
  116. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/util/rounding.py +0 -0
  117. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant/util/visualisation.py +0 -0
  118. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant.egg-info/SOURCES.txt +0 -0
  119. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant.egg-info/dependency_links.txt +0 -0
  120. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant.egg-info/entry_points.txt +0 -0
  121. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant.egg-info/requires.txt +0 -0
  122. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/src/ghosalquant.egg-info/top_level.txt +0 -0
  123. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_backtester.py +0 -0
  124. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_blocks.py +0 -0
  125. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_cboe_datasource.py +0 -0
  126. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_config.py +0 -0
  127. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_contracts.py +0 -0
  128. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_depth.py +0 -0
  129. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_dolt_datasource.py +0 -0
  130. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_example_options_momentum.py +0 -0
  131. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_example_order_book.py +0 -0
  132. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_execution.py +0 -0
  133. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_execution_loop.py +0 -0
  134. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_execution_order_book.py +0 -0
  135. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_fair_value.py +0 -0
  136. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_ibkr_broker.py +0 -0
  137. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_ibkr_real_client.py +0 -0
  138. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_legacy_backtester.py +0 -0
  139. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_limit_orders.py +0 -0
  140. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_live_sources.py +0 -0
  141. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_markouts.py +0 -0
  142. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_metrics.py +0 -0
  143. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_nse_datasource.py +0 -0
  144. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_order_book_sim_broker.py +0 -0
  145. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_order_book_types.py +0 -0
  146. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_pre_trade_rules.py +0 -0
  147. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_public_api.py +0 -0
  148. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_replace_orders.py +0 -0
  149. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_risk_rules.py +0 -0
  150. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_shocks.py +0 -0
  151. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_state.py +0 -0
  152. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_valuation.py +0 -0
  153. {ghosalquant-0.2.0 → ghosalquant-0.2.1}/tests/test_volatility.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ghosalquant
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Event-driven quantitative trading research framework
5
5
  Author: Ghosal Quants
6
6
  License-Expression: MIT
@@ -16,7 +16,7 @@ from ghosalquant.core.types import (
16
16
  Trade,
17
17
  )
18
18
 
19
- __version__ = "0.2.0"
19
+ __version__ = "0.2.1"
20
20
 
21
21
  __all__ = [
22
22
  "OrderBookStrategy",
@@ -18,6 +18,8 @@ DEFAULT_ROTATE_SECONDS = 24 * 60 * 60
18
18
  DEFAULT_MIN_AGE_ROTATION_BYTES = 1024 * 1024
19
19
  DEFAULT_BATCH_ROWS = 4096
20
20
  SEGMENT_SCHEMA_VERSION = 1
21
+ # A segment's name starts with when its records start, in UTC; older names put `.ffffffZ` after the seconds.
22
+ SEGMENT_TIME_FORMAT = "%Y%m%dT%H%M%S"
21
23
 
22
24
  STORAGE_METADATA_KEY = b"ghosalquant_storage"
23
25
  STORAGE_METADATA_VALUE = b"segmented_recorder"
@@ -96,7 +98,7 @@ class SegmentedParquetWriter:
96
98
  if self.segments_dir.is_dir():
97
99
  self._recover_unsealed_logs()
98
100
  if self.path.is_file() and self.path.stat().st_size >= self.rotate_bytes:
99
- self._seal_legacy_path()
101
+ compact_jsonl_segment(self._move_log_into_segments())
100
102
  self._active_path = self._next_active_path()
101
103
  self._open_active()
102
104
  except Exception:
@@ -131,11 +133,7 @@ class SegmentedParquetWriter:
131
133
  return None
132
134
 
133
135
  self._file.close()
134
- raw_segment = self._active_path
135
- if raw_segment == self.path:
136
- self.segments_dir.mkdir(parents=True, exist_ok=True)
137
- raw_segment = self.segments_dir / f"segment-{_segment_id()}.jsonl"
138
- retry_while_locked(lambda: self.path.replace(raw_segment))
136
+ raw_segment = self._move_log_into_segments() if self._active_path == self.path else self._active_path
139
137
  try:
140
138
  return compact_jsonl_segment(raw_segment)
141
139
  finally:
@@ -184,11 +182,11 @@ class SegmentedParquetWriter:
184
182
  for raw_path in sorted(self.segments_dir.glob("segment-*.jsonl")):
185
183
  compact_jsonl_segment(raw_path)
186
184
 
187
- def _seal_legacy_path(self) -> None:
185
+ def _move_log_into_segments(self) -> Path:
188
186
  self.segments_dir.mkdir(parents=True, exist_ok=True)
189
- raw_segment = self.segments_dir / f"segment-{_segment_id()}.jsonl"
187
+ raw_segment = self.segments_dir / f"segment-{_segment_id(_first_record_time(self.path))}.jsonl"
190
188
  retry_while_locked(lambda: self.path.replace(raw_segment))
191
- compact_jsonl_segment(raw_segment)
189
+ return raw_segment
192
190
 
193
191
  def _acquire_lock(self) -> None:
194
192
  lock_path = self.path.with_suffix(".writer.lock")
@@ -239,24 +237,34 @@ def discover_logical_streams(root: str | Path) -> list[Path]:
239
237
 
240
238
 
241
239
  def stream_source_files(logical_path: str | Path) -> list[Path]:
242
- """The files backing a stream, sealed segments preferred over their unsealed logs."""
240
+ """
241
+ The files backing a stream in time order, sealed segments preferred over their unsealed logs.
242
+
243
+ A log beside the segments goes before the first one that starts after it.
244
+ """
243
245
  logical = Path(logical_path)
244
- paths: list[Path] = []
245
- if logical.is_file():
246
- paths.append(logical)
246
+ segments: list[Path] = []
247
247
  segments_dir = segments_dir_for(logical)
248
- if not segments_dir.is_dir():
249
- return paths
250
-
251
- by_stem: dict[str, dict[str, Path]] = {}
252
- for path in segments_dir.glob("segment-*"):
253
- if not path.is_file() or path.suffix not in {".jsonl", ".parquet"}:
254
- continue
255
- by_stem.setdefault(path.stem, {})[path.suffix] = path
256
- for stem in sorted(by_stem):
257
- variants = by_stem[stem]
258
- paths.append(variants.get(".parquet") or variants[".jsonl"])
259
- return paths
248
+ if segments_dir.is_dir():
249
+ by_stem: dict[str, dict[str, Path]] = {}
250
+ for path in segments_dir.glob("segment-*"):
251
+ if not path.is_file() or path.suffix not in {".jsonl", ".parquet"}:
252
+ continue
253
+ by_stem.setdefault(path.stem, {})[path.suffix] = path
254
+ segments = [by_stem[stem].get(".parquet") or by_stem[stem][".jsonl"] for stem in sorted(by_stem)]
255
+ if not logical.is_file():
256
+ return segments
257
+
258
+ started = _first_record_time(logical) if segments else None
259
+ if started is None:
260
+ return [logical, *segments]
261
+ before = len(segments)
262
+ for index, path in enumerate(segments):
263
+ segment_started = _segment_started_at(path)
264
+ if segment_started is not None and segment_started > started:
265
+ before = index
266
+ break
267
+ return [*segments[:before], logical, *segments[before:]]
260
268
 
261
269
 
262
270
  def iter_stream_records(logical_path: str | Path) -> Iterator[dict[str, Any]]:
@@ -606,5 +614,19 @@ def _unlock(handle) -> None:
606
614
  fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
607
615
 
608
616
 
609
- def _segment_id() -> str:
610
- return f"{datetime.now(timezone.utc):%Y%m%dT%H%M%S%f}-{uuid4().hex[:8]}"
617
+ def _segment_id(started: datetime | None = None) -> str:
618
+ at = (started or datetime.now(timezone.utc)).astimezone(timezone.utc)
619
+ return f"{at.strftime(SEGMENT_TIME_FORMAT)}{at:%f}-{uuid4().hex[:8]}"
620
+
621
+
622
+ def _segment_started_at(path: Path) -> datetime | None:
623
+ seconds = path.stem.removeprefix("segment-")[: len("YYYYmmddTHHMMSS")]
624
+ try:
625
+ return datetime.strptime(seconds, SEGMENT_TIME_FORMAT).replace(tzinfo=timezone.utc)
626
+ except ValueError:
627
+ return None
628
+
629
+
630
+ def _first_record_time(path: Path) -> datetime | None:
631
+ first = next(_iter_jsonl_records(path), None)
632
+ return None if first is None else record_time(first[1])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ghosalquant
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Event-driven quantitative trading research framework
5
5
  Author: Ghosal Quants
6
6
  License-Expression: MIT
@@ -109,6 +109,40 @@ def test_sealed_segment_is_preferred_over_a_stale_copy_of_its_log(tmp_path):
109
109
  assert stale not in storage.stream_source_files(path)
110
110
 
111
111
 
112
+ def test_a_log_beside_segments_is_read_in_time_order(tmp_path):
113
+ path = tmp_path / "stream.jsonl"
114
+ segments = storage.segments_dir_for(path)
115
+ segments.mkdir()
116
+ for minutes, index in ((0, 1), (5, 3)):
117
+ opened = T0 + timedelta(minutes=minutes)
118
+ (segments / f"segment-{opened:%Y%m%dT%H%M%S%f}-{index:08x}.jsonl").write_text(
119
+ json.dumps({"observed_at": opened.isoformat(), "index": index}) + "\n", encoding="utf-8"
120
+ )
121
+ path.write_text(
122
+ json.dumps({"observed_at": (T0 + timedelta(minutes=2)).isoformat(), "index": 2}) + "\n", encoding="utf-8"
123
+ )
124
+
125
+ assert [row["index"] for row in storage.iter_stream_records(path)] == [1, 2, 3]
126
+
127
+
128
+ def test_a_sealed_log_keeps_its_place_among_the_segments(tmp_path):
129
+ path = tmp_path / "stream.jsonl"
130
+ segments = storage.segments_dir_for(path)
131
+ segments.mkdir()
132
+ later = T0 + timedelta(minutes=5)
133
+ (segments / f"segment-{later:%Y%m%dT%H%M%S%f}-00000003.jsonl").write_text(
134
+ json.dumps({"observed_at": later.isoformat(), "index": 3}) + "\n", encoding="utf-8"
135
+ )
136
+ path.write_text(
137
+ json.dumps({"observed_at": (T0 + timedelta(minutes=2)).isoformat(), "index": 2}) + "\n", encoding="utf-8"
138
+ )
139
+
140
+ storage.SegmentedParquetWriter(path, rotate_bytes=1).close()
141
+
142
+ assert not path.exists()
143
+ assert [row["index"] for row in storage.iter_stream_records(path)] == [2, 3]
144
+
145
+
112
146
  def test_duplicated_records_are_not_counted_twice(tmp_path):
113
147
  path = tmp_path / "stream.jsonl"
114
148
  with storage.SegmentedParquetWriter(path) as writer:
File without changes
File without changes
File without changes
File without changes