在这里编辑...我正在从以下 current_tick = json.loads(response)
获取实时数据,如下所示{'symbol': 'TSLA', 'minute': '2020/06/02 17:31', 'open': '882.4', 'high': '882.4', 'low': '882.93', 'close': '885.93'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:33', 'open': '307.96', 'high': '307.99', 'low': '307.96', 'close': '307.99'},
{'symbol': 'TSLA', 'minute': '2020/06/02 17:32', 'open': '882.0', 'high': '882.0', 'low': '307.94', 'close': '307.94'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:34', 'open': '307.94', 'high': '307.97', 'low': '307.94', 'close': '307.96'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:35', 'open': '307.96', 'high': '307.98', 'low': '307.96', 'close': '307.98'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:36', 'open': '308.02', 'high': '308.04', 'low': '308.02', 'close': '308.04'}
我已经在变量current_tick中插入了以下内容:
在这里编辑...
{'symbol': 'TSLA', 'minute': '2020/06/02 17:31', 'open': '882.4', 'high': '882.4', 'low': '882.93', 'close': '885.93'}
{'symbol': 'SPY', 'minute': '2020/06/02 17:33', 'open': '307.96', 'high': '307.99', 'low': '307.96', 'close': '307.99'}
{'symbol': 'TSLA', 'minute': '2020/06/02 17:32', 'open': '882.0', 'high': '882.0', 'low': '307.94', 'close': '307.94'}
{'symbol': 'SPY', 'minute': '2020/06/02 17:34', 'open': '307.94', 'high': '307.97', 'low': '307.94', 'close': '307.96'}
{'symbol': 'SPY', 'minute': '2020/06/02 17:35', 'open': '307.96', 'high': '307.98', 'low': '307.96', 'close': '307.98'}
{'symbol': 'SPY', 'minute': '2020/06/02 17:36', 'open': '308.02', 'high': '308.04', 'low': '308.02', 'close': '308.04'}
我已经实现了一个代码,如果该股票代号为一个,并且从变量current_tick中获取数据,那么该代码可以正常工作;但是,如果该股票代号为多个,则它将覆盖现有的一个代号,并导致其他问题。
if not tick_dt in minutes_processed:
minutes_processed[tick_dt] = True
print(minutes_processed)
if len(minute_candlesticks) > 0:
minute_candlesticks[-1]["close"] = previous_tick["last"]
minute_candlesticks.append(
{
"symbol": current_tick["symbol"],
"minute": tick_dt,
"open": current_tick["last"],
"high": current_tick["last"],
"low": current_tick["last"],
}
)
if len(minute_candlesticks) > 0:
current_candlestick = minute_candlesticks[-1]
if current_tick["last"] > current_candlestick["high"]:
current_candlestick["high"] = current_tick["last"]
if current_tick["last"] < current_candlestick["low"]:
current_candlestick["low"] = current_tick["last"]
现在,我正在尝试在字典上创建词典列表,并插入实时交易股票数据以映射其特定交易代码,如下所示。
app_stock_list = [
{'TSLA': {'symbol': 'TSLA', 'minute': '2020/06/02 17:31', 'open': '882.4', 'high': '882.4', 'low': '882.93', 'close': '885.93'},
{'symbol': 'TSLA', 'minute': '2020/06/02 17:32', 'open': '882.0', 'high': '882.0', 'low': '307.94', 'close': '307.94'}},
{'SPY': {'symbol': 'SPY', 'minute': '2020/06/02 17:33', 'open': '307.96', 'high': '307.99', 'low': '307.96', 'close': '307.99'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:34', 'open': '307.94', 'high': '307.97', 'low': '307.94', 'close': '307.96'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:35', 'open': '307.96', 'high': '307.98', 'low': '307.96', 'close': '307.98'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:36', 'open': '308.02', 'high': '308.04', 'low': '308.02', 'close': '308.04'}
]
如何更正上面的代码以创建如上的字典,或者编写代码为每个符号附加实时数据的正确方法是什么?
注意:我进行了一些编辑,以使您理解正确。
答案 0 :(得分:0)
我认为这个dict理解将对您有用:
stock_list = [
{'symbol': 'TSLA', 'minute': '2020/06/02 17:31', 'open': '882.4', 'high': '882.4', 'low': '882.93', 'close': '885.93'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:33', 'open': '307.96', 'high': '307.99', 'low': '307.96', 'close': '307.99'},
{'symbol': 'TSLA', 'minute': '2020/06/02 17:32', 'open': '882.0', 'high': '882.0', 'low': '307.94', 'close': '307.94'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:34', 'open': '307.94', 'high': '307.97', 'low': '307.94', 'close': '307.96'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:35', 'open': '307.96', 'high': '307.98', 'low': '307.96', 'close': '307.98'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:36', 'open': '308.02', 'high': '308.04', 'low': '308.02', 'close': '308.04'}
]
stocks = {symbol: [i for i in stock_list if i['symbol'] == symbol]
for symbol in {i['symbol'] for i in stock_list}} # use a set() comprehension to get the unique items
# how to update with a price dict
updated_price = {'symbol': 'TSLA', 'minute': '2020/06/02 17:33', 'open': '123.0', 'high': '456.0', 'low': '789.94', 'close': '032.94'}
stocks[updated_price['symbol']].append(updated_price)
import json
print(json.dumps(stocks, indent=4))
输出(JSON格式):
{
"TSLA": [
{
"symbol": "TSLA",
"minute": "2020/06/02 17:31",
"open": "882.4",
"high": "882.4",
"low": "882.93",
"close": "885.93"
},
{
"symbol": "TSLA",
"minute": "2020/06/02 17:32",
"open": "882.0",
"high": "882.0",
"low": "307.94",
"close": "307.94"
},
{
"symbol": "TSLA",
"minute": "2020/06/02 17:33",
"open": "123.0",
"high": "456.0",
"low": "789.94",
"close": "032.94"
}
],
"SPY": [
{
"symbol": "SPY",
"minute": "2020/06/02 17:33",
"open": "307.96",
"high": "307.99",
"low": "307.96",
"close": "307.99"
},
{
"symbol": "SPY",
"minute": "2020/06/02 17:34",
"open": "307.94",
"high": "307.97",
"low": "307.94",
"close": "307.96"
},
{
"symbol": "SPY",
"minute": "2020/06/02 17:35",
"open": "307.96",
"high": "307.98",
"low": "307.96",
"close": "307.98"
},
{
"symbol": "SPY",
"minute": "2020/06/02 17:36",
"open": "308.02",
"high": "308.04",
"low": "308.02",
"close": "308.04"
}
]
}