DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Data Analytics Using Python
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)

Trending

  • AI-Driven RAG Systems: Practical Implementation With LangChain
  • Building a Vector Index in Azure AI Search: HNSW, Profiles, and RAG Retrieval
  • Engineering Closed-Loop Graph-RAG Systems, Part 4: Evaluating a Graph-RAG System
  • Testing Is Not About Finding Bugs
  1. DZone
  2. Data Engineering
  3. Data
  4. Data Visualization Using Python

Data Visualization Using Python

A step-by-step guide to visualize forex data using Python via TraderMade's API.

By 
Rahul Khanna user avatar
Rahul Khanna
·
Dec. 17, 22 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.1K Views

Join the DZone community and get the full member experience.

Join For Free

This tutorial is for beginners, and it guides you through the practical process of visualizing forex data, considering the least or no knowledge of Python. Let us have an overview of the libraries and setup we will use before we begin.

You can quickly manipulate the data, including converting and storing it in various forms using pandas, an open-source Python library. For convenience, we will use the Jupyter Notebook, an open-source web app for creating documents and programming different languages. This tutorial shows you how to obtain, change, and store forex data from JSON API and explores pandas, Jupyter Notebook, and DataFrame during this process.

Navigate to https://jupyter.org/try in your browser. Then, select the “Classic Notebook” link as shown here: 

Jupyter Classic Notebook


Hold for a few moments till a Jupyter Notebook launches for you in the browser. As you start the notebook, you can get an API key from https://tradermade.com/signup. Open this link in another tab and sign up for free. You get 1000 requests free in a month. Please register an account, and you will find the API key under the myAccount page. Note it down securely. 

tradermade


As you have the API key now, you can get started with the procedure.

To begin with, click on the scissor sign thrice. You can see that third from the left under the ‘Edit’ tab. It clears the initial welcome content, and you can start to code.

It’s Time To Write Some Code

Write the following code in the next cell and click the ‘Run’ button:                 

Shell
 
import pandas as pd


The command above imports the Pandas Data Frame to use in your notebook. You can get additional information about pandas at https://pandas.pydata.org/. 

You can type or copy the following variables in the next cell for setting up. Through these variables, you will pass in the data request. You would need to substitute your API key precisely.               

Python
 
currency = "EURUSD"
api_key = “paste your api key here”
start_date = "2019-10-01"
end_date= "2019-10-30"
format = "records"
fields = "ohlc"


Now, you need to copy the following code in the next cell. The first line of code sends a request to the data server and formats the JSON response in a Pandas Dataframe. The second line of code is for setting the index for the data to the date field. That’s why you can see that in the date order. While the third line of code, “df,” brings the output data frame in a presentable format.                   

Python
 
df = 
pd.read_json("https://marketdata.tradermade.com/api/v1/pandasDF?currency="+currency+"&api_key="+api_key+"&start_date="+start_date+"&end_date="+end_date+"&format="+format+"&fields="+fields)
df = df.set_index("date")
df


Please copy-paste the code, substitute your API key, and run the program, and you will get the following output. The program requests the data from the server, converts the response into Pandas Dataframe format, and presents the output data in a tabular form.

Pandas Dataframe format


You get the data for the period specified in a data frame, identical to a table. The best thing about Pandas is the ability to manipulate data. It can also store it in various readable and presentable formats.

Let’s take a look at some basic commands before we deep-dive:

df.head() - This command provides us with the initial 5 lines of data.

df.tail() - This command gives the last 5 lines of data.

Plotting the Data as a Line Chart

It is easy to plot a line chart. Use the df.plot(kind=’line’) command to get the following:


Plotting the data as a line chart

You can visualize the data set using the above command. After this, we will use a feature in Excel to calculate the 5-day moving average:        

Python
 
df['5_day_MA'] =  df.close.rolling(5).mean()
df['5_day_volatility'] = df.close.rolling(5).std()
df = df.dropna()
df.head(10)



data


You can observe that the dropna() function drops N/A values. Eventually, we need 5 close days values to calculate the first moving average. The data set we took starts from 07/10/2019. With the help of the plotting function, we can see if the current data we extracted shows increasing or decreasing volatility.                   

Python
 
df["5_day_volatility"].plot(title="EURUSD Volatility")



Plotting the data as a line chart



Till here, we have seen working on a single data set. Now, let’s utilize the API for a comparison between two currencies:           

Python
 
currency = "EURUSD,GBPUSD"
fields = "close"
df = pd.read_json("https://marketdata.tradermade.com/api/v1/pandasDF?currency="+currency+"&api_key="+api_key+"&start_date="+start_date+
"&end_date="+end_date+"&format="+format+"&fields="+fields)
df = df.set_index("date")
df.head()



data



Initially, we will calculate the percentage change for the currencies. Then we can assess the correlation between these two currencies. Finally, we will plot it.                   

Python
 
df = df.pct_change(periods=1)
df = df.dropna()
df.corr()



data


Using the above function, we can see the daily correlation between EURUSD and GBPUSD for the complete data set. Then, we can see the changes in this relationship over time.           

Python
 
df["EURUSD_GBPUSD"] = df["EURUSD"].rolling(window=5).corr(other= df["GBPUSD"])
df = df.dropna()
df["EURUSD_GBPUSD"].plot(title="5 day correlation between EURUSD and GBPUSD")



Plotting the data as a line chart


You can see the change in the correlation between the 15th to the 21st of October. You can also save the data in CSV or JSON format by using the following code:               

Python
 
df.to_json(‘mydata.json’)
df.to_csv(‘mydata.csv’)


You can see the available data by clicking File > Open > myfile.json. Similarly, by clicking File > Download, you can download the data.

Data visualization Data (computing) Python (language)

Published at DZone with permission of Rahul Khanna. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Data Analytics Using Python
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook