0

当我在 pyq 中运行以下代码时...

from bittrex.bittrex import Bittrex, API_V2_0
import time, json
from pyq import q, K

get_bittrex = Bittrex(None, None)
starttime = time.time()

market_result = get_bittrex.get_market_summaries()['result']

while True:
    for res in market_result:
        market_name = res['MarketName']
        ask = str(res['Ask'])
        bid = str(res['Bid'])
        last = str(res['Last'])
        timeStamp = str(res['TimeStamp'])
        if market_name in ['USDT-BTC', 'USDT-ETH', 'USDT-LTC']:
            mkt = {"Name": market_name,"Bid": bid,"Ask": ask,"Time":timeStamp}
            q.set(':alpha', [mkt])
            q.upsert(':alpha', mkt)
            q.get(':alpha').show()

    time.sleep(10.0)

它每 10 秒返回一次控制台...

Name     Bid            Ask            Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
Name     Bid          Ask      Time
------------------------------------------------------
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
Name     Bid          Ask          Time
----------------------------------------------------------
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
Name     Bid            Ask            Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
Name     Bid          Ask      Time
------------------------------------------------------
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
Name     Bid          Ask          Time
----------------------------------------------------------
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703

为什么每行打印 2 次?另外,我如何修改它以便将每个market_name都添加到同一个表中,即,它当前正在将每个打印market_name到它自己的表中,而不是附加到现有表中。

理想的输出如下...

Name     Bid            Ask            Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
4

1 回答 1

1

你在循环中所做的相当于下面的 q 代码:

q)`:alpha set enlist`a`b!1 2
q)`:alpha upsert`a`b!1 2
q)show get`:alpha
a b
---
1 2
1 2

这里没有什么令人惊讶的。该set命令在文件中保存一个 1 行表(回想一下,在 qa 表中是一个字典列表)alpha,然后该upsert命令附加一个与保存的行相同的行,最后get读回生成的 2 行表。

您可能想要的是在循环之外初始化表,在循环中运行一系列 upsert 并在循环后显示结果。

请注意,您可以像这样初始化表:

>>> q.set(':alpha', q('!', ["Name","Bid","Ask","Time"], ()).flip)
k('`:alpha')
>>> q.get(':alpha').show()
Name Bid Ask Time
-----------------

最后,如果您的目标是保存提要,那么在每条消息到达后立即将其写入磁盘几乎不是一个好主意。大多数饲料处理程序在内存中收集一天的市场数据,并在一天结束时将其保存在 HDB 中。

于 2017-12-09T03:59:42.220 回答