Beyond the Backtest: How to Go Live with Python and Oanda
Lasha4 min read·Just now--
Chapter 8. CFD Trading with Oanda
You have spent weeks refining your approach. You have scrubbed your data, conducted backtesting, and your Sharpe ratio seems ideal. However, a realization hits you; backtesting is just an illusion of the past.
It does not matter to the real world how beautiful your historical charts are.
During the research stage, you are a scientist. During the live trading environment, you are a pilot. The last mile of algorithmic trading is the most thrilling yet perilous phase — the point where your programming interacts with the market.
This week, we shall discuss the transition process from theory to practice by using Python and Oanda.
Why Oanda and CFDs?
For many years, participating in the worldwide markets required being one of the “Big Players” in business suits. In today’s world, you can participate from the comfort of your laptop through Contracts for Difference (CFDs).
Whatever your interest is — EUR/USD, Gold, S&P 500 index, Oanda’s API will get you there. But here is the best news: you don’t have to be millionaires. However, you will need to know the concept of leverage.
Imagine leverage as an engine that enables you to drive a bigger car with little gasoline. On the one hand, it has the potential to multiply your gains. On the other hand, it has the ability to empty your pocket and your bank account. And that is why we use code: to eliminate emotions.
The Secret Weapon: The tpqoa Library
This book describes an awesome wrapper known as tpqoa that makes working with Oanda’s complicated API easy, even for beginners.
First, you must make a connection to the “mothership.” Here’s how you can obtain your account details and verify your balance:
import tpqoa
# Connect to Oanda using a configuration file
api = tpqoa.tpqoa('oanda.cfg')
# Get your account summary
print(api.get_account_summary())Note: The oanda.cfg file holds your API key and account ID. Keep it secret!
Moving from “Static” to “Live”
The majority of traders remain confined to working with CSV files. However, the markets themselves are dynamic entities. You have to transition from historic data analysis to streaming data analysis to trade live.
As opposed to waiting for the entire day to download an immense file, the Python program taps directly into the Oanda heartbeat to obtain “ticks” (any change in price).
# Get historical data for the last 100 minutes
data = api.get_history(
instrument='EUR_USD',
start='2023-10-01',
end='2023-10-02',
granularity='M1',
price='M'
)
print(data.head())Step 2: The Momentum Bot
The true trading bot does not simply wait around. It waits for information, analyzes that information, and takes action based on it. The author employs the MomentumTrader class in Chapter 8.
Below is an illustration of how a bot thinks:
class MyTrader(tpqoa.tpqoa):
def on_success(self, time, bid, ask):
# This function runs every time a new price comes in
print(f"New Price: {time} | Bid: {bid} | Ask: {ask}")
# Logic: If price goes up, maybe we buy?
# (This is where your strategy goes!)
# Start streaming live prices
trader = MyTrader('oanda.cfg')
trader.stream_data('EUR_USD', stop=10) # Get 10 updates and stopThe “Online” Mindset
Coding a bot means creating an “online algorithm.” While a backtest is able to see the entire “movie,” an online algorithm will only be able to view one “frame” at any given moment.
Your bot needs to:
- Listen: Collect the raw tick data.Resample:
- Convert the ticks into bars (for example, 5 seconds or 1 minute).
- Decide: Apply your indicators to generate a “Buy” or “Sell” signal.
- Act: Immediately send your order to Oanda.
Don’t Gamble — Use Paper Trading
Mistake number one? Going live on day one with real money!
There is the Oanda Practice Account. The account comes with actual market data but not actual dollars. This way you get to play around and check whether your bot breaks if you lose Internet connection or fails on a highly volatile news-driven event.
Summary
Algorithmic trading is more than simply knowing the correct direction of the market. It is about having a robust platform which can deliver in real-time conditions. Being able to cross that final frontier from writing a Python program to executing in real-time puts you above 90% of retail traders.
If you have a trading idea you want to bring to life or need help with the technical implementation, feel free to reach out on LinkedIn.
I appreciate the read.
sources:
Python for Algorithmic Trading (Yves Hilpisch)