Getting Started with Forex Trading Using Python

Book description

Discover the inner workings of today’s forex market, the essential risks in forex algo trading, and how to mitigate them

Key Features

  • Build trading applications with research and without advanced Python programming skills
  • Dive into professional fx trading while enhancing your trading apps to be more accurate
  • Develop simple yet efficient backtesting applications to help keep your expectations realistic

Book Description

Algorithm-based trading is a popular choice for Python programmers due to its apparent simplicity. However, very few traders get the results they want, partly because they aren’t able to capture the complexity of the factors that influence the market. Getting Started with Forex Trading Using Python helps you understand the market and build an application that reaps desirable results.

The book is a comprehensive guide to everything that is market-related: data, orders, trading venues, and risk. From the programming side, you’ll learn the general architecture of trading applications, systemic risk management, de-facto industry standards such as FIX protocol, and practical examples of using simple Python codes. You’ll gain an understanding of how to connect to data sources and brokers, implement trading logic, and perform realistic tests. Throughout the book, you’ll be encouraged to further study the intricacies of algo trading with the help of code snippets.

By the end of this book, you’ll have a deep understanding of the fx market from the perspective of a professional trader. You’ll learn to retrieve market data, clean it, filter it, compress it into various formats, apply trading logic, emulate the execution of orders, and test the trading app before trading live.

What you will learn

  • Explore the forex market organization and operations
  • Understand the sources of alpha and the concept of algo trading
  • Get a grasp on typical risks and ways to mitigate them
  • Understand fundamental and technical analysis
  • Connect to data sources and check the integrity of market data
  • Use API and FIX protocol to send orders
  • Translate trading ideas into code
  • Run reliable backtesting emulating real-world market conditions

Who this book is for

This book is for financial traders and python developers who are interested in forex trading. Academic researchers looking to focus on practical applications will find this book useful. This book can also help established fx market professionals who want to take the first steps in algo trading. Familiarity with Python and object-oriented programming within the scope of an online course or self-study is a must. Knowledge of network protocols and interfaces is a plus but not a prerequisite, as is specific knowledge about markets and trading.

