Reputation: 1
I'm having an issue while coding a small bot in python. I have a Json file that I retrieved from an api looking like the following:
> { > "10000NFTUSDT": { > "ret_code": 0, > "ret_msg": "OK", > "ext_code": "", > "ext_info": "", > "result": \[ > { > "symbol": "10000NFTUSDT", > "period": "60", > "start_at": 1671559200, > "open": 0.004533, > "high": 0.004538, > "low": 0.004523, > "close": 0.004535 > }, > { > "symbol": "10000NFTUSDT", > "period": "60", > "start_at": 1671562800, > "open": 0.004535, > "high": 0.004537, > "low": 0.004531, > "close": 0.004533 > }, > { > "symbol": "10000NFTUSDT", > "period": "60", > "start_at": 1671566400, > "open": 0.004533, > "high": 0.00454, > "low": 0.004533, > "close": 0.004534 > },
I'm trying to extract the close price from each coins (to then compare it them with each other) but I'm stuck at the extracting process. My main file look like this:
import json
from func_cointegration import get_cointegrated_pairs
\#STEP 3 Find Cointegrated pairs
print("Calculating co-integration...")
with open("1_price_list.json") as json_file:
price_data = json.load(json_file)
if len(price_data) > 0:
coint_pairs = get_cointegrated_pairs(price_data)
I'm calling get_cointegrated_pairs that use extract_close_prices
import math
#Put close prices into a list
def extract_close_prices(prices):
#trying to extract close price
close_prices = []
for price_values in prices:
print(type(price_values))
if math.isnan(price_values["close"]):
return []
close_prices.append(price_values["close"])
print(close_prices)
return close_prices
#Calculate cointegrated pairs
def get_cointegrated_pairs(prices):
#Loop through all the coin and check co-integration.
coint_pair_list = []
included_list = []
for sym_1 in prices.keys():
#check each coin against the first (sym1)
for sym_2 in prices.keys():
if sym_2 != sym_1:
#Get unique combination id and ensure one off check
sorted_characters = sorted(sym_1 + sym_2)
unique = "".join(sorted_characters)
if unique in included_list:
break
#get close prices
series_1 = extract_close_prices(prices[sym_1])
series_2 = extract_close_prices(prices[sym_2])
I don't understand why I can't access "price_values" with the key "close". I though that opening the json file with load would ensure it loads as a dictionary, I'm following a tutorial and the guy run a similar code without this "typeError" issue. I want to feed my "close_price" list with all the close price from my json file. Any ideas on what i'm doing wrong?
Traceback (most recent call last):
File "/Users/xxxxxxxx/PycharmProjects/StatArbBot/Strategy/main.py", line 27, in <module>
coint_pairs = get_cointegrated_pairs(price_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxxx/PycharmProjects/StatArbBot/Strategy/func_cointegration.py", line 35, in get_cointegrated_pairs
series_1 = extract_close_prices(prices[sym_1])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxxx/PycharmProjects/StatArbBot/Strategy/func_cointegration.py", line 12, in extract_close_prices
if math.isnan(price_values["close"]):
~~~~~~~~~~~~^^^^^^^^^
TypeError: string indices must be integers, not 'str'
Upvotes: 0
Views: 49
Reputation: 33
So I tried to replicate your code and issue.
Turns out that you were passing a dictionary and trying to iterate through it.
for price_values in prices:
in your extract_close_prices function.
So instead you need to iterate to the list nested in the dictionary under the key result
like so:
for price_values in prices['result']:
Then you try to see if the list item is not a number and since it's an object (dictionary technically) with if math.isnan(price_values):
then you get an empty list returned.
instead you need to check if the value of the key close
is a number like so: if math.isnan(price_values["close"])
After that the rest of your code should work.
Here is a working copy I created from your posted question.
https://gist.github.com/HH0718/a6dcfa07c0eb2f83070f01544d04270c
Original Answer below:
You are attempting to get an item from a list that's nested in a JSON file.
Therefore, you need to provide the index of the item you are looking for. Once you provide the index of that item within the list you can get the value of that object.
Notice how "result" is a list:
"result": [ { "symbol": "10000NFTUSDT", "period": "60", "start_at": 1671559200, "open": 0.004533, "high": 0.004538, "low": 0.004523, "close": 0.004535 },...
as indicated by the [
for example:
response.get("10000NFTUSDT").get("result")[0].get["close"]
will get you,0.004535
Upvotes: 1