Jarloo

Menu

 

Get FX Data from Yahoo for all FX Pairs

Yahoo Finance FX REST

Yahoo has a REST based URL you can use to get FX (currency) data. Because it’s REST based it’s easy to get in any language from C#, Java, Python etc…

Here is the format: http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote

From that you will see a large amount of data returned that looks like so:

 

REST XML Format for Currency

NOTE: list shortened, note the ellipsis

<list version="1.0">
	<meta>
		<type>resource-list</type>
	</meta>
	<resources start="0" count="188">
		<resource classname="Quote">
			<field name="change">0.000000</field>
			<field name="chg_percent">0.000000</field>
			<field name="day_high">10.347000</field>
			<field name="day_low">10.347000</field>
			<field name="issuer_name">USD/BWP</field>
			<field name="name">USD/BWP</field>
			<field name="price">10.347000</field>
			<field name="symbol">BWP=X</field>
			<field name="ts">1470906900</field>
			<field name="type">currency</field>
			<field name="utctime">2016-08-11T09:15:00+0000</field>
			<field name="volume">0</field>
			<field name="year_high">11.585000</field>
			<field name="year_low">9.905700</field>
		</resource>
		<resource classname="Quote">
			<field name="change">0.002150</field>
			<field name="chg_percent">0.160056</field>
			<field name="day_high">1.345970</field>
			<field name="day_low">1.342580</field>
			<field name="issuer_name">USD/SGD</field>
			<field name="name">USD/SGD</field>
			<field name="price">1.345390</field>
			<field name="symbol">SGD=X</field>
			<field name="ts">1470973590</field>
			<field name="type">currency</field>
			<field name="utctime">2016-08-12T03:46:30+0000</field>
			<field name="volume">0</field>
			<field name="year_high">1.444080</field>
			<field name="year_low">1.332360</field>
		</resource>
                ...
	</resources>
</list>

C# Source Code

Program.cs

This is a simple console application that demonstrates how to get the currency data and display it. The currency data is returned in an XML format  from Yahoo finance.

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.Serialization;

namespace Jarloo.YahooFX
{
    internal class Program
    {
        private static void Main()
        {
            string xml;

            using (var web = new WebClient())
            {
                xml = web.DownloadString("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote");
            }

            var list = FromXml<list>(xml);

            foreach (var l in list.resources.resource)
            {
                Console.WriteLine($"Symbol: {l.field[0].Value}: {l.field[1].Value}");
            }


            Console.Read();
        }

        public static T FromXml<T>(string input)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(input)))
            {
                var x = new XmlSerializer(typeof(T));
                var obj = (T) x.Deserialize(stream);
                return obj;
            }
        }
    }
}

Model Classes

These classes map the XML to C# so we can expose them as proper objects and deliver the FX pairs.

 
using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Jarloo.YahooFX
{
    [Serializable]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = true)]
    [XmlRoot(Namespace = "", IsNullable = false)]
    public class list
    {
        private listMeta metaField;

        private listResources resourcesField;

        private decimal versionField;

        public listMeta meta
        {
            get { return metaField; }
            set { metaField = value; }
        }

        public listResources resources
        {
            get { return resourcesField; }
            set { resourcesField = value; }
        }

        [XmlAttribute]
        public decimal version
        {
            get { return versionField; }
            set { versionField = value; }
        }
    }


    [Serializable]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = true)]
    public class listMeta
    {
        private string typeField;


        public string type
        {
            get { return typeField; }
            set { typeField = value; }
        }
    }


    [Serializable]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = true)]
    public class listResources
    {
        private byte countField;

        private listResourcesResource[] resourceField;

        private byte startField;


        [XmlElement("resource")]
        public listResourcesResource[] resource
        {
            get { return resourceField; }
            set { resourceField = value; }
        }


        [XmlAttribute]
        public byte start
        {
            get { return startField; }
            set { startField = value; }
        }


        [XmlAttribute]
        public byte count
        {
            get { return countField; }
            set { countField = value; }
        }
    }


    [Serializable]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = true)]
    public class listResourcesResource
    {
        private string classnameField;

        private listResourcesResourceField[] fieldField;


        [XmlElement("field")]
        public listResourcesResourceField[] field
        {
            get { return fieldField; }
            set { fieldField = value; }
        }


        [XmlAttribute]
        public string classname
        {
            get { return classnameField; }
            set { classnameField = value; }
        }
    }


    [Serializable]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = true)]
    public class listResourcesResourceField
    {
        private string nameField;

        private string valueField;


        [XmlAttribute]
        public string name
        {
            get { return nameField; }
            set { nameField = value; }
        }


        [XmlText]
        public string Value
        {
            get { return valueField; }
            set { valueField = value; }
        }
    }
} 

 

Putting it all together

The code is just a small Console application in C#, but I hope it demonstrates how to get and parse the data from the Yahoo FX API (currency). With this you should easily be able to add a UI on top such as WinForms or WPF.

 

Categories:   Finance, Uncategorized

Comments

Sorry, comments are closed for this item.