cTrader TrueFx Real Time Prices free

by ClickAlgo in category Other at 27/12/2015
Description

Get free clean untouched real time Forex price data using this example robot, you can retrieve the data asynchronous or synchronously. This robot is an example from ClickAlgo.com to demonstrate the use of web services that are available on the internet and how you can use this data to give you that edge on your trading.

Asynchronous tasks is also demonstrated in the code using the Task parallel Library with the .NET Framework 4, the TPL is the preferred way to write multi-threaded and parallel code in C#

TrueFx offer free real-time price data for Forex and I wrote a wrapper to call this web service and expose an interface for accessing the values.

The web service they expose is described in the document below.

http://www.truefx.com/dev/data/TrueFX_MarketDataWebAPI_DeveloperGuide.pdf

You will need to register to access the price data from TrueFx, this is free and you only give away your email that needs to be validated.

Register at TrueFx for Free Data Access

You can run the demo by adding your username and password in the cBot user defined parameters as shown below:

To add this list of currency pairs enter them as shown in the above screen shot, so each pair is separated by a comma and the pairs include a forward slash; example: AUD/USD,USD/JPY,EUR/USD - do not include any spaces. The web service requires it in the format.

The results from the web service are displayed in the log file as shown below:

The web service just returns the data as CSV or HTML in a way that is not really usable for consumption like XML or JSON, I think they designed this for display purposes for websites and not for use with algorithms.

The code you see in the algo file has the external class library cAlgo4u.TrueFx.dll included so that you do not need to manage references and add the assembly to you project.


HOW TO USE

For all you programmers out there I would just use the external class library cAlgo4u.TrueFx.dll as a reference in your code and not copy and paste it in each time. To access the data the simplest method is described below.

The code snippet below is the data class which is declared in the cAlgo4u.TrueFx library which just shows the the type of data returned.

 

/// TrueFX brings you real, deal-able prices from market making banks, delivered directly with no intermediary. 
public class Currency { 

/// Currency-pair symbol 
public string Pair { get; set; } 

/// Millisecond time-stamp 
public string TimeStamp { get; set; } 

/// Bid big figure 
public string BidBig { get; set; } 

/// Bid points 
public string BidPoints { get; set; } 

/// Offer big figure 
public string OfferBig { get; set; } 

/// Offer points 
public string OfferPoints { get; set; } 

/// High, the greatest offer price since the currency pair’s roll time 
public string High { get; set; } 

/// Low, the smallest bid price since the currency pair’s roll times 
public string Low { get; set; } 

/// Open, the mid price at the currency pair’s roll time 
public string Open { get; set; } }

 

Threading Concerns

Starting with the .NET Framework 4, the TPL is the preferred way to write multithreaded and parallel code. However, not all code is suitable for parallelization; for example, if a loop performs only a small amount of work on each iteration, or it doesn't run for many iterations, then the overhead of parallelization can cause the code to run more slowly. Furthermore, parallelization like any multithreaded code adds complexity to your program execution. Although the TPL simplifies multithreaded scenarios, we recommend that you have a basic understanding of threading concepts, for example, locks, deadlocks, and race conditions, so that you can use the TPL effectively.

Note on CTrader and cAlgo

If you do not code threading correctly the platform could crash and close down when you are in the middle of an important trade execution, my advice is to seek expert advice or test your code to death before attempting to run it on a live account.


Paul Hayes
Sales & Marketing
Emailcontact@clickalgo.com
Phone: (44) 203 289 6573
Websitehttps://clickalgo.com

Twitter | Facebook | YouTube | Pinterest | LinkedIn

PS: Why not join our instant chat group on Telegram.

 


 

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Threading.Tasks;
using cAlgo4u.TrueFx;
using System.Collections.Generic;
using System.Threading;

// This has been written by Paul Hayes | http://www.cAlgo4u.com | 26/12/2015

// cAlgo4u.TrueFx.dll 
// ==================
// This is a class library which calls the TrueFx web service and parses the html return string into a collection of strongly typed objects for ease of use.
// This external class library has been added to the CTDN example to make it easier to run from cAlgo without having to manage additional references.

// TrueFX brings you real, deal-able prices from market making banks, delivered directly with no intermediary. With TrueFX, everyone with an Internet connection has access to clean, 
// untouched liquidity.

