Skip to content

Commit

Permalink
Merge pull request #415 from movy/is_price_near-fix
Browse files Browse the repository at this point in the history
Is price near fix to cover all price ranges
  • Loading branch information
saleh-mir authored Mar 16, 2024
2 parents 99baf9b + 9b1da3a commit 3dba632
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
21 changes: 6 additions & 15 deletions jesse/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,21 +1034,12 @@ def get_candle_start_timestamp_based_on_timeframe(timeframe: str, num_candles_to
return finish_date - (num_candles_to_fetch * one_min_count * 60_000)


def is_price_near(order_price, price_to_compare):
def is_price_near(order_price, price_to_compare, percentage_threshold=0.0001):
"""
Check if given order price is near the specified price.
This function checks a range of price values and applies the associated threshold.
:param order_price: float
:param price_to_compare: float
:return: bool
Check if the given order price is near the specified price.
Default percentage_threshold is 0.01% (0.0001)
We calculate percentage difference between the two prices rounded to 4 decimal places,
so low-priced orders can be properly compared within 0.01% range.
"""
# Define the price ranges and associated thresholds
conditions = [order_price < 0.01, order_price < 1, order_price < 100, order_price < 10000, order_price >= 10000]
threshold_ratios = [0.015, 0.01, 0.005, 0.001, 0.0001]

# Use np.select to choose the threshold ratio based on the conditions
threshold_ratio = np.select(conditions, threshold_ratios)
return round(abs(1 - (order_price / price_to_compare)), 4) <= percentage_threshold

threshold = threshold_ratio * order_price
return abs(order_price - price_to_compare) <= threshold
3 changes: 2 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,8 @@ def test_round_or_none():

def test_is_price_near():
assert jh.is_price_near(0.007386, 0.007385) == True
assert jh.is_price_near(0.007386, 0.007396) == True
assert jh.is_price_near(0.007386, 0.007396) == False
assert jh.is_price_near(0.0250, 0.0249) == False
assert jh.is_price_near(60000, 60000) == True
assert jh.is_price_near(60000, 60000.1) == True
assert jh.is_price_near(60000, 60100) == False
Expand Down

0 comments on commit 3dba632

Please sign in to comment.