I am trying to download currency pair data from Oanda using its API I was able to create a code to download one currency from Oanda
I have 2 questions
First, from what I understand from basic python, I have to first create a list of currency pairs, then I loop through each pair in the list to download data of the pair and then I append it each individual dataframe for each pair.
# Input for downloading data using Oanda API
d1 = '2017-05-18'
d2 = str(datetime.today().strftime('%Y-%m-%d'))
list = ['USD_JPY','EUR_USD']
df = pd.DataFrame({'Pairs': list})
gbl = globals()
for i in list:
gbl['df_'+i] = df[df.Pairs==i]
# Downloading Data
for pair in list:
data = oanda.get_history(instrument=list, start=d1, end=d2, granularity='D')
df_EUR_USD= df_EUR_USD.append(pd.DataFrame(data['candles']))
I was able to create database for each pair in the list but I got stuck at downloading data and then appending those downloaded data to each individual dataframe.
Second question, it is about general handling in pandas. Is this the best way to handle all these data? My idea is to download those data and append them to individual dataframe and then run a loop through those lists of dataframe to do some math function and finally run another loop through the lists of dataframe again to extract the calculated data and append all of them to a new dataframe. Is this the best approach? Or is there a better way in handling this situation.
I think you need:
d1 = '2017-05-18'
#strftime return string, so cast is not necessary
d2 = pd.datetime.today().strftime('%Y-%m-%d')
L = ['USD_JPY','EUR_USD']
dfs = []
for pair in L:
#return in loop DataFrame (not tested, because no access to oanda)
data = oanda.get_history(instrument=pair, start=d1, end=d2, granularity='D')
#append column to list
dfs.append(data['candles'])
#create new DataFrame with columns by L
df = pd.concat(dfs, axis=1, keys=L)
If need output as dict
:
dfs = {}
for pair in L:
data = oanda.get_history(instrument=pair, start=d1, end=d2, granularity='D')
#add data to dict
dfs[pair] = data['candles']
print (dfs['USD_JPY'])