// The idea here is to demonstrate use of retrieving data from web services on the internet and analysing this data to assist in your trading using asyncronous technology for running parallel tasks.

// This is all provided free of charge so that you can see the type of work we can do at http://www.cAlgo4u.com
// To sign up for free real time price data you need to visit this site http://www.truefx.com/ and for more information visit http://www.truefx.com/?page=faqs

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.Internet)]
    public class ClickAlgoTrueFxRealTimePrices : Robot
    {
        [Parameter("Currency List", DefaultValue = "AUD/USD,USD/JPY")]
        public string CurrencyList { get; set; }

        [Parameter("User Name", DefaultValue = "TrueFx User-name")]
        public string UserName { get; set; }

        [Parameter("Password", DefaultValue = "TrueFx Password")]
        public string Password { get; set; }

        [Parameter("Asynchronous Operation", DefaultValue = true)]
        public bool AsyncOperation { get; set; }

        public IList<FxPrices.Currency> CurrencyPrices;

        protected override void OnStart()
        {
            // clean up spaces
            CurrencyList = CurrencyList.Replace(" ", "");

            if (AsyncOperation)
            {
                AsyncRequest();
            }
            else
            {
                SyncRequest();
            }
        }

        /// <summary>
        /// Use this if you do not need a background task and when the delay in getting prices is of no concern.
        /// </summary>
        private void SyncRequest()
        {
            FxPrices fx = new FxPrices(UserName, Password);
            IList<FxPrices.Currency> currencies = fx.GetLivePrices(CurrencyList);
            UpdateCurrencyPrices(currencies);
        }

        /// <summary>
        /// Use this if you need to use concurrent processing - you can run this as a background task and continue to interact with the UI.
        /// </summary>
        private void AsyncRequest()
        {
            try
            {
                // start a new background task.
                var asyncTask = new System.Threading.Tasks.Task(() =>
                {
                    // background thread
                    FxPrices fx = new FxPrices(UserName, Password);
                    IList<FxPrices.Currency> currencies = fx.GetLivePrices(CurrencyList);

                    // when task is complete re-join the main thread
                    BeginInvokeOnMainThread(() => UpdateCurrencyPrices(currencies));
                });

                // start thread
                asyncTask.Start();

            } catch (Exception ex)
            {
                Print(ex.Message);
                this.Stop();
            }
        }

        protected override void OnTick()
        {
        }

        protected override void OnStop()
        {
        }

        private void UpdateCurrencyPrices(IList<FxPrices.Currency> prices)
        {
            CurrencyPrices = prices;
            foreach (FxPrices.Currency c in CurrencyPrices)
            {
                Print("Currency Pair | " + c.Pair);
                Print("Millisecond time-stamp | " + c.TimeStamp);
                Print("Bid big figure | " + c.BidBig);
                Print("Bid points | " + c.BidPoints);
                Print("Offer big figure | " + c.OfferBig);
                Print("Offer points | " + c.OfferPoints);
                Print("High, the greatest offer price since the currency pair’s roll time | " + c.High);
                Print("Low, the smallest bid price since the currency pair’s roll times | " + c.Low);
                Print("Open, the mid price at the currency pair’s roll time | " + c.Open);
            }
        }
    }
}

// This has been written by Paul Hayes | http://www.cAlgo4u.com | 26/12/2015
// Calls the web service of TrueFx | http://www.truefx.com/dev/data/TrueFX_MarketDataWebAPI_DeveloperGuide.pdf

// The TrueFX Real-Time Market Data Web API is a simple HTTP query/response protocol used by clients to access the streaming real-time market data available on TrueFX

namespace cAlgo4u.TrueFx
{
    public class FxPrices
    {
        private string UserName { get; set; }
        private string Password { get; set; }
        private string SessionCode { get; set; }

        private string baseUrl = "http://webrates.truefx.com/rates/connect.html?u=USERNAME&p=PASSWORD&q=ozrates";
        private string priceUrl = "http://webrates.truefx.com/rates/connect.html?id=USERNAME:PASSWORD:ozrates:";

        public IList<Currency> Currencies;

        public FxPrices(string userName, string password)
        {
            UserName = userName;
            Password = password;

            baseUrl = baseUrl.Replace("USERNAME", UserName);
            baseUrl = baseUrl.Replace("PASSWORD", Password);

            priceUrl = priceUrl.Replace("USERNAME", UserName);
            priceUrl = priceUrl.Replace("PASSWORD", Password);

            Currencies = new List<Currency>();
        }

        /// <summary>
        /// Returns all live prices for a currency list provided in the following format: EUR/USD,EUR/GBP,USD/JPY
        /// </summary>
        /// <param name="currencies"></param>
        /// <returns></returns>
        public IList<Currency> GetLivePrices(string currencies)
        {
            string authUrl = baseUrl + "&c=" + currencies + "&f=csv&s=n";

            try
            {
                var request = System.Net.WebRequest.Create(authUrl);
                var response = request.GetResponse();
                var dataStream = response.GetResponseStream();
                var reader = new System.IO.StreamReader(dataStream);
                var stringResponse = reader.ReadToEnd();

                // first get the authentication code.
                string[] list = stringResponse.Split(':');
                string authCode = list[3];

                authCode = authCode.Replace("\r\n", string.Empty);
                priceUrl += authCode;

                // now we can get the prices.
                request = System.Net.WebRequest.Create(priceUrl);
                response = request.GetResponse();
                dataStream = response.GetResponseStream();
                reader = new System.IO.StreamReader(dataStream);
                stringResponse = reader.ReadToEnd();

                reader.Close();
                response.Close();


                // bit of a hack because of smelly code.
                string[] lines = stringResponse.Split(new string[] 
                {
                    "\r\n",
                    "\n"
                }, StringSplitOptions.None);

                foreach (string line in lines)
                {
                    if (line == string.Empty)
                        continue;

                    string[] values = line.Split(',');

                    Currency c = new Currency();
                    c.Pair = values[0];
                    c.TimeStamp = values[1];
                    c.BidBig = values[2];
                    c.BidPoints = values[3];
                    c.OfferBig = values[4];
                    c.OfferPoints = values[5];
                    c.High = values[6];
                    c.Low = values[7];
                    c.Open = values[8];
                    Currencies.Add(c);
                }

                return Currencies;
            } catch
            {
                return null;
            }
        }

        /// <summary>
        /// TrueFX brings you real, deal-able prices from market making banks, delivered directly with no intermediary. 
        /// </summary>
        public class Currency
        {
            /// <summary>
            /// Currency-pair symbol
            /// </summary>
            public string Pair { get; set; }

            /// <summary>
            /// Millisecond time-stamp
            /// </summary>
            public string TimeStamp { get; set; }

            /// <summary>
            /// Bid big figure
            /// </summary>
            public string BidBig { get; set; }

            /// <summary>
            /// Bid points
            /// </summary>
            public string BidPoints { get; set; }

            /// <summary>
            /// Offer big figure
            /// </summary>
            public string OfferBig { get; set; }

            /// <summary>
            /// Offer points
            /// </summary>
            public string OfferPoints { get; set; }

            /// <summary>
            /// High, the greatest offer price since the currency pair’s roll time
            /// </summary>
            public string High { get; set; }

            /// <summary>
            /// Low, the smallest bid price since the currency pair’s roll times
            /// </summary>
            public string Low { get; set; }

            /// <summary>
            /// Open, the mid price at the currency pair’s roll time
            /// </summary>
            public string Open { get; set; }
        }
    }
}
Comments

aimerdoux - January 11, 2016 @ 18:46

Hi Paul its me Omar Thank you for sharing this amazing tools i would like to know if TrueFX also offers Market Depth or Level II Real-Time Data Appreciate it.

scandisk - January 12, 2016 @ 19:14

Hi Paul mine not working anymore because the DOM isn't working on the CTrader anymore I don't know why??

ClickAlgo - January 13, 2016 @ 18:48

Hi Omar, 

I do not think so, but take a look at their FAQ.

http://www.truefx.com/?page=faqs

 

ClickAlgo - January 13, 2016 @ 18:50

Hi Scandisk,

The cBot does not use the DOM, it is just a demonstration of how to get external data via a web service into your robot. The platform could be disconnected.

moneybiz - May 06, 2016 @ 20:06

