Market Data Provider
The Market Data Provider service simplifies access to real-time market data with the following methods.
Any scripts can instantiate the Market Data Provider:
Below are a some methods that it contains. Each method receives the connector name, trading pair, and other arguments that can be defined as config parameters.
Price¶
def get_price_by_type(self, connector_name: str, trading_pair: str, price_type: PriceType):
"""
Retrieves the price for a trading pair from the specified connector.
:param connector_name: str
:param trading_pair: str
:param price_type: str
:return: Price instance.
"""
connector = self.get_connector(connector_name)
return connector.get_price_by_type(trading_pair, price_type)
Example:
def get_price_for_volume(self, connector_name: str, trading_pair: str, volume: float,
is_buy: bool) -> OrderBookQueryResult:
"""
Gets the price for a specified volume on the order book.
:param connector_name: The name of the connector.
:param trading_pair: The trading pair for which to retrieve the data.
:param volume: The volume for which to find the price.
:param is_buy: True if buying, False if selling.
:return: OrderBookQueryResult containing the result of the query.
"""
order_book = self.get_order_book(connector_name, trading_pair)
return order_book.get_price_for_volume(is_buy, volume)
Example:
Volume¶
def get_volume_for_price(self, connector_name: str, trading_pair: str, price: float, is_buy: bool) -> OrderBookQueryResult:
"""
Gets the volume for a specified price on the order book.
:param connector_name: The name of the connector.
:param trading_pair: The trading pair for which to retrieve the data.
:param price: The price for which to find the volume.
:param is_buy: True if buying, False if selling.
:return: OrderBookQueryResult containing the result of the query.
"""
order_book = self.get_order_book(connector_name, trading_pair)
return order_book.get_volume_for_price(is_buy, price)
Example:
Order Book¶
def get_order_book_snapshot(self, connector_name, trading_pair) -> Tuple[pd.DataFrame, pd.DataFrame]:
"""
Retrieves the order book snapshot for a trading pair from the specified connector, as a tuple of bid and ask in
DataFrame format.
:param connector_name: str
:param trading_pair: str
:return: Tuple of bid and ask in DataFrame format.
"""
order_book = self.get_order_book(connector_name, trading_pair)
return order_book.snapshot
Example:
Candles¶
Candles are trailing intervals of OHCLV data that can be used to generate custom indicators.
def get_candles_df(self, connector_name: str, trading_pair: str, interval: str, max_records: int = 500):
"""
Retrieves the candles for a trading pair from the specified connector.
:param connector_name: str
:param trading_pair: str
:param interval: str
:param max_records: int
:return: Candles dataframe.
"""
candles = self.get_candles_feed(CandlesConfig(
connector=connector_name,
trading_pair=trading_pair,
interval=interval,
max_records=max_records,
))
return candles.candles_df.iloc[-max_records:]
Example: