Search code examples
pythonarrayspython-3.xquantitative-finance

How to make calculations with Python List imported from JSON?


So I have a stock data that I am trying to calculate the difference of the volume between best ask and best bid price for each time moment.

The time moments are 0-1-2-3-4 shown in both Bid and Ask. And first element in those sublists of 0-1-2-3-4 are the best price with its volume (Second element is the second best price with its volume, third is third and goes on..)

{"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]]
}
}

I need help calculating

1- The Volume difference between best ask price and best bid price for each time moment

2- The price difference between best ask price and best bid price for each moment.

(First elements like 10.xx decimals are price, Second elements are Volume)


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' object is not iterable


Solution

  • dct['Bid']['0'][0][0] is a float value equalling 10.12, and you cannot iterate over a float.

    You should either take dct['Bid']['0'][0] which is the sublist with best price and volume, or take dct['Bid']['0'] which is a list of all price and volume sublists.
    For my approach, we start by getting the ask and bid dictionary

    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']
    

    Then we iterate over both dictionaries, pick the best ask and bid which is the first element, and then take a difference between prices and volumes.

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