Do you know other free tick sources other than TrueFx?
I downloaded all of their EURUSD data since 2009 and imported to a DB which is around 13GB with indexes.
It's quite useful for testing purposes where you can produce bars from ticks and any timeframe you want.

I'd like to have data from another source also if you can help.

ClickAlgo - May 09, 2016 @ 09:33

Hi MoneyBiz, 

This example on cTDN is just for getting real time price data on the fly. Have you considered paying for data for that extra edge? Have a look at the ClickAlgo website for some other free and paid data. Also take a look at HistData.com

moneybiz - May 12, 2016 @ 11:47

Hi Paul,

TrueFX has web api and I'm not sure how fast it is to retrieve data like this. Since there are many thicks in a second how should I fetch the data? Repeadetly making new requests? Although I didn't test it it looks slow way to do it. Because the overhead of http headers plus the connect and the disconnect of socket connection on every call. (Open soket connection to the server, send the request data, receive the data, close the connection then repeat all the steps again.)

I think it'd be a better way to have a consistent open connection with the feed server and it to push the data to the client as soon as it is available. Probably this is the paid way? But this time I need to know which data provider is faster. Their prices vary a lot from 30 to 5000 USD a month at a first glance, so what is the difference? Does cheaper mean less accurate, more delayed data? I don't know, maybe you have more knowledge?

moneybiz - May 12, 2016 @ 16:32

I made a live test on TrueFX api and according to my results their data is around 17-18 seconds delayed.
Let's look at the first request/response and what is returned.
The request is made on: 13:23:35.325 (UTC) and response is received on: 13:23:35.447 but the returned price timestamp is: 13:23:18.130. It's 17 seconds back from the current time.

Fetching: 13:23:35.325
Fetched: 13:23:35.447 / 122.13 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:18.130,1.14047,1.14055

Fetching: 13:23:35.463
Fetched: 13:23:35.593 / 130.22 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:18.316,1.14049,1.14055

Fetching: 13:23:35.593
Fetched: 13:23:35.717 / 123.92 ms.
Price didn't change.

Fetching: 13:23:35.717
Fetched: 13:23:35.848 / 130.44 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:18.630,1.14047,1.14055

Fetching: 13:23:35.848
Fetched: 13:23:35.967 / 119.19 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:18.761,1.14050,1.14055

Fetching: 13:23:35.967
Fetched: 13:23:36.102 / 134.46 ms.
Price didn't change.

Fetching: 13:23:36.102
Fetched: 13:23:36.230 / 127.32 ms.
Price didn't change.

Fetching: 13:23:36.230
Fetched: 13:23:36.357 / 127.08 ms.
Price didn't change.

Fetching: 13:23:36.357
Fetched: 13:23:36.484 / 127.26 ms.
Price didn't change.

Fetching: 13:23:36.484
Fetched: 13:23:36.611 / 126.81 ms.
Price didn't change.

Fetching: 13:23:36.611
Fetched: 13:23:36.738 / 127.08 ms.
Price didn't change.

Fetching: 13:23:36.738
Fetched: 13:23:36.865 / 126.95 ms.
Price didn't change.

Fetching: 13:23:36.865
Fetched: 13:23:36.993 / 127.39 ms.
Price didn't change.

Fetching: 13:23:36.993
Fetched: 13:23:37.107 / 114.62 ms.
Price didn't change.

Fetching: 13:23:37.107
Fetched: 13:23:37.246 / 138.82 ms.
Price didn't change.

Fetching: 13:23:37.247
Fetched: 13:23:37.374 / 127.12 ms.
Price didn't change.

Fetching: 13:23:37.374
Fetched: 13:23:37.487 / 113.39 ms.
Price didn't change.

Fetching: 13:23:37.487
Fetched: 13:23:37.628 / 140.79 ms.
Price didn't change.

Fetching: 13:23:37.628
Fetched: 13:23:37.755 / 126.92 ms.
Price didn't change.

Fetching: 13:23:37.756
Fetched: 13:23:37.882 / 126.59 ms.
Price didn't change.

Fetching: 13:23:37.883
Fetched: 13:23:38.010 / 127.03 ms.
Price didn't change.

Fetching: 13:23:38.010
Fetched: 13:23:38.137 / 126.57 ms.
Price didn't change.

