

AI-Powered Algorithmic Trading with Python
Blogs from ODSC SpeakersEurope 2022posted by ODSC Community May 31, 2022 ODSC Community

Editor’s Note: The author is a speaker for ODSC Europe 2022 this June 15th and 16th. Be sure to check out his talk, “AI-Powered Algorithmic Trading with Python,” there on Wednesday, June 15th.
Algorithmic Trading in financial markets used to be the domain of big institutional players a few years ago. Nowadays, with the advent of newly built retail trading platforms, even individual traders with low funds available for trading can enter the Algorithmic Trading space with relatively low effort. Generally speaking, a typical notebook, a Python environment, several open-source packages, and an Internet connection are enough. You then only need to open a trading account – either a paper trading account to get started with, or a real trading account with say 100 USD/EUR/GBP initial capital.
This half-day training walks you through the steps necessary to start Algorithmic Trading using Python and additional open-source packages, such as NumPy, pandas, matplotlib, and scikit-learn. The idea is to get started by setting up a trading infrastructure first and using it to retrieve data, to formulate and backtest algorithmic trading strategies, to place orders, and to automate the algorithmic trading strategy for continuous deployment.
The trading platform used for this training is the one from Oanda for which you can open a demo account here https://www.oanda.com/eu-en/trading/.
Assuming that you already have a Python environment with the typical data science packages installed, the only additional packages required is https://github.com/yhilpisch/tpqoa. You can install it as follows:
pip install git+https://github.com/yhilpisch/tpqoa.git
In order to make use of the package, you should create a credentials file, say with filename oanda.cfg, with the following structure:
[oanda] account_id = XYZ-ABC-... access_token = ZYXCAB... account_type = practice
The required information is found in the demo account that you have created. Once this is done, you can check whether you can connect to the Oanda API as follows:
Python 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:00:30) Type 'copyright', 'credits' or 'license' for more information IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import tpqoa In [2]: oanda = tpqoa.tpqoa('oanda.cfg') In [3]: data = oanda.get_history(instrument='EUR_USD', ...: start='2021-06-01', ...: end='2022-05-31', ...: granularity='D', ...: price='M') In [4]: data.head() Out[4]: o h l c volume complete time 2021-05-31 21:00:00 1.22258 1.22545 1.22116 1.22138 81745 True 2021-06-01 21:00:00 1.22134 1.22266 1.21640 1.22104 84234 True 2021-06-02 21:00:00 1.22104 1.22142 1.21182 1.21271 95659 True 2021-06-03 21:00:00 1.21273 1.21856 1.21040 1.21668 93352 True 2021-06-06 21:00:00 1.21671 1.22018 1.21449 1.21894 64527 True In [5]: data.tail() Out[5]: o h l c volume complete time 2022-05-24 21:00:00 1.07362 1.07390 1.06421 1.06798 166318 True 2022-05-25 21:00:00 1.06819 1.07317 1.06626 1.07302 135007 True 2022-05-26 21:00:00 1.07257 1.07652 1.06968 1.07334 124909 True 2022-05-29 21:00:00 1.07375 1.07870 1.07262 1.07800 85924 True 2022-05-30 21:00:00 1.07775 1.07798 1.07278 1.07446 50284 False In [6]:
With the API connection, you can also place orders easily:
In [6]: oanda.create_order('EUR_USD', units=1000) {'id': '3142', 'time': '2022-05-31T08:22:26.949998325Z', 'userID': 13834683, 'accountID': '101-004-13834683-001', 'batchID': '3141', 'requestID': '60980512881182334', 'type': 'ORDER_FILL', 'orderID': '3141', 'instrument': 'EUR_USD', 'units': '1000.0', 'gainQuoteHomeConversionFactor': '0.926305211066', 'lossQuoteHomeConversionFactor': '0.935614811178', 'price': 1.07424, 'fullVWAP': 1.07424, 'fullPrice': {'type': 'PRICE', 'bids': [{'price': 1.07408, 'liquidity': '10000000'}], 'asks': [{'price': 1.07424, 'liquidity': '10000000'}], 'closeoutBid': 1.07408, 'closeoutAsk': 1.07424}, 'reason': 'MARKET_ORDER', 'pl': '0.0', 'financing': '0.0', 'commission': '0.0', 'guaranteedExecutionFee': '0.0', 'accountBalance': '101359.1229', 'tradeOpened': {'tradeID': '3142', 'units': '1000.0', 'price': 1.07424, 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '0.0745', 'initialMarginRequired': '33.3'}, 'halfSpreadCost': '0.0745'} In [7]: oanda.create_order('EUR_USD', units=-1000) {'id': '3144', 'time': '2022-05-31T08:22:37.121812044Z', 'userID': 13834683, 'accountID': '101-004-13834683-001', 'batchID': '3143', 'requestID': '60980512927332557', 'type': 'ORDER_FILL', 'orderID': '3143', 'instrument': 'EUR_USD', 'units': '-1000.0', 'gainQuoteHomeConversionFactor': '0.926279341195', 'lossQuoteHomeConversionFactor': '0.935588681308', 'price': 1.07411, 'fullVWAP': 1.07411, 'fullPrice': {'type': 'PRICE', 'bids': [{'price': 1.07411, 'liquidity': '10000000'}], 'asks': [{'price': 1.07427, 'liquidity': '10000000'}], 'closeoutBid': 1.07411, 'closeoutAsk': 1.07427}, 'reason': 'MARKET_ORDER', 'pl': '-0.1216', 'financing': '0.0', 'commission': '0.0', 'guaranteedExecutionFee': '0.0', 'accountBalance': '101359.0013', 'tradesClosed': [{'tradeID': '3142', 'units': '-1000.0', 'price': 1.07411, 'realizedPL': '-0.1216', 'financing': '0.0', 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '0.0745'}], 'halfSpreadCost': '0.0745'} In [8]:
Real-time, or streaming, data is also at your fingertips:
In [8]: oanda.stream_data('EUR_USD', stop=10) 2022-05-31T08:24:05.263842391Z 1.07406 1.07422 2022-05-31T08:24:07.321639227Z 1.07405 1.0742 2022-05-31T08:24:08.445782000Z 1.07405 1.07421 2022-05-31T08:24:09.585760607Z 1.07405 1.07419 2022-05-31T08:24:10.336223320Z 1.07406 1.07419 2022-05-31T08:24:11.235281237Z 1.07409 1.07423 2022-05-31T08:24:11.397952230Z 1.07406 1.0742 2022-05-31T08:24:11.572900914Z 1.07405 1.0742 2022-05-31T08:24:12.000995719Z 1.07405 1.07419 2022-05-31T08:24:14.192202399Z 1.07404 1.0742
In [9]:
These are already the basic ingredients required to get started with Algorithmic Trading: historical data, the ability to place orders and the ability to retrieve data in real-time. In the training, we will make use of these capabilities to implement an algorithmic trading system in a basic fashion. In particular, we will make use of AI (= machine learning) to come up with the algorithmic trading strategy, representing the core of the system.
About the Author/ODSC Europe 2022 Speaker:
Dr. Yves J. Hilpisch is the founder and CEO of The Python Quants (https://tpq.io), a group focusing on the use of open source technologies for financial data science, artificial intelligence, algorithmic trading, computational finance, and asset management. He is also the founder and CEO of The AI Machine (https://aimachine.io), a company focused on AI-powered algorithmic trading based on a proprietary strategy execution platform.
Yves has a Diploma in Business Administration, a Ph.D. in Mathematical Finance, and is Adjunct Professor for Computational Finance.
Yves is the author of six books (https://home.tpq.io/books):
* Financial Theory with Python (2021, O’Reilly, 2021)
* Artificial Intelligence in Finance (2020, O’Reilly)
* Python for Algorithmic Trading (2020, O’Reilly)
* Python for Finance (2018, 2nd ed., O’Reilly)
* Listed Volatility and Variance Derivatives (2017, Wiley Finance)
* Derivatives Analytics with Python (2015, Wiley Finance)
Yves is the director of the first online training program leading to University Certificates in Python for Finance (https://platinum.tpq.io). He also lectures on computational finance, machine learning, and algorithmic trading at the CQF Program (http://cqf.com).
Yves is the originator of the financial analytics library DX Analytics (http://dx-analytics.com) and organizes Meetup group events, conferences, and bootcamps about Python, artificial intelligence and algorithmic trading in London (http://pqf.tpq.io), New York (http://aifat.tpq.io), Frankfurt, Berlin, and Paris. He has given keynote speeches at technology conferences in the United States, Europe, and Asia.
SOCIAL MEDIA
============
https://home.tpq.io
https://twitter.com/dyjh
https://linkedin.com/in/dyjh/
https://github.com/yhilpisch
https://youtube.com/c/yves-hilpisch