Skip to content

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

Closed
ajwinkworth wants to merge 1 commit into
kernc:masterfrom
ajwinkworth:patch-1
Closed

BUG: trying to round none / string(tag) / Boolean (is_contingent)#995
ajwinkworth wants to merge 1 commit into
kernc:masterfrom
ajwinkworth:patch-1

Conversation

@ajwinkworth

@ajwinkworth ajwinkworth commented May 28, 2023

Copy link
Copy Markdown

BUG: Fix attempting to round none / string(tag) / Boolean (is_contingent) in backtesting.py:408

Optional float values(limit, stop, sl, tp) can be float | None. Testing for none to not pass it to round()
Boolean values (is_contingent) shouldnt be rounded.
Str values (tag) shouldn't be rounded.

See:
A fix for Comment

@ajwinkworth ajwinkworth changed the title Fix trying to round none / string(tag) / Boolean (is_contingent) BUG: trying to round none / string(tag) / Boolean (is_contingent) May 28, 2023
setattr(self, f'_{self.__class__.__qualname__}__{k}', v)
return self


Copy link
Copy Markdown
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.

('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
Copy Markdown
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.

@ajwinkworth ajwinkworth May 28, 2023

Copy link
Copy Markdown
Author

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
Copy Markdown
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
Copy Markdown
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.

@jfncs jfncs Apr 19, 2024

Copy link
Copy Markdown

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))

@kernc
kernc force-pushed the master branch 5 times, most recently from 428c361 to 0ce6cab Compare January 21, 2025 07:25
@kernc

kernc commented Jan 22, 2025

Copy link
Copy Markdown
Owner

Thanks and apologies. Already fixed in 8dd1e36.

@kernc kernc closed this Jan 22, 2025
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.

3 participants