Advertisement
J2897

CCXT Ticker Example

Jan 24th, 2024
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import ccxt
  2. import time
  3. from datetime import datetime
  4. import os
  5.  
  6. symbol = 'BTC/USDT'
  7.  
  8. def fetch_and_print_ticker():
  9.     ticker = exchange.fetch_ticker(symbol)
  10.     timestamp = ticker['timestamp']
  11.     datetime_str = datetime.utcfromtimestamp(timestamp / 1000.0).strftime('%Y-%m-%d %H:%M:%S')
  12.  
  13.     os.system('cls' if os.name == 'nt' else 'clear')  # Clear the console based on the operating system
  14.     # print('Ticker:', ticker)
  15.     print(f"Symbol: {ticker['symbol']}")
  16.     print(f"Timestamp: {datetime_str}")
  17.     print(f"High: {ticker['high']}")
  18.     print(f"Low: {ticker['low']}")
  19.     print(f"Bid Price: {ticker['bid']}")
  20.     print(f"Bid Volume: {ticker['bidVolume']}")
  21.     print(f"Ask Price: {ticker['ask']}")
  22.     print(f"Ask Volume: {ticker['askVolume']}")
  23.     print(f"Last Price: {ticker['last']}")
  24.     print(f"Change: {ticker['change']}")
  25.     print(f"Percentage Change: {ticker['percentage']}")
  26.     print("------------------------------")
  27.  
  28. # Create a Binance instance without API credentials
  29. exchange = ccxt.binance()
  30.  
  31. # Fetch and print ticker information every 5 seconds (adjust as needed)
  32. while True:
  33.     fetch_and_print_ticker()
  34.     time.sleep(5)
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement