Sort by profit after sort by count/pair

This commit is contained in:
Leif Segen 2020-11-28 12:17:03 -06:00
parent 6a74c57c3d
commit 323c0657f8
2 changed files with 25 additions and 13 deletions

View File

@ -31,17 +31,17 @@ class PerformanceFilter(IPairList):
def short_desc(self) -> str:
"""
Short whitelist method description - used for startup-messages
Short allowlist method description - used for startup-messages
"""
return f"{self.name} - Sorting pairs by performance."
def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]:
"""
Filters and sorts pairlist and returns the whitelist again.
Filters and sorts pairlist and returns the allowlist again.
Called on each bot iteration - please use internal caching if necessary
:param pairlist: pairlist to filter or sort
:param tickers: Tickers (from exchange.get_tickers()). May be cached.
:return: new whitelist
:return: new allowlist
"""
# Get the trading performance for pairs from database
performance = pd.DataFrame(Trade.get_overall_performance())
@ -50,12 +50,17 @@ class PerformanceFilter(IPairList):
if len(performance) == 0:
return pairlist
# get pairlist from performance dataframe values
# Get pairlist from performance dataframe values
list_df = pd.DataFrame({'pair': pairlist})
# set initial value for pairs with no trades to 0
# and sort the list using performance and count
# Set initial value for pairs with no trades to 0
# Sort the list using:
# - primarily performance (high to low)
# - then count (low to high, so as to favor same performance with fewer trades)
# - then pair name alphametically
sorted_df = list_df.merge(performance, on='pair', how='left')\
.fillna(0).sort_values(by=['profit', 'count', 'pair'], ascending=False)
.fillna(0).sort_values(by=['count', 'pair'], ascending=True)\
.sort_values(by=['profit'], ascending=False)
pairlist = sorted_df['pair'].tolist()
return pairlist

View File

@ -748,13 +748,20 @@ def test_pairlistmanager_no_pairlist(mocker, whitelist_conf):
[{'pair': 'ETH/BTC', 'profit': -5, 'count': 100},
{'pair': 'TKN/BTC', 'profit': 4, 'count': 2}],
['TKN/BTC', 'LTC/BTC', 'ETH/BTC']),
# Tie in performance data broken by count
# Tie in performance data broken by count (ascending)
([{"method": "StaticPairList"}, {"method": "PerformanceFilter"}],
['ETH/BTC', 'TKN/BTC', 'LTC/BTC'],
[{'pair': 'LTC/BTC', 'profit': -5, 'count': 101},
{'pair': 'TKN/BTC', 'profit': -5, 'count': 2},
{'pair': 'ETH/BTC', 'profit': -5, 'count': 100}],
['LTC/BTC', 'ETH/BTC', 'TKN/BTC']),
[{'pair': 'LTC/BTC', 'profit': -5.01, 'count': 101},
{'pair': 'TKN/BTC', 'profit': -5.01, 'count': 2},
{'pair': 'ETH/BTC', 'profit': -5.01, 'count': 100}],
['TKN/BTC', 'ETH/BTC', 'LTC/BTC']),
# Tie in performance and count, broken by alphabetical sort
([{"method": "StaticPairList"}, {"method": "PerformanceFilter"}],
['ETH/BTC', 'TKN/BTC', 'LTC/BTC'],
[{'pair': 'LTC/BTC', 'profit': -5.01, 'count': 1},
{'pair': 'TKN/BTC', 'profit': -5.01, 'count': 1},
{'pair': 'ETH/BTC', 'profit': -5.01, 'count': 1}],
['ETH/BTC', 'LTC/BTC', 'TKN/BTC']),
])
def test_performance_filter(mocker, whitelist_conf, pairlists, pair_allowlist, overall_performance,
allowlist_result, tickers, markets, ohlcv_history_list):