Install the Python wrapper package
pip install fxcmpy
Open a FAXM Demo Account
Create a unique API token
Connect to the API
import fxcmpy
api = fxcmpy.fxcmpy(access_token=YOUR_FXCM_API_TOKEN, log_level='error')
The configuration file
[FXCM]
log_level = error
log_file = PATH_TO_AND_NAME_OF_LOG_FILE
access_token = YOUR_FXCM_API_TOKEN
import fxcmpy
api = fxcmpy.fxcmpy(config_file='pyalgo.cfg')
api = fxcmpy.fxcmpy(config_file='pyalgo.cfg', server='demo')
api = fxcmpy.fxcmpy(config_file='pyalgo.cfg', server='real')
Retrieving data
Retrieving tick data
import time
import numpy as np
import pandas as pd
import datetime as dt
from pylab import mpl, plt
plt.style.use('seaborn')
mpl.rcParams['savefig.dpi'] = 300
mpl.rcParams['font.family'] = 'serif'
The available symbols
from fxcmpy import fxcmpy_tick_data_reader as tdr
print(tdr.get_available_symbols())
Retrieves one week’s worth of tick data for a single symbol
start = dt.datetime(2020, 3, 25)
stop = dt.datetime(2020, 3, 30)
td = tdr('EURUSD', start, stop)
td.get_raw_data().info()
td.get_data().info()
td.get_data().head()
Pick a subset of the data and implement typical financial analytics tasks
sub = td.get_data(start='2020-03-25 12:00:00', end='2020-03-25 12:15:00')
sub.head()
sub['Mid'] = sub.mean(axis=1)
sub['SMA'] = sub['Mid'].rolling(1000).mean()
sub[['Mid', 'SMA']].plot(figsize=(10, 6), lw=1.5)
Retrieving candles data
Candles data is data for certain homogeneous time intervals (“bar”) with open, high, low, and close values for both bid and ask prices.
from fxcmpy import fxcmpy_condles_data_reader as cdr
print(cdr.get_available_symbols())
start = dt.datetime(2020, 4, 1)
stop = dt.datetime(2020, 5, 1)
period = 'H1'
candles = cdr('EURUSD', start, stop, period)
data = candles.get_data()
data.info()
data[data.columns[:4]].tail()
data[data.columns[4:]].tail()
data['MidClose'] = data[['BidClose', 'AskClose']].mean(axis=1)
data['SMA1'] = data['Midclose'].rolling(30).mean()
data['SMA2'] = data['Midclose'].rolling(100).mean()
data[['MidClose', 'SMA1', 'SMA2']].plot(figsize=(10, 6))
Working with the API
import fxcmpy
fxcmpy.__version__
api = fxcmpy.fxcmpy(config_file='../pyalgo.cfg')
instruments = api.get_instruments()
Retrieving historical data
candles = api.get_candles('USD/JPY', period='D1', number=10)
candles[candles.columns[:4]]
candles[candles.column[4:]]
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 6, 1)
candles = api.get_candles('EUR/GBP', period='D1', start=start, stop=end)
candles.info()
candles = api.get_candles('EUR/USD', period='m1', number=250)
candles['askclose'].plot(figsize=(10, 6)
Retrieving streaming data
def output(data, dataframe):
print('%3d | %s | %s | %6.5f, %6m5f' %(len(dataframe), data['Symbol'], pd.to_datetime(int(date['Updated']), unit='ms'), data['Rates'][0], data['Rates'][1]))
api.subscribe_market_data('EUR/USD', (output,))
api.get_last_price('EUR/USD')
api.unsubscribe_market_data('EUR/USD')
Placing orders
Verifies that there are no open positions and then opens different positions via the .create_makret_buy_order() method:
api.get_open_positions()
order = api.create_market_buy_order('EUR/USD', 100)
sel = ['tradeId', 'amountK', 'currency', 'grossPL', 'isBuy']
api.get_open_positions()[sel]
order = api.create_market_buy_order('EUR/GBP', 50)
api.get_open_positions()[sel]
order = api.create_market_sell_order('EUR/USD', 25)
order = api.create_market_buy_order('EUR/GBP', 50)
api.get_open_positions()[sel]
api.close_all_for_symbol('EUR/GBP')
api.get_open_positions()[sel]
api.close_all()
api.get_open_positions()
Account information
api.get_default_account()
api.get_accounts().T









