use fetch_trades' public trades to populate dataframe

This commit is contained in:
Joe Schr
2023-04-26 15:14:45 +02:00
parent b88a561eb5
commit 0f4e147035
6 changed files with 953 additions and 94 deletions

View File

@@ -12,8 +12,9 @@ from typing import Any, Dict, List, Optional, Tuple
from pandas import DataFrame, Timedelta, Timestamp, to_timedelta
from freqtrade.configuration import TimeRange
from freqtrade.data.history.idatahandler import get_datahandler
from freqtrade.constants import (FULL_DATAFRAME_THRESHOLD, Config, ListPairsWithTimeframes,
PairWithTimeframe)
PairWithTimeframe, ListTicksWithTimeframes)
from freqtrade.data.history import load_pair_history
from freqtrade.enums import CandleType, RPCMessageType, RunMode
from freqtrade.exceptions import ExchangeError, OperationalException
@@ -24,6 +25,7 @@ from freqtrade.rpc import RPCManager
from freqtrade.rpc.rpc_types import RPCAnalyzedDFMsg
from freqtrade.util import PeriodicCache
from freqtrade.data.converter import public_trades_to_dataframe
logger = logging.getLogger(__name__)
@@ -442,7 +444,12 @@ class DataProvider:
if self._exchange is None:
raise OperationalException(NO_EXCHANGE_EXCEPTION)
final_pairs = (pairlist + helping_pairs) if helping_pairs else pairlist
# refresh latest ohlcv data
self._exchange.refresh_latest_ohlcv(final_pairs)
# refresh latest trades data (if enabled)
self._exchange.refresh_latest_trades(final_pairs,
get_datahandler(self._config['datadir'],
data_format=self._config['dataformat_trades']))
@property
def available_pairs(self) -> ListPairsWithTimeframes:
@@ -482,6 +489,44 @@ class DataProvider:
else:
return DataFrame()
def trades(
self,
pair: str,
timeframe: Optional[str] = None,
copy: bool = True,
candle_type: str = ''
) -> DataFrame:
"""
Get candle (TRADES) data for the given pair as DataFrame
Please use the `available_pairs` method to verify which pairs are currently cached.
:param pair: pair to get the data for
:param timeframe: Timeframe to get data for
:param candle_type: '', mark, index, premiumIndex, or funding_rate
:param copy: copy dataframe before returning if True.
Use False only for read-only operations (where the dataframe is not modified)
"""
if self._exchange is None:
raise OperationalException(NO_EXCHANGE_EXCEPTION)
if self.runmode in (RunMode.DRY_RUN, RunMode.LIVE):
_candle_type = CandleType.from_string(
candle_type) if candle_type != '' else self._config['candle_type_def']
return self._exchange.trades(
(pair, timeframe or self._config['timeframe'], _candle_type),
copy=copy
)
elif self.runmode in (RunMode.BACKTEST, RunMode.HYPEROPT):
_candle_type = CandleType.from_string(
candle_type) if candle_type != '' else self._config['candle_type_def']
data_handler = get_datahandler(
self._config['datadir'], data_format=self._config['dataformat_trades'])
ticks = data_handler.trades_load(pair)
trades_df = public_trades_to_dataframe(ticks, timeframe, pair=pair, fill_missing=False,
drop_incomplete=False)
return trades_df
else:
return DataFrame()
def market(self, pair: str) -> Optional[Dict[str, Any]]:
"""
Return market data for the pair