Skip to content

fix(models): resolve 'deal_expire_at' error by adding 30s dead time protection#1

Open
D1ther wants to merge 2 commits intoChipaDevTeam:mainfrom
D1ther:fix/dead-time-protection
Open

fix(models): resolve 'deal_expire_at' error by adding 30s dead time protection#1
D1ther wants to merge 2 commits intoChipaDevTeam:mainfrom
D1ther:fix/dead-time-protection

Conversation

@D1ther
Copy link
Copy Markdown

@D1ther D1ther commented Apr 21, 2026

Fixes an issue where turbo options trades were systematically rejected by the server if placed within the last 30 seconds of the current candle duration.

Problem: The 'to_payload' method in 'TradeOrder' was calculating 'expire_at' by simply shifting the time to the nearest next candle boundary. However, the Binomo platform enforces a 30-second 'dead time' (a freeze period) before expiration. If the trade request was sent when (expire_at - now) < 30, the WebSocket server would return a 'deal_expire_at' validation error and reject the trade.

Solution: Added a dead time protection check. If the calculated 'expire_at' is less than 30 seconds away from the current time, we add another full 'duration_seconds' period to safely shift the expiration to the end of the subsequent candle boundary. This ensures compliance with the broker's minimum expiration limits constraint.

Summary by CodeRabbit

  • Bug Fixes
    • Improved trade order expiry time calculation to prevent orders from expiring too close to the current time, ensuring more reliable order execution timing.

…rotection

Fixes an issue where turbo options trades were systematically rejected by the server if placed within the last 30 seconds of the current candle duration.

Problem: The 'to_payload' method in 'TradeOrder' was calculating 'expire_at' by simply shifting the time to the nearest next candle boundary. However, the Binomo platform enforces a 30-second 'dead time' (a freeze period) before expiration. If the trade request was sent when (expire_at - now) < 30, the WebSocket server would return a 'deal_expire_at' validation error and reject the trade.

Solution: Added a dead time protection check. If the calculated 'expire_at' is less than 30 seconds away from the current time, we add another full 'duration_seconds' period to safely shift the expiration to the end of the subsequent candle boundary. This ensures compliance with the broker's minimum expiration limits constraint.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 21, 2026

Warning

Rate limit exceeded

@D1ther has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 47 minutes and 16 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 47 minutes and 16 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 07346bb3-909a-4d93-bb20-2fd4d4bbaf73

📥 Commits

Reviewing files that changed from the base of the PR and between f27592b and 3a9fca5.

📒 Files selected for processing (1)
  • BinomoAPI/models.py
📝 Walkthrough

Walkthrough

The TradeOrder.to_payload() method in BinomoAPI/models.py was enhanced to handle near-boundary expiry timing. After aligning expire_at to the next candle duration boundary, the code now checks if the result falls within 30 seconds of the current time and, if so, adds an additional duration period to ensure sufficient expiry buffer.

Changes

Cohort / File(s) Summary
Trade Order Expiry Logic
BinomoAPI/models.py
Enhanced expire_at computation in to_payload() to add an additional duration period when the aligned expiry time is less than 30 seconds from current time, preventing premature expirations at candle boundaries.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A boundary so tight, near thirty seconds hence,
Our expiry aligned, but lacking good sense,
So we add one more tick to make room for the trade,
At the candle's edge, a buffer's been made.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding 30-second dead time protection to resolve deal_expire_at validation errors in the TradeOrder.to_payload() method.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies the expiration time calculation in BinomoAPI/models.py to ensure a minimum 30-second buffer before the next candle boundary. Feedback suggests using the float representation of the current time to prevent precision errors at the boundary and implementing a while loop instead of an if statement to robustly handle durations shorter than 30 seconds.

Comment thread BinomoAPI/models.py Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@BinomoAPI/models.py`:
- Around line 76-78: Replace the single if-adjustment with a loop that
repeatedly adds self.duration_seconds to expire_at until expire_at - now_seconds
>= FREEZE_PERIOD_SECONDS, and introduce a module-level constant (e.g.,
FREEZE_PERIOD_SECONDS = 30) to replace the magic 30; also consider adding
validation in _place_option to ensure duration_seconds >= 1 (or a sensible lower
bound) so callers cannot pass extremely small durations that require many
iterations. Ensure you update the check that currently reads (expire_at -
now_seconds) < 30 to use FREEZE_PERIOD_SECONDS and a while loop so the
adjustment is correct for any self.duration_seconds.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3ed250a7-63bf-4ac3-8e83-8e502f5f3209

📥 Commits

Reviewing files that changed from the base of the PR and between f66d7f7 and f27592b.

📒 Files selected for processing (1)
  • BinomoAPI/models.py

Comment thread BinomoAPI/models.py
Comment on lines +76 to +78

if (expire_at - now_seconds) < 30:
expire_at += self.duration_seconds
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Dead-time check may be insufficient for small duration_seconds; use a loop and a named constant.

The single if only guarantees expire_at - now_seconds >= 30 when duration_seconds >= 30. _place_option (BinomoAPI/api.py:843) only validates duration_seconds > 0, so a caller passing e.g. duration_seconds=15 can still end up with expire_at - now_seconds in the dead-time window after a single adjustment (worst case ~2·duration < 30), causing the same deal_expire_at rejection this PR intends to fix.

Also, the 30 literal is the broker's freeze period and appears unexplained; promoting it to a module-level constant (and optionally validating a minimum duration_seconds upstream) makes intent explicit and keeps the logic robust.

🛠️ Suggested refactor
+# Broker's pre-expiry freeze window ("dead time"): trades whose expiry falls
+# within this many seconds of 'now' are rejected with 'deal_expire_at'.
+DEAD_TIME_SECONDS = 30
+
@@
-        # expire_at must be aligned to the next candle boundary (in seconds)
-        now_seconds = int(now)
-        expire_at = now_seconds - (now_seconds % self.duration_seconds) + self.duration_seconds
-
-        if (expire_at - now_seconds) < 30:
-            expire_at += self.duration_seconds
+        # expire_at must be aligned to the next candle boundary (in seconds)
+        now_seconds = int(now)
+        expire_at = now_seconds - (now_seconds % self.duration_seconds) + self.duration_seconds
+
+        # Push past the broker's dead-time window; loop in case duration < DEAD_TIME_SECONDS.
+        while (expire_at - now_seconds) < DEAD_TIME_SECONDS:
+            expire_at += self.duration_seconds
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@BinomoAPI/models.py` around lines 76 - 78, Replace the single if-adjustment
with a loop that repeatedly adds self.duration_seconds to expire_at until
expire_at - now_seconds >= FREEZE_PERIOD_SECONDS, and introduce a module-level
constant (e.g., FREEZE_PERIOD_SECONDS = 30) to replace the magic 30; also
consider adding validation in _place_option to ensure duration_seconds >= 1 (or
a sensible lower bound) so callers cannot pass extremely small durations that
require many iterations. Ensure you update the check that currently reads
(expire_at - now_seconds) < 30 to use FREEZE_PERIOD_SECONDS and a while loop so
the adjustment is correct for any self.duration_seconds.

Fix:
changed validation to ensure deal time is calculated accurately to avoid issues

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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.

1 participant