-4

Hello I am programming using Python and I have a script which allows to get the price of bitcoin on Binance. Here is my code :

import requests
import json

url = requests.get('https://api.binance.com/api/v1/ticker/price?symbol=BTCUSDT')
data = url.json()

print(data['price'])

But I would like to have a script which allows to update when the price changed. Do you know how can I do this ?

Thank you very much !

bcosta12
  • 2,364
  • 16
  • 28
John Fichter
  • 1
  • 1
  • 3
  • 2
    Update *what* exactly? – glibdud Aug 01 '19 at 12:58
  • I mean the price changes oftenly for instance if you refresh this page : `https://api.binance.com/api/v1/ticker/price? symbol=BTCUSDT` you will see the value of price change. And I would like to do a print every time the price change. – John Fichter Aug 01 '19 at 13:04

2 Answers2

1

Unfortunately, this seems like a problem where you can't, say, listen for an event, more that you have to 'ask' for data.

In that case, you could do something like asking for the price every few minutes or so and doing something if it changes.

import requests
import json
import time

lastPrice = 0

def priceChanged():
    # Handle the price change here
    print("The price changed!")

# Forever
while True:
    url = requests.get('https://api.binance.com/api/v1/ticker/price?symbol=BTCUSDT')
    data = url.json()
    # Change the string price into a number
    newPrice = float(data['price'])

    # Is it different to last time?
    if (newPrice != lastPrice):
        lastPrice = newPrice
        priceChanged()

    # Wait 2 mintues
    time.sleep(120)
deeBo
  • 836
  • 11
  • 24
  • Are you sure ? I thought at the beginning to do something like a listener ? – John Fichter Aug 01 '19 at 13:12
  • I'm not entirely sure, since it would depend on what binance.com offers in terms of dev tools, but this would be my assumption for what you'd have to do for a REST API. – deeBo Aug 01 '19 at 13:15
0

There is now way to make binance server notify you when the price has changed.

The only solution you have is to implement a job which can listen for any changes.

For example like this

last_price = None
try:
    price_file = 'price.txt'
    f = open(price_file, "r")
    last_price = f.read()
except Exception as e:
    # failed to read last price
    pass

price_file = 'price.txt'

def get_last_price():
    last_price = None
    try:
        f = open(price_file, "r")
        last_price = f.read()
    except Exception as e:
        # failed to read last price
        pass
    return last_price


def update_price(new_price):
    f = open(price_file, "w")
    f.write(new_price)
    f.close()


def get_biance_price():
    url = requests.get('https://api.binance.com/api/v1/ticker/price?symbol=BTCUSDT')
    data = url.json()
    return data['price']


last_price = get_last_price()
new_price = get_biance_price()

if last_price != new_price:
    print('price changed!') # implement notification
    update_price(new_price)
else:
    print('price is the same')

Now calling this script will save the newest price in 'price.txt' and notify you if the new price is different. Now you can put the scirpt in some linux cron job for example and configure it to call the script with interval

Borislav Stoilov
  • 3,247
  • 2
  • 21
  • 46