Connecting FXCM over FIX – A detailed Tutorial

Share on Facebook13Tweet about this on Twitter3Share on LinkedIn3Share on Google+0
Connecting FXCM with Fix engine - A tutorial

Connecting FXCM with Fix engine – A tutorial

by Sunith Reddy

We talked about the defacto standard for message communication in our previous article on FIX protocol.

“The Financial Information Exchange (FIX) Protocol is a message standard developed to facilitate the electronic exchange of information related to securities transactions. It is intended for use between trading partners wishing to automate communications.[1]

In this article, we are going to take a step further and talk about some operations involved in connecting FXCM over fix. We will be using the QuickFix engine to code the examples. Quickfix is an open source FIX engine.

“QuickFIX is an open source FIX engine. It integrates with financial applications giving them the connectivity they need to communicate with hundreds of systems around the world that support FIX. QuickFIX gives your applications the power to electronically interact with all these systems using a simple interface.”

You can learn more about QuickFIX from http://www.quickfixengine.org/

Credentials

Just like we had credentials for connecting with Interactive Brokers using IBridgePY, we have credentials here as well. Connecting FCMX over fix will need some credentials which will identify the connecting entity for FXCM. The credentials consist of the following:

  • Socket connection host, port – where you would be connecting
  • Sender comp Id – identifies the sender of the message.
  • Target comp id – identifies the intended recipient of the message
  • Target sub id – a sub identifier which completes the recipient identification.

In case of quickFix, these credentials need to be configured in a configuration file. The configuration file would look as follows:

Connecting FXCM over Fix and learn algo trading only from quantinsti

Logging In

All set? Good, if your configuration is correct then the quickfix engine will initiate the connection. This involves connecting to the host and port, and sending the login request.  The login request is the first message of any session. In case it is not the first message, all messages that were sent before the login request are simply ignored.

“All messages sent before login request are ignored.”

The example login message would look as follows:

 

Tags 553 and 554 are meant to pass on the username and password.  Note that 34=1 assumes that this is the first attempt of the day. In other cases it will just be the sequence number of the message. If all credentials are right, then the logon response from FXCM would look as follows

Note that since the response is being sent by fxcm and the intended recipient is our system, the 49 tag is FXCM (sender) and tag 56 = somesenderCompId(intended recipient).

Once the credentials have been verified and successful response received, One might want to get a few initialization parameters in place. For eg, what is the state of the market (open/closed), symbol information (lot sizes, ticksizes).  In order to do this, we use the Trading Session Status Request (35=g)

The following code snippet shows how to send the trading session status request

 

This sends the request, the response to which is the trading session status message.  The session status message looks like the following

 

 

However we are interested in reading this message from code.  The way to read each of these system parameters is as follows:

FXCM sends additional tags in the tradingSessionStatus message. These can be found in the securityList and system Parameters. This information will be used while sending orders.

Custom fields in SecurityList

  • 9001 – symPrecision – precision of the security. Foreg, USD/JPY the value is 3 => the value fields for this symbol are quoted to a precision of 3 decimal places.
  • 9002 – point size – this represents what we mean by a pip for a security. For eg. EUR/USD would show a value of 0.0001 for this field.
  • 9003 – symInterestBuy – the price in the currency of your account for the default lot size of your server. For eg, if your account is in USD and the server default lot size is 10000. Then 9003=0.64 => you will get $0.64 for 10K size.
  • 9004 – symInterestSell – same as above. 9004=-1.48 => you ll pay $1.48 for every 10K size. You can read the default lot size from the tag 9017=BASE_UNIT_SIZE 9018=10000.
  • 9080 – productId – every security belongs to a product type. 1=Forex, 2=Index, 3=Commodity, 4=treasury, 5=bullion. For eg, EUR/USD would be 1.
  • 9090 – conditionalDistanceStop – The minimum distance of the stop orders from the current market price. If you have a buy position, then your stop order must be atleast this distance away from the current bid.
  • 9091 – conditionalDistanceLimit – The minimum distance of the limit order from the current market price. If you have a buy position, then your limit order must be atleast this distance away from the current bid.
  • 9092 – conditionalDistanceEntryStop – The minimum distance for a new stop entry (pending) order. If you want to place a stop entry order to buy then the price of this order must be atleast this distance away from the current ask.
  • 9093 – conditionalDistanceEntryLimit – The minimum distance for a new limit entry (pending) order. If you wanted to place a new limit entry order to buy, then the price of the order must be atleast this distance away from the current ask.
  • 9094 – maxQuantity – maximum size of a single order.
  • 9095 – minQuantity – minimum size of a single order.
  • 9096 – tradingStatus – This indicates whether the destination is open (‘O’) or closed (‘C’).

 

In addition to the above custom fields, the system parameters are also returned in this message (combination of 9017,9018 tags)

  • BASE_CRNCY – currency of your account
  • SERVER_TIME_UTC – if this field is set to UTC then the server will express all time fields in UTC. If not, then it ll express it in the local timezone.
  • BASE_TIME_ZONE – shows the local timezone of the server for example “Asia/Kolkatta”
  • COND_DIST – minimum distance between the price of the new stop or limit orders from the current market price. This value is expressed in pips and is generally defaulted to 0.10.
  • COND_DIST_ENTRY – mimum distance between the price of the new stop entry or limit entry orders from the current market price. This value is expressed in pips and is generally defaulted to 0.10
  • BASE_UNIT_SIZE – minimum order size for FX securities. For CFD securities, we need to check the 9095 tag.
  • END_TRADING_DAY – Timing for close of the trading day. It is expressed in hh:mm:ss format. This time is always in UTC.

 

