Skip to main content

Getting Prices for the Period of P&L Calculation

This code will extract the price of the specified token on the first and last day of the period for which you need the PL. You can modify the query and parameters to get the prices for a different token, time period, or multiple tokens.

  1. Import the necessary libraries:
import requests
import json
from balanceUpdates import min_time, max_time
import pandas as pd

  1. Define the URL and payload for the v2 API request:

In this query we use DEXTradeByTokens to get the price for the token. We get it for the trades that happened in the earliest timestamp in the first day and last day of the focused time period.

url = "https://streaming.bitquery.io/graphql"

payload = json.dumps({
"query": "query MyQuery {\n EVM(dataset: combined, network: eth) {\n min_trade: DEXTradeByTokens(\n where: {Trade: {Currency: {SmartContract: {is: \"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48\"}}}, Block: {Date: {after: \"" +min_time+ "\"}}}\n limitBy: {by: Block_Date, count: 1}\n orderBy: {ascending: Block_Date}\n limit: {count: 1}\n ) {\n Block {\n Date\n }\n Trade {\n Price\n }\n }\n latest: DEXTradeByTokens(\n where: {Trade: {Currency: {SmartContract: {is: \"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48\"}}}, Block: {Date: {is: \"" +max_time+ "\"}}}\n limitBy: {by: Block_Date, count: 1}\n orderBy: {ascending: Block_Date}\n limit: {count: 1}\n ) {\n Block {\n Date\n }\n Trade {\n Price\n }\n }\n }\n}\n",
"variables": "{}"
})

  1. Set the headers for the request, including your API key:
headers = {
'Content-Type': 'application/json',
'X-API-KEY': 'keyy'
}

  1. Make the POST request to the v2 API:
response = requests.request("POST", url, headers=headers, data=payload)

  1. Check the response status code to make sure the request was successful:
if response.status_code == 200:
print("Request successful")
else:
print("Request failed. Error code:", response.status_code)

  1. Parse the JSON response:
response_json = json.loads(response.text)

  1. Extract the trade price data:
latest_prices = response_json['data']['EVM']['latest']
earliest_prices = response_json['data']['EVM']['min_trade']

  1. Create DataFrames from the latest and earliest trade price data:
trade_latest = pd.DataFrame(latest_prices, columns=[ 'Block', 'Trade'])

trade_earliest = pd.DataFrame(earliest_prices, columns=[ 'Block', 'Trade'])

  1. Print the DataFrames for Reference:
print(trade_latest)
print(trade_earliest)