Trading forex/CFDs on margin carries a high level of risk and may not be suitable for all investors as you could sustain losses in excess of deposits. Leverage can work against you. Due to the certain restrictions imposed by the local law and regulation, German resident retail client(s) could sustain a total loss of deposited funds but are not subject to subsequent payment obligations beyond the deposited funds. Be aware and fully understand all risks associated with the market and trading. Prior to trading any products, carefully consider your financial situation and experience level. Any opinions, news, research, analyses, prices, or other information is provided as general market commentary, and does not constitute investment advice. FXCM Forex Capital Markets Ltd. (henceforth "FXCM", see http://fxcm.com) will not accept liability for any loss or damage, including without limitation to, any loss of profit, which may arise directly or indirectly from use of or reliance on such information.
FXCM provides a RESTful API (henceforth the "API" to interact with its trading platform. Among others, it allows the retrieval of historical data as well as of streaming data. In addition, it allows to place different types of orders and to read out account information. The overall goal is to allow the implementation automated, algortithmic trading programs.
In this documentation, you learn all about the fxcmpy.py
Python
wrapper package (henceforth just fxcmpy.py
or "package").
To get started with the the API and the package, a demo account with FXCM is sufficient. You can open such an account under https://www.fxcm.com/uk/algorithmic-trading/api-trading/
Installation happens via pip
install on the command line.
pip install fxcmpy
Working in an interactive context (e.g. IPython
or Jupyter
), you
can then check whether the package is installed via:
import fxcmpy
fxcmpy.__version__
To connect to the API, you need an API token that you can create or revoke from within your (demo) account in the Trading Station https://tradingstation.fxcm.com/.
In an interactive context, you can use e.g. a variable called TOKEN
to reference your unique API token.
TOKEN = YOUR_FXCM_API_TOKEN
Connecting to the server, then boils down to the following line of code.
con = fxcmpy.fxcmpy(access_token=TOKEN, log_level='error')
However, it is recommended to store the API token in a configuration file which allows for re-usability and hides the token on the GUI level. The file should contain the following lines.
[FXCM] log_level = error log_file = PATH_TO_AND_NAME_OF_LOG_FILE access_token = YOUR_FXCM_API_TOKEN
It is assumed onwards that this file is in the current working directory
and that its name is fxcm.cfg
.
With such a configuration file in the current working directory, only the filename need to be passed as a parameter to connect to the API.
con = fxcmpy.fxcmpy(config_file='fxcm.cfg')
Having established the connection to the API, data retrieval is straightforward.
For example, you can look up which instruments are available via the
con.get_instruments()
method.
print(con.get_instruments())
['EUR/USD', 'USD/JPY', 'GBP/USD', 'USD/CHF', 'EUR/CHF', 'AUD/USD', 'USD/CAD', 'NZD/USD', 'EUR/GBP', 'EUR/JPY', 'GBP/JPY', 'CHF/JPY', 'GBP/CHF', 'EUR/AUD', 'EUR/CAD', 'AUD/CAD', 'AUD/JPY', 'CAD/JPY', 'NZD/JPY', 'GBP/CAD', 'GBP/NZD', 'GBP/AUD', 'AUD/NZD', 'USD/SEK', 'EUR/SEK', 'EUR/NOK', 'USD/NOK', 'USD/MXN', 'AUD/CHF', 'EUR/NZD', 'USD/ZAR', 'USD/HKD', 'ZAR/JPY', 'USD/TRY', 'EUR/TRY', 'NZD/CHF', 'CAD/CHF', 'NZD/CAD', 'TRY/JPY', 'USD/CNH', 'AUS200', 'ESP35', 'FRA40', 'GER30', 'HKG33', 'JPN225', 'NAS100', 'SPX500', 'UK100', 'US30', 'Copper', 'EUSTX50', 'USDOLLAR', 'USOil', 'UKOil', 'NGAS', 'Bund', 'XAU/USD', 'XAG/USD']
Simlarly, historical data is retrieved via the con.get_cancles()
method.
data = con.get_candles('EUR/USD', period='m1', number=250)
data.head()
bidopen | bidclose | bidhigh | bidlow | askopen | askclose | askhigh | asklow | tickqty | |
---|---|---|---|---|---|---|---|---|---|
date | |||||||||
2018-02-23 17:50:00 | 1.23033 | 1.23044 | 1.23044 | 1.23033 | 1.23034 | 1.23045 | 1.23045 | 1.23034 | 60 |
2018-02-23 17:51:00 | 1.23045 | 1.23057 | 1.23057 | 1.23044 | 1.23046 | 1.23058 | 1.23058 | 1.23044 | 148 |
2018-02-23 17:52:00 | 1.23057 | 1.23058 | 1.23059 | 1.23054 | 1.23058 | 1.23059 | 1.23060 | 1.23053 | 56 |
2018-02-23 17:53:00 | 1.23058 | 1.23054 | 1.23059 | 1.23049 | 1.23059 | 1.23054 | 1.23061 | 1.23049 | 62 |
2018-02-23 17:54:00 | 1.23053 | 1.23056 | 1.23063 | 1.23053 | 1.23053 | 1.23055 | 1.23065 | 1.23053 | 125 |
data.tail()
bidopen | bidclose | bidhigh | bidlow | askopen | askclose | askhigh | asklow | tickqty | |
---|---|---|---|---|---|---|---|---|---|
date | |||||||||
2018-02-23 21:55:00 | 1.22962 | 1.22965 | 1.22966 | 1.22958 | 1.22973 | 1.22977 | 1.22978 | 1.22971 | 61 |
2018-02-23 21:56:00 | 1.22965 | 1.22948 | 1.22969 | 1.22931 | 1.22977 | 1.22970 | 1.22980 | 1.22946 | 138 |
2018-02-23 21:57:00 | 1.22948 | 1.22942 | 1.22949 | 1.22934 | 1.22970 | 1.22971 | 1.22977 | 1.22965 | 52 |
2018-02-23 21:58:00 | 1.22942 | 1.22936 | 1.22948 | 1.22931 | 1.22971 | 1.22978 | 1.22988 | 1.22964 | 77 |
2018-02-23 21:59:00 | 1.22937 | 1.22933 | 1.22938 | 1.22926 | 1.22979 | 1.22987 | 1.22987 | 1.22979 | 14 |
Such data can be visualized with standard functionality of Python and pandas, for instance.
from pylab import plt
plt.style.use('seaborn')
%matplotlib inline
data['askclose'].plot(figsize=(10, 6));
If you have questions regarding demo or full accounts, reach out to:
- info@fxcm.co.uk
- +44 (0) 207 398 4050
If you have questions regarding the RESTful API, reach out to:
The detailed documentation of this wrapper is found under:
The detailed documentation of the API is found under:
The book Python for Finance — Mastering Data-Driven Finance (O'Reilly) provides detailed information about the use of Python in Finance:
In-depth courses and programs about Python for Algorithmic Trading: