我有一个来自彭博社的公式,我想每 15 分钟执行一次。我希望这段代码每天从上午 9 点到下午 4 点运行,间隔为 15 分钟。每次迭代之后,我希望将这些值存储在某个地方,以便我最终可以创建连续的日内折线图。我尝试创建计划运行,但它只是打印我的值。
我已尝试创建一个此时每 15 分钟运行一次的计划。我希望它打印包含我的值的 datapull_final 代码。每 15 分钟,应随市场更新一次。
import sched, time
from xbbg import blp
import datetime
import schedule
today = datetime.datetime.now().ctime()
def action():
datapull = blp.bdp(tickers='BBG000BBV9N3 FIGI', flds=['YAS_YLD_SPREAD'])
datapull_final = round(datapull.yas_yld_spread[0], 2)
print(datapull_final)
# Set up scheduler
s = sched.scheduler(time.localtime, time.sleep)
# Schedule when you want the action to occur
s.enterabs(time.strptime(today), 900, action)
# Block until the action has been run
s.run()
import sched, time
from xbbg import blp
import datetime
import pandas as pd
spreads = pd.DataFrame()
def action():
global spreads
datapull = blp.bdp('LQD US EQUITY', 'YAS_YLD_SPREAD')
spreads = pd.concat([spreads, datapull])
action()
# Set up scheduler
s = sched.scheduler(time.time, time.sleep)
while True:
# Get the current time
now = datetime.datetime.now()
# Check if the current time is between 9am and 4pm
if 4 <= now.hour < 16:
# Schedule when you want the action to occur
next_run = time.time() + 900
s.enterabs(next_run, 1, action)
# Countdown timer
while time.time() < next_run:
time_left = int(next_run - time.time())
print(f"Current Value: {spreads['yas_yld_spread'].round(2).values[-1]} | Time until next run: {round(time_left/60,2)} minutes", end='\r', flush=True)
time.sleep(1)
# Run the scheduled events
s.run()
else:
print("It's not between 9am and 4pm. Waiting...")
time.sleep(60)