Transferring the First Price from Binance to Google Sheets: A Step-by-Step Guide

Are you a cryptocurrency enthusiast or a trader looking for ways to automate data analysis using Google Sheets and the Binance API? In this article, we will walk you through the process of getting the first price from Binance’s P2P page in Google Sheets.

Prerequisites:

  • You have a Binance account with P2P trading enabled.
  • You have a Google Sheet set up to receive data from Binance.
  • The “IMPORTXML” function is not available in Google Sheets, so we will use a workaround where we use the “JSON” function and parse the response manually.

Step 1: Parse the response

Binance’s P2P page returns an XML response that contains the price information. To parse this data in Google Sheets, we need to:

  • Use the “JSON.stringify()” method to convert the XML string into a JSON object.
  • Then use the “JSON.parse()” function to parse the JSON object.

Here is the code snippet:

Import JSON






Define the Binance API endpoint URL

api_url = " + "UZS"


Parse the XML response into a JSON object

json_data = json.loads(API URL)

Step 2: Extract the first price

Once we have the JSON object, we need to extract the first price. Since JSON.parse() returns an empty string if the input is not valid JSON, we use a simple loop to iterate through the data and find the first value that matches our search criteria.

Here is the code snippet:


Define search term (in this case first price)

search term = "price"


Initialize price variable

first_price = None


Iterate through the JSON object to find the matching price

for key, value in json_data.items():

if isinstance(value, str) and search_term.lower() in value.lower():


Update first price variable with the matching price

if first_price is None or float(value) < float(first_price):

first_price = Float(value)


If we find a match, we output the price in Google Sheets

if first_price is not None:

print(f"Price: {first_price}")

Step 3: Output to Google Sheets

To output the first price in Google Sheets, you need to create an array with the Create a value and then use “IMPORTXML” or a similar method in Google Sheets to upload that data.

Here is an example of how you can do this:

Import JSON


Define the Binance API endpoint URL

api_url = " + "UZS"


Parse the XML response into a JSON object

json_data = json.loads(API URL)


Extract the first price from the JSON object

search term = "price"

first_price = None


Loop through the JSON object to find the matching price

for key, value in json_data.items():

if isinstance(value, str) and search_term.lower() in value.lower():


Update the first price variable with the corresponding price

if first_price is None or float(value) < float(first_price):

first_price = float(value)


Create a new row array with the price value

price_array = [first_price]


Upload the data to Google Sheets using IMPORTXML

importxml_value = "YOUR_BINARYSEAFD_API_URL_HERE"

importxml_output = f"YOUR_BINARYSEAFD_API_URL_HERE?action=importdata&json={importxml_value}"

print(importxml_output)

Example use case:

Suppose you have a Binance API endpoint URL that returns an XML response like this:

You can modify the above code snippet to extract the first price from the XML response and output it to Google Sheets as follows:

  • Replace “api_url” with the URL of your Binance API endpoint.
  • Update the “search term” variable to match the value of the “price” element in the XML response.

TESTNET BINANCE TRADING