UniswapV2TradeFeed#

tradingstrategy.direct_feed.uniswap_v2.UniswapV2TradeFeed Python class in Trading Strategy framework.

class UniswapV2TradeFeed[source]#

Bases: TradeFeed

Uniswap v2 compatible DXE candle generator.

Uses multiple threads to speed up blockchain read.

__init__(pairs, web3_factory, oracles, reorg_mon, timeframe, data_retention_time=None, threads=10, chunk_size=100)[source]#
Parameters:
  • pairs (List[PairDetails]) – List of Uniswap v2 pool addresses

  • web3_factory (Web3Factory) – Web3 connecion creator (multithread safe)

  • oracles (Dict[str, PriceOracle]) – Price oracles needed for the exchange rate conversion

  • timeframe (Timeframe) –

    Maximum timeframe for produced candles when doing candle refreshes.

    This must be the width of the buffer so that we can render the candles at their longest resolution. E.g. get at least 50 hourly candles when 1h candles are rendered, which means we need to have data for ~2 days in the buffer.

  • reorg_mon (ReorganisationMonitor) – Chain reorganistaion manager

  • data_retention_time (Optional[Timedelta]) –

  • threads – Number of threads used in the reader pool. Set 1 to disable thread pooling.

  • chunk_size – Max block chunk read at a time

Methods

__init__(pairs, web3_factory, oracles, ...)

param pairs:

add_trades(trades[, start_block, end_block])

Add trade to the ring buffer with support for fixing chain reorganisations.

backfill_buffer(block_count[, tqdm, save_hook])

Populate the backbuffer before starting real-time tracker.

check_correct_block_range(df, start_block, ...)

Check that trades in the given DataFrame are for correct block range.

check_current_trades_for_duplicates()

Check for duplicate trades.

check_duplicates_data_frame(df)

Check data for duplicate trades.

check_enough_history(required_duration[, ...])

Check that the dafa we have is good time window wise.

check_reorganisations_and_purge()

Check if any of block data has changed

construct_trade_from_uniswap_v2_events(...)

Figure out Uniswap v2 swap and volume.

fetch_trades(start_block, end_block[, tqdm])

Read data between logs.

find_first_included_block_in_candle(ts)

Find the first block number that contains data going into the candle.

get_all_pair_details()

get_block_number_at_chain_tip()

get_block_number_of_last_trade()

Get the last block number for which we have good data.

get_exchange_rate(pair)

Get the current exchange rate for the pair.

get_latest_price(pair)

Return the latest price of a pair.

get_latest_trades(n[, pair])

Returns the latest trades.

get_pair_details(pair)

get_trade_count()

How many trades we track currently.

perform_duty_cycle([verbose])

Update the candle data

restore(df)

Restore data from the previous disk save.

to_pandas(partition_size)

truncate_reorganised_data(latest_good_block)

Discard data because of the chain reorg.

update_cycle(start_block, end_block, ...)

Update the internal work buffer.

Attributes

pair_map

Pair address -> details mapping

__init__(pairs, web3_factory, oracles, reorg_mon, timeframe, data_retention_time=None, threads=10, chunk_size=100)[source]#
Parameters:
  • pairs (List[PairDetails]) – List of Uniswap v2 pool addresses

  • web3_factory (Web3Factory) – Web3 connecion creator (multithread safe)

  • oracles (Dict[str, PriceOracle]) – Price oracles needed for the exchange rate conversion

  • timeframe (Timeframe) –

    Maximum timeframe for produced candles when doing candle refreshes.

    This must be the width of the buffer so that we can render the candles at their longest resolution. E.g. get at least 50 hourly candles when 1h candles are rendered, which means we need to have data for ~2 days in the buffer.

  • reorg_mon (ReorganisationMonitor) – Chain reorganistaion manager

  • data_retention_time (Optional[Timedelta]) –

  • threads – Number of threads used in the reader pool. Set 1 to disable thread pooling.

  • chunk_size – Max block chunk read at a time

pair_map: Dict[str, PairDetails]#

Pair address -> details mapping

fetch_trades(start_block, end_block, tqdm=None)[source]#

Read data between logs.

Raises:

ChainReorganisationDetected – In the case we notice chain data has changed during the reading

Parameters:
Return type:

Iterable[Trade]

construct_trade_from_uniswap_v2_events(prev_sync, swap)[source]#

Figure out Uniswap v2 swap and volume.

This is a stateful mapping: we need to be able to access previous Pair events to correctly deduct the price.

Parameters:
  • prev_sync (Optional[dict]) – The previous Sync() event that defines the price for this swap.

  • swap (dict) –

Return type:

Optional[Trade]