In this tutorial, we will explore how to calculate the top gainers and losers in pair trading using the Bitquery API. We’ll explain the method used for determining the performance of trading pairs and show you how to construct the Bitquery query to retrieve the necessary data. Let’s dive in!
Understanding Trading Pairs Performance Calculation
Pair trading involves comparing the performance of two assets or tokens in a trading pair. To calculate the performance and identify the top gainer or loser, we use the following formula:
Percentage Change = ((Current Price - Initial Price) / Initial Price) * 100
By applying this formula to each trading pair, we can determine the percentage change and identify the top gainers and losers based on their performance within a specific time frame.
Bitquery DexTrades API
we can retrieve the necessary data for calculating pair trading performance using Bitquery GraphQL query. Here’s an example query that retrieves the required data:
query ($network: EthereumNetwork!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
ethereum(network: $network) {
dexTrades(
date: { since: $from, till: $till }
options: { limitBy: { limit: 1, each: "pair_address" }, limit: 2, desc: "final" }
) {
pair_address: smartContract {
address {
address
}
}
first_trade_price: minimum(of: block, get: price)
last_trade_price: maximum(of: block, get: price)
diff: expression(get: "last_trade_price - first_trade_price")
div: expression(get: "diff / first_trade_price")
final: expression(get: "div * 100")
baseCurrency {
symbol
address
}
quoteCurrency {
symbol
address
}
}
}
}
Query
Within the GraphQL query, we have several fields to calculate the top gainers and losers. These fields include:
-
first_trade_quote_price
: Represents the First hour quote price of a token within a specific block. -
last_trade_quote_price
: Represents the Last hour quote price of a token within a given block. -
diff
: Calculates the difference between the last trade’s quote price and the first trade’s quote price. -
div
: Calculates the percentage change between the first trade’s quote price and the last trade’s quote price. -
final
: Expresses the percentage change as a whole number by multiplyingdiv
by 100. -
pair_address
: Retrieves the address of the smart contract. -
baseCurrency
: Provides information about the currency being bought in the pair, including its symbol and address. -
quoteCurrency
: Provides details about the currency being sold in the pair, including its symbol and address.
Executing the Query
To execute the constructed query and obtain the pair trading data, you can use the Bitquery IDE available at Bitquery IDE. Once you execute the query, you can visualize the data in the form of charts or tables. Here is the visual representation of the data : Top Gainers & Top Losers.
That’s it. Enjoy experimenting with different time periods and blockchain networks to analyze the pair trading performance of your favourite tokens.