Requesting Market Data

After making the connection with the server, we are ready to request for Market Data. MarketData consists of a snapshot message and an incremental update message.  The MarketDataSnapshotFullRefresh(W) message contains the updates to market data. It is obtained as a response to marketdatarequest (v) message.

The following snippet shows how to send the marketdatarequest message:

 

The sample marketDataSnapshotFullRefresh message looks like this:

 

MDEntryTypes

The types of data you can receive are referred to as MDEntryTypes in FIX.  FXCM supports:

  • Bid(0)
  • Ask(1)
  • HighPrice(7)
  • LowPrice(8)

Additional MDEntryTypes such as MDEntryDate, MDEntryTime etc. are found only once within the first repeating group of the message.

Position Information

Before starting a strategy it is important to always retrieve the position information.  This is done using the Position Report (35=AP) message. One could not only request for a position report but also subscribe to updates. Sending a RequestForPositions (AN) message with subscription Request type (263) set to  1 will subscribe to updates.

PosReqType (tag 724) is used to determine if a received report is an open (indicated by 0) or closed (indicated by 1) position.

When an open position report is received, it also contains the price at which the position was opened (tag 730).   In case of a closed position report,  the price at which the position as closed, as well as some additional tags are present:

  • 9052 – gross P&L of the position.
  • 9040 – rollover interest applied to the position
  • 9053 – commission applied
  • 9043 – close price of the position

 

Example Position Report

5 Currency USD USD
37 OrderID 134757321 134757321
55 Symbol USD/JPY USD/JPY
58 Text I I
325 UnsolicitedIndicator Y Y
336 TradingSessionID FXCM FXCM
447 PartyIDSource D D
448 PartyID FXCM ID FXCMID
452 PartyRole 3 3
453 NoPartyIDs 1 1
523 PartySubID 32 32
581 AccountType 6 6
625 TradingSessionSubID U100D1 U100D1
702 NoPositions 1 1
703 PosType TQ TQ
704 LongQty 1000 1000
707 PosAmtType CASH CASH
708 PosAmt 0 0
715 ClearingBusinessDate 20121220 20121220
721 PosMaintRptID 2610968257 2610968446
724 PosReqType 0 1
727 TotalNumPosReports 0 0
728 PosReqResult 0 0
730 SettlPrice 84.242 84.242
731 SettlPriceType 1 1
734 PriorSettlPrice 0 0
753 NoPosAmt 1 1
802 NoPartySubIDs 4 4
803 PartySubIDType 26 26
9000 FXCMSymID 2 2
9038 FXCMUsedMargin 5
9040 FXCMPosInterest 0 0
9041 FXCMPosID 46961794 46961794
9042 FXCMPosOpenTime 20121220-03:46:25 20121220-03:46:25
9043 FXCMCloseSettlPrice 84.231
9044 FXCMCloseTime 20121220-03:46:37
9048 FXCMCloseClOrdID 1_157
9052 FXCMPosClosePNL -0.13
9053 FXCMPosCommission 0 0
9054 FXCMCloseOrderID 134757323

 

 

 

Overview of Basic Order types

Time In Force

Time in force represents the duration for which an order stays in the order book in case it is not executed. The different values possible are:

  • GTC (Good till cancel) – This means the order stays in force until its executed or cancelled out explicitly. Typically used when you don’t expect the order to be filled immediately. You want to keep the order active until it is filled.
  • Day – This means the order stays in force till its executed or the day expires.  Typically, this is used when your intent of the order gets obsolete with time.
  • IOC (Immediate or Cancel) – The order gets filled as much as possible at the time it enters the order book. The remaining gets cancelled. You want to take the fill at a particular price.
  • FOK(Fill or Kill) – The order either gets completely filled at the time it enters the orderbook or gets cancelled immediately. You are interested in taking the full fill at a particular price.

 

Order Types

Market

This order will hit the opposite side irrespective of the price. It will take the fill at the next available price as long as the order book is not empty. Typically used when filling the order is more important than the price of the fill. For a market order, the TIF can be GTC, DAY, IOC, FOK.

 

Stop Limit

This order is a market order that has a range for the fill price. This provides a protection against slippage of the fill price. The tag 40=4 means it’s a stopLimit order and the stopPrice is set in tag 99. This represents the worst price you are willing to take the fill at. The TIF can be IOC or FOK

 

Limit

The order will be filled at a price that is equal or better than the price mentioned in the order message (tag 44).  This order type is used when the price of fill is more important than the fill itself. Supported TIFs are GTC, Day,IOC, FOK.

 

Stop

This is a market order that gets placed when the current market price is worse than your stop price as mentioned in the stopOrder message.  The price of the fill is not guaranteed, as this is a market order. Supported TIFs are GTC and Day.

Become an algotrader. learn EPAT for algorithmic trading

Code

Example

This piece of code assumes that the quickfix library is installed. The include path needs to be edited accordingly.  This should give a basic understanding of how a FXCM session can be established, market data subscribed, and orders sent.

Next Step

FXCM is definitely one of the leading online trading platforms that allows retail clients to speculate on forex. If you’re a retail trader or a tech professional looking to start your own automated trading desk or curious to know more about forex and algorithmic trading, begin with basic concepts like automated trading architecture, market microstructure, strategy backtesting system and order management system. Start learning algo trading today!

 

Share on Facebook13Tweet about this on Twitter3Share on LinkedIn3Share on Google+0

Leave a Reply

Your email address will not be published. Required fields are marked *