fix(models): resolve 'deal_expire_at' error by adding 30s dead time protection#1
fix(models): resolve 'deal_expire_at' error by adding 30s dead time protection#1D1ther wants to merge 2 commits intoChipaDevTeam:mainfrom
Conversation
…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.
|
Warning Rate limit exceeded
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 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. 📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
|
||
| if (expire_at - now_seconds) < 30: | ||
| expire_at += self.duration_seconds |
There was a problem hiding this comment.
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>
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