如何使用从JSON导入的Python List进行计算?

问题描述 投票:-3回答:1

所以我有一个股票数据,我试图计算每个时刻的最佳询价和最佳买入价之间的数量差异。

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'对象不可迭代

python arrays python-3.x quantitative-finance
1个回答
1
投票

dct['Bid']['0'][0][0]是一个等于10.12的浮点值,你不能迭代浮点数。

您应该选择具有最佳价格和数量的子列表dct['Bid']['0'][0],或者使用dct['Bid']['0'],它是所有价格和数量子列表的列表。 对于我的方法,我们首先得到ask和bid词典

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}}

© www.soinside.com 2019 - 2024. All rights reserved.