【发布时间】:2015-11-25 15:42:30
【问题描述】:
我在这里迷路了,我有一个流式传输价格的 API,我正在尝试将倒数第二个价格与最后一个价格进行比较,例如,如果 x > y 则执行某些操作。我不知道如何在价格流动时将最后一个价格与第二个价格进行比较。有人可以阐明这可能是如何工作的吗?提前致谢!
我的直播:
def stream_to_queue(self):
response = self.connect_to_stream()
if response.status_code != 200:
return
for line in response.iter_lines(1):
if line:
try:
msg = json.loads(line)
except Exception as e:
print "Caught exception when converting message into json\n" + str(e)
return
if msg.has_key("instrument") or msg.has_key("tick"):
price = msg["tick"]["ask"]
print price
这会打印 1.23004 这样的价格,然后继续循环并打印更多价格。我试图将当前价格保存在循环外的变量中,然后在出现新价格时引用它,但它不起作用..
我的尝试:
def stream_to_queue(self):
response = self.connect_to_stream()
if response.status_code != 200:
return
oldLine = ''
for line in response.iter_lines(1):
if line:
try:
msg = json.loads(line)
except Exception as e:
print "Caught exception when converting message into json\n" + str(e)
return
if msg.has_key("instrument") or msg.has_key("tick"):
price = msg["tick"]["ask"]
oldLine = price
newLine = oldLine
if newLine > oldLine:
print newLine
【问题讨论】:
标签: python python-2.7 stream compare store