Skip to content

Commit

Permalink
Merge pull request #207 from macanudo527/fix_pricing_bug
Browse files Browse the repository at this point in the history
Fix Pricing Bug when Using Default Exchange
  • Loading branch information
eprbell committed Nov 4, 2023
2 parents 8d07b7c + 69d339a commit 18d0316
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/dali/plugin/pair_converter/ccxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
binance,
binanceus,
bitfinex,
coinbasepro,
gateio,
huobi,
kraken,
Expand Down Expand Up @@ -99,7 +98,6 @@
_BINANCE: binance,
_BINANCEUS: binanceus,
_BITFINEX: bitfinex,
_COINBASE_PRO: coinbasepro,
_GATE: gateio,
_HUOBI: huobi,
_KRAKEN: kraken,
Expand Down Expand Up @@ -203,6 +201,11 @@ class AssetPairAndHistoricalPrice(NamedTuple):
historical_data: Optional[HistoricalBar] = None


class ExchangeNameAndClass(NamedTuple):
name: str
klass: Any


class PairConverterPlugin(AbstractPairConverterPlugin):
def __init__(
self,
Expand All @@ -227,7 +230,12 @@ def __init__(
self.__google_api_key: Optional[str] = google_api_key
self.__exchange_locked: bool = exchange_locked if exchange_locked is not None else False
self.__default_exchange: str = _DEFAULT_EXCHANGE if default_exchange is None else default_exchange

# CSV Reader variables
self.__csv_pricing_dict: Dict[str, Any] = _CSV_PRICING_DICT
self.__default_csv_reader: ExchangeNameAndClass = ExchangeNameAndClass(_KRAKEN, _CSV_PRICING_DICT[_KRAKEN])
self.__exchange_csv_reader: Dict[str, Any] = {}

# key: name of exchange, value: AVLTree of all snapshots of the graph
# TO BE IMPLEMENTED - Combine all graphs into one graph where assets can 'teleport' between exchanges
# This will eliminate the need for markets and this dict, replacing it with just one AVLTree
Expand Down Expand Up @@ -412,11 +420,13 @@ def find_historical_bars(
raise RP2ValueError("Internal error: Invalid timespan passed to find_historical_bars.")
current_exchange: Any = self.__exchanges[exchange]
ms_timestamp: int = int(timestamp.timestamp() * _MS_IN_SECOND)
csv_pricing: Any = _CSV_PRICING_DICT.get(exchange)
csv_pricing: Any = self.__csv_pricing_dict.get(exchange)
csv_reader: Any = None

if self.__exchange_csv_reader.get(exchange):
csv_reader = self.__exchange_csv_reader[exchange]
elif csv_pricing == self.__default_csv_reader.klass and self.__exchange_csv_reader.get(self.__default_csv_reader.name) is not None:
csv_reader = self.__exchange_csv_reader.get(self.__default_csv_reader.name)
elif csv_pricing is not None:
csv_signature: Signature = signature(csv_pricing)

Expand All @@ -431,6 +441,9 @@ def find_historical_bars(
else:
csv_reader = csv_pricing()

if csv_pricing == self.__default_csv_reader.klass:
self.__exchange_csv_reader[self.__default_csv_reader.name] = csv_reader

if csv_reader:
csv_bar: Optional[List[HistoricalBar]]
if all_bars:
Expand Down Expand Up @@ -683,6 +696,11 @@ def _get_pricing_exchange_for_exchange(self, exchange: str) -> str:
self.__logger.debug("Price routing locked to %s type for %s.", self.__default_exchange, exchange)
else:
self.__logger.debug("Using default exchange %s type for %s.", self.__default_exchange, exchange)

csv_pricing_class: Any = self.__csv_pricing_dict.get(self.__default_exchange)
if csv_pricing_class:
self.__default_csv_reader = ExchangeNameAndClass(self.__default_exchange, csv_pricing_class)
self.__csv_pricing_dict[exchange] = self.__default_csv_reader.klass
exchange = self.__default_exchange

# The exchange could have been added as an alt; if so markets wouldn't have been built
Expand Down

0 comments on commit 18d0316

Please sign in to comment.