From f39e6112e48460d6c314e9bd39e07de2bf1aecd2 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 15 Apr 2022 14:58:18 +0100 Subject: [PATCH] Handle LBTC in autoprice --- handler/markets.py | 32 ++++++++++++++++++-------------- handler/tests/test_markets.py | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/handler/markets.py b/handler/markets.py index b9106c7..fb5e6be 100644 --- a/handler/markets.py +++ b/handler/markets.py @@ -42,8 +42,12 @@ class Markets(util.Base): """ if platform == "agora": username = settings.Agora.Username + min_margin = settings.Agora.MinMargin + max_margin = settings.Agora.MaxMargin elif platform == "lbtc": username = settings.LocalBitcoins.Username + min_margin = settings.LocalBitcoins.MinMargin + max_margin = settings.LocalBitcoins.MaxMargin to_update = [] # NOTES: @@ -78,7 +82,7 @@ class Markets(util.Base): our_ads = [ad for ad in public_ads_filtered if ad[1] == username] if not our_ads: continue - new_margin = self.autoprice(public_ads_filtered, currency) + new_margin = self.autoprice(username, min_margin, max_margin, public_ads_filtered, currency) # self.log.info("New rate for {currency}: {rate}", currency=currency, rate=new_margin) new_formula = f"coingecko{asset.lower()}usd*usd{currency.lower()}*{new_margin}" for ad in our_ads: @@ -90,7 +94,7 @@ class Markets(util.Base): return to_update - def autoprice(self, ads, currency): + def autoprice(self, username, min_margin, max_margin, ads, currency): """ Helper function to automatically adjust the price up/down in certain markets in order to gain the most profits and sales. @@ -109,10 +113,10 @@ class Markets(util.Base): # Find second cheapest that is not us # Remove results from ads that are us - ads_without_us = [ad for ad in ads if not ad[1] == settings.Agora.Username] + ads_without_us = [ad for ad in ads if not ad[1] == username] # self.log.debug("Ads without us: {x}", x=ads_without_us) # Find ads above our min that are not us - ads_above_our_min_not_us = [ad for ad in ads_without_us if ad[6] > float(settings.Agora.MinMargin)] + ads_above_our_min_not_us = [ad for ad in ads_without_us if ad[6] > float(min_margin)] # self.log.debug("Ads above our min not us: {x}", x=ads_above_our_min_not_us) # Check that this list without us is not empty if ads_without_us: @@ -124,26 +128,26 @@ class Markets(util.Base): # self.log.debug("Lowball lowest not ours: {x}", x=lowball_lowest_not_ours) # Check if the username field of the cheapest ad matches ours - if min_margin_ad[1] == settings.Agora.Username: + if min_margin_ad[1] == username: # self.log.debug("We are the cheapest for: {x}", x=currency) # We are the cheapest! # Are all of the ads ours? - all_ads_ours = all([ad[1] == settings.Agora.Username for ad in ads]) + all_ads_ours = all([ad[1] == username for ad in ads]) if all_ads_ours: # self.log.debug("All ads are ours for: {x}", x=currency) # Now we know it's safe to return the maximum value - return float(settings.Agora.MaxMargin) + return float(max_margin) else: # self.log.debug("All ads are NOT ours for: {x}", x=currency) # All the ads are not ours, but we are first... # Check if the lowballed, lowest (that is not ours) ad's margin # is less than our minimum - if lowball_lowest_not_ours < float(settings.Agora.MinMargin): + if lowball_lowest_not_ours < float(min_margin): # self.log.debug("Lowball lowest not ours less than MinMargin") - return float(settings.Agora.MinMargin) - elif lowball_lowest_not_ours > float(settings.Agora.MaxMargin): + return float(min_margin) + elif lowball_lowest_not_ours > float(max_margin): # self.log.debug("Lowball lowest not ours more than MaxMargin") - return float(settings.Agora.MaxMargin) + return float(max_margin) else: # self.log.debug("Returning lowballed figure: {x}", x=lowball_lowest_not_ours) return lowball_lowest_not_ours @@ -153,13 +157,13 @@ class Markets(util.Base): # Check if this list is empty if not ads_above_our_min_not_us: # Return the maximum margin? - return float(settings.Agora.MaxMargin) + return float(max_margin) # Find cheapest ad above our min that is not us cheapest_ad = min(ads_above_our_min_not_us, key=lambda x: x[4]) cheapest_ad_margin = cheapest_ad[6] # - 0.005 - if cheapest_ad_margin > float(settings.Agora.MaxMargin): + if cheapest_ad_margin > float(max_margin): # self.log.debug("Cheapest ad not ours more than MaxMargin") - return float(settings.Agora.MaxMargin) + return float(max_margin) # self.log.debug("Cheapest ad above our min that is not us: {x}", x=cheapest_ad) return cheapest_ad_margin diff --git a/handler/tests/test_markets.py b/handler/tests/test_markets.py index 0fa616b..afc481c 100644 --- a/handler/tests/test_markets.py +++ b/handler/tests/test_markets.py @@ -59,7 +59,7 @@ class TestMarkets(TestCase): ], ] currency = "EUR" - margin = self.markets.autoprice(ads, currency) + margin = self.markets.autoprice("topmonero", 1.1, 1.3, ads, currency) expected_margin = 1.18 self.assertEqual(margin, expected_margin)