Fetching: 13:23:38.137
Fetched: 13:23:38.264 / 127.43 ms.
Price didn't change.

Fetching: 13:23:38.264
Fetched: 13:23:38.387 / 123.03 ms.
Price didn't change.

Fetching: 13:23:38.387
Fetched: 13:23:38.518 / 130.98 ms.
Price didn't change.

Fetching: 13:23:38.518
Fetched: 13:23:38.646 / 127.29 ms.
Price didn't change.

Fetching: 13:23:38.646
Fetched: 13:23:38.773 / 127.16 ms.
Price didn't change.

Fetching: 13:23:38.773
Fetched: 13:23:38.901 / 127.56 ms.
Price didn't change.

Fetching: 13:23:38.901
Fetched: 13:23:39.028 / 126.93 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:21.816,1.14047,1.14055

Fetching: 13:23:39.028
Fetched: 13:23:39.155 / 126.69 ms.
Price didn't change.

Fetching: 13:23:39.155
Fetched: 13:23:39.282 / 126.95 ms.
Price didn't change.

Fetching: 13:23:39.282
Fetched: 13:23:39.397 / 115.70 ms.
Price didn't change.

Fetching: 13:23:39.397
Fetched: 13:23:39.536 / 138.51 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:22.343,1.14047,1.14054

Fetching: 13:23:39.536
Fetched: 13:23:39.663 / 127.43 ms.
Price didn't change.

Fetching: 13:23:39.663
Fetched: 13:23:39.777 / 114.18 ms.
Price didn't change.

Fetching: 13:23:39.777
Fetched: 13:23:39.907 / 129.82 ms.
Price didn't change.

Fetching: 13:23:39.907
Fetched: 13:23:40.044 / 137.19 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:22.869,1.14049,1.14054

Fetching: 13:23:40.045
Fetched: 13:23:40.172 / 127.21 ms.
Price didn't change.

Fetching: 13:23:40.173
Fetched: 13:23:40.300 / 127.39 ms.
Price didn't change.

Fetching: 13:23:40.300
Fetched: 13:23:40.428 / 127.64 ms.
Price didn't change.

Fetching: 13:23:40.428
Fetched: 13:23:40.555 / 127.21 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:23.329,1.14047,1.14054

Fetching: 13:23:40.555
Fetched: 13:23:40.682 / 127.63 ms.
Price didn't change.

Fetching: 13:23:40.682
Fetched: 13:23:40.810 / 127.20 ms.
Price didn't change.

Fetching: 13:23:40.810
Fetched: 13:23:40.937 / 127.28 ms.
Price didn't change.

Fetching: 13:23:40.937
Fetched: 13:23:41.064 / 126.82 ms.
Price didn't change.

Fetching: 13:23:41.064
Fetched: 13:23:41.187 / 123.39 ms.
Price didn't change.

Fetching: 13:23:41.187
Fetched: 13:23:41.317 / 129.78 ms.
Price didn't change.

Fetching: 13:23:41.317
Fetched: 13:23:41.444 / 126.89 ms.
Result: 1.13779,1.14296,1.14256,EUR/USD,0,2016-05-12 13:23:24.295,1.14049,1.14055

 

moneybiz - May 12, 2016 @ 19:50

Sorry for the last message, I was suspecting that my time could be not in sync with the global time servers but dismissed it because it was fresh installed windows.

Anyway the timestamps are correct. My computer was running 17-18 seconds ahead somehow :)

ClickAlgo - May 13, 2016 @ 08:55

Hi MoneyBiz,

The TrueFx API is a free service for real-time Forex price data, it uses a simple Web API HTTP protocol and does not use sockets. If you would like a fast feed of tick data then I would recommend you do some research and pay for the service if you need speed.

You need to define your requirements, if what you need is speed and accuracy then you will need to pay for the service, if it is just for R&D then speed is not an issue, once you have a system that works, I would invest into your business and get fast data.

Regards,

Paul.

 

 

djh.paganprophet@gmail.com - September 17, 2017 @ 18:09

Hi Paul,

First off, thanks for this great plugin. :D

Second, can this cBot be modified to display on-screen prices from another cTrader terminal OR any real-time/raw from fx servers?

If yes, can you provide me with a quote for this job?

David.

0