#import required packagesfromdatetimeimport datetime
importpandasaspdimportnumpyasnpimportrequests#get data from quantapi csv formatdefget_quantapi(symbol, token, duration, format):
query ='https://quantapi.co/api/'+ symbol +'/'+ token +'/?d='+ duration +'&f='+ format
r = requests.get(query)
lines = r.text.splitlines()
#get first line as column namesfor names in lines[0:1]:
name = names.split(',')
columns = name
records = []
for price in lines[1:]:
cols = price.split(',')
date = datetime.strptime(cols[0], '%Y-%m-%d')
open=float(cols[1])
high =float(cols[2])
low =float(cols[3])
close =float(cols[4])
records.append(date)
records.append(open)
records.append(high)
records.append(low)
records.append(close)
df = pd.DataFrame.from_records(np.array(records).reshape(int(len(records)/len(columns)), len(columns)), columns = columns)
df = df.sort_values('date', ascending =True)
print(df)
return df
#sample code to get gold price
symbol ='gold'
token ='123456789xyz'
duration ='1m'
format ='csv'
df = get_quantapi(symbol, token, duration, format)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import required packagesimportpandasaspd#get data from quantapi json formatdefget_quantapi(symbol, token, duration, format):
query ='https://quantapi.co/api/'+ symbol +'/'+ token +'/?d='+ duration +'&f='+ format
df = pd.read_json(query)
df = df[['date', 'open', 'high', 'low', 'close']]
df = df.sort_values('date', ascending =True)
print(df)
return df
#sample code to get gold price
symbol ='gold'
token ='123456789xyz'
duration ='1m'
format ='json'
df = get_quantapi(symbol, token, duration, format)
7. For hkex, shex, szex (OHLCV) stocks data, please use below code. Just change symbol='':