如何使用从 JSON 导入的 Python 列表进行计算?
How to make calculations with Python List imported from JSON?
所以我有一个股票数据,我正在尝试计算每个时刻的最佳卖价和最佳买价之间的交易量差异。
Bid 和Ask 显示的时刻都是0-1-2-3-4。 0-1-2-3-4 子列表中的第一个元素是其交易量的最佳价格(第二个元素是其交易量的第二个最佳价格,第三个是第三个并继续..)
{"Ask":
{"0":[[10.13,30500],[10.14,106456],[10.15,53772],[10.16,58104],[10.17,45589]],
"1":[[10.14,106976],[10.15,53782],[10.16,58104],[10.17,45899],[10.18,31521]],
"2":[[10.14,106986],[10.15,53652],[10.16,58504],[10.17,45589],[10.18,37821]],
"3":[[10.14,106996],[10.15,57872],[10.16,58104],[10.17,45789],[10.18,89721]],
"4":[[10.14,106936],[10.15,53982],[10.16,58154],[10.17,4495],[10.18,2521]]
}
,
"Bid":{
"0":[[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381],[10.08,98178]],
"1":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"2":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"3":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"4":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]]
}
}
我需要帮助计算
1-每个时刻最佳卖价和最佳买价之间的成交量差
2-每个时刻的最佳卖价和最佳买价之间的差价。
(第一个元素如 10.xx 小数是价格,第二个元素是数量)
I have read the json formula and trying to print best ask price to get started but failing it.
import json
with open(r"C:\Users\User\Desktop\FILE.json") as BOB:
data=json.load(BOB)
for x in data['Bid']['0'][0][0]:
print(x)
'float' 对象不可迭代
dct['Bid']['0'][0][0]
是等于 10.12
的浮点值,您不能迭代浮点数。
您应该选择 dct['Bid']['0'][0]
这是具有最佳价格和数量的子列表,或者选择 dct['Bid']['0']
这是所有价格和数量子列表的列表。
对于我的方法,我们首先获取询价和出价字典
dct = {"Ask":
{"0":[[10.13,30500],[10.14,106456],[10.15,53772],[10.16,58104],[10.17,45589]],
"1":[[10.14,106976],[10.15,53782],[10.16,58104],[10.17,45899],[10.18,31521]],
"2":[[10.14,106986],[10.15,53652],[10.16,58504],[10.17,45589],[10.18,37821]],
"3":[[10.14,106996],[10.15,57872],[10.16,58104],[10.17,45789],[10.18,89721]],
"4":[[10.14,106936],[10.15,53982],[10.16,58154],[10.17,4495],[10.18,2521]]
},
"Bid":{
"0":[[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381],[10.08,98178]],
"1":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"2":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"3":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"4":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]]
}
}
ask_dct = dct['Ask']
bid_dct = dct['Bid']
然后我们遍历两个字典,选择第一个元素的最佳卖价和出价,然后计算价格和交易量之间的差异。
result = {}
for k, v in ask_dct.items():
diff_dct = {}
#Take best ask and best bid as the first element of list
best_ask = v[0]
best_bid = bid_dct[k][0]
#Calculate vol and price diff and save it in a dict
diff_dct['vol_diff'] = best_ask[1]-best_bid[1]
diff_dct['price_diff'] = best_ask[0] - best_bid[0]
#For each moment, make another bigger dict and save diff dct to it
result[k] = diff_dct
print(result)
#{'0': {'vol_diff': -168307, 'price_diff': 0.010000000000001563},
#'1': {'vol_diff': 93476, 'price_diff': 0.009999999999999787},
#'2': {'vol_diff': 93486, 'price_diff': 0.009999999999999787},
#'3': {'vol_diff': 93496, 'price_diff': 0.009999999999999787},
#'4': {'vol_diff': 93436, 'price_diff': 0.009999999999999787}}
所以我有一个股票数据,我正在尝试计算每个时刻的最佳卖价和最佳买价之间的交易量差异。
Bid 和Ask 显示的时刻都是0-1-2-3-4。 0-1-2-3-4 子列表中的第一个元素是其交易量的最佳价格(第二个元素是其交易量的第二个最佳价格,第三个是第三个并继续..)
{"Ask":
{"0":[[10.13,30500],[10.14,106456],[10.15,53772],[10.16,58104],[10.17,45589]],
"1":[[10.14,106976],[10.15,53782],[10.16,58104],[10.17,45899],[10.18,31521]],
"2":[[10.14,106986],[10.15,53652],[10.16,58504],[10.17,45589],[10.18,37821]],
"3":[[10.14,106996],[10.15,57872],[10.16,58104],[10.17,45789],[10.18,89721]],
"4":[[10.14,106936],[10.15,53982],[10.16,58154],[10.17,4495],[10.18,2521]]
}
,
"Bid":{
"0":[[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381],[10.08,98178]],
"1":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"2":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"3":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"4":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]]
}
}
我需要帮助计算
1-每个时刻最佳卖价和最佳买价之间的成交量差
2-每个时刻的最佳卖价和最佳买价之间的差价。
(第一个元素如 10.xx 小数是价格,第二个元素是数量)
I have read the json formula and trying to print best ask price to get started but failing it.
import json
with open(r"C:\Users\User\Desktop\FILE.json") as BOB:
data=json.load(BOB)
for x in data['Bid']['0'][0][0]:
print(x)
'float' 对象不可迭代
dct['Bid']['0'][0][0]
是等于 10.12
的浮点值,您不能迭代浮点数。
您应该选择 dct['Bid']['0'][0]
这是具有最佳价格和数量的子列表,或者选择 dct['Bid']['0']
这是所有价格和数量子列表的列表。
对于我的方法,我们首先获取询价和出价字典
dct = {"Ask":
{"0":[[10.13,30500],[10.14,106456],[10.15,53772],[10.16,58104],[10.17,45589]],
"1":[[10.14,106976],[10.15,53782],[10.16,58104],[10.17,45899],[10.18,31521]],
"2":[[10.14,106986],[10.15,53652],[10.16,58504],[10.17,45589],[10.18,37821]],
"3":[[10.14,106996],[10.15,57872],[10.16,58104],[10.17,45789],[10.18,89721]],
"4":[[10.14,106936],[10.15,53982],[10.16,58154],[10.17,4495],[10.18,2521]]
},
"Bid":{
"0":[[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381],[10.08,98178]],
"1":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"2":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"3":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]],
"4":[[10.13,13500],[10.12,198807],[10.11,1110],[10.1,42110],[10.09,84381]]
}
}
ask_dct = dct['Ask']
bid_dct = dct['Bid']
然后我们遍历两个字典,选择第一个元素的最佳卖价和出价,然后计算价格和交易量之间的差异。
result = {}
for k, v in ask_dct.items():
diff_dct = {}
#Take best ask and best bid as the first element of list
best_ask = v[0]
best_bid = bid_dct[k][0]
#Calculate vol and price diff and save it in a dict
diff_dct['vol_diff'] = best_ask[1]-best_bid[1]
diff_dct['price_diff'] = best_ask[0] - best_bid[0]
#For each moment, make another bigger dict and save diff dct to it
result[k] = diff_dct
print(result)
#{'0': {'vol_diff': -168307, 'price_diff': 0.010000000000001563},
#'1': {'vol_diff': 93476, 'price_diff': 0.009999999999999787},
#'2': {'vol_diff': 93486, 'price_diff': 0.009999999999999787},
#'3': {'vol_diff': 93496, 'price_diff': 0.009999999999999787},
#'4': {'vol_diff': 93436, 'price_diff': 0.009999999999999787}}