avatarRoman Paolucci

Summary

The website content provides a guide on implementing multithreading in Python for finance applications, demonstrating how parallel computing can improve efficiency in algorithmic trading systems.

Abstract

The article "Multithreading in Python for Finance" serves as a quick guide to parallel computing, emphasizing its applications in the finance sector. It illustrates the concept of parallel computing by comparing it to painting walls, where multithreading can significantly reduce the time required by executing tasks simultaneously. The guide discusses the use of an abstract class TradingSystem to manage the execution of trading strategies across different timeframes, ensuring that data analysis and trade execution can occur concurrently. An example implementation for a trading system for NVIDIA stocks is provided, which fetches option chain data every ten seconds. The article also references additional resources for developing algorithmic trading systems in Java and Python, and promotes an AI service called ZAI.chat as a cost-effective alternative to ChatGPT Plus for those interested in further exploring AI in trading.

Opinions

  • The author believes that parallel computing is essential for finance applications, particularly in algorithmic trading, to handle multiple processes requiring data from different timeframes.
  • Multithreading is presented as a solution to improve computational efficiency, reducing the time to execute multiple tasks from sequential to parallel processing.
  • The use of an abstract class is advocated for creating flexible and scalable trading systems, allowing for the development of specific trading rulesets across varying timeframes.
  • The article suggests that the concepts discussed can be applied in a live trading environment, with references to the author's other works for further learning.
  • The author endorses ZAI.chat as a valuable AI service for those interested in AI-driven trading strategies, highlighting its affordability compared to other services like ChatGPT Plus.

Multithreading in Python for Finance

A quick guide to parallel computing with applications in finance

Photo Credit

Parallel Computing

Imagine a computer was trying to paint four walls, and each wall takes 30m to paint. If the computer painted all four walls sequentially, as it normally executes code it would take (30m)*(4 walls) or 120m to paint all four walls. The code below illustrates this in Python.

What if we could simultaneously paint all four walls as the same time? This idea is called parallel computing, running code at the same time, asynchronously, on their own threads. If the computer multithreaded (ran each paint_wall function on its own thread concurrently) this process it would only take 30m to paint all four walls. The code below illustrates multithreading this process in Python.

Where might this help us in finance? Algorithmic trading systems are responsible for executing trades across a variety of asset classes with varying timeframes. A trading system may be high frequency trading an order book imbalance strategy while analyzing position size in indexes to hedge the strategy. In this system, both processes occur simultaneously, but require data from different timeframes. In this article, I aim to give a brief object-oriented approach to support multithreading within systems for financial analysis.

Threading an Abstract Class

Consider an abstract class TradingSystem. The abstract class maintains the barebones requirements for all trading systems created as subclasses. That is, it houses a function residing in a timed infinite loop. This is a stripped version of an abstract TradingSystem to illustrate multithreading, but you can expect a more practical abstract class to include the number of trades, profit/loss, and open/filled/closed orders.

In this case, the abstract class takes two parameters, a ticker and a timeframe. Every instance of the TradingSystem class will assign itself the ticker and timeframe, and create a threaded infinite loop sleeping for the duration of the timeframe. This literally allows us to manage the what and when in every subclass.

Abstract Class Implementation

Below is a trading system for NVIDIA, created as an implementation of the abstract TradingSystem class. Structuring the TradingSystem class as a superclass gives us the flexibility to develop specific trading rulesets across varying timeframes. The NVIDIA trading system is requesting an option chain (the what) from yahoo finance’s API every ten seconds (the when).

Quantitative Development and Trading

If you wish to implement this in a live trading environment you can look to my other articles on developing algorithmic trading systems and strategies in Java and Python.

Finance
Trading
Python
Coding
Programming
Recommended from ReadMedium