You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR modifies the parse_order_size method to round forex lot sizes to 0.01 multiples to comply with standard broker requirements. The changes include refactoring the order calculation logic and adding a use_floor parameter for rounding control.
✅ Strengths
Clear intent: The PR addresses a real-world requirement for forex trading lot size precision
Minimal scope: Changes are focused and don't introduce unnecessary modifications
Backward compatibility: The use_floor parameter defaults to False, maintaining existing behavior for non-resized orders
⚠️ Concerns
Critical Issues
Logic error - Quantity field handling removed (Lines 394-397)
The original code handled three input scenarios: quantity, leverage, or value
The refactored code completely removes the if quantity is not None: branch
If a signal comes in with only quantity set, it will now be None throughout the calculation, resulting in incorrect results
Impact: This is a breaking change that could cause silent calculation errors
Floating point precision issues (Line 400)
quantity=math.trunc(quantity/0.01) *0.01
Direct floating point division/multiplication by 0.01 can introduce precision errors
Recommendation: Use integer arithmetic or Decimal for financial calculations
Missing validation (Line 400)
No check for quantity being None before the forex rounding logic
If signal["quantity"] is the only field set, quantity would be None at line 400, causing a TypeError
Design Issues
Inconsistent rounding behavior (Line 293)
use_floor=True is only applied when orders are resized due to limits
Normal orders use regular rounding, resized orders use floor rounding
This inconsistency could lead to unexpected behavior
Question: What's the business logic reasoning for this difference?
Missing test coverage
No test files included in the PR
Financial calculations require comprehensive unit tests
Edge cases like minimum lot sizes, very small quantities, and boundary conditions should be tested
💡 Suggestions
High Priority
Fix the quantity input handling:
ifquantityisnotNone:
# Keep original quantity, just apply forex rounding if neededpasselifleverageisnotNone:
quantity= (leverage*portfolio_value*usd_base_conversion) /trade_pair.lot_sizeelifvalueisnotNone:
quantity= (value*usd_base_conversion) /trade_pair.lot_size# Apply forex roundingiftrade_pair.is_forex:
quantity=math.trunc(quantity/0.01) *0.01ifuse_floorelseround(quantity/0.01) *0.01
Use integer arithmetic for precision:
iftrade_pair.is_forex:
# Convert to integer cents, round, then back to decimalquantity_cents=int(quantity*100)
ifuse_floor:
quantity= (quantity_cents//1) /100else:
quantity=round(quantity_cents) /100
Or better yet, use Python's Decimal module for financial calculations.
Add validation:
iftrade_pair.is_forexandquantityisnotNone:
# Rounding logic here
Medium Priority
Clarify the use_floor parameter:
Add documentation explaining when and why floor vs round should be used
Consider renaming to something more descriptive like force_downsize or use_conservative_rounding
Add comprehensive tests:
deftest_parse_order_size_forex_rounding():
# Test with quantity input# Test with leverage input# Test with value input# Test floor vs round behavior# Test edge cases (0.001, 0.005, 0.009, etc.)
Consider lot size minimums:
What happens if rounding results in 0.0 quantity?
Should there be a minimum order size check?
Low Priority
Update docstring (Line 382):
"""Parses an order signal and calculates leverage, value, and quantity.Args: signal: Dict with one of 'leverage', 'value', or 'quantity' usd_base_conversion: Price conversion to USD trade_pair: TradePair object with lot_size and is_forex portfolio_value: Total portfolio value use_floor: If True, floors forex quantities; if False, rounds (default: False)Returns: Tuple of (quantity, leverage, value)"""
Consider extracting forex rounding logic:
@staticmethoddefround_forex_quantity(quantity: float, use_floor: bool=False) ->float:
"""Rounds quantity to 0.01 precision for forex compliance."""returnmath.trunc(quantity/0.01) *0.01ifuse_floorelseround(quantity/0.01) *0.01
🔒 Security Notes
Financial calculation accuracy: The floating point precision issue could result in incorrect trade sizes, potentially leading to financial loss
Input validation: Ensure trade_pair.is_forex is properly validated and cannot be manipulated
No direct security vulnerabilities identified, but financial correctness is critical
🎯 Verdict
❌ NEEDS REVISION - The removal of the quantity input handling is a critical bug that must be fixed before merging. Additionally, floating point precision issues should be addressed for financial calculations.
Recommended Actions:
Restore the quantity input handling logic
Add comprehensive unit tests
Consider using Decimal for financial precision
Clarify the use_floor parameter usage and document it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
round forex lot sizes to 0.01 multiples to match standard brokers