0% found this document useful (0 votes)
116 views

Rest Quick Start Guide Python

This document provides a quick start guide for using FXCM's REST API with Python: 1. Import necessary libraries and files for connecting to the FXCM API. 2. Login to the API using your account token. 3. Access real-time market data and account information, place market orders, and set and modify entry orders using simple code examples.

Uploaded by

Anderson Muniz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Rest Quick Start Guide Python

This document provides a quick start guide for using FXCM's REST API with Python: 1. Import necessary libraries and files for connecting to the FXCM API. 2. Login to the API using your account token. 3. Access real-time market data and account information, place market orders, and set and modify entry orders using simple code examples.

Uploaded by

Anderson Muniz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

FXCM REST API

Quick start for Python

1. Overview:

FXCM offers a web-based REST API which can be used to establish secure connectivity with FXCM’s
trading systems for the purpose of receiving market data and trading.

This document provides the easiest way to use the REST API for the basic steps of trading.

To get started with the API and the example code, a demo account with FXCM is sufficient. You can open
such an account under https://www.fxcm.com/uk/forex-trading-demo/. Also a PyCharm(IDE for python)
and Anaconda version 3 should be installed. Python 3.6 is the version that is used in the sample.

2. Prerequisite:
- A token should be revoked from the Trading Station on
https://tradingstation.fxcm.com/
- The Socked.IO library should be installed using Python:
https://pypi.python.org/pypi/socketIO-client
- The Python Example should be cloned or downloaded and loaded into the PyCharm
from https://github.com/fxcm/fxcm-api-rest-python3-example

3. Get connected.
In order to connect you should perform the following steps:
- Add the following imports to the project as it is shown in the
“fxcm_rest_api_token.py” file from the https://github.com/fxcm/fxcm-api-rest-
python3-example:
from collections import namedtuple
import requests
from socketIO_client import SocketIO
import logging
import json
import uuid
import threading
from dateutil.parser import parse
from datetime import datetime
import time
import types
- Link your account through the account token that you have revoked from
https://tradingstation.fxcm.com/
trader = fxcm_rest_api.Trader(’YOUR_TOKEN_HERE’, 'demo') # demo for demo
trader.login()

For the example purposes we will enter the following rows and the previous imports will be used
through the “fxcm_rest_api_token”:
import fxcm_rest_api_token

trading_symbol = 'EUR/USD'
trader = fxcm_rest_api_token.Trader(YOURTOKEN , 'demo') # demo for demo
trader.login()
trade_amount = 10
trade_start_buy_price = SHOULD BE MUCH LOWER THAN THE CURRENT PRICE
trade_start_sell_price = SHOULD BE MUCH HIGHER THAN THE CURRENT PRICE

4. Get live feed (EUR/USD as example)

live_feed = trader.subscribe_symbol('EUR/USD')
print(live_feed)

As an only parameter you need to enter the symbol you are trading

5. Retrieve historical price

historical_prices = trader.candles("EUR/USD", "m1", 15, dt_fmt="%Y/%m/%d %H:


%M:%S")['candles']
for candle in historical_prices:
print(candle)

Parameters:

- “EUR/USD” is the symbol you are trading


- “m1” is the period fitted in a single candle. The example is 1 minute you can choose
between (minutes) m1, m5, m15, m30, (hours) H1, H2, H3, H4, H6, H8, (day )D1,
(week) W1,(month) M1
- 15 is the amount of candles that you will request to se
- dt_fmt="%Y/%m/%d %H:%M:%S" is actually not a parameter, but changing the date
format to an adequate one
6. Retrieve acct/ open position / closed table.
All tree requests Retrieve account, open positions information and closed positions information
are simple and intuitive lines of code thanks to the fxcm_rest_api_token used in the example:

6.1.
theAccountID = trader.account_id

6.2.
open_positions = trader.open_positions
for position in open_positions:
print(position)

6.3.
closed_positions = trader.closed_positions
for position in closed_positions:
print(position)

7. Place market/ entry order. (just simple one)


7.1. Market order:

trader.open_trade(theAccountID, trading_symbol, True, 10)

Parameters:
- theAccountID you can extract as shown in step 6.1
- trading_symbol is the symbol you will start a trade in
- True actually stand there as an answer of the question if this is going to be a BUY
trade or a SELL trade
- 10 is the amount of lots you are going to trade

7.2. Entry order:

trader.create_entry_order(theAccountID , trading_symbol , True ,


trade_start_buy_price, 10, True,
'Entry', 'GTC' )

Parameters:

- theAccountID you can extract as shown in step 6.1


- trading_symbol is the symbol you will start a trade in
- True actually stand there as an answer of the question if this is going to be a BUY
trade or a SELL trade. So True equals BUY and False equals SELL
- trade_start_buy_price or trade_start_sell_price (as seen in 3. Get started) should
actually be the price that you want to create your Entry order with
- 10 is the amount of lots you are going to trade
- The second True is for choosing if the amount of the trade is in PIPs
- ‘Entry’ is for the type of the trade
- ‘GTC’ stands for “Good till close” is a parameter for when to start the entry
8. Modify price of entry order.

If you want to change the parameters of an entry order you have to use the ID and this is how
you extract them:

open_orders = []
orders = trader.orders
for order in orders:
open_orders.append(order['orderId'])

Entry order change:

trader.change_order(open_orders[1], trade_start_buy_price, 0, 15)

Parameters:

- First parameter is the order ID


- Second parameter is the actual price that you want to change your entry price to
- Third parameter is the range of the price
- 15 stands for the amount of lots you want to use in the changed entry

You might also like