由于缺乏清晰度,我重复了我的问题。下面的代码得到一个随机数,然后检查它是否是一个新的高,并记录它是否是一个。然后经过一定的延迟(1分钟)后,它会再次从前一个时期的最后一个数字开始做同样的事情。
我想以不同的频率运行相同的所以我将有很多句号,如果我在函数中重写下面的代码,例如,我可以得到agregateinfrequency(5min),它将只返回下面的打印(或列表)< / p>
希望现在更有意义。
for random_number, current_time,i in generator():
previous_last_price = mynumber
mynumber = mynumber + (random_number*0.0001)
#print (random_number*0.01)
if mynumber >high_price: #new one
high_price = mynumber
if mynumber <low_price: #new one
low_price = mynumber
last_price = mynumber
if keyfunc(current_time,1) != previous_reftime1min: #new one
print mynumber,",",i, current_time,",", keyfunc(current_time,1),",", previous_reftime1min,",", open_price,",", high_price,",", low_price,",", last_price
wrtr.writerow([mynumber, i,current_time, keyfunc(current_time,1), previous_reftime1min, open_price, high_price, low_price, last_price])
myfile.flush() # whenever you want, and/or
open_price = previous_last_price
high_price = mynumber
low_price = mynumber
last_price = mynumber
previous_reftime1min = keyfunc(current_time,1)
答案 0 :(得分:0)
以下是基于我理解你想要它做什么的脚本可能结构的一些伪代码。
def check_price(random_number,last_price):
new_price = do_some_logic() #do your logic
write(new_price) #some abstract code to write your output to a file
return new_price
price=some_initval
while True:
g=get_new_random_number()
price=check_price(g,price)
sleep(desired_period)
我没有使用for循环,而是在这里使用while循环。我的理解是,您打算将此程序用作连续运行的守护程序,每分钟左右醒来,检查一些数据(例如当前价格)并运行计算。 while True
确保程序将重复执行此操作,直到您将其删除为止。