Skip to content

Commit

Permalink
implement and use the round_or_none() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
saleh-mir committed Jun 25, 2022
1 parent 2b9cc97 commit 2664061
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion jesse/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import string
import sys
import uuid
from typing import List, Tuple, Union, Any
from typing import List, Tuple, Union, Any, Optional
from pprint import pprint
import arrow
import click
Expand Down Expand Up @@ -595,6 +595,15 @@ def relative_to_absolute(path: str) -> str:
return os.path.abspath(path)


def round_or_none(x: Union[float, None], digits: int = 0) -> Optional[float]:
"""
Rounds a number to a certain number of digits or returns None if the number is None
"""
if x is None:
return None
return round(x, digits)


def round_price_for_live_mode(price, precision: int) -> Union[float, np.ndarray]:
"""
Rounds price(s) based on exchange requirements
Expand Down
2 changes: 1 addition & 1 deletion jesse/services/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def positions() -> list:
'opened_at': p.opened_at,
'qty': p.qty,
'value': round(p.value, 2),
'entry': p.entry_price,
'entry': jh.round_or_none(p.entry_price, 2),
'current_price': p.current_price,
'liquidation_price': p.liquidation_price,
'pnl': p.pnl,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,9 @@ class TestClass:

# if string is passed, it will return the string
assert jh.get_class_name('TestClass') == 'TestClass'


def test_round_or_none():
assert jh.round_or_none(1.23) == 1
assert jh.round_or_none(1.23456789, 2) == 1.23
assert jh.round_or_none(None) is None

0 comments on commit 2664061

Please sign in to comment.