Table of contents

  1. Getting Started with Forex Trading Using Python
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1: Introduction to FX Trading Strategy Development
  7. Chapter 1: Developing Trading Strategies – Why They Are Different
    1. Trading strategies – it’s all about how you manage risk
      1. Trading strategies – how we make money in financial markets
      2. Trading app – what can be simpler?
      3. So, what about that risk?
    2. Automated trading – operational risk and regulatory limitations
    3. Retrieving market data – quality and consistency as keys to success
      1. Receiving data – when size does matter
      2. Received data – looking at it from a critical angle
    4. Making actual trading decisions – trading logic and credit risk
    5. Ordering – transactional risk
    6. The general architecture of a trading application
    7. Summary
  8. Chapter 2: Using Python for Trading Strategies
    1. Technical requirements
    2. The advantages of using Python in trading strategy development
      1. Memory management
      2. Interactive computing
      3. Ease of integration and routing
      4. Native language structures – lists and dictionaries
      5. Libraries
    3. Modeling – forecasting the future doesn’t necessarily mean practical benefits
    4. Paper trading and backtesting – an essential part of a systemic trader’s risk management
      1. What are paper trading and backtesting?
      2. Backtesting and paper trading in Python
    5. The disadvantages of using Python in trading strategy development
    6. Live trading – where Python faces its limits
    7. Summary
  9. Chapter 3: FX Market Overview from a Developer's Standpoint
    1. Trading venues – where money meets… other money
      1. Organizing chaos – types of trading venues
      2. Auction (open outcry)
      3. Exchange and order book
      4. OTC markets
      5. Liquidity and order limitations
    2. Trade mechanics – again, some terminology
    3. Market makers – comfortable, sophisticated, expensive
      1. Counterparties that provide liquidity
      2. How do market makers make money?
      3. Market risk and how market makers mitigate it
      4. Reasons to trade with market makers
    4. Liquidity providers – the whales that support this planet
      1. Liquidity and volatility – how one transforms into another
    5. ECN – looks like a fair game, but is it?
      1. The organization of ECNs
      2. ECN – not all market participants were created equal
    6. Aggregation – in search of the best price
    7. Trading the FX market – what and how
      1. FX instruments
      2. Naming conventions
      3. How do I place a trade?
    8. Why do I need all this?
    9. Summary
  10. Part 2: General Architecture of a Trading Application and A Detailed Study of Its Components
  11. Chapter 4: Trading Application: What’s Inside?
    1. Technical requirements
    2. Have your app talk to the world – the gloomy world of communication protocols
      1. FIX – universal but too flexible
    3. Retrieving data – garbage in, garbage out
      1. Tick sequence
      2. Time gaps
      3. Price gaps
    4. Trading logic – this is where a small mistake may cost a fortune
    5. Risk management – your safety belt
    6. Ordering – make sure you are understood correctly
    7. Summary
  12. Chapter 5: Retrieving and Handling Market Data with Python
    1. Navigating through data
      1. Tick data and snapshots
      2. Timestamps – be careful comparing apples to apples!
      3. Storing and retrieving tick data
      4. Order book (depth of market)
      5. Storing and retrieving order book data
    2. Data compression – keeping the amounts to a reasonable minimum
      1. Retrieving compressed data
      2. Compressing market data in Python
    3. Working with saved and live data – keeping your app universal
      1. Universal data connector
      2. Data compression revisited
    4. Summary
  13. Chapter 6: Basics of Fundamental Analysis and Its Possible Use in FX Trading
    1. Fundamental analysis – intuitive but of little practical use
    2. Economic news
      1. US non-farm payrolls
      2. Gross domestic product (GDP)
    3. Economic news from a fundamental analysis perspective
    4. Political events
      1. US presidential elections, November 8, 2016
      2. UK Brexit, June 23, 2016
    5. Industry-specific information
      1. Crude oil and the Canadian dollar
    6. Summary
  14. Chapter 7: Technical Analysis and Its Implementation in Python
    1. Technical analysis – ideal for computing, but missing real market processes
      1. Velocity of the market
      2. Digital filters
      3. Range indicators
      4. Volatility indicators
      5. Key takeaways – what TA indicators are all about and how to use them
    2. Implementation of TA indicators in Python
      1. Sliding windows
      2. Peeking ahead
      3. The ultimate solution to the peek-ahead issue
      4. Sliding window as a queue
      5. Moving average – implementation
      6. Stochastic oscillator – implementation
    3. Summary
  15. Chapter 8: Data Visualization in FX Trading with Python
    1. Technical requirements
    2. The basics of charting with Python
      1. Graphical backends
      2. Static versus real-time data and related issues
      3. Installing Matplotlib
      4. Using Matplotlib in your code
    3. Simple plots of market data
    4. Visualizing static market data with pandas
      1. Installing pandas
      2. Reading data from CSV files with pandas
      3. Setting index for a dataframe
      4. Creating simple bar charts using pandas and mplfinance
      5. Making slices and subsets in pandas
      6. Plotting market charts with mplfinance
    5. Visualizing live market data
      1. Plotting live tick data
      2. Plotting live bar or candlestick charts
    6. Adding objects to price charts
    7. Summary
  16. Part 3: Orders, Trading Strategies, and Their Performance
  17. Chapter 9: Trading Strategies and Their Core Elements
    1. Alpha and beta – widely used, widely confused
      1. Alpha – earn from changes in price
      2. Using currency rates as a benchmark
      3. Common FX benchmarks – the USDX
      4. Bank indices – more details, more confusion
      5. Beta – earn on volatility
    2. Options – stable income with unlimited risk
    3. Alpha classics – trend-following, mean reversion, breakout, and momentum
      1. Trend following
      2. Mean reversion
      3. Breakouts
    4. Arbitrage – let’s earn from others’ mistakes
    5. Statistical arbitrage
    6. Event-driven trading strategies
    7. Market-making – profiting on liquidity provision and associated risks
    8. High frequency, low latency – where Python fails
    9. Summary
  18. Chapter 10: Types of Orders and Their Simulation in Python
    1. Order ticket – what you send is what you get
    2. Market orders – the way to get maximum control over transactional risk
      1. Possible execution issues
    3. Limit orders – guaranteed price, but not execution
      1. Possible execution issues
    4. Time in force – better control over execution
      1. Immediate or cancel
      2. Fill or kill
      3. Good for a day (GTD) and good till canceled (GTC)
    5. Stop orders – maximum uncontrolled risk
      1. Possible execution issues
    6. Compound orders
    7. Summary
  19. Chapter 11: Backtesting and Theoretical Performance
    1. Trading app architecture – revised and improved
      1. Market data component
      2. Data aggregation component
      3. Trading logic component
      4. Order execution component
      5. Advantages of the modular architecture
    2. Multithreading – convenient but full of surprises
    3. Trading application with live data feed
    4. Backtesting – speeding up the research
      1. Syncing threads using events
      2. Backtesting platform with a historical data feed
      3. Emulating order execution during backtesting
      4. Equity curve and statistics
    5. Summary – where do we go now?
  20. Part 4: Strategies, Performance Analysis, and Vistas
  21. Chapter 12: Sample Strategy – Trend-Following
    1. Trend-following revisited – trading setup
      1. Determining a trend, part 1 – market model
      2. Determining a trend, part 2 – moving averages
      3. Entry and exit rules
      4. Money management
    2. Choosing the market and preparing data
      1. Compressing data to a daily timeframe
      2. Be careful with time!
    3. Trend-following strategy – implementation
    4. Summary
  22. Chapter 13: To Trade or Not to Trade – Performance Analysis
    1. Trade analysis
    2. Average trade and trading costs
      1. Spread
      2. Slippage
      3. Commission
      4. Overnight swaps
    3. Measuring performance – alpha and beta revisited
      1. Alpha in investment and trading
      2. Beta
    4. Net profit versus buy and hold
    5. Drawdown
      1. Risk/reward and return on drawdown
    6. The power of leverage – how much do I need to trade it?
    7. Summary
  23. Chapter 14: Where to Go Now?
    1. Implementing limit and stop orders
    2. The correct way to calculate the number of trades
    3. From trading ideas to implementation – another sample strategy using limit and stop orders
      1. Contingent orders
    4. Money management and multiple entries
    5. Strategy performance revisited – more metrics
    6. More about the risks specific to algo trading
    7. Classical technical trading setups
    8. Optimization – the blessing and the curse of algo trading
    9. Final words
  24. Index
    1. Why subscribe?
  25. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Getting Started with Forex Trading Using Python
  • Author(s): Alex Krishtop
  • Release date: March 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781804616857