Skip to content
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

BUG: trying to round none / string(tag) / Boolean (is_contingent) #995

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,18 +405,20 @@ def _replace(self, **kwargs):
setattr(self, f'_{self.__class__.__qualname__}__{k}', v)
return self


Copy link
Owner

Choose a reason for hiding this comment

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

PEP 8 prescribes one empty line between methods.

def __repr__(self):
return '<Order {}>'.format(', '.join(f'{param}={round(value, 5)}'
return '<Order {}>'.format(', '.join(f'{param}={value}'
for param, value in (
('size', self.__size),
('limit', self.__limit_price),
('stop', self.__stop_price),
('sl', self.__sl_price),
('tp', self.__tp_price),
('size', round(self.__size,5)),
('limit', round(self.__limit_price,5) if self.__limit_price is not None else None),
('stop', round(self.__stop_price,5) if self.__stop_price is not None else None),
('sl', round(self.__sl_price,5) if self.__sl_price is not None else None),
('tp', round(self.__tp_price,0) if self.__tp_price is not None else None),
Copy link
Owner

Choose a reason for hiding this comment

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

I'd prefer this round(value, 5) if value is not None else '' be done once in the comprehension value expression (as before).

Also don't see a problem with round(is_contingent, 5) since the boolean rounds to either 1 or 0.

Copy link
Author

@ajwinkworth ajwinkworth May 28, 2023

Choose a reason for hiding this comment

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

How would you prefer to deal with string tags? Take them out of the comprehension and then append them separately?

Copy link
Author

Choose a reason for hiding this comment

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

After some further investigation it could be:

    def __repr__(self):
        return '<Order {}>'.format(', '.join(f'{param}={round(value, 5) if isinstance(value, SupportsRound) else value}'
                                             for param, value in (
                                                 ('size', self.__size),
                                                 ('limit', self.__limit_price),
                                                 ('stop', self.__stop_price),
                                                 ('sl', self.__sl_price),
                                                 ('tp', self.__tp_price),
                                                 ('contingent', self.is_contingent),
                                                 ('tag', self.__tag),
                                             ) if value is not None))

Which I haven't tested yet. Would just need an additional import from typing for SupportsRound.

Copy link
Owner

Choose a reason for hiding this comment

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

Something like that. Can also use isinstance(value, Number) like we do in some other places.

Copy link

@jaysonfrancis jaysonfrancis Apr 19, 2024

Choose a reason for hiding this comment

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

  def __repr__(self):
      return '<Order {}>'.format(', '.join(f'{param}={round(value, 5) if isinstance(value, float) else value}'
                                           for param, value in (
                                               ('size', self.__size),
                                               ('limit', self.__limit_price),
                                               ('stop', self.__stop_price),
                                               ('sl', self.__sl_price),
                                               ('tp', self.__tp_price),
                                               ('contingent', self.is_contingent),
                                               ('tag', self.__tag),
                                           ) if value is not None))

('contingent', self.is_contingent),
('tag', self.__tag),
) if value is not None))


def cancel(self):
"""Cancel the order."""
self.__broker.orders.remove(self)
Expand Down