Created
December 30, 2022 04:22
-
-
Save HH0718/a6dcfa07c0eb2f83070f01544d04270c to your computer and use it in GitHub Desktop.
stack overflow: https://stackoverflow.com/questions/74957740/json-file-parsing-string-indices-must-be-integers-not-str-error/74957829#74957829
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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 | |
} | |
] | |
}, | |
"10001NFTUSDT": { | |
"ret_code": 0, | |
"ret_msg": "OK", | |
"ext_code": "", | |
"ext_info": "", | |
"result": [ | |
{ | |
"symbol": "10001NFTUSDT", | |
"period": "60", | |
"start_at": 1671559200, | |
"open": 0.004533, | |
"high": 0.004538, | |
"low": 0.004523, | |
"close": 0.004535 | |
}, | |
{ | |
"symbol": "10001NFTUSDT", | |
"period": "60", | |
"start_at": 1671562800, | |
"open": 0.004535, | |
"high": 0.004537, | |
"low": 0.004531, | |
"close": 0.004533 | |
}, | |
{ | |
"symbol": "10001NFTUSDT", | |
"period": "60", | |
"start_at": 1671566400, | |
"open": 0.004533, | |
"high": 0.00454, | |
"low": 0.004533, | |
"close": 0.004534 | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from pprint import pprint | |
#Put close prices into a list | |
def extract_close_prices(prices: dict): | |
#trying to extract close price | |
close_prices = [] | |
for price_values in prices["result"]: | |
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: dict): | |
#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: | |
# print(sym_2) | |
#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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from func_cointegration import get_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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment