Reader small image

You're reading from  Getting Started with Forex Trading Using Python

Product typeBook
Published inMar 2023
PublisherPackt
ISBN-139781804616857
Edition1st Edition
Right arrow
Author (1)
Alex Krishtop
Alex Krishtop
author image
Alex Krishtop

Alexey Krishtop is a quantitative trader and researcher with 20 years of experience in developing automated trading solutions. He is currently the head of trading and research at Edgesense Technologies and CTO at ForexVox Ltd. He develops market models and trading algorithms for FX, commodities, and crypto. He was one of the first traders who started using Python as the ultimate environment for quantitative trading and suggested a few approaches to developing trading apps that, today, have become standard among many quant traders. He has worked as a director of education with the Algorithmic Traders Association, where he developed an exhaustive course in systematic and algo trading, which covers the worlds of both quantitative models and discretionary approaches.
Read more about Alex Krishtop

Right arrow

Data Visualization in FX Trading with Python

In previous chapters, we learned how to receive and store market data, how to process it, and how to calculate various technical indicators. However, working with large amounts of time series data frequently leads to errors typically caused by sad mistakes – for example, using incorrect data feed or wrong timestamps. Besides that, when working with TA indicators, it’s really wise to check the result of the calculations visually – for example, you want to use a large period moving average to determine long-term price movements, but you make a mistake, enter a small period value, and then find yourself lost in debugging because no real long-term trend can be found. Making your research visual helps identify various mistakes very quickly and saves a lot of time.

In this chapter, we will learn how to visualize data using one of the industry standard libraries, matplotlib, and then go on to plotting bar and candlestick...

Technical requirements

To run the practical examples in this chapter, you only require Python 3.9 or above.

The basics of charting with Python

There are many libraries that implement charting with Python but at the time of writing, two of them are industry standards – matplotlib and plotly:

  • Matplotlib is the oldest charting library (in heavy use since 2003), which was created in order to bring the well-developed charting facilities of Matlab to Python. It can create charts based on any array-like objects, including native Python lists and numpy arrays, support numerous types of charts, including financial ones (which is what we need!), provides full control over chart objects, features almost unlimited chart customizations, and can be used with different backends.
  • plotly is a relatively young competitor (released in 2014). It offers pretty much the same charting facilities as matplotlib so the choice between the two is not obvious. Plotly definitely wins when it comes to interactivity and working with chart objects via an API but loses the competition in speed and abilities...

Simple plots of market data

In the following examples, we will use historical data only. We will learn how to plot live data received from a broker later in this chapter.

There are many ways to read and handle market data, some of which were considered in Chapter 5, Retrieving and Handling Market Data with Python. Now, we are going to learn some alternative approaches so that you can best choose what suits your current research and development needs.

Let’s start with the most straightforward approach, which uses only native Python data structures. As we saw in Chapter 5, the preferred way of storing and manipulating market data is a dictionary because of its full compatibility with the JSON standard and the ability to extract the necessary data by keywords. We will start with dictionaries as well:

  1. First, we still need to do some imports:
    import matplotlib.pyplot as plt
    import csv

The csv module contains very convenient methods to read and parse comma-separated...

Visualizing static market data with pandas

pandas is “a fast, powerful, flexible, and easy to use open source data analysis and manipulation tool, built on top of the Python programming language”, as declared on its official web page at https://pandas.pydata.org. It was originally developed exactly for the purpose of manipulating time series data, especially market prices.

Instead of native Python lists or NumPy arrays, pandas uses DataFrames as a core data object. You can think of a DataFrame as a table, where columns represent various named time series (or any other series) and rows contain actual data, with the first row always containing the names of the series. Pretty much the same as with the historical market data file that we’ve used so far? Yes, and this makes the learning curve with pandas really steep.

pandas offers methods to add, delete, and rearrange columns, create and modify indices, slice and create subsets, merge and reshape DataFrames,...

Visualizing live market data

Before we move on, I strongly recommend you reread the Working with saved and live data – keep your app universal section in Chapter 5, Retrieving and Handling Market Data with Python, and the Sliding windows section in Chapter 7, Technical Analysis and Its Implementation in Python. We are going to use the same architecture to create live plots of market data.

Important reminder

Whatever data we receive from a live data source should go into a queue. This should be done in a separate thread. Then, data is read from the queue into a sliding window that controls the actual amount of data – for any processing or plotting.

When we worked with static historical data, we used very convenient methods that allowed us to read an entire dataset into memory in one line of code and then navigate through it. Of course, any convenience is always paid for, and in this case, the fee is running the risk of peeking ahead (see the Trading logic –...

Adding objects to price charts

It is not difficult to add any objects to the chart if we know their coordinates because all matplotlib methods always plot one array-like object versus another. So, basically, all we need to do to add any special objects to a chart is to calculate their position in the list, or the array along the X axis and the corresponding value along the Y axis.

Let’s consider a simple yet valuable example. In Chapter 3, FX Market Overview from a Developer’s Standpoint, we saw that price takers can only buy at the ask and sell at the bid. We also saw that a large order can move the price a few points (pips) up or down because it consumes the liquidity from several levels in the order book. So, we can assume with a good degree of confidence that if the best bid suddenly became greater than the best ask at the previous tick, then it was possibly a trace of a significant buy order. And it works vice versa – if we observe a plunge of the best...

Summary

In this chapter, we learned about the general principles of handling charts with Python. Now, we can quickly find the required part of historical data and plot it as line or candlestick charts. We also learned how to plot live market data and update the charts in real time. Finally, we learned how to add custom graphics to a price chart and discovered that crossing the spread can indeed be a potentially valuable trading signal. We are prepared to visualize any data, be it market prices or the performance of our trading algorithm, so it’s high time we step into the domain of trading strategies to understand how and why they work, and to make the right choice for further development. This is what we are going to consider in the next chapter.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Forex Trading Using Python
Published in: Mar 2023Publisher: PacktISBN-13: 9781804616857
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alex Krishtop

Alexey Krishtop is a quantitative trader and researcher with 20 years of experience in developing automated trading solutions. He is currently the head of trading and research at Edgesense Technologies and CTO at ForexVox Ltd. He develops market models and trading algorithms for FX, commodities, and crypto. He was one of the first traders who started using Python as the ultimate environment for quantitative trading and suggested a few approaches to developing trading apps that, today, have become standard among many quant traders. He has worked as a director of education with the Algorithmic Traders Association, where he developed an exhaustive course in systematic and algo trading, which covers the worlds of both quantitative models and discretionary approaches.
Read more about Alex Krishtop