In this section I will use Alpha Vantage API to analyze forex data. Let's first import some required libraries. To use any API we are generally required to pass an API key with the search parameters. So, for AlphaVantageAPI we can get one from here. In this approach I'll rather use a library by RomellTorres to explore the AlphaVantageAPI, and not the API site. Let's begin.
import pandas as pd #for dataframe visualization
from alpha_vantage.foreignexchange import ForeignExchange as FX #library to use AlphaVantageAPI without link requests
- The alpha_vantage Library is the library I'm going to use to interact with Alpha Vantage API. Full documentation is available at https://github.com/RomelTorres/alpha_vantage.
APIkey = 'ABC'
- We'll replace 'ABC' with our API key
fx = FX(key="APIkey") # defining the forex instance
pd.set_option("display.max_rows", 500) # optional, to display all the rows of the dataframe
data = fx.get_currency_exchange_intraday(from_symbol='EUR', to_symbol='USD', interval='60min', outputsize='full')
- In the just above cell I passed the parameters to get the intraday data of EURUSD currency pair, and the time interval is set to 1h.
type(data)
tuple
- The data retreived is a tuple. Let's convert it to a datframe for our future operations.
df = pd.DataFrame(data)
df
2021-02-26 11:00:00 | 2021-02-26 10:00:00 | 2021-02-26 09:00:00 | 2021-02-26 08:00:00 | 2021-02-26 07:00:00 | 2021-02-26 06:00:00 | 2021-02-26 05:00:00 | 2021-02-26 04:00:00 | 2021-02-26 03:00:00 | 2021-02-26 02:00:00 | ... | 2021-01-01 05:00:00 | 2021-01-01 04:00:00 | 2021-01-01 03:00:00 | 1. Information | 2. From Symbol | 3. To Symbol | 4. Last Refreshed | 5. Interval | 6. Output Size | 7. Time Zone | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | {'1. open': '1.2125', '2. high': '1.2129', '3.... | {'1. open': '1.2149', '2. high': '1.2151', '3.... | {'1. open': '1.2134', '2. high': '1.2160', '3.... | {'1. open': '1.2155', '2. high': '1.2155', '3.... | {'1. open': '1.2154', '2. high': '1.2160', '3.... | {'1. open': '1.2155', '2. high': '1.2160', '3.... | {'1. open': '1.2160', '2. high': '1.2161', '3.... | {'1. open': '1.2172', '2. high': '1.2176', '3.... | {'1. open': '1.2164', '2. high': '1.2183', '3.... | {'1. open': '1.2157', '2. high': '1.2165', '3.... | ... | {'1. open': '1.2214', '2. high': '1.2214', '3.... | {'1. open': '1.2214', '2. high': '1.2214', '3.... | {'1. open': '1.2214', '2. high': '1.2214', '3.... | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | FX Intraday (60min) Time Series | EUR | USD | 2021-02-26 11:00:00 | 60min | Full size | UTC |
2 rows × 968 columns
So, just above is the output. And, it's quite confusing. Last 7 columns comprise different information. Let's separate them to have a clear idea of the information in these columns.
df[df.columns[-7:]]
1. Information | 2. From Symbol | 3. To Symbol | 4. Last Refreshed | 5. Interval | 6. Output Size | 7. Time Zone | |
---|---|---|---|---|---|---|---|
0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1 | FX Intraday (60min) Time Series | EUR | USD | 2021-02-26 11:00:00 | 60min | Full size | UTC |
These columns represent the parameters that we passed with our query to fetch data about the currency pair. The data of our interest lies in the columns before these columns. So, I'll leave these columns as alone and select the rest of columns.
df_col = df.iloc[0, 0:-7]
df_col
2021-02-26 11:00:00 {'1. open': '1.2125', '2. high': '1.2129', '3....
2021-02-26 10:00:00 {'1. open': '1.2149', '2. high': '1.2151', '3....
2021-02-26 09:00:00 {'1. open': '1.2134', '2. high': '1.2160', '3....
2021-02-26 08:00:00 {'1. open': '1.2155', '2. high': '1.2155', '3....
2021-02-26 07:00:00 {'1. open': '1.2154', '2. high': '1.2160', '3....
...
2021-01-01 07:00:00 {'1. open': '1.2214', '2. high': '1.2214', '3....
2021-01-01 06:00:00 {'1. open': '1.2214', '2. high': '1.2214', '3....
2021-01-01 05:00:00 {'1. open': '1.2214', '2. high': '1.2214', '3....
2021-01-01 04:00:00 {'1. open': '1.2214', '2. high': '1.2214', '3....
2021-01-01 03:00:00 {'1. open': '1.2214', '2. high': '1.2214', '3....
Name: 0, Length: 961, dtype: object
Here the data looks somewhat legible. But, still not as beautiful. Let's decorate it and make it quite legible.
json_data = df_col.to_json('json_data.json')
I converted it into a JSON file to store it locally. Now, let's have a look at this data.
First, read this file from the directory.
record = pd.read_json('json_data.json')
record
2021-02-26 11:00:00 | 2021-02-26 10:00:00 | 2021-02-26 09:00:00 | 2021-02-26 08:00:00 | 2021-02-26 07:00:00 | 2021-02-26 06:00:00 | 2021-02-26 05:00:00 | 2021-02-26 04:00:00 | 2021-02-26 03:00:00 | 2021-02-26 02:00:00 | ... | 2021-01-01 12:00:00 | 2021-01-01 11:00:00 | 2021-01-01 10:00:00 | 2021-01-01 09:00:00 | 2021-01-01 08:00:00 | 2021-01-01 07:00:00 | 2021-01-01 06:00:00 | 2021-01-01 05:00:00 | 2021-01-01 04:00:00 | 2021-01-01 03:00:00 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. open | 1.2125 | 1.2149 | 1.2134 | 1.2155 | 1.2154 | 1.2155 | 1.2160 | 1.2172 | 1.2164 | 1.2157 | ... | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
2. high | 1.2129 | 1.2151 | 1.2160 | 1.2155 | 1.2160 | 1.2160 | 1.2161 | 1.2176 | 1.2183 | 1.2165 | ... | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
3. low | 1.2112 | 1.2121 | 1.2132 | 1.2127 | 1.2145 | 1.2147 | 1.2143 | 1.2158 | 1.2160 | 1.2149 | ... | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
4. close | 1.2114 | 1.2125 | 1.2150 | 1.2135 | 1.2153 | 1.2153 | 1.2155 | 1.2160 | 1.2172 | 1.2163 | ... | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
4 rows × 961 columns
Eye-catching!
But, I want the dates as indexes for our price values. And for that, I'll use transpose function of Pandas.
intraday_EURUSD = pd.DataFrame.transpose(record)
intraday_EURUSD.head()
1. open | 2. high | 3. low | 4. close | |
---|---|---|---|---|
2021-02-26 11:00:00 | 1.2125 | 1.2129 | 1.2112 | 1.2114 |
2021-02-26 10:00:00 | 1.2149 | 1.2151 | 1.2121 | 1.2125 |
2021-02-26 09:00:00 | 1.2134 | 1.2160 | 1.2132 | 1.2150 |
2021-02-26 08:00:00 | 1.2155 | 1.2155 | 1.2127 | 1.2135 |
2021-02-26 07:00:00 | 1.2154 | 1.2160 | 1.2145 | 1.2153 |
It looks much better.
intraday_EURUSD.reset_index()
index | 1. open | 2. high | 3. low | 4. close | |
---|---|---|---|---|---|
0 | 2021-02-26 11:00:00 | 1.2125 | 1.2129 | 1.2112 | 1.2114 |
1 | 2021-02-26 10:00:00 | 1.2149 | 1.2151 | 1.2121 | 1.2125 |
2 | 2021-02-26 09:00:00 | 1.2134 | 1.2160 | 1.2132 | 1.2150 |
3 | 2021-02-26 08:00:00 | 1.2155 | 1.2155 | 1.2127 | 1.2135 |
4 | 2021-02-26 07:00:00 | 1.2154 | 1.2160 | 1.2145 | 1.2153 |
... | ... | ... | ... | ... | ... |
956 | 2021-01-01 07:00:00 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
957 | 2021-01-01 06:00:00 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
958 | 2021-01-01 05:00:00 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
959 | 2021-01-01 04:00:00 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
960 | 2021-01-01 03:00:00 | 1.2214 | 1.2214 | 1.2214 | 1.2214 |
961 rows × 5 columns
Let's plot this.
import matplotlib.pyplot as plt #the plotting library
plt.subplots(figsize=(15, 7.5))
intraday_EURUSD['4. close'].plot()
plt.title('Intraday EURUSD (60 min)')
plt.show()
Beautiful!
Let's represent this in candlestick format.
import plotly.graph_objects as go
fig = go.Figure(data=[go.Candlestick(
open=intraday_EURUSD['1. open'],
high=intraday_EURUSD['2. high'],
low=intraday_EURUSD['3. low'],
close=intraday_EURUSD['4. close'])])
fig.update_layout(
width = 1000,
height = 700,
paper_bgcolor="LightSteelBlue",
)
Hurrah, we have plotted candlesticks for EURUSD.