The Python Quants

FXCM Algorithmic Trading Initiative

RESTful API & Automated Trading

Dr. Yves J. Hilpisch

The Python Quants GmbH

Risk Disclaimer

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 & TPQ 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.

Speaker Disclaimer

The speaker is neither an employee, agent nor representative of FXCM and is therefore acting independently. The opinions given are their own, constitute general market commentary, and do not constitute the opinion or advice of FXCM or any form of personal or investment advice. FXCM assumes no responsibility for any loss or damage, including but not limited to, any loss or gain arising out of the direct or indirect use of this or any other content. 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.

Reading FXCM Tick Data

In [1]:
import pandas as pd
import datetime as dt
import cufflinks as cf  # Cufflinks
cf.set_config_file(offline=True)  # set the plotting mode to offline

The Tick Reader Class

In [2]:
from fxcm_tick_reader import fxcm_tick_reader

Available Symbols

In [3]:
print(fxcm_tick_reader.get_available_symbols())
('AUDCAD', 'AUDCHF', 'AUDJPY', 'AUDNZD', 'CADCHF', 'EURAUD', 'EURCHF', 'EURGBP', 'EURJPY', 'EURUSD', 'GBPCHF', 'GBPJPY', 'GBPNZD', 'GBPUSD', 'GBPCHF', 'GBPJPY', 'GBPNZD', 'NZDCAD', 'NZDCHF', 'NZDJPY', 'NZDUSD', 'USDCAD', 'USDCHF', 'USDJPY')

Retrieving Tick Data

In [4]:
start = dt.datetime(2018, 3, 7)
stop = dt.datetime(2018, 3, 8)
In [5]:
%time td = fxcm_tick_reader('EURUSD', start, stop)
Fetching data from: https://tickdata.fxcorporate.com/EURUSD/2018/10.csv.gz
CPU times: user 2.81 s, sys: 486 ms, total: 3.3 s
Wall time: 3.96 s
In [6]:
type(td)
Out[6]:
fxcm_tick_reader.fxcm_tick_reader
In [7]:
td.get_raw_data().info()
<class 'pandas.core.frame.DataFrame'>
Index: 1446427 entries, 03/04/2018 22:00:03.859 to 03/09/2018 21:59:23.423
Data columns (total 2 columns):
Bid    1446427 non-null float64
Ask    1446427 non-null float64
dtypes: float64(2)
memory usage: 33.1+ MB
In [8]:
td.get_raw_data().tail(10)
Out[8]:
Bid Ask
DateTime
03/09/2018 21:58:51.369 1.23055 1.23067
03/09/2018 21:58:52.369 1.23056 1.23067
03/09/2018 21:58:52.883 1.23056 1.23068
03/09/2018 21:59:00.009 1.23056 1.23067
03/09/2018 21:59:00.066 1.23057 1.23071
03/09/2018 21:59:00.075 1.23056 1.23084
03/09/2018 21:59:00.082 1.23056 1.23086
03/09/2018 21:59:00.093 1.23057 1.23086
03/09/2018 21:59:00.603 1.23058 1.23086
03/09/2018 21:59:23.423 1.23034 1.23086

Working with the Tick Data

In [9]:
%time td.get_data().info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1446427 entries, 2018-03-04 22:00:03.859000 to 2018-03-09 21:59:23.423000
Data columns (total 2 columns):
Bid    1446427 non-null float64
Ask    1446427 non-null float64
dtypes: float64(2)
memory usage: 33.1 MB
CPU times: user 5.05 s, sys: 16.6 ms, total: 5.07 s
Wall time: 5.06 s
In [10]:
%time td.get_data().info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1446427 entries, 2018-03-04 22:00:03.859000 to 2018-03-09 21:59:23.423000
Data columns (total 2 columns):
Bid    1446427 non-null float64
Ask    1446427 non-null float64
dtypes: float64(2)
memory usage: 33.1 MB
CPU times: user 10.5 ms, sys: 2.66 ms, total: 13.1 ms
Wall time: 9.66 ms
In [11]:
%%time
data = td.get_data(start='2018-03-06 08:00:00', end='2018-03-06 16:00:00')
data.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 144516 entries, 2018-03-06 08:00:00.338000 to 2018-03-06 15:59:59.719000
Data columns (total 2 columns):
Bid    144516 non-null float64
Ask    144516 non-null float64
dtypes: float64(2)
memory usage: 3.3 MB
CPU times: user 35.5 ms, sys: 18.1 ms, total: 53.6 ms
Wall time: 51.1 ms
In [12]:
data['Bid'].iplot()