stable/freqtrade/pairlist/PerformanceFilter.py

62 lines
2.1 KiB
Python
Raw Normal View History

"""
Performance pair list filter
"""
import logging
from typing import Any, Dict, List
import pandas as pd
from freqtrade.pairlist.IPairList import IPairList
from freqtrade.persistence import Trade
2020-11-28 07:59:30 +00:00
logger = logging.getLogger(__name__)
2020-11-28 07:34:18 +00:00
class PerformanceFilter(IPairList):
def __init__(self, exchange, pairlistmanager,
config: Dict[str, Any], pairlistconfig: Dict[str, Any],
pairlist_pos: int) -> None:
super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos)
@property
def needstickers(self) -> bool:
"""
Boolean property defining if tickers are necessary.
If no Pairlist requries tickers, an empty List is passed
as tickers argument to filter_pairlist
"""
return False
def short_desc(self) -> str:
"""
Short whitelist 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.
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
"""
# Get the trading performance for pairs from database
performance = pd.DataFrame(Trade.get_overall_performance())
# Skip performance-based sorting if no performance data is available
if len(performance) == 0:
return pairlist
2020-11-28 07:15:36 +00:00
# get pairlist from performance dataframe values
2020-11-28 07:34:18 +00:00
list_df = pd.DataFrame({'pair': pairlist})
# set initial value for pairs with no trades to 0
# and sort the list using performance and count
sorted_df = list_df.merge(performance, on='pair', how='left')\
.fillna(0).sort_values(by=['profit', 'count', 'pair'], ascending=False)
pairlist = sorted_df['pair'].tolist()
2020-11-28 07:34:18 +00:00
return pairlist