目的是制作比特币价格实时图表。 如何使用添加到list1和list2的值制作图形。
使用重复功能将值附加到列表中。这是完整的代码:
from poloniex import Poloniex
import random
import sched
import datetime
import time
polo = Poloniex()
list1 = []
list2 = []
class PeriodicScheduler(object):
def __init__(self):
self.scheduler = sched.scheduler(time.time, time.sleep)
def setup(self, interval, action, actionargs=()):
action(*actionargs)
self.scheduler.enter(interval, 1, self.setup,
(interval, action, actionargs))
def run(self):
self.scheduler.run()
def periodic_event():
ticker = float(polo.returnTicker()['USDT_BTC']['last'])
ticker2 = float(polo.returnTicker()['BTC_BCHSV']['last'])
UTCTime = datetime.datetime.now().timestamp()
random_num = (random.uniform(-0.0004, 0.0004))
ticker3 = float(ticker) * (1 + random_num)
ticker4 = float(ticker2) * (1 + random_num)
list1.append(ticker3)
list2.append(UTCTime)
print(round(ticker, 2), "BTC/USDT - Poloniex")
print(round(ticker2, 7), "BSV/BTC - Poloniex")
print(UTCTime)
print(round(ticker3, 2), "BTC/USDT - PriceWars")
print(round(ticker4, 7), "BSV/BTC - PriceWars")
print(list1)
print(list2)
INTERVAL = 2 # every 2 seconds
periodic_scheduler = PeriodicScheduler()
periodic_scheduler.setup(INTERVAL, periodic_event) # it executes the event just once
periodic_scheduler.run() # it starts the scheduler