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

Is price near fix to cover all price ranges #415

Merged
merged 3 commits into from
Mar 16, 2024

Conversation

movy
Copy link
Contributor

@movy movy commented Nov 23, 2023

The current is_price_near implementation still gives false positives for mid-range priced assets, e.g. ~0.0250, which once again leads to totally unrealistic backtests:

image

I have updated the method to be more universal + added test cases.

The following code explains the logic behind my formula:

def is_price_near_old(order_price, price_to_compare, threshold_ratio=0.0001):
    """Check if the given order price is near the specified price."""
    percentage_threshold = threshold_ratio * price_to_compare
    # Enforcing a minimum threshold for smaller numbers
    fixed_threshold = 0.001
    threshold = max(percentage_threshold, fixed_threshold)
    return abs(order_price - price_to_compare) < threshold


def is_price_near(order_price, price_to_compare, percentage_threshold=0.0001):
    """
    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.
    """
    return round(abs(1 - (order_price / price_to_compare)), 4) <= percentage_threshold

for prices in [[60000, 60000], [30000, 30005], [30000, 29800], [200, 200.01], [10000, 10001], [0.0250, 0.0249], [0.007386, 0.007385]]: 
    order_price = prices[0]
    price_to_compare = prices[1]
    print(order_price, 'vs', price_to_compare, round(abs(1 - (order_price / price_to_compare)) * 100, 4), '% difference')
    print('is_price_near_old:', is_price_near_old(order_price, price_to_compare))
    print('but is it really within 0.01%?', is_price_near(order_price, price_to_compare))
    print('-----')

Output:

backtest:~/jesse-bot git:(main) ✗ python price_near_test.py
60000 vs 60000 0.0 % difference
is_price_near_old: True
but is it really within 0.01%? True
-----
30000 vs 30005 0.0167 % difference
is_price_near_old: False
but is it really within 0.01%? False
-----
30000 vs 29800 0.6711 % difference
is_price_near_old: False
but is it really within 0.01%? False
-----
200 vs 200.01 0.005 % difference
is_price_near_old: True
but is it really within 0.01%? True
-----
10000 vs 10001 0.01 % difference
is_price_near_old: True
but is it really within 0.01%? True
-----
0.025 vs 0.0249 0.4016 % difference
is_price_near_old: True  <---- current fn gives true
but is it really within 0.01%? False  <----- but should be false
-----
0.007386 vs 0.007385 0.0135 % difference
is_price_near_old: True
but is it really within 0.01%? True

Backtest with the updated method:

image

Copy link

stale bot commented Jan 22, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale No recent activity. label Jan 22, 2024
@stale stale bot closed this Jan 30, 2024
@saleh-mir saleh-mir reopened this Jan 30, 2024
@saleh-mir
Copy link
Member

I have modified this function so many times to improve it. It seems like it's not perfect yet. I'm going to try yours with my unit tests. So I'm keeping this open. I'm sorry about the delay.

@stale stale bot closed this Feb 6, 2024
@saleh-mir saleh-mir reopened this Feb 6, 2024
@stale stale bot removed the stale No recent activity. label Feb 6, 2024
@saleh-mir saleh-mir merged commit 3dba632 into jesse-ai:master Mar 16, 2024
3 checks passed
@saleh-mir
Copy link
Member

So I digged into your code and compared it to the existing one. It is certainly better. However, no matter how much I tried I couldn't get it to pass for this assertion too:

# for meme coins such as PEPE
assert jh.is_price_near(0.00000709, 0.00000708) == True

Any ideas?

@saleh-mir
Copy link
Member

Or maybe we shouldn't expect this value to be true at all?

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.

2 participants