Algo Trading with REST API and Python | Preparing Your Computer
Welcome to our Instruction Series about using FXCM’s REST API to automate your strategies using Python. In this multi-part series we will dive in-depth into how algorithms are created, starting from the very basics. This article is a primer for our series and will help you prepare your computer so that you can follow along. For a video version of this tutorial, click here.
STEP 1: INSTALL PYTHON
As mentioned before, for our tutorial series we will be using Python version 3.6 to connect to the FXCM REST API. Python is one of the most widely used programming languages for most individual traders. With that said, one of the benefits of FXCM’s REST API is that it will work with almost any programming language. You can find documentation and tutorials for other languages by visiting FXCM’s Github.
Installing Python and the necessary packages is a fairly straight-forward process. To make things even easier, we will be using the Anaconda Distribution installer, which will installs most of the packages that are needed in order to use the REST API, including Python itself and the always useful Jupyter. As mentioned before, this tutorial will utilize Python version 3.6 which can be downloaded from the Anaconda website, www.anaconda.com/download. To do download Anaconda, simply click on the download button.
Open and run the installation wizard that appears in your browser. The default settings in the installation wizard work well most of the time, but if you run into any issues Anaconda has a great installation guide here.
STEP 2: INSTALL NECESSARY PACKAGES
Once the install wizard has finished, launch the Anaconda Navigator by going to the Windows start menu and selecting Anaconda Navigator. Once open, search the ‘environments’ tab to find out if the necessary packages have already been installed. Some packages, like Pandas and requests, should already be installed and will appear when searched for as shown below:
Requests and Pandas should already be installed, but the last library you will need for this tutorial, SocketIO, will need to be installed separately. To install the SocketIO package, go back to the start menu and launch the Anaconda Prompt. You can find the Anaconda Prompt in the same folder as Anaconda Navigator. In this prompt, install the correct version of SocketIO by typing the following and hitting enter:
pip install -U socketIO-client
If the socketIO installation was successful, you will see a message indicating so, similar to the one below. Those are all of the packages needed at this time, so you can close the window.
STEP 3: GET API TOKEN
To get an API token you will need an FXCM account. If you do not have an account, go to www.fxcm.com and register for a free practice account by clicking Try Demo in the upper right corner. Complete the registration form with your information and click Trade Now to generate your practice account credentials, which will be sent to you via email. Then, navigate to https://tradingstation.fxcm.com/ and enter your newly created login and password.
Once the platform has loaded, locate the button showing your login ID in the upper right corner. Click this button and then click on token management and enter your password once again. Clicking on generate token will create your authentication token for the REST API. Make sure to keep this in a safe place because this token is unique to your account. But don’t worry, if you ever do lose it you can always generate a new token and update your program to utilize the newly generated token.
STEP 4: OPEN JUPYTER
Now that everything is installed and you have your token, it’s time to ensure everything is working properly. Open a Jupyter Notebook through the Anaconda Prompt to check on the packages installed in step 2. To open a Jupyter notebook, navigate to your Anaconda 3 folder, open the Anaconda Prompt, type in jupyter notebook and hit enter. This will open Jupyter as a new tab on your Internet browser. Click New and select Python 3 as the notebook type as shown below:
Now you have opened a Jupyter notebook in which you can execute python code. You can check and make sure that all of the packages have been installed properly by importing them. To do that, type the following:
import pandas as pd import json import requests from socketIO_client import SocketIO
Then hit shift+enter to run this cell and execute the code. You should not receive any error messages, indicating the packages were able to be imported, meaning they were installed properly. If you do get an error, ensure your code was entered correctly, including proper spelling and capitalization. If the error persists, feel free to reach out to us at email@example.com for help.
And that’s it! Your computer is now prepped and ready to connect to the FXCM REST API. In the next tutorials, we’ll be utilizing this same Jupyter Notebook to learn how to establish a connection to the FXCM REST API server, authenticate with our demo account credentials, build a function to retrieve and display live streaming prices, and then create a strategy to execute orders based off of this live streaming data.
Click here for the next tutorial where you’ll learn how to connect to the REST API, stream live and historical prices and more.
Risk Warning: The FXCM Group does not guarantee accuracy and will not accept liability for any loss or damage which arise directly or indirectly from use of or reliance on information contained within the webinars. The FXCM Group may provide general commentary which is not intended as investment advice and must not be construed as such. FX/CFD trading carries a risk of losses in excess of your deposited funds and may not be suitable for all investors. Please ensure that you fully understand the risks involved.