-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dev #511
Conversation
Reviewer's Guide by SourceryThis pull request introduces three new handler classes for different centralized exchange (CEX) clients: FXCM, Oanda, and Schwab. Each handler extends the CexClient base class and implements a common set of methods for interacting with the respective exchange. File-Level Changes
Tips
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @mraniki - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider removing commented-out code and implementing actual functionality, even if basic. If these are meant as placeholders, consider using abstract base classes or interfaces instead.
- Improve class-level documentation to explain the purpose of each handler and how they differ from each other. Also, consider adding more meaningful docstrings for methods.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 3 issues found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
@@ -0,0 +1,100 @@ | |||
# """ | |||
|
|||
# Fxcm client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider implementing a common base class for handlers to reduce code duplication.
The current implementation introduces unnecessary complexity through code duplication across multiple handler files. To reduce complexity and improve maintainability, consider refactoring these handlers to use a common base class. This approach will reduce duplication while still allowing for service-specific implementations. Here's a suggested structure:
from abc import ABC, abstractmethod
class BaseHandler(ABC):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@abstractmethod
async def get_quote(self, instrument):
pass
@abstractmethod
async def get_account_balance(self):
pass
@abstractmethod
async def get_account_position(self):
pass
@abstractmethod
async def get_trading_asset_balance(self):
pass
@abstractmethod
async def execute_order(self, order_params):
pass
class FxcmHandler(BaseHandler):
async def get_quote(self, instrument):
# FXCM-specific implementation
raise NotImplementedError("get_quote not implemented for FXCM")
# Implement other methods...
This structure ensures that:
- Common functionality is centralized in the base class.
- Service-specific implementations are clearly separated.
- Unimplemented methods raise NotImplementedError, making their status explicit.
Additionally, replace all instances of pass
or return 0
in unimplemented methods with raise NotImplementedError("Method not implemented")
. This clearly indicates the status of these methods and prevents silent failures or unexpected behavior.
By adopting this approach, you'll significantly reduce code duplication and improve the overall structure and maintainability of your codebase.
# from .client import CexClient | ||
|
||
|
||
# class OandaHandler(CexClient): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider implementing a base class for CEX clients and refactoring OandaHandler to inherit from it.
To reduce complexity and improve maintainability, consider the following refactoring:
- Create a base class for CEX clients:
from abc import ABC, abstractmethod
class BaseCexClient(ABC):
def __init__(self, **kwargs):
super().__init__()
# Common initialization code here
@abstractmethod
async def get_quote(self, instrument):
raise NotImplementedError("get_quote must be implemented")
@abstractmethod
async def get_account_balance(self):
raise NotImplementedError("get_account_balance must be implemented")
@abstractmethod
async def get_account_position(self):
raise NotImplementedError("get_account_position must be implemented")
@abstractmethod
async def get_trading_asset_balance(self):
raise NotImplementedError("get_trading_asset_balance must be implemented")
@abstractmethod
async def execute_order(self, order_params):
raise NotImplementedError("execute_order must be implemented")
- Refactor OandaHandler to inherit from BaseCexClient:
from .base_cex_client import BaseCexClient
class OandaHandler(BaseCexClient):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Oanda-specific initialization here
async def get_quote(self, instrument):
# Implement Oanda-specific logic here
pass
async def get_account_balance(self):
# Implement Oanda-specific logic here
pass
async def get_account_position(self):
# Implement Oanda-specific logic here
pass
async def get_trading_asset_balance(self):
# Implement Oanda-specific logic here
pass
async def execute_order(self, order_params):
# Implement Oanda-specific logic here
pass
This refactoring:
- Reduces code duplication across different CEX clients.
- Provides a clear contract for what methods each client should implement.
- Uses
NotImplementedError
in the base class to catch unimplemented methods early. - Allows for easy addition of new CEX clients in the future.
Implement the Oanda-specific logic in each method instead of using pass
or returning 0. This will make the code more robust and prevent silent failures.
@@ -0,0 +1,100 @@ | |||
# """ | |||
|
|||
# Schwab client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider implementing a common base class for all handlers to reduce code duplication and enforce a consistent interface.
While the current implementation isn't overly complex on its own, it contributes to overall system complexity through code repetition across multiple handler classes. To address this, consider the following improvements:
- Create a common base class for all handlers (e.g.,
BaseHandler
) to reduce code duplication. - Use
NotImplementedError
for unimplemented methods instead ofpass
orreturn 0
.
Here's an example of how you could start refactoring:
from abc import ABC, abstractmethod
class BaseHandler(ABC):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@abstractmethod
async def get_quote(self, instrument):
raise NotImplementedError("get_quote method must be implemented")
@abstractmethod
async def get_account_balance(self):
raise NotImplementedError("get_account_balance method must be implemented")
# ... other common methods ...
class SchwabHandler(BaseHandler):
async def get_quote(self, instrument):
# Implement Schwab-specific logic here
pass
async def get_account_balance(self):
# Implement Schwab-specific logic here
pass
# ... implement other methods ...
This approach:
- Ensures all handlers have a consistent interface
- Reduces code duplication
- Clearly indicates which methods need implementation
- Fails fast if an unimplemented method is called
Consider applying this pattern to your other handler classes (e.g., for FXCM and OANDA) to further reduce overall system complexity.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #511 +/- ##
==========================================
+ Coverage 69.40% 70.43% +1.02%
==========================================
Files 8 8
Lines 487 487
==========================================
+ Hits 338 343 +5
+ Misses 149 144 -5 ☔ View full report in Codecov by Sentry. |
Summary by Sourcery
Add new handlers for FXCM, Oanda, and Schwab, each providing methods for financial operations such as retrieving quotes, account balances, positions, and executing orders.